[PATCH 7/9] switch_root: clean up argv[] usage Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 sys-utils/switch_root.c |   43 ++++++++++++++++++++-----------------------
 1 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index 9fd4bbe..cb89431 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)
 {
@@ -149,38 +143,41 @@ static int switchroot(const char *newroot)
 	return 0;
 }
 
-static void usage(FILE *output)
+static void usage()
 {
-	fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
+	fprintf(stderr, "usage: %s <newrootdir> <init> <args to init>\n",
 			program_invocation_short_name);
-	if (output == stderr)
-		exit(EXIT_FAILURE);
-	exit(EXIT_SUCCESS);
+	exit(EXIT_FAILURE);
 }
 
 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' ) {
-		usage(stderr);
-	}
+	if (argc < 3)
+		usage();
+
+	newroot = argv[1];
+	init = argv[2];
+	initargs = &argv[2];
+
+	if (!*newroot || !*init)
+		usage();
 
 	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);
 }
 
-- 
1.6.2.2

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux