1 /* chroot -- run command or shell with special root directory
2 Copyright (C) 95, 96, 1997, 1999-2004, 2007-2008
3 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Roland McGrath. */
23 #include <sys/types.h>
27 #include "long-options.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "chroot"
33 #define AUTHORS proper_name ("Roland McGrath")
38 if (status != EXIT_SUCCESS)
39 fprintf (stderr, _("Try `%s --help' for more information.\n"),
44 Usage: %s NEWROOT [COMMAND [ARG]...]\n\
46 "), program_name, program_name);
48 Run COMMAND with root directory set to NEWROOT.\n\
51 fputs (HELP_OPTION_DESCRIPTION, stdout);
52 fputs (VERSION_OPTION_DESCRIPTION, stdout);
55 If no command is given, run ``${SHELL} -i'' (default: /bin/sh).\n\
57 emit_bug_reporting_address ();
63 main (int argc, char **argv)
65 initialize_main (&argc, &argv);
66 set_program_name (argv[0]);
67 setlocale (LC_ALL, "");
68 bindtextdomain (PACKAGE, LOCALEDIR);
71 initialize_exit_failure (EXIT_FAILURE);
72 atexit (close_stdout);
74 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
75 usage, AUTHORS, (char const *) NULL);
76 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
81 error (0, 0, _("missing operand"));
85 if (chroot (argv[optind]) != 0)
86 error (EXIT_FAILURE, errno, _("cannot change root directory to %s"), argv[1]);
89 error (EXIT_FAILURE, errno, _("cannot chdir to root directory"));
91 if (argc == optind + 1)
93 /* No command. Run an interactive shell. */
94 char *shell = getenv ("SHELL");
103 /* The following arguments give the command. */
107 /* Execute the given command. */
108 execvp (argv[0], argv);
111 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
112 error (0, errno, _("cannot run command %s"), quote (argv[0]));