From 2d54b32d2406de4ad624c011bf4b30ed45dabdaf Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 24 Aug 2018 10:35:23 +0200 Subject: Cleanup shutdown command, make reboot a symlink to shutdown Signed-off-by: David Oberhollenzer --- Makefile.am | 3 +++ cmd/Makemodule.am | 11 ++------ cmd/shutdown.c | 79 +++++++++++++++++++++++-------------------------------- 3 files changed, 38 insertions(+), 55 deletions(-) diff --git a/Makefile.am b/Makefile.am index 3914c91..1a34912 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,6 +24,9 @@ if USYSLOGD include syslogd/Makemodule.am endif +install-exec-hook: + (cd $(DESTDIR)$(sbindir); $(LN_S) shutdown reboot) + install-data-local: $(MKDIR_P) $(DESTDIR)$(SVCDIR) $(LN_S) $(TEMPLATEDIR)/loopback $(DESTDIR)$(SVCDIR)/loopback diff --git a/cmd/Makemodule.am b/cmd/Makemodule.am index f4559a9..ef22284 100644 --- a/cmd/Makemodule.am +++ b/cmd/Makemodule.am @@ -1,14 +1,7 @@ shutdown_SOURCES = cmd/shutdown.c -shutdown_CPPFLAGS = $(AM_CPPFLAGS) -DPROGNAME=shutdown +shutdown_CPPFLAGS = $(AM_CPPFLAGS) shutdown_CFLAGS = $(AM_CFLAGS) shutdown_LDFLAGS = $(AM_LDFLAGS) -shutdown_LDADD = libinit.a - -reboot_SOURCES = cmd/shutdown.c -reboot_CPPFLAGS = $(AM_CPPFLAGS) -DPROGNAME=reboot -reboot_CFLAGS = $(AM_CFLAGS) -reboot_LDFLAGS = $(AM_LDFLAGS) -reboot_LDADD = libinit.a runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h runsvc_CPPFLAGS = $(AM_CPPFLAGS) @@ -44,5 +37,5 @@ endif EXTRA_DIST += $(SRVHEADERS) -sbin_PROGRAMS += service reboot shutdown +sbin_PROGRAMS += service shutdown helper_PROGRAMS += killall5 runsvc diff --git a/cmd/shutdown.c b/cmd/shutdown.c index 0d11cc5..39145ec 100644 --- a/cmd/shutdown.c +++ b/cmd/shutdown.c @@ -19,19 +19,15 @@ #include #include #include +#include #include #include #include #include -#include "telinit.h" #include "util.h" -#define STRINIFY(x) #x -#define STRINIFY_VALUE(x) STRINIFY(x) -#define PROGRAM_NAME STRINIFY_VALUE(PROGNAME) - #define FL_FORCE 0x01 #define FL_NOSYNC 0x02 @@ -48,9 +44,9 @@ static const struct option options[] = { static const char *shortopt = "hVprfn"; static const char *defact_str = "power-off"; -static int defact = TI_SHUTDOWN; +static int defact = RB_POWER_OFF; -static NORETURN void usage(int status) +static NORETURN void usage(const char *progname, int status) { fprintf(status == EXIT_SUCCESS ? stdout : stderr, "%s [OPTIONS...]\n\n" @@ -63,32 +59,34 @@ static NORETURN void usage(int status) " init system.\n" " -n, --no-sync Don't sync storage media before power-off or reboot.\n\n" "If no option is specified, the default action is %s.\n", - PROGRAM_NAME, defact_str); + progname, defact_str); exit(status); } -static NORETURN void version(void) +static NORETURN void version(const char *progname) { - fputs( -PROGRAM_NAME " (Pygos init) " PACKAGE_VERSION "\n" + fprintf(stdout, +"%s (Pygos init) %s\n" "Copyright (C) 2018 David Oberhollenzer\n" "License GPLv3+: GNU GPL version 3 or later .\n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n", - stdout); + progname, PACKAGE_VERSION); exit(EXIT_SUCCESS); } int main(int argc, char **argv) { - int c, fd, flags = 0; - ti_msg_t msg; - ssize_t ret; + int c, ret, flags = 0; + char *ptr; + + ptr = strrchr(argv[0], '/'); + ptr = (ptr == NULL) ? argv[0] : (ptr + 1); - if (!strcmp(PROGRAM_NAME, "reboot")) { + if (strcmp(ptr, "reboot") == 0) { defact_str = "reboot"; - defact = TI_REBOOT; + defact = RB_AUTOBOOT; } while (1) { @@ -104,53 +102,42 @@ int main(int argc, char **argv) flags |= FL_NOSYNC; break; case 'p': - defact = TI_SHUTDOWN; + defact = RB_POWER_OFF; break; case 'r': - defact = TI_REBOOT; + defact = RB_AUTOBOOT; break; case 'V': - version(); + version(ptr); case 'h': - usage(EXIT_SUCCESS); + usage(ptr, EXIT_SUCCESS); default: - exit(EXIT_FAILURE); + usage(ptr, EXIT_FAILURE); } } if (flags & FL_FORCE) { if (!(flags & FL_NOSYNC)) sync(); - - switch (defact) { - case TI_REBOOT: - reboot(RB_AUTOBOOT); - break; - case TI_SHUTDOWN: - reboot(RB_POWER_OFF); - break; - } - + reboot(defact); perror("reboot system call"); return EXIT_FAILURE; } - fd = opensock(); - if (fd < 0) - return EXIT_FAILURE; - - msg.type = defact; -retry: - ret = write(fd, &msg, sizeof(msg)); + switch (defact) { + case RB_AUTOBOOT: + ret = kill(1, SIGINT); + break; + case RB_POWER_OFF: + ret = kill(1, SIGTERM); + break; + default: + return EXIT_SUCCESS; + } - if (ret < 0) { - if (errno == EINTR) - goto retry; - perror("write on init socket"); - close(fd); + if (ret) { + perror("sending signal to init"); return EXIT_FAILURE; } - - close(fd); return EXIT_SUCCESS; } -- cgit v1.2.3