From b79245ddb3d54343871e0428e1f9513a0a589e05 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 18 May 2021 08:23:42 +0200 Subject: [PATCH] Add support for sys/pwr/fail and sys/key/ctrlaltdel conditions This patch drops the default signal handlers for SIGPWR and SIGINT with optional tasks in /etc/finit.conf, triggered by two new conditions: sys/pwr/fail and sys/key/ctrlaltdel, respectively. Signed-off-by: Joachim Wiberg --- ChangeLog.md | 15 ++++++++++----- contrib/finit.conf | 8 ++++++++ doc/conditions.md | 2 ++ src/sig.c | 25 +++++++++++++++++++++---- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index e6f9b907..5d6eaf1d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -17,11 +17,16 @@ Bug fix release. * Change how `contrib/debian/install.sh` sets up a Grub boot entry for finit. We now modify the $SUPPORTED_INITS variable in `10_linux` * Disable default kernel ctrl-alt-delete handler and let Finit instead - catch `SIGINT` from kernel to be able to perform a proper reboot. - See the TODO for a future `sys/key/ctrlaltdel` condition extension -* Added keventd to provide `sys/pwr/ac` condition to Finit. keventd is - currently only responsible for monitoring `/sys/class/power_supply` - for changes to active AC mains power online status. + catch `SIGINT` from kernel to be able to perform a proper reboot. + There is no default command for this, you need to set up a task that + triggers on `` to issue `initctl reboot` +* Added keventd to provide `` condition to Finit. keventd + is currently only responsible for monitoring `/sys/class/power_supply` + for changes to active AC mains power online status +* For handling power fail events (from UPS and similar) a process may + send `SIGPWR` to PID 1. Finit no longer redirects this to `SIGUSR1` + (poweroff). There is no default command for this, you need to set up + a task that triggers on `` to issue `initctl poweroff` ### Fixes * Stricter interface name validation in netlink plugin, modeled after diff --git a/contrib/finit.conf b/contrib/finit.conf index f5823d11..3faf0928 100644 --- a/contrib/finit.conf +++ b/contrib/finit.conf @@ -35,6 +35,14 @@ tty /dev/tty1 tty /dev/tty2 tty /dev/tty3 +# Task to run if ctrl-alt-del is pressed. This condition is asserted by +# Finit upon receiving SIGINT (from the kernel). +task [12345789] initctl reboot -- Rebooting system + +# Task to run if the kernel gets a power fail condition is pressed. +# Asserted by Finit upon receiving SIGPWR (from the kernel). +task [12345789] initctl poweroff -- Shutting down system + # Alternative method instead of runparts #task [S] /etc/init.d/keyboard-setup start -- Setting up preliminary keymap #task [S] /etc/init.d/acpid start -- Starting ACPI Daemon diff --git a/doc/conditions.md b/doc/conditions.md index 6d88333c..892f6ebf 100644 --- a/doc/conditions.md +++ b/doc/conditions.md @@ -115,6 +115,8 @@ Built-in conditions: - `net//up` - `net//running` - `sys/pwr/ac` +- `sys/pwr/fail` +- `sys/key/ctrlaltdel` - `usr/foo` **Note:** `up` means administratively up, the interface flag `IFF_UP`. diff --git a/src/sig.c b/src/sig.c index ad479ffc..77878570 100644 --- a/src/sig.c +++ b/src/sig.c @@ -311,7 +311,9 @@ static void sighup_cb(uev_t *w, void *arg, int events) } /* - * SIGINT: Should generate condition, for now reboot + * SIGINT: generates condition, which the sys.so + * plugin picks up and tells Finit to start any service(s) or + * task(s) associated with the condition. */ static void sigint_cb(uev_t *w, void *arg, int events) { @@ -321,8 +323,23 @@ static void sigint_cb(uev_t *w, void *arg, int events) return; } - halt = SHUT_REBOOT; - service_runlevel(6); + cond_set_oneshot_noupdate("sys/key/ctrlaltdel"); +} + +/* + * SIGPWR: generates condition, which the sys.so plugin + * picks up and tells Finit to start any service(s) or task(s) + * associated with the condition. + */ +static void sigpwr_cb(uev_t *w, void *arg, int events) +{ + _d("..."); + if (UEV_ERROR == events) { + _e("Unrecoverable error in signal watcher"); + return; + } + + cond_set_oneshot_noupdate("sys/pwr/fail"); } /* @@ -531,7 +548,7 @@ void sig_setup(uev_ctx_t *ctx) /* BusyBox/SysV init style signals for halt, power-off and reboot. */ uev_signal_init(ctx, &sigusr1_watcher, sigusr1_cb, NULL, SIGUSR1); uev_signal_init(ctx, &sigusr2_watcher, sigusr2_cb, NULL, SIGUSR2); - uev_signal_init(ctx, &sigpwr_watcher, sigusr2_cb, NULL, SIGPWR); + uev_signal_init(ctx, &sigpwr_watcher, sigpwr_cb, NULL, SIGPWR); uev_signal_init(ctx, &sigterm_watcher, sigterm_cb, NULL, SIGTERM); /* Some C APIs may need SIGALRM for implementing timers. */