mirror of
https://github.com/pygos/init.git
synced 2024-12-22 07:10:49 +01:00
Use services to implement shutdown/reboot sequence
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
parent
0a28074071
commit
021f091082
12 changed files with 61 additions and 77 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -25,3 +25,8 @@ services/hostname
|
|||
services/loopback
|
||||
services/sysctl
|
||||
services/hwclock
|
||||
services/shutdown
|
||||
services/reboot
|
||||
services/sigkill
|
||||
services/sigterm
|
||||
services/sync
|
||||
|
|
|
@ -23,5 +23,10 @@ AC_CONFIG_FILES([services/hostname])
|
|||
AC_CONFIG_FILES([services/loopback])
|
||||
AC_CONFIG_FILES([services/sysctl])
|
||||
AC_CONFIG_FILES([services/hwclock])
|
||||
AC_CONFIG_FILES([services/reboot])
|
||||
AC_CONFIG_FILES([services/shutdown])
|
||||
AC_CONFIG_FILES([services/sigkill])
|
||||
AC_CONFIG_FILES([services/sigterm])
|
||||
AC_CONFIG_FILES([services/sync])
|
||||
|
||||
AC_OUTPUT([Makefile])
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
init_SOURCES = initd/main.c initd/runlst.c initd/init.h initd/setup_tty.c
|
||||
init_SOURCES += initd/status.c initd/mksock.c initd/shutdown.c initd/svclist.c
|
||||
init_SOURCES += initd/status.c initd/mksock.c initd/svclist.c
|
||||
init_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
init_CFLAGS = $(AM_CFLAGS)
|
||||
init_LDFLAGS = $(AM_LDFLAGS)
|
||||
|
|
10
initd/init.h
10
initd/init.h
|
@ -98,16 +98,6 @@ void print_status(const char *msg, int type, bool update);
|
|||
*/
|
||||
int mksock(void);
|
||||
|
||||
/********** shutdown.c **********/
|
||||
|
||||
/*
|
||||
Kindly tell all processes to go kill themselves, then send
|
||||
a SIGKILL if they don't and perform system shutdown.
|
||||
|
||||
The argument is passed directly to the reboot() syscall.
|
||||
*/
|
||||
NORETURN void do_shutdown(int type);
|
||||
|
||||
/********** svclist.c **********/
|
||||
|
||||
/*
|
||||
|
|
16
initd/main.c
16
initd/main.c
|
@ -226,18 +226,10 @@ int main(void)
|
|||
pfd[0].events = pfd[1].events = POLLIN;
|
||||
|
||||
for (;;) {
|
||||
if (!svclist_have_singleshot()) {
|
||||
if (target != runlevel) {
|
||||
start_runlevel(target);
|
||||
runlevel = target;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (runlevel == TGT_SHUTDOWN)
|
||||
do_shutdown(RB_POWER_OFF);
|
||||
|
||||
if (runlevel == TGT_REBOOT)
|
||||
do_shutdown(RB_AUTOBOOT);
|
||||
if (!svclist_have_singleshot() && target != runlevel) {
|
||||
start_runlevel(target);
|
||||
runlevel = target;
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = poll(pfd, sizeof(pfd) / sizeof(pfd[0]), -1);
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "init.h"
|
||||
|
||||
void do_shutdown(int type)
|
||||
{
|
||||
struct timespec req, rem;
|
||||
|
||||
print_status("sending SIGTERM to all processes", STATUS_WAIT, false);
|
||||
kill(-1, SIGTERM);
|
||||
|
||||
memset(&req, 0, sizeof(req));
|
||||
memset(&rem, 0, sizeof(rem));
|
||||
req.tv_sec = 5; /* TODO: make configurable? */
|
||||
|
||||
while (nanosleep(&req, &rem) != 0 && errno == EINTR)
|
||||
req = rem;
|
||||
|
||||
print_status("sending SIGTERM to all processes", STATUS_OK, true);
|
||||
kill(-1, SIGKILL);
|
||||
print_status("sending SIGKILL to remaining processes",
|
||||
STATUS_OK, false);
|
||||
|
||||
print_status("sync", STATUS_WAIT, false);
|
||||
sync();
|
||||
print_status("sync", STATUS_OK, true);
|
||||
|
||||
reboot(type);
|
||||
perror("reboot system call");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
initdir = @TEMPLATEDIR@
|
||||
init_DATA = services/agetty services/hostname services/loopback
|
||||
init_DATA += services/sysctl services/hwclock services/sysinit
|
||||
init_DATA += services/reboot services/shutdown services/sigkill
|
||||
init_DATA += services/sigterm services/sync
|
||||
|
||||
EXTRA_DIST += services/sysinit
|
||||
|
|
8
services/reboot.in
Normal file
8
services/reboot.in
Normal file
|
@ -0,0 +1,8 @@
|
|||
name = "reboot"
|
||||
description = "system reboot"
|
||||
exec = "@SBINPATH@/shutdown -nrf"
|
||||
type = wait
|
||||
target = reboot
|
||||
after = "sync"
|
||||
after = "sigkill"
|
||||
after = "sigterm"
|
8
services/shutdown.in
Normal file
8
services/shutdown.in
Normal file
|
@ -0,0 +1,8 @@
|
|||
name = "shutdown"
|
||||
description = "system shutdown"
|
||||
exec = "@SBINPATH@/shutdown -npf"
|
||||
type = wait
|
||||
target = shutdown
|
||||
after = "sync"
|
||||
after = "sigkill"
|
||||
after = "sigterm"
|
9
services/sigkill.in
Normal file
9
services/sigkill.in
Normal file
|
@ -0,0 +1,9 @@
|
|||
name = "sigkill"
|
||||
description = "send SIGKILL to remaining processes"
|
||||
exec = "@SCRIPTDIR@/killall5 9"
|
||||
type = wait
|
||||
target = "%0"
|
||||
after = "sigterm"
|
||||
before = "sync"
|
||||
before = "shutdown"
|
||||
before = "reboot"
|
10
services/sigterm.in
Normal file
10
services/sigterm.in
Normal file
|
@ -0,0 +1,10 @@
|
|||
name = "sigterm"
|
||||
description = "send SIGTERM to all processes"
|
||||
exec = "@SCRIPTDIR@/killall5 15"
|
||||
exec = "@BINPATH@/sleep 5"
|
||||
type = wait
|
||||
target = "%0"
|
||||
before = "sigkill"
|
||||
before = "sync"
|
||||
before = "reboot"
|
||||
before = "shutdown"
|
9
services/sync.in
Normal file
9
services/sync.in
Normal file
|
@ -0,0 +1,9 @@
|
|||
name = "sync"
|
||||
description = "sync"
|
||||
exec = "@BINPATH@/sync"
|
||||
type = wait
|
||||
target = "%0"
|
||||
after = "sigkill"
|
||||
after = "sigterm"
|
||||
before = "reboot"
|
||||
before = "shutdown"
|
Loading…
Reference in a new issue