• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/reboot.h>
6 #include <sys/wait.h>
7 
8 const char *mkfs = "/system/bin/make_ext4fs";
9 
setup_fs(const char * blockdev)10 int setup_fs(const char *blockdev)
11 {
12     char buf[256], path[128];
13     pid_t child;
14     int status, n;
15 
16         /* we might be looking at an indirect reference */
17     n = readlink(blockdev, path, sizeof(path) - 1);
18     if (n > 0) {
19         path[n] = 0;
20         if (!memcmp(path, "/dev/block/", 11))
21             blockdev = path + 11;
22     }
23 
24     if (strchr(blockdev,'/')) {
25         fprintf(stderr,"not a block device name: %s\n", blockdev);
26         return 0;
27     }
28 
29     sprintf(buf,"/sys/fs/ext4/%s", blockdev);
30     if (access(buf, F_OK) == 0) {
31         fprintf(stderr,"device %s already has a filesystem\n", blockdev);
32         return 0;
33     }
34     sprintf(buf,"/dev/block/%s", blockdev);
35 
36     fprintf(stderr,"+++\n");
37 
38     child = fork();
39     if (child < 0) {
40         fprintf(stderr,"error: fork failed\n");
41         return 0;
42     }
43     if (child == 0) {
44         execl(mkfs, mkfs, buf, NULL);
45         exit(-1);
46     }
47 
48     while (waitpid(-1, &status, 0) != child) ;
49 
50     fprintf(stderr,"---\n");
51     return 1;
52 }
53 
54 
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57     int need_reboot = 0;
58 
59     while (argc > 1) {
60         if (strlen(argv[1]) < 128)
61             need_reboot |= setup_fs(argv[1]);
62         argv++;
63         argc--;
64     }
65 
66     if (need_reboot) {
67         sync();
68         sync();
69         sync();
70         fprintf(stderr,"REBOOT!\n");
71         reboot(RB_AUTOBOOT);
72         exit(-1);
73     }
74     return 0;
75 }
76