2 * switchroot.c - switch to new root directory and start init.
4 * Copyright 2002-2009 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Peter Jones <pjones@redhat.com>
21 * Jeremy Katz <katzj@redhat.com>
23 #include <sys/mount.h>
24 #include <sys/types.h>
26 #include <sys/param.h>
41 /* remove all files/directories below dirName -- don't cross mountpoints */
42 static int recursiveRemove(int fd
)
49 if (!(dir
= fdopendir(fd
))) {
50 warn("failed to open directory");
54 /* fdopendir() precludes us from continuing to use the input fd */
57 if (fstat(dfd
, &rb
)) {
58 warn("failed to stat directory");
66 if (!(d
= readdir(dir
))) {
68 warn("failed to read directory");
71 break; /* end of directory */
74 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
77 if (d
->d_type
== DT_DIR
) {
80 if (fstatat(dfd
, d
->d_name
, &sb
, AT_SYMLINK_NOFOLLOW
)) {
81 warn("failed to stat %s", d
->d_name
);
85 /* remove subdirectories if device is same as dir */
86 if (sb
.st_dev
== rb
.st_dev
) {
89 cfd
= openat(dfd
, d
->d_name
, O_RDONLY
);
98 if (unlinkat(dfd
, d
->d_name
,
99 d
->d_type
== DT_DIR
? AT_REMOVEDIR
: 0))
100 warn("failed to unlink %s", d
->d_name
);
103 rc
= 0; /* success */
111 static int switchroot(const char *newroot
)
113 /* Don't try to unmount the old "/", there's no way to do it. */
114 const char *umounts
[] = { "/dev", "/proc", "/sys", NULL
};
118 for (i
= 0; umounts
[i
] != NULL
; i
++) {
119 char newmount
[PATH_MAX
];
121 snprintf(newmount
, sizeof(newmount
), "%s%s", newroot
, umounts
[i
]);
123 if (mount(umounts
[i
], newmount
, NULL
, MS_MOVE
, NULL
) < 0) {
124 warn("failed to mount moving %s to %s",
125 umounts
[i
], newmount
);
126 warnx("forcing unmount of %s", umounts
[i
]);
127 umount2(umounts
[i
], MNT_FORCE
);
131 if (chdir(newroot
)) {
132 warn("failed to change directory to %s", newroot
);
136 cfd
= open("/", O_RDONLY
);
138 recursiveRemove(cfd
);
142 if (mount(newroot
, "/", NULL
, MS_MOVE
, NULL
) < 0) {
143 warn("failed to mount moving %s to /", newroot
);
148 warn("failed to change root");
154 static void usage(FILE *output
)
156 fprintf(output
, "usage: %s <newrootdir> <init> <args to init>\n",
157 program_invocation_short_name
);
158 exit(output
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
161 static void version(void)
163 fprintf(stdout
, "%s from %s\n", program_invocation_short_name
,
168 int main(int argc
, char *argv
[])
170 char *newroot
, *init
, **initargs
;
172 if (argv
[1] && (!strcmp(argv
[1], "--help") || !strcmp(argv
[1], "-h")))
174 if (argv
[1] && (!strcmp(argv
[1], "--version") || !strcmp(argv
[1], "-V")))
183 if (!*newroot
|| !*init
)
186 if (switchroot(newroot
))
187 errx(EXIT_FAILURE
, "failed. Sorry.");
189 if (access(init
, X_OK
))
190 warn("cannot access %s", init
);
192 /* get session leader */
195 /* set controlling terminal */
196 if (ioctl (0, TIOCSCTTY
, 1))
197 warn("failed to TIOCSCTTY");
199 execv(init
, initargs
);
200 err(EXIT_FAILURE
, "failed to execute %s", init
);