PDA

View Full Version : [Solved] Porting code


osmano807
December 19th, 2009, 20:00
How to port this code to FreeBSD?

#include <stdio.h>
#include <fcntl.h>
#include <linux/hdreg.h>

int main()
{
struct hd_driveid hd;
int ide;
ide=open("/dev/hda",O_RDONLY);
ioctl(ide,HDIO_GET_IDENTITY,&hd);
printf("Serial number - %s\n",hd.serial_no);
return 1;
}

SirDice
December 19th, 2009, 20:34
Looking at what smartctl (sysutils/smartmontools) does it looks like you can get that info with a IOCATAREQUEST ioctl. It's defined in sys/ata.h.

mickey
December 19th, 2009, 20:44
This code looks b0rken by design, as it

Does not close file descriptor 'ide'
Always returns with error status


But here you go:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ata.h>

int main(int argc, char *argv[])
{
int fd;
int rv = -1;
const char *dvc = "/dev/ad0";

if((fd = open(dvc, O_RDONLY)) != -1)
{
struct ata_params atap;

if(ioctl(fd, IOCATAGPARM, &atap) != -1)
{
printf("Drive serial number: %s\n", atap.serial);
rv = 0;
}
else
{
fprintf(stderr, "ioctl(IOCATAGPARM) failed (%s)\n",
strerror(errno));
}

close(fd);
}
else
{
fprintf(stderr, "failed to open %s (%s)\n",
dvc, strerror(errno));
}

return rv;
}

osmano807
December 19th, 2009, 21:03
Thank you very much!
I had looked at the smartctl, but as I'm a little new to FreeBSD, I had not found it.

mickey
December 19th, 2009, 21:13
You might also find it interesting, to take a look at /usr/src/sbin/atacontrol.