PageRenderTime 93ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/util-linux-ng/sys-utils/switch_root.c

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