1 /* chroot -- run command or shell with special root directory
2 Copyright (C) 95, 96, 1997, 1999-2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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 "Roland McGrath"
35 /* The name this program was run with, for error messages. */
41 if (status
!= EXIT_SUCCESS
)
42 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
47 Usage: %s NEWROOT [COMMAND...]\n\
49 "), program_name
, program_name
);
51 Run COMMAND with root directory set to NEWROOT.\n\
54 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
55 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
58 If no command is given, run ``${SHELL} -i'' (default: /bin/sh).\n\
60 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
66 main (int argc
, char **argv
)
68 initialize_main (&argc
, &argv
);
69 program_name
= argv
[0];
70 setlocale (LC_ALL
, "");
71 bindtextdomain (PACKAGE
, LOCALEDIR
);
74 initialize_exit_failure (EXIT_FAIL
);
75 atexit (close_stdout
);
77 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
78 usage
, AUTHORS
, (char const *) NULL
);
79 if (getopt_long (argc
, argv
, "+", NULL
, NULL
) != -1)
84 error (0, 0, _("missing operand"));
88 if (chroot (argv
[optind
]) != 0)
89 error (EXIT_FAIL
, errno
, _("cannot change root directory to %s"), argv
[1]);
92 error (EXIT_FAIL
, errno
, _("cannot chdir to root directory"));
94 if (argc
== optind
+ 1)
96 /* No command. Run an interactive shell. */
97 char *shell
= getenv ("SHELL");
106 /* The following arguments give the command. */
110 /* Execute the given command. */
111 execvp (argv
[0], argv
);
114 int exit_status
= (errno
== ENOENT
? EXIT_ENOENT
: EXIT_CANNOT_INVOKE
);
115 error (0, errno
, _("cannot run command %s"), quote (argv
[0]));