]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/switch_root.c
switch_root: use file descriptor instead of path for recursiveRemove()
[thirdparty/util-linux.git] / sys-utils / switch_root.c
1 /*
2 * switchroot.c - switch to new root directory and start init.
3 *
4 * Copyright 2002-2009 Red Hat, Inc. All rights reserved.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Authors:
20 * Peter Jones <pjones@redhat.com>
21 * Jeremy Katz <katzj@redhat.com>
22 */
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>
36
37 #ifndef MS_MOVE
38 #define MS_MOVE 8192
39 #endif
40
41 /* remove all files/directories below dirName -- don't cross mountpoints */
42 static int recursiveRemove(int fd)
43 {
44 struct stat rb;
45 DIR *dir;
46 int rc = -1;
47 int dfd;
48
49 if (!(dir = fdopendir(fd))) {
50 warn("failed to open directory");
51 goto done;
52 }
53
54 /* fdopendir() precludes us from continuing to use the input fd */
55 dfd = dirfd(dir);
56
57 if (fstat(dfd, &rb)) {
58 warn("failed to stat directory");
59 goto done;
60 }
61
62 while(1) {
63 struct dirent *d;
64
65 errno = 0;
66 if (!(d = readdir(dir))) {
67 if (errno) {
68 warn("failed to read directory");
69 goto done;
70 }
71 break; /* end of directory */
72 }
73
74 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
75 continue;
76
77 if (d->d_type == DT_DIR) {
78 struct stat sb;
79
80 if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
81 warn("failed to stat %s", d->d_name);
82 continue;
83 }
84
85 /* remove subdirectories if device is same as dir */
86 if (sb.st_dev == rb.st_dev) {
87 int cfd;
88
89 cfd = openat(dfd, d->d_name, O_RDONLY);
90 if (cfd >= 0) {
91 recursiveRemove(cfd);
92 close(cfd);
93 }
94 } else
95 continue;
96 }
97
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);
101 }
102
103 rc = 0; /* success */
104
105 done:
106 if (dir)
107 closedir(dir);
108 return rc;
109 }
110
111 static int switchroot(const char *newroot)
112 {
113 /* Don't try to unmount the old "/", there's no way to do it. */
114 const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
115 int i;
116 int cfd;
117
118 for (i = 0; umounts[i] != NULL; i++) {
119 char newmount[PATH_MAX];
120
121 snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
122
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);
128 }
129 }
130
131 if (chdir(newroot)) {
132 warn("failed to change directory to %s", newroot);
133 return -1;
134 }
135
136 cfd = open("/", O_RDONLY);
137 if (cfd >= 0) {
138 recursiveRemove(cfd);
139 close(cfd);
140 }
141
142 if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
143 warn("failed to mount moving %s to /", newroot);
144 return -1;
145 }
146
147 if (chroot(".")) {
148 warn("failed to change root");
149 return -1;
150 }
151 return 0;
152 }
153
154 static void usage(FILE *output)
155 {
156 fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
157 program_invocation_short_name);
158 exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
159 }
160
161 static void version(void)
162 {
163 fprintf(stdout, "%s from %s\n", program_invocation_short_name,
164 PACKAGE_STRING);
165 exit(EXIT_SUCCESS);
166 }
167
168 int main(int argc, char *argv[])
169 {
170 char *newroot, *init, **initargs;
171
172 if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
173 usage(stdout);
174 if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
175 version();
176 if (argc < 3)
177 usage(stderr);
178
179 newroot = argv[1];
180 init = argv[2];
181 initargs = &argv[2];
182
183 if (!*newroot || !*init)
184 usage(stderr);
185
186 if (switchroot(newroot))
187 errx(EXIT_FAILURE, "failed. Sorry.");
188
189 if (access(init, X_OK))
190 warn("cannot access %s", init);
191
192 /* get session leader */
193 setsid();
194
195 /* set controlling terminal */
196 if (ioctl (0, TIOCSCTTY, 1))
197 warn("failed to TIOCSCTTY");
198
199 execv(init, initargs);
200 err(EXIT_FAILURE, "failed to execute %s", init);
201 }
202