1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 03:09:46 +01:00

Cleanup shutdown command, make reboot a symlink to shutdown

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-08-24 10:35:23 +02:00
parent ec6264bad5
commit 2d54b32d24
3 changed files with 39 additions and 56 deletions

View file

@ -24,6 +24,9 @@ if USYSLOGD
include syslogd/Makemodule.am include syslogd/Makemodule.am
endif endif
install-exec-hook:
(cd $(DESTDIR)$(sbindir); $(LN_S) shutdown reboot)
install-data-local: install-data-local:
$(MKDIR_P) $(DESTDIR)$(SVCDIR) $(MKDIR_P) $(DESTDIR)$(SVCDIR)
$(LN_S) $(TEMPLATEDIR)/loopback $(DESTDIR)$(SVCDIR)/loopback $(LN_S) $(TEMPLATEDIR)/loopback $(DESTDIR)$(SVCDIR)/loopback

View file

@ -1,14 +1,7 @@
shutdown_SOURCES = cmd/shutdown.c shutdown_SOURCES = cmd/shutdown.c
shutdown_CPPFLAGS = $(AM_CPPFLAGS) -DPROGNAME=shutdown shutdown_CPPFLAGS = $(AM_CPPFLAGS)
shutdown_CFLAGS = $(AM_CFLAGS) shutdown_CFLAGS = $(AM_CFLAGS)
shutdown_LDFLAGS = $(AM_LDFLAGS) 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_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h
runsvc_CPPFLAGS = $(AM_CPPFLAGS) runsvc_CPPFLAGS = $(AM_CPPFLAGS)
@ -44,5 +37,5 @@ endif
EXTRA_DIST += $(SRVHEADERS) EXTRA_DIST += $(SRVHEADERS)
sbin_PROGRAMS += service reboot shutdown sbin_PROGRAMS += service shutdown
helper_PROGRAMS += killall5 runsvc helper_PROGRAMS += killall5 runsvc

View file

@ -19,19 +19,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <sys/reboot.h> #include <sys/reboot.h>
#include <linux/reboot.h> #include <linux/reboot.h>
#include "telinit.h"
#include "util.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_FORCE 0x01
#define FL_NOSYNC 0x02 #define FL_NOSYNC 0x02
@ -48,9 +44,9 @@ static const struct option options[] = {
static const char *shortopt = "hVprfn"; static const char *shortopt = "hVprfn";
static const char *defact_str = "power-off"; 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, fprintf(status == EXIT_SUCCESS ? stdout : stderr,
"%s [OPTIONS...]\n\n" "%s [OPTIONS...]\n\n"
@ -63,32 +59,34 @@ static NORETURN void usage(int status)
" init system.\n" " init system.\n"
" -n, --no-sync Don't sync storage media before power-off or reboot.\n\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", "If no option is specified, the default action is %s.\n",
PROGRAM_NAME, defact_str); progname, defact_str);
exit(status); exit(status);
} }
static NORETURN void version(void) static NORETURN void version(const char *progname)
{ {
fputs( fprintf(stdout,
PROGRAM_NAME " (Pygos init) " PACKAGE_VERSION "\n" "%s (Pygos init) %s\n"
"Copyright (C) 2018 David Oberhollenzer\n" "Copyright (C) 2018 David Oberhollenzer\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\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" "This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n", "There is NO WARRANTY, to the extent permitted by law.\n",
stdout); progname, PACKAGE_VERSION);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c, fd, flags = 0; int c, ret, flags = 0;
ti_msg_t msg; char *ptr;
ssize_t ret;
if (!strcmp(PROGRAM_NAME, "reboot")) { ptr = strrchr(argv[0], '/');
ptr = (ptr == NULL) ? argv[0] : (ptr + 1);
if (strcmp(ptr, "reboot") == 0) {
defact_str = "reboot"; defact_str = "reboot";
defact = TI_REBOOT; defact = RB_AUTOBOOT;
} }
while (1) { while (1) {
@ -104,53 +102,42 @@ int main(int argc, char **argv)
flags |= FL_NOSYNC; flags |= FL_NOSYNC;
break; break;
case 'p': case 'p':
defact = TI_SHUTDOWN; defact = RB_POWER_OFF;
break; break;
case 'r': case 'r':
defact = TI_REBOOT; defact = RB_AUTOBOOT;
break; break;
case 'V': case 'V':
version(); version(ptr);
case 'h': case 'h':
usage(EXIT_SUCCESS); usage(ptr, EXIT_SUCCESS);
default: default:
exit(EXIT_FAILURE); usage(ptr, EXIT_FAILURE);
} }
} }
if (flags & FL_FORCE) { if (flags & FL_FORCE) {
if (!(flags & FL_NOSYNC)) if (!(flags & FL_NOSYNC))
sync(); sync();
reboot(defact);
switch (defact) {
case TI_REBOOT:
reboot(RB_AUTOBOOT);
break;
case TI_SHUTDOWN:
reboot(RB_POWER_OFF);
break;
}
perror("reboot system call"); perror("reboot system call");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
fd = opensock(); switch (defact) {
if (fd < 0) case RB_AUTOBOOT:
return EXIT_FAILURE; ret = kill(1, SIGINT);
break;
msg.type = defact; case RB_POWER_OFF:
retry: ret = kill(1, SIGTERM);
ret = write(fd, &msg, sizeof(msg)); break;
default:
if (ret < 0) { return EXIT_SUCCESS;
if (errno == EINTR)
goto retry;
perror("write on init socket");
close(fd);
return EXIT_FAILURE;
} }
close(fd); if (ret) {
perror("sending signal to init");
return EXIT_FAILURE;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }