Sponsored Content
Top Forums Programming write() issue during a low level hdd access Post 302397217 by sponnusa on Sunday 21st of February 2010 12:29:56 PM
Old 02-21-2010
write() issue during a low level hdd access

Hi,

I am trying to write zeroes to the hdd using a c program. I don't want to use the dd or ddrescue or any such inbuilt program because of reasons like real time progress, writing custom patterns. (my program is more like an erasure application, but does only zero fill).

here are the steps which i have followed. (I am executing under root).

Linux version used is ubuntu with (Intrepid) 8.10, with 2.6.27-7-generic.

opened the device /dev/sda using open call.

Code:
fd = open("/dev/sda", O_WRONLY);

and called a write call with
Code:
write(fd,buff,512);

The write call never fails for some strange reasons. It always returns 512 even in the case of a bad hdd connected to system. (The dmesg shows either the hdd is bad or not responding the write requests, or has I/O errors, or has sectors errors, or has failed the hard / soft reset operations).

I have tried using errno to get the error value, but nothing returns the error condition. Is that something i am missing?

Following is the code for the write function.

Code:
int writesector(const uint64_t offset, const int fd, const uint8_t *mybuff, const uint16_t len)
{
 if(!fd)
  {
    return 4;
  }
  errno = 0;
  if(lseek64(fd, offset, SEEK_SET) != -1)
  {
    if (errno!=0)
    {
      return 1;
    }
    
    errno = 0;
    
    if( (write(fd, mybuff, len)) != len)
    {
      return 1;
    }
    
    if (errno!=0)
    {
      return 1;
    }
  }
  else
  {
    return 2;
  }
  return 0;
}

Has anyone tried a similar approach in writing to hdd's at low level?

What else I can check for? I am currently out of options to identify the issue. I have tried implementing a write timeout, but it bombs badly! Smilie

Any inputs / help on this issue please?

Last edited by Franklin52; 02-21-2010 at 01:57 PM.. Reason: Please use code tags!
 

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Low level format?

I want to do a low level format like in windows (C:\format c:) but I don't know how it works in unix or linux.. Can somebody help me ? thnx :) (3 Replies)
Discussion started by: day
3 Replies

2. HP-UX

Access to a second HDD

Hello How to access to a second hard disk on a HP-UX system? Thanks (3 Replies)
Discussion started by: ouniss
3 Replies

3. IP Networking

Best reference for understanding low level info on nic cards drivers and functionality

Hi, What is the best reference that gives in detail on nic cards configuration , assigning multiple ip addresses to a single interface, netlink library etc and all basic stuff at this level..? Thanks (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

4. AIX

High Runqueue (R) LOW CPU LOW I/O Low Network Low memory usage

Hello All I have a system running AIX 61 shared uncapped partition (with 11 physical processors, 24 Virtual 72GB of Memory) . The output from NMON, vmstat show a high run queue (60+) for continous periods of time intervals, but NO paging, relatively low I/o (6000) , CPU % is 40, Low network.... (9 Replies)
Discussion started by: IL-Malti
9 Replies

5. Programming

System calls and C language low-level qualities???

Hi friends, I hope everyone is fine and doing well. I queried in my previous thread about the low-level qualities of C/C++ languages.I really thank you people for explaining, it was really helpful. One more ambiquity that I have in my mind is regarding the unix system calls like open, creat,... (1 Reply)
Discussion started by: gabam
1 Replies

6. Programming

Why is C/C++ considered low-level languages???

Hi friends, I hope everyone is doing well and fine. I have always been hearing that C/C++ are relatively low-level as compared to Java/C# etc. Could you please tell me some low-level qualities of C/C++? And I think disk deframenters are written in C/C++, please correct me if I am wrong. And please... (5 Replies)
Discussion started by: gabam
5 Replies

7. Programming

Low level X11 programming

How to use X11 without Xlib not XCB? How draw window directly on low level? I must use anyway window manager like Motif? I have ridden that X11 has server-client architecture, client send via TCP/IP to port 6000 request for primitives and get replies. Where is detailed description of it? In X11... (0 Replies)
Discussion started by: AndrzejB
0 Replies
WRITE(2)						     Linux Programmer's Manual							  WRITE(2)

NAME
write - write to a file descriptor SYNOPSIS
#include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); DESCRIPTION
write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).) For a seekable file (i.e., one to which lseek(2) may be applied, for example, a regular file) writing takes place at the current file off- set, and the file offset is incremented by the number of bytes actually written. If the file was open(2)ed with O_APPEND, the file offset is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic step. POSIX requires that a read(2) which can be proved to occur after a write() has returned returns the new data. Note that not all file sys- tems are POSIX conforming. RETURN VALUE
On success, the number of bytes written is returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately. If count is zero and fd refers to a regular file, then write() may return a failure status if one of the errors below is detected. If no errors are detected, 0 will be returned without causing any other effect. If count is zero and fd refers to a file other than a regular file, the results are not specified. ERRORS
EAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the write would block. EAGAIN or EWOULDBLOCK The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the write would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable appli- cation should check for both possibilities. EBADF fd is not a valid file descriptor or is not open for writing. EDESTADDRREQ fd refers to a datagram socket for which a peer address has not been set using connect(2). EFAULT buf is outside your accessible address space. EFBIG An attempt was made to write a file that exceeds the implementation-defined maximum file size or the process's file size limit, or to write at a position past the maximum allowed offset. EINTR The call was interrupted by a signal before any data was written; see signal(7). EINVAL fd is attached to an object which is unsuitable for writing; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned. EIO A low-level I/O error occurred while modifying the inode. ENOSPC The device containing the file referred to by fd has no room for the data. EPIPE fd is connected to a pipe or socket whose reading end is closed. When this happens the writing process will also receive a SIGPIPE signal. (Thus, the write return value is seen only if the program catches, blocks or ignores this signal.) Other errors may occur, depending on the object connected to fd. CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001. Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written. NOTES
A successful return from write() does not make any guarantee that data has been committed to disk. In fact, on some buggy implementations, it does not even guarantee that space has successfully been reserved for the data. The only way to be sure is to call fsync(2) after you are done writing all your data. If a write() is interrupted by a signal handler before any bytes are written, then the call fails with the error EINTR; if it is inter- rupted after at least one byte has been written, the call succeeds, and returns the number of bytes written. SEE ALSO
close(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), pwrite(2), read(2), select(2), writev(2), fwrite(3) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-08-29 WRITE(2)
All times are GMT -4. The time now is 12:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy