From 44bca4f83d837cc5a044af6911d6cb560962b8ae Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Mon, 5 Nov 2018 11:05:47 +0000 Subject: [PATCH] shutdown: use argv 0 to determine default action. If called as "reboot", default action is to reboot, otherwise to halt and power down. --- doc/manpages/shutdown.8 | 10 ++++++---- src/includes/dinit-util.h | 12 ++++++++++++ src/shutdown.cc | 26 ++++++++++++++++---------- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/doc/manpages/shutdown.8 b/doc/manpages/shutdown.8 index c19e609..863646d 100644 --- a/doc/manpages/shutdown.8 +++ b/doc/manpages/shutdown.8 @@ -20,6 +20,9 @@ service manager package. See \fBdinit\fR(8). The shutdown, reboot and halt commands can be used to instruct the service manager daemon to perform a service rollback and then to shutdown the system. They can also perform shutdown directly, without service rollback. + +Note that for consistency with other packages a "halt" alias is provided, +however it has no special significance. .\" .SH OPTIONS .TP @@ -28,12 +31,11 @@ Request a shutdown followed by restart. This is the default if executed as \fBreboot\fR. .TP \fB\-h\fP -Shutdown and then halt the system. This is the default if executed as -neither \fBhalt\fR nor \fBreboot\fR. +Shutdown and then halt the system (without powering down). .TP \fB\-p\fP -Shutdown and then power down the system. This is the default if executed as -\fBhalt\fR. +Shutdown and then power down the system. This is the default unless executed +as \fBreboot\fR. .TP \fB\-\-use\-passed\-cfd\fR Instead of attempting to open a socket connection to the service daemon, diff --git a/src/includes/dinit-util.h b/src/includes/dinit-util.h index a99e9cb..d9b689c 100644 --- a/src/includes/dinit-util.h +++ b/src/includes/dinit-util.h @@ -63,4 +63,16 @@ inline std::string parent_path(const std::string &p) return p.substr(0, spos + 1); } +// Find the base name of a path (the name after the final '/'). +inline const char * base_name(const char *path) +{ + const char * basen = path; + const char * s = path; + while (*s != 0) { + if (*s == '/') basen = s + 1; + s++; + } + return basen; +} + #endif diff --git a/src/shutdown.cc b/src/shutdown.cc index e20858d..947896b 100644 --- a/src/shutdown.cc +++ b/src/shutdown.cc @@ -18,6 +18,7 @@ #include "control-cmds.h" #include "service-constants.h" #include "dinit-client.h" +#include "dinit-util.h" #include "dasynq.h" @@ -246,6 +247,11 @@ int main(int argc, char **argv) bool use_passed_cfd = false; auto shutdown_type = shutdown_type_t::POWEROFF; + + const char *execname = base_name(argv[0]); + if (strcmp(execname, "reboot")) { + shutdown_type = shutdown_type_t::REBOOT; + } for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { @@ -281,16 +287,16 @@ int main(int argc, char **argv) } if (show_help) { - cout << "dinit-shutdown : shutdown the system" << endl; - cout << " --help : show this help" << endl; - cout << " -r : reboot" << endl; - cout << " -h : halt system" << endl; - cout << " -p : power down (default)" << endl; - cout << " --use-passed-cfd : use the socket file descriptor identified by the DINIT_CS_FD" << endl; - cout << " environment variable to communicate with the init daemon." << endl; - cout << " --system : perform shutdown immediately, instead of issuing shutdown" << endl; - cout << " command to the init program. Not recommended for use" << endl; - cout << " by users." << endl; + cout << execname << " : shutdown the system\n" + " --help : show this help\n" + " -r : reboot\n" + " -h : halt system\n" + " -p : power down (default)\n" + " --use-passed-cfd : use the socket file descriptor identified by the DINIT_CS_FD\n" + " environment variable to communicate with the init daemon.\n" + " --system : perform shutdown immediately, instead of issuing shutdown\n" + " command to the init program. Not recommended for use\n" + " by users.\n"; return 1; } -- 2.25.1