0

I wrote a program to get the details of hard disk drive using HDIO_ ioctl calls.

For writing program, I'm referring Documentation/ioctl/hdio.txt in kernel source(2.6.32).

Here is my main part of code:

unsigned char driveid[512];
fd = open("/dev/sda", O_RDONLY);  // validated fd.
retval = ioctl(fd, HDIO_GET_IDENTITY, &driveid);
if(retval < 0) {
            perror("ioctl(HDIO_GET_IDENTITY)");
            exit(3);
}

When I run(as root) the above code, I got below error:

ioctl(HDIO_GET_IDENTITY): Invalid argument

What is the wrong in the program? Why I'm getting error?

Additional Info: OS: CentOS-6.5, kernel version: 2.6.32, IA:x86_64 (running on VMware).

gangadhars
  • 2,584
  • 7
  • 41
  • 68

1 Answers1

1

the HDIO_GET_IDENTITY ioctl() doesn`t take a raw character buffer as its 3rd argument.

it uses a struct defined in linux/hdreg.h.

struct hd_driveid  driveid; 
fd = open("/dev/sda", O_RDONLY);  // validated fd.
retval = ioctl(fd, HDIO_GET_IDENTITY, &driveid);
if(retval < 0) {
            perror("ioctl(HDIO_GET_IDENTITY)");
            exit(3);
}

this way it should work. Be aware that it only works for IDE/SATA drives, SCSI is not supported. has if you are wondering on how to get the information after the command ioctl() has returned succesfully, I suggest going through

http://lxr.free-electrons.com/source/include/linux/hdreg.h?v=2.6.36

user3021085
  • 407
  • 2
  • 5
  • 16