Upload Tizen:Base source
[framework/base/util-linux-ng.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 #include <libgen.h>
37
38 #ifndef MS_MOVE
39 #define MS_MOVE 8192
40 #endif
41
42 /* remove all files/directories below dirName -- don't cross mountpoints */
43 static int recursiveRemove(int fd)
44 {
45         struct stat rb;
46         DIR *dir;
47         int rc = -1;
48         int dfd;
49
50         if (!(dir = fdopendir(fd))) {
51                 warn("failed to open directory");
52                 goto done;
53         }
54
55         /* fdopendir() precludes us from continuing to use the input fd */
56         dfd = dirfd(dir);
57
58         if (fstat(dfd, &rb)) {
59                 warn("failed to stat directory");
60                 goto done;
61         }
62
63         while(1) {
64                 struct dirent *d;
65
66                 errno = 0;
67                 if (!(d = readdir(dir))) {
68                         if (errno) {
69                                 warn("failed to read directory");
70                                 goto done;
71                         }
72                         break;  /* end of directory */
73                 }
74
75                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
76                         continue;
77
78                 if (d->d_type == DT_DIR) {
79                         struct stat sb;
80
81                         if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
82                                 warn("failed to stat %s", d->d_name);
83                                 continue;
84                         }
85
86                         /* remove subdirectories if device is same as dir */
87                         if (sb.st_dev == rb.st_dev) {
88                                 int cfd;
89
90                                 cfd = openat(dfd, d->d_name, O_RDONLY);
91                                 if (cfd >= 0) {
92                                         recursiveRemove(cfd);
93                                         close(cfd);
94                                 }
95                         } else
96                                 continue;
97                 }
98
99                 if (unlinkat(dfd, d->d_name,
100                              d->d_type == DT_DIR ? AT_REMOVEDIR : 0))
101                         warn("failed to unlink %s", d->d_name);
102         }
103
104         rc = 0; /* success */
105
106 done:
107         if (dir)
108                 closedir(dir);
109         return rc;
110 }
111
112 /* find the enclosing mount point of a path, by examining the backing device
113  * of parent directories until we reach / or find a parent with a differing
114  * device. Result must be freed.
115  */
116 static char *get_parent_mount(const char *path)
117 {
118         struct stat sb;
119         char *dir;
120         char tmp[PATH_MAX];
121         dev_t inner_dev;
122
123         if (stat(path, &sb) != 0) {
124                 warn("failed to stat %s", path);
125                 return NULL;
126         }
127
128         inner_dev = sb.st_dev;
129         dir = strdup(path);
130
131         while (dir) {
132                 char *parent;
133
134                 strncpy(tmp, dir, PATH_MAX);
135                 tmp[PATH_MAX - 1] = '\0';
136                 parent = dirname(tmp);
137
138                 if (stat(parent, &sb) != 0) {
139                         warn("failed to stat %s", parent);
140                         return NULL;
141                 }
142                 if (sb.st_dev != inner_dev)
143                         return dir;
144
145                 strncpy(dir, parent, PATH_MAX);
146                 dir[PATH_MAX - 1] = '\0';
147
148                 /* maybe we've reached / */
149                 if (*dir == '/' && !*(dir + 1))
150                         return dir;
151         }
152         return NULL;
153 }
154
155 static int switchroot(const char *newroot)
156 {
157         /*  Don't try to unmount the old "/", there's no way to do it. */
158         const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
159         int i;
160         int cfd, rc = -1;
161         pid_t pid;
162         const char *chroot_path = NULL;
163         char *newroot_mnt;
164
165         for (i = 0; umounts[i] != NULL; i++) {
166                 char newmount[PATH_MAX];
167
168                 snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
169
170                 if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
171                         warn("failed to mount moving %s to %s",
172                                 umounts[i], newmount);
173                         warnx("forcing unmount of %s", umounts[i]);
174                         umount2(umounts[i], MNT_FORCE);
175                 }
176         }
177
178         newroot_mnt = get_parent_mount(newroot);
179         if (newroot_mnt && strcmp(newroot, newroot_mnt)) {
180                 /* newroot is not a mount point, so we have to MS_MOVE the
181                  * parent mount point and then chroot in to the "subroot"
182                  */
183                 chroot_path = newroot + strlen(newroot_mnt);
184                 newroot = newroot_mnt;
185         }
186
187         if (chdir(newroot)) {
188                 warn("failed to change directory to %s", newroot);
189                 goto done;
190         }
191
192         cfd = open("/", O_RDONLY);
193
194         if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
195                 warn("failed to mount moving %s to /", newroot);
196                 goto done;
197         }
198
199         /* move to the real root of the device */
200         if (chroot(".")) {
201                 warn("failed to change root");
202                 goto done;
203         }
204
205         /* move to the subdirectory on the root device (subroot) */
206         if (chroot_path) {
207                 if (chdir(chroot_path)) {
208                         warn("failed to chdir to subroot %s", chroot_path);
209                         goto done;
210                 }
211                 if (chroot(".")) {
212                         warn("failed to change root to subroot %s", chroot_path);
213                         goto done;
214                 }
215         }
216
217         if (cfd >= 0) {
218                 pid = fork();
219                 if (pid <= 0) {
220                         recursiveRemove(cfd);
221                         if (pid == 0)
222                                 exit(EXIT_SUCCESS);
223                 }
224                 close(cfd);
225         }
226         rc = 0;
227 done:
228         free(newroot_mnt);
229         return rc;
230 }
231
232 static void usage(FILE *output)
233 {
234         fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
235                         program_invocation_short_name);
236         exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
237 }
238
239 static void version(void)
240 {
241         fprintf(stdout,  "%s from %s\n", program_invocation_short_name,
242                         PACKAGE_STRING);
243         exit(EXIT_SUCCESS);
244 }
245
246 int main(int argc, char *argv[])
247 {
248         char *newroot, *init, **initargs;
249
250         if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
251                 usage(stdout);
252         if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
253                 version();
254         if (argc < 3)
255                 usage(stderr);
256
257         newroot = argv[1];
258         init = argv[2];
259         initargs = &argv[2];
260
261         if (!*newroot || !*init)
262                 usage(stderr);
263
264         if (switchroot(newroot))
265                 errx(EXIT_FAILURE, "failed. Sorry.");
266
267         if (access(init, X_OK))
268                 warn("cannot access %s", init);
269
270         execv(init, initargs);
271         err(EXIT_FAILURE, "failed to execute %s", init);
272 }
273