Split grml-scripts into grml-scripts and grml-scripts-core
[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  *******************************************************************************/
8
9 // diet gcc -s -Os -o reread_partition_table reread_partition_table.c
10
11 #include <fcntl.h>
12 #include <linux/fs.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 void usage()
21 {
22   printf("Usage: reread_partition_table <device>\n");
23 }
24
25 int reread_partition_table(char *dev)
26 {
27   int fd;
28
29   sync();
30
31   if ((fd = open(dev, O_RDONLY)) < 0) {
32     perror("error opening device");
33     return(1);
34   }
35
36   if (ioctl(fd, BLKRRPART) < 0) {
37     perror("unable to reload partition table");
38     return(1);
39   }
40   else {
41     printf("successfully reread partition table\n");
42     return(0);
43   }
44 }
45
46 int main(int argc, char** argv)
47 {
48   if (getuid() != 0){
49     printf("Error: reread_partition_table requires root permissions\n");
50     exit(1);
51   }
52
53   if (argc < 2) {
54     usage();
55     exit(1);
56   }
57   else {
58     if (strncmp(argv[1], "/dev/", 5) != 0) {
59       printf("Invalid argument.\n");
60       usage();
61       exit(1);
62     }
63     reread_partition_table(argv[1]);
64   }
65   return EXIT_SUCCESS;
66 }
67
68 /* END OF FILE ****************************************************************/