Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 1 | /* vi:set ts=4:*/ |
| 2 | |
| 3 | #include <dirent.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <stdio.h> |
| 6 | #include <sys/mount.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/vfs.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "busybox.h" |
| 13 | |
| 14 | // Make up for header deficiencies. |
| 15 | |
| 16 | #ifndef RAMFS_MAGIC |
| 17 | #define RAMFS_MAGIC 0x858458f6 |
| 18 | #endif |
| 19 | |
| 20 | #ifndef TMPFS_MAGIC |
| 21 | #define TMPFS_MAGIC 0x01021994 |
| 22 | #endif |
| 23 | |
| 24 | #ifndef MS_MOVE |
| 25 | #define MS_MOVE 8192 |
| 26 | #endif |
| 27 | |
| 28 | dev_t rootdev; |
| 29 | |
| 30 | // Recursively delete contents of rootfs. |
| 31 | |
| 32 | static void delete_contents(char *directory) |
| 33 | { |
| 34 | DIR *dir; |
| 35 | struct dirent *d; |
| 36 | struct stat st; |
| 37 | |
| 38 | // Don't descend into other filesystems |
| 39 | if (stat(directory,&st) || st.st_dev != rootdev) return; |
| 40 | |
| 41 | // Recursively delete the contents of directories. |
| 42 | if (S_ISDIR(st.st_mode)) { |
| 43 | if((dir = opendir(directory))) { |
| 44 | while ((d = readdir(dir))) { |
| 45 | char *newdir=d->d_name; |
| 46 | |
| 47 | // Skip . and .. |
| 48 | if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2]))) |
| 49 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 50 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 51 | // Recurse to delete contents |
| 52 | newdir = alloca(strlen(directory) + strlen(d->d_name) + 2); |
| 53 | sprintf(newdir, "%s/%s", directory, d->d_name); |
| 54 | delete_contents(newdir); |
| 55 | } |
| 56 | closedir(dir); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 57 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 58 | // Directory should now be empty. Zap it. |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 59 | rmdir(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 60 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 61 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 62 | // It wasn't a directory. Zap it. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 64 | } else unlink(directory); |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int switch_root_main(int argc, char *argv[]) |
| 68 | { |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 69 | char *newroot, *console=NULL; |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 70 | struct stat st1, st2; |
| 71 | struct statfs stfs; |
| 72 | |
| 73 | // Parse args (-c console) |
| 74 | |
| 75 | bb_opt_complementally="-2"; |
| 76 | bb_getopt_ulflags(argc,argv,"c:",&console); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 77 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 78 | // Change to new root directory and verify it's a different fs. |
| 79 | |
| 80 | newroot=argv[optind++]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 81 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 82 | if (chdir(newroot) || stat(".", &st1) || stat("/", &st2) || |
| 83 | st1.st_dev == st2.st_dev) |
| 84 | { |
| 85 | bb_error_msg_and_die("bad newroot %s",newroot); |
| 86 | } |
| 87 | rootdev=st2.st_dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 88 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 89 | // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE |
| 90 | // we mean it. (I could make this a CONFIG option, but I would get email |
| 91 | // from all the people who WILL eat their filesystemss.) |
| 92 | |
| 93 | if (stat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) || |
| 94 | (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) || |
| 95 | getpid() != 1) |
| 96 | { |
| 97 | bb_error_msg_and_die("not rootfs"); |
| 98 | } |
| 99 | |
| 100 | // Zap everything out of rootdev |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 101 | |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 102 | delete_contents("/"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 103 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 104 | // Overmount / with newdir and chroot into it. The chdir is needed to |
| 105 | // recalculate "." and ".." links. |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 106 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 107 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/")) |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 108 | bb_error_msg_and_die("moving root"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 109 | |
Rob Landley | 5d84c23 | 2005-12-20 17:25:51 +0000 | [diff] [blame] | 110 | // If a new console specified, redirect stdin/stdout/stderr to that. |
| 111 | |
| 112 | if (console) { |
| 113 | close(0); |
| 114 | if(open(console, O_RDWR) < 0) |
| 115 | bb_error_msg_and_die("Bad console '%s'",console); |
| 116 | dup2(0, 1); |
| 117 | dup2(0, 2); |
| 118 | } |
Rob Landley | 0f34a82 | 2005-10-27 22:55:50 +0000 | [diff] [blame] | 119 | |
| 120 | // Exec real init. (This is why we must be pid 1.) |
| 121 | execv(argv[optind],argv+optind+1); |
| 122 | bb_error_msg_and_die("Bad init '%s'",argv[optind]); |
| 123 | } |