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