Build the argument list for halt/reboot without excessive use of
authornaddy <naddy@openbsd.org>
Wed, 21 Jan 2015 19:38:53 +0000 (19:38 +0000)
committernaddy <naddy@openbsd.org>
Wed, 21 Jan 2015 19:38:53 +0000 (19:38 +0000)
the conditional operator.  Adapted from NetBSD.  ok miod@

sbin/shutdown/shutdown.c

index af6b4c7..31e814f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: shutdown.c,v 1.39 2015/01/21 19:29:52 naddy Exp $     */
+/*     $OpenBSD: shutdown.c,v 1.40 2015/01/21 19:38:53 naddy Exp $     */
 /*     $NetBSD: shutdown.c,v 1.9 1995/03/18 15:01:09 cgd Exp $ */
 
 /*
@@ -359,20 +359,29 @@ die_you_gravy_sucking_pig_dog(void)
                (void)printf(" with dump");
        (void)printf("\nkill -HUP 1\n");
 #else
-       if (doreboot) {
-               execle(_PATH_REBOOT, "reboot", "-l",
-                   (nosync ? "-n" : (dodump ? "-d" : NULL)),
-                   (dodump ? "-d" : NULL), (char *)NULL, (char *)NULL);
-               syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT);
-               warn(_PATH_REBOOT);
-       }
-       else if (dohalt || dopower) {
-               execle(_PATH_HALT, "halt", "-l",
-                   (dopower ? "-p" : (nosync ? "-n" : (dodump ? "-d" : NULL))),
-                   (nosync ? "-n" : (dodump ? "-d" : NULL)),
-                   (dodump ? "-d" : NULL), (char *)NULL, (char *)NULL);
-               syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT);
-               warn(_PATH_HALT);
+       if (dohalt || dopower || doreboot) {
+               char *args[10];
+               char **arg, *path;
+
+               arg = &args[0];
+               if (doreboot) {
+                       path = _PATH_REBOOT;
+                       *arg++ = "reboot";
+               } else {
+                       path = _PATH_HALT;
+                       *arg++ = "halt";
+               }
+               *arg++ = "-l";
+               if (dopower)
+                       *arg++ = "-p";
+               if (nosync)
+                       *arg++ = "-n";
+               if (dodump)
+                       *arg++ = "-d";
+               *arg++ = NULL;
+               execve(path, args, NULL);
+               syslog(LOG_ERR, "shutdown: can't exec %s: %m.", path);
+               warn(path);
        }
        if (access(_PATH_RC, R_OK) != -1) {
                pid_t pid;