vmware-detect.c: implemented io-ports based check
[grml-scripts.git] / compile / reread_partition_table.c
1 /*
2  * Filename:      reread_partition_table.c
3  * Purpose:       re-read partition table on Linux
4  * Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5  * Bug-Reports:   see http://grml.org/bugs/
6  * License:       This file is licensed under the GPL v2.
7  * Latest change: Die Sep 05 23:13:57 CEST 2006 [mika]
8  *******************************************************************************/
9
10 // diet gcc -s -Os -o reread_partition_table reread_partition_table.c
11
12 #include <fcntl.h>
13 #include <linux/fs.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 void usage()
22 {
23   printf("Usage: reread_partition_table <device>\n");
24 }
25
26 int reread_partition_table(char *dev)
27 {
28   int fd;
29
30   sync();
31
32   if ((fd = open(dev, O_RDONLY)) < 0) {
33     perror("error opening device");
34     return(1);
35   }
36
37   if (ioctl(fd, BLKRRPART) < 0) {
38     perror("unable to reload partition table");
39     return(1);
40   }
41   else {
42     printf("successfully reread partition table\n");
43     return(0);
44   }
45 }
46
47 int main(int argc, char** argv)
48 {
49   if (getuid() != 0){
50     printf("Error: reread_partition_table requires root permissions\n");
51     exit(1);
52   }
53
54   if (argc < 2) {
55     usage();
56     exit(1);
57   }
58   else {
59     if (strncmp(argv[1], "/dev/", 5) != 0) {
60       printf("Invalid argument.\n");
61       usage();
62       exit(1);
63     }
64     reread_partition_table(argv[1]);
65   }
66   return EXIT_SUCCESS;
67 }
68
69 /* END OF FILE ****************************************************************/