summaryrefslogtreecommitdiffstats
path: root/sys-utils/switch_root.c
diff options
context:
space:
mode:
authorKarel Zak2009-06-09 16:16:46 +0200
committerKarel Zak2009-06-22 21:30:47 +0200
commit8b6457d06a1e27228975b35418cacb1bc3027201 (patch)
tree6ab53e94813b899d427decd578c1bcda3038c458 /sys-utils/switch_root.c
parentswitch_root: use err.h, clean up return codes (diff)
downloadkernel-qcow2-util-linux-8b6457d06a1e27228975b35418cacb1bc3027201.tar.gz
kernel-qcow2-util-linux-8b6457d06a1e27228975b35418cacb1bc3027201.tar.xz
kernel-qcow2-util-linux-8b6457d06a1e27228975b35418cacb1bc3027201.zip
switch_root: clean up argv[] usage, add -h and -V
Signed-off-by: Peter Jones <pjones@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/switch_root.c')
-rw-r--r--sys-utils/switch_root.c47
1 files changed, 27 insertions, 20 deletions
diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index 5dcc7f1e4..14d2916bf 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -38,12 +38,6 @@
#define MS_MOVE 8192
#endif
-enum {
- ok,
- err_no_directory,
- err_usage,
-};
-
/* remove all files/directories below dirName -- don't cross mountpoints */
static int recursiveRemove(char *dirName)
{
@@ -115,7 +109,6 @@ static int switchroot(const char *newroot)
{
/* Don't try to unmount the old "/", there's no way to do it. */
const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
- int errnum;
int i;
for (i = 0; umounts[i] != NULL; i++) {
@@ -153,34 +146,48 @@ static void usage(FILE *output)
{
fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
program_invocation_short_name);
- if (output == stderr)
- exit(EXIT_FAILURE);
+ exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+static void version(void)
+{
+ fprintf(stdout, "%s from %s\n", program_invocation_short_name,
+ PACKAGE_STRING);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
- char *newroot = argv[1];
- char *init = argv[2];
- char **initargs = &argv[2];
+ char *newroot, *init, **initargs;
- if (newroot == NULL || newroot[0] == '\0' ||
- init == NULL || init[0] == '\0' ) {
+ if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
+ usage(stdout);
+ if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
+ version();
+ if (argc < 3)
+ usage(stderr);
+
+ newroot = argv[1];
+ init = argv[2];
+ initargs = &argv[2];
+
+ if (!*newroot || !*init)
usage(stderr);
- }
if (switchroot(newroot))
errx(EXIT_FAILURE, "failed. Sorry.");
- if (access(initargs[0], X_OK))
- warn("cannot access %s", initargs[0]);
+ if (access(init, X_OK))
+ warn("cannot access %s", init);
/* get session leader */
setsid();
+
/* set controlling terminal */
- ioctl (0, TIOCSCTTY, 1);
+ if (ioctl (0, TIOCSCTTY, 1))
+ warn("failed to TIOCSCTTY");
- execv(initargs[0], initargs);
- err(EXIT_FAILURE, "failed to execute %s", initargs[0]);
+ execv(init, initargs);
+ err(EXIT_FAILURE, "failed to execute %s", init);
}