chroot.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini chroot implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config CHROOT
  10. //config: bool "chroot"
  11. //config: default y
  12. //config: help
  13. //config: chroot is used to change the root directory and run a command.
  14. //config: The default command is `/bin/sh'.
  15. //applet:IF_CHROOT(APPLET(chroot, BB_DIR_USR_SBIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_CHROOT) += chroot.o
  17. /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
  18. //usage:#define chroot_trivial_usage
  19. //usage: "NEWROOT [PROG ARGS]"
  20. //usage:#define chroot_full_usage "\n\n"
  21. //usage: "Run PROG with root directory set to NEWROOT"
  22. //usage:
  23. //usage:#define chroot_example_usage
  24. //usage: "$ ls -l /bin/ls\n"
  25. //usage: "lrwxrwxrwx 1 root root 12 Apr 13 00:46 /bin/ls -> /BusyBox\n"
  26. //usage: "# mount /dev/hdc1 /mnt -t minix\n"
  27. //usage: "# chroot /mnt\n"
  28. //usage: "# ls -l /bin/ls\n"
  29. //usage: "-rwxr-xr-x 1 root root 40816 Feb 5 07:45 /bin/ls*\n"
  30. #include "libbb.h"
  31. int chroot_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  32. int chroot_main(int argc UNUSED_PARAM, char **argv)
  33. {
  34. ++argv;
  35. if (!*argv)
  36. bb_show_usage();
  37. xchroot(*argv);
  38. ++argv;
  39. if (!*argv) { /* no 2nd param (PROG), use shell */
  40. argv -= 2;
  41. argv[0] = (char *) get_shell_name();
  42. argv[1] = (char *) "-i"; /* GNU coreutils 8.4 compat */
  43. /*argv[2] = NULL; - already is */
  44. }
  45. BB_EXECVP_or_die(argv);
  46. }