aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-08-24 10:35:23 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-08-24 21:17:31 +0200
commit2d54b32d2406de4ad624c011bf4b30ed45dabdaf (patch)
treefa023ddc62750e3ed7f82b07eb9f3d655688fd7f
parentec6264bad59d103671c1529e47d1598941dba4b1 (diff)
Cleanup shutdown command, make reboot a symlink to shutdown
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
-rw-r--r--Makefile.am3
-rw-r--r--cmd/Makemodule.am11
-rw-r--r--cmd/shutdown.c79
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 <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <sys/reboot.h>
#include <linux/reboot.h>
-#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 <http://gnu.org/licenses/gpl.html>.\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;
}