diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-11 23:44:50 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-11 23:44:50 +0000 |
| commit | 39acf453353a41a78fbc220360e884eb0eb33a59 (patch) | |
| tree | 92344fe71153d61f64d744183d9c1006307fac3f /util-linux | |
| parent | e7067e38ea8c6e0e498270ee6e8cd6aa5e92796e (diff) | |
| download | busybox-w32-39acf453353a41a78fbc220360e884eb0eb33a59.tar.gz busybox-w32-39acf453353a41a78fbc220360e884eb0eb33a59.tar.bz2 busybox-w32-39acf453353a41a78fbc220360e884eb0eb33a59.zip | |
switch_root: shrink
function old new delta
switch_root_main 402 401 -1
rootdev 8 - -8
delete_contents 226 179 -47
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/2 up/down: 0/-56) Total: -56 bytes
Diffstat (limited to 'util-linux')
| -rw-r--r-- | util-linux/switch_root.c | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c index 1f6ec2dc7..21cc99229 100644 --- a/util-linux/switch_root.c +++ b/util-linux/switch_root.c | |||
| @@ -9,9 +9,7 @@ | |||
| 9 | #include "libbb.h" | 9 | #include "libbb.h" |
| 10 | #include <sys/vfs.h> | 10 | #include <sys/vfs.h> |
| 11 | 11 | ||
| 12 | |||
| 13 | // Make up for header deficiencies. | 12 | // Make up for header deficiencies. |
| 14 | |||
| 15 | #ifndef RAMFS_MAGIC | 13 | #ifndef RAMFS_MAGIC |
| 16 | #define RAMFS_MAGIC ((unsigned)0x858458f6) | 14 | #define RAMFS_MAGIC ((unsigned)0x858458f6) |
| 17 | #endif | 15 | #endif |
| @@ -24,18 +22,16 @@ | |||
| 24 | #define MS_MOVE 8192 | 22 | #define MS_MOVE 8192 |
| 25 | #endif | 23 | #endif |
| 26 | 24 | ||
| 27 | static dev_t rootdev; | ||
| 28 | |||
| 29 | // Recursively delete contents of rootfs. | 25 | // Recursively delete contents of rootfs. |
| 30 | 26 | static void delete_contents(const char *directory, dev_t rootdev) | |
| 31 | static void delete_contents(const char *directory) | ||
| 32 | { | 27 | { |
| 33 | DIR *dir; | 28 | DIR *dir; |
| 34 | struct dirent *d; | 29 | struct dirent *d; |
| 35 | struct stat st; | 30 | struct stat st; |
| 36 | 31 | ||
| 37 | // Don't descend into other filesystems | 32 | // Don't descend into other filesystems |
| 38 | if (lstat(directory, &st) || st.st_dev != rootdev) return; | 33 | if (lstat(directory, &st) || st.st_dev != rootdev) |
| 34 | return; | ||
| 39 | 35 | ||
| 40 | // Recursively delete the contents of directories. | 36 | // Recursively delete the contents of directories. |
| 41 | if (S_ISDIR(st.st_mode)) { | 37 | if (S_ISDIR(st.st_mode)) { |
| @@ -45,13 +41,13 @@ static void delete_contents(const char *directory) | |||
| 45 | char *newdir = d->d_name; | 41 | char *newdir = d->d_name; |
| 46 | 42 | ||
| 47 | // Skip . and .. | 43 | // Skip . and .. |
| 48 | if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2]))) | 44 | if (DOT_OR_DOTDOT(newdir)) |
| 49 | continue; | 45 | continue; |
| 50 | 46 | ||
| 51 | // Recurse to delete contents | 47 | // Recurse to delete contents |
| 52 | newdir = alloca(strlen(directory) + strlen(d->d_name) + 2); | 48 | newdir = concat_path_file(directory, newdir); |
| 53 | sprintf(newdir, "%s/%s", directory, d->d_name); | 49 | delete_contents(newdir, rootdev); |
| 54 | delete_contents(newdir); | 50 | free(newdir); |
| 55 | } | 51 | } |
| 56 | closedir(dir); | 52 | closedir(dir); |
| 57 | 53 | ||
| @@ -60,7 +56,6 @@ static void delete_contents(const char *directory) | |||
| 60 | } | 56 | } |
| 61 | 57 | ||
| 62 | // It wasn't a directory. Zap it. | 58 | // It wasn't a directory. Zap it. |
| 63 | |||
| 64 | } else unlink(directory); | 59 | } else unlink(directory); |
| 65 | } | 60 | } |
| 66 | 61 | ||
| @@ -70,15 +65,14 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv) | |||
| 70 | char *newroot, *console = NULL; | 65 | char *newroot, *console = NULL; |
| 71 | struct stat st1, st2; | 66 | struct stat st1, st2; |
| 72 | struct statfs stfs; | 67 | struct statfs stfs; |
| 68 | dev_t rootdev; | ||
| 73 | 69 | ||
| 74 | // Parse args (-c console) | 70 | // Parse args (-c console) |
| 75 | |||
| 76 | opt_complementary = "-2"; // minimum 2 params | 71 | opt_complementary = "-2"; // minimum 2 params |
| 77 | getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option | 72 | getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option |
| 78 | argv += optind; | 73 | argv += optind; |
| 79 | 74 | ||
| 80 | // Change to new root directory and verify it's a different fs. | 75 | // Change to new root directory and verify it's a different fs. |
| 81 | |||
| 82 | newroot = *argv++; | 76 | newroot = *argv++; |
| 83 | 77 | ||
| 84 | xchdir(newroot); | 78 | xchdir(newroot); |
| @@ -90,7 +84,6 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv) | |||
| 90 | // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE | 84 | // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE |
| 91 | // we mean it. (I could make this a CONFIG option, but I would get email | 85 | // we mean it. (I could make this a CONFIG option, but I would get email |
| 92 | // from all the people who WILL eat their filesystems.) | 86 | // from all the people who WILL eat their filesystems.) |
| 93 | |||
| 94 | if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) | 87 | if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) |
| 95 | || (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC)) | 88 | || (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC)) |
| 96 | || (getpid() != 1) | 89 | || (getpid() != 1) |
| @@ -99,24 +92,21 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv) | |||
| 99 | } | 92 | } |
| 100 | 93 | ||
| 101 | // Zap everything out of rootdev | 94 | // Zap everything out of rootdev |
| 102 | 95 | delete_contents("/", rootdev); | |
| 103 | delete_contents("/"); | ||
| 104 | 96 | ||
| 105 | // Overmount / with newdir and chroot into it. The chdir is needed to | 97 | // Overmount / with newdir and chroot into it. The chdir is needed to |
| 106 | // recalculate "." and ".." links. | 98 | // recalculate "." and ".." links. |
| 107 | |||
| 108 | if (mount(".", "/", NULL, MS_MOVE, NULL)) | 99 | if (mount(".", "/", NULL, MS_MOVE, NULL)) |
| 109 | bb_error_msg_and_die("error moving root"); | 100 | bb_error_msg_and_die("error moving root"); |
| 110 | xchroot("."); | 101 | xchroot("."); |
| 111 | xchdir("/"); | 102 | xchdir("/"); |
| 112 | 103 | ||
| 113 | // If a new console specified, redirect stdin/stdout/stderr to that. | 104 | // If a new console specified, redirect stdin/stdout/stderr to that. |
| 114 | |||
| 115 | if (console) { | 105 | if (console) { |
| 116 | close(0); | 106 | close(0); |
| 117 | xopen(console, O_RDWR); | 107 | xopen(console, O_RDWR); |
| 118 | dup2(0, 1); | 108 | xdup2(0, 1); |
| 119 | dup2(0, 2); | 109 | xdup2(0, 2); |
| 120 | } | 110 | } |
| 121 | 111 | ||
| 122 | // Exec real init. (This is why we must be pid 1.) | 112 | // Exec real init. (This is why we must be pid 1.) |
