add documentation of the init hooks
[dracut/plouj.git] / switch_root.c
blob8daacb1d0e85bdc0be77e680df7ad65048c4b75b
1 /*
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/>.
19 * Authors:
20 * Peter Jones <pjones@redhat.com>
21 * Jeremy Katz <katzj@redhat.com>
23 #include <sys/mount.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <err.h>
37 #ifndef MS_MOVE
38 #define MS_MOVE 8192
39 #endif
41 /* remove all files/directories below dirName -- don't cross mountpoints */
42 static int recursiveRemove(char *dirName)
44 struct stat rb;
45 DIR *dir;
46 int rc = -1;
47 int dfd;
49 if (!(dir = opendir(dirName))) {
50 warn("failed to open %s", dirName);
51 goto done;
54 dfd = dirfd(dir);
56 if (fstat(dfd, &rb)) {
57 warn("failed to stat %s", dirName);
58 goto done;
61 while(1) {
62 struct dirent *d;
64 errno = 0;
65 if (!(d = readdir(dir))) {
66 if (errno) {
67 warn("failed to read %s", dirName);
68 goto done;
70 break; /* end of directory */
73 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
74 continue;
76 if (d->d_type == DT_DIR) {
77 struct stat sb;
79 if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
80 warn("failed to stat %s/%s", dirName, d->d_name);
81 continue;
84 /* remove subdirectories if device is same as dir */
85 if (sb.st_dev == rb.st_dev) {
86 char subdir[ strlen(dirName) +
87 strlen(d->d_name) + 2 ];
89 sprintf(subdir, "%s/%s", dirName, d->d_name);
90 recursiveRemove(subdir);
91 } else
92 continue;
95 if (unlinkat(dfd, d->d_name,
96 d->d_type == DT_DIR ? AT_REMOVEDIR : 0))
97 warn("failed to unlink %s/%s", dirName, d->d_name);
100 rc = 0; /* success */
102 done:
103 if (dir)
104 closedir(dir);
105 return rc;
108 static int switchroot(const char *newroot)
110 /* Don't try to unmount the old "/", there's no way to do it. */
111 const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
112 int i;
114 for (i = 0; umounts[i] != NULL; i++) {
115 char newmount[PATH_MAX];
117 snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
119 if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
120 warn("failed to mount moving %s to %s",
121 umounts[i], newmount);
122 warnx("forcing unmount of %s", umounts[i]);
123 umount2(umounts[i], MNT_FORCE);
127 if (chdir(newroot)) {
128 warn("failed to change directory to %s", newroot);
129 return -1;
132 recursiveRemove("/");
134 if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
135 warn("failed to mount moving %s to /", newroot);
136 return -1;
139 if (chroot(".")) {
140 warn("failed to change root");
141 return -1;
143 return 0;
146 static void usage(FILE *output)
148 fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
149 program_invocation_short_name);
150 exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
153 static void version(void)
155 fprintf(stdout, "%s from %s\n", program_invocation_short_name,
156 PACKAGE_STRING);
157 exit(EXIT_SUCCESS);
160 int main(int argc, char *argv[])
162 char *newroot, *init, **initargs;
164 if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
165 usage(stdout);
166 if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
167 version();
168 if (argc < 3)
169 usage(stderr);
171 newroot = argv[1];
172 init = argv[2];
173 initargs = &argv[2];
175 if (!*newroot || !*init)
176 usage(stderr);
178 if (switchroot(newroot))
179 errx(EXIT_FAILURE, "failed. Sorry.");
181 if (access(init, X_OK))
182 warn("cannot access %s", init);
184 /* get session leader */
185 setsid();
187 /* set controlling terminal */
188 if (ioctl (0, TIOCSCTTY, 1))
189 warn("failed to TIOCSCTTY");
191 execv(init, initargs);
192 err(EXIT_FAILURE, "failed to execute %s", init);