Created
October 24, 2017 21:07
-
-
Save Sebastian-Roth/026b977b0006bce8f81acec5bc3425a4 to your computer and use it in GitHub Desktop.
Get HDD infos like serial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
#include <linux/hdreg.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) | |
{ | |
static struct hd_driveid hd; | |
int fd; | |
if (geteuid() > 0) { | |
printf("ERROR: Must be root to use\n"); //===>> Must be root to use | |
exit(1); | |
} | |
if ((fd = open(argv[1], O_RDONLY|O_NONBLOCK)) < 0) { | |
printf("ERROR: Cannot open device %s\n", argv[1]); | |
exit(1); | |
} | |
if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) { | |
printf("Hard Disk Model: %.40s\n", hd.model); | |
printf(" Serial Number: %.20s\n", hd.serial_no); | |
} else if (errno == -ENOMSG) { | |
printf("No hard disk identification information available\n"); | |
} else { | |
perror("ERROR: HDIO_GET_IDENTITY"); | |
exit(1); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment