switch_root.c (3439B)
1 /* see LICENSE file for copyright and license details */ 2 3 #if FEATURE_SWITCH_ROOT_EXT 4 #ifndef _DEFAULT_SOURCE 5 #define _DEFAULT_SOURCE 6 #endif 7 #ifndef _XOPEN_SOURCE 8 #define _XOPEN_SOURCE 700 9 #endif 10 #endif 11 #include <sys/mount.h> 12 #include <sys/stat.h> 13 #include <sys/vfs.h> 14 15 #include <dirent.h> 16 #include <fcntl.h> 17 #include <limits.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <unistd.h> 22 23 #include "util.h" 24 #include "wexec.h" 25 26 #define RAMFS_MAGIC 0x858458f6 /* some random number */ 27 #define TMPFS_MAGIC 0x01021994 28 29 static void 30 delete_content(const char *dir, dev_t curdevice) 31 { 32 char path[PATH_MAX]; 33 DIR *d; 34 struct stat st; 35 struct dirent *dent; 36 37 /* dont dive into other filesystems */ 38 if (lstat(dir, &st) < 0 || st.st_dev != curdevice) 39 return; 40 if (!(d = opendir(dir))) 41 return; 42 while ((dent = readdir(d))) { 43 if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) 44 continue; 45 46 /* build path and dive deeper */ 47 if (strlcpy(path, dir, sizeof(path)) >= sizeof(path)) 48 eprintf("path too long\n"); 49 if (path[strlen(path) - 1] != '/') 50 if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) 51 eprintf("path too long\n"); 52 if (strlcat(path, dent->d_name, sizeof(path)) >= sizeof(path)) 53 eprintf("path too long\n"); 54 55 if (lstat(path, &st) < 0) 56 weprintf("lstat %s:", path); 57 58 if (S_ISDIR(st.st_mode)) { 59 delete_content(path, curdevice); 60 if (rmdir(path) < 0) 61 weprintf("rmdir %s:", path); 62 } else { 63 if (unlink(path) < 0) 64 weprintf("unlink %s:", path); 65 } 66 } 67 closedir(d); 68 } 69 70 static void 71 usage(void) 72 { 73 eprintf("usage: %s [-c console] [newroot] [init] (PID 1)\n", argv0); 74 } 75 76 // ?man switch_root: switch to another root filesystem 77 // ?man arguments: newroot [init] (PID 1) 78 // ?man switch to another filesystem as the root of the mount tree 79 int 80 main(int argc, char *argv[]) 81 { 82 char *console = NULL; 83 dev_t curdev; 84 struct stat st; 85 struct statfs stfs; 86 87 ARGBEGIN 88 { 89 // ?man -c:str: print count or perform stdout action 90 case 'c': 91 console = EARGF(usage()); 92 break; 93 default: 94 usage(); 95 } 96 ARGEND; 97 98 /* check number of args and if we are PID 1 */ 99 if (argc != 2 || getpid() != 1) 100 usage(); 101 102 /* chdir to newroot and make sure its a different fs */ 103 if (chdir(argv[0])) 104 eprintf("chdir %s:", argv[0]); 105 106 if (stat("/", &st)) 107 eprintf("stat %s:", "/"); 108 109 curdev = st.st_dev; 110 if (stat(".", &st)) 111 eprintf("stat %s:", "."); 112 if (st.st_dev == curdev) 113 usage(); 114 115 /* avoids trouble with real filesystems */ 116 if (stat("/init", &st) || !S_ISREG(st.st_mode)) 117 eprintf("/init is not a regular file\n"); 118 119 statfs("/", &stfs); 120 if ((unsigned)stfs.f_type != RAMFS_MAGIC && (unsigned)stfs.f_type != TMPFS_MAGIC) 121 eprintf("current filesystem is not a RAMFS or TMPFS\n"); 122 123 /* wipe / */ 124 delete_content("/", curdev); 125 126 /* overmount / with newroot and chroot into it */ 127 if (mount(".", "/", NULL, MS_MOVE, NULL)) 128 eprintf("mount %s:", "."); 129 130 if (chroot(".")) 131 eprintf("chroot failed\n"); 132 133 /* if -c is set, redirect stdin/stdout/stderr to console */ 134 if (console) { 135 close(0); 136 if (open(console, O_RDWR) == -1) 137 eprintf("open %s:", console); 138 dup2(0, 1); 139 dup2(0, 2); 140 } 141 142 /* execute init */ 143 wexecv_self(argv[1], argv); 144 eprintf("can't execute '%s':", argv[1]); 145 return 1; 146 }