From 8b6457d06a1e27228975b35418cacb1bc3027201 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 9 Jun 2009 16:16:46 +0200 Subject: switch_root: clean up argv[] usage, add -h and -V Signed-off-by: Peter Jones Signed-off-by: Karel Zak --- sys-utils/switch_root.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'sys-utils/switch_root.c') 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 \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); } -- cgit v1.2.3-55-g7522