From ae9c706359d21fc23e9260b0a98afe083681c29b Mon Sep 17 00:00:00 2001 From: naddy Date: Wed, 21 Jan 2015 19:38:53 +0000 Subject: [PATCH] Build the argument list for halt/reboot without excessive use of the conditional operator. Adapted from NetBSD. ok miod@ --- sbin/shutdown/shutdown.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/sbin/shutdown/shutdown.c b/sbin/shutdown/shutdown.c index af6b4c7b22e..31e814ffb4a 100644 --- a/sbin/shutdown/shutdown.c +++ b/sbin/shutdown/shutdown.c @@ -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; -- 2.20.1