From 07da8aecac60de71e91b07a097bf421244f09da2 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 17 Feb 2021 11:32:13 +0100 Subject: [PATCH 01/17] doc: some helpful pointers Signed-off-by: Joachim Wiberg --- doc/config.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/config.md b/doc/config.md index 0b6bee01..30f9ebf0 100644 --- a/doc/config.md +++ b/doc/config.md @@ -182,6 +182,8 @@ optional arguments and description. `run` commands are guaranteed to be completed before running the next command. Highly useful if true serialization is needed. +> `` is described in the [Services](#services) section. + ### One-shot Commands (parallel) @@ -194,6 +196,8 @@ redirects can be freely used: task [s] echo "foo" | cat >/tmp/bar +> `` is described in the [Services](#services) section. + ### SysV Init Scripts @@ -212,6 +216,8 @@ calls `init-script restart` on `initctl reload`. Similar to how Forking services started with `sysv` scripts can be monitored by Finit by declaring the PID file to look for: `pid:!/path/to/pidfile.pid`. +> `` is described in the [Services](#services) section. + ### Services From 4ff6f6a1f1d3fcc3c37c523cb52a55c0db26c46e Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 17 Feb 2021 11:32:47 +0100 Subject: [PATCH 02/17] doc: minor, pid condition syntax has changed Signed-off-by: Joachim Wiberg --- doc/conditions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/conditions.md b/doc/conditions.md index 6019c6b3..9076b194 100644 --- a/doc/conditions.md +++ b/doc/conditions.md @@ -81,7 +81,7 @@ file touched (re-created). Built-in conditions: -- `pid/` +- `pid/` - `net/route/default` - `net//exist` - `net//up` From 72a892ef55098b171ecf3ba35d5e99b85223cdcb Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Wed, 17 Feb 2021 11:30:40 +0100 Subject: [PATCH 03/17] Fix #151: Install logit in libexec Signed-off-by: Magnus Malm --- src/Makefile.am | 6 +++--- src/service.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 5e873a53..fd84585d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,9 +15,9 @@ pkglibexec_PROGRAMS = watchdogd endif if LOGIT -bin_PROGRAMS = logit -logit_SOURCES = logit.c -logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99 +pkglibexec_PROGRAMS = logit +logit_SOURCES = logit.c +logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99 endif finit_SOURCES = api.c cgroup.c cgroup.h \ diff --git a/src/service.c b/src/service.c index 357a3e67..8967456d 100644 --- a/src/service.c +++ b/src/service.c @@ -213,7 +213,7 @@ static int lredirect(svc_t *svc) snprintf(sz, sizeof(sz), "%d", logfile_size_max); snprintf(num, sizeof(num), "%d", logfile_count_max); - execlp("logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); + execlp(FINIT_LIBPATH_ "/logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); #else logit(LOG_INFO, "logit disabled, logging %s to syslog instead", svc->name); fallback_logger(tag, prio); @@ -227,7 +227,7 @@ static int lredirect(svc_t *svc) prio = svc->log.prio; #ifdef LOGIT_ENABLED - execlp("logit", "logit", "-t", tag, "-p", prio, NULL); + execlp("FINIT_LIBPATH_ logit", "logit", "-t", tag, "-p", prio, NULL); #else if (whichp("logger")) execlp("logger", "logger", "-t", tag, "-p", prio, NULL); From 27aa7ad1e1fc7d053a1cb0a4fadbc7346a295bc5 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 17 Feb 2021 11:41:49 +0100 Subject: [PATCH 04/17] Follow-up to 72a892e, fix string concatenation Signed-off-by: Joachim Wiberg --- src/service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service.c b/src/service.c index 8967456d..fa49a803 100644 --- a/src/service.c +++ b/src/service.c @@ -227,7 +227,7 @@ static int lredirect(svc_t *svc) prio = svc->log.prio; #ifdef LOGIT_ENABLED - execlp("FINIT_LIBPATH_ logit", "logit", "-t", tag, "-p", prio, NULL); + execlp(FINIT_LIBPATH_ "logit", "logit", "-t", tag, "-p", prio, NULL); #else if (whichp("logger")) execlp("logger", "logger", "-t", tag, "-p", prio, NULL); From 4d4fd402863c9d69507329dc086c27e0ca2528f7 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Wed, 17 Feb 2021 11:45:30 +0100 Subject: [PATCH 05/17] Follow-up to 72a892e, Add missing '/' Signed-off-by: Magnus Malm --- src/service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service.c b/src/service.c index fa49a803..cd4f17f8 100644 --- a/src/service.c +++ b/src/service.c @@ -227,7 +227,7 @@ static int lredirect(svc_t *svc) prio = svc->log.prio; #ifdef LOGIT_ENABLED - execlp(FINIT_LIBPATH_ "logit", "logit", "-t", tag, "-p", prio, NULL); + execlp(FINIT_LIBPATH_ "/logit", "logit", "-t", tag, "-p", prio, NULL); #else if (whichp("logger")) execlp("logger", "logger", "-t", tag, "-p", prio, NULL); From 5903ebeb6210c85ad3df33cc49fbb76aafa70d7c Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 17 Feb 2021 11:59:04 +0100 Subject: [PATCH 06/17] cgreaper.sh: update path to logit after relocation Signed-off-by: Joachim Wiberg --- src/cgreaper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cgreaper.sh b/src/cgreaper.sh index cc4f687b..1634817d 100755 --- a/src/cgreaper.sh +++ b/src/cgreaper.sh @@ -5,7 +5,7 @@ BASE=/sys/fs/cgroup/finit PROC=`basename $1` DIR="$BASE$1" -logit -p daemon.info -t finit "$PROC stopped, cleaning up control group $DIR" +/libexec/finit/logit -p daemon.info -t finit "$PROC stopped, cleaning up control group $DIR" rmdir $DIR exit 0 From 549aeda1fdad590c7c031399b9d4752cfd87daf3 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 17 Feb 2021 12:56:14 +0100 Subject: [PATCH 07/17] Set process name of finit fallback shell, when active Signed-off-by: Joachim Wiberg --- src/tty.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tty.c b/src/tty.c index d8f914d6..2e0a8845 100644 --- a/src/tty.c +++ b/src/tty.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -474,6 +475,7 @@ int tty_fallback(pid_t lost) setsid(); ioctl(STDIN_FILENO, TIOCSCTTY, 1); + prctl(PR_SET_NAME, "finitsh", 0, 0, 0); _exit(execl(_PATH_BSHELL, _PATH_BSHELL, NULL)); #else (void)lost; From dda59503ac251d2c725174f3216919d08ba32b01 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 17 Feb 2021 12:58:39 +0100 Subject: [PATCH 08/17] Refactor, drop #ifdefs and logger fallback, probe for logit Simplify log redirection, probe for logit early and fall back to use a while-loop of syslog() instead. Signed-off-by: Joachim Wiberg --- src/finit.h | 2 ++ src/service.c | 30 ++++++++++++------------------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/finit.h b/src/finit.h index 973d3ebd..bb9b8686 100644 --- a/src/finit.h +++ b/src/finit.h @@ -45,6 +45,8 @@ #define _PATH_VARRUN "/var/run/" #endif +#define LOGIT_PATH FINIT_LIBPATH_ "/logit" + #define CMD_SIZE 256 #define LINE_SIZE 1024 #define BUF_SIZE 4096 diff --git a/src/service.c b/src/service.c index cd4f17f8..6bbd9486 100644 --- a/src/service.c +++ b/src/service.c @@ -27,6 +27,7 @@ #include /* isblank() */ #include /* sched_yield() */ #include +#include #include #include #include @@ -149,7 +150,6 @@ static int fredirect(const char *file) return -1; } -#ifndef LOGIT_ENABLED /* * Fallback in case we don't even have logger on the system. * XXX: we should parse 'prio' here to get facility.level @@ -160,13 +160,13 @@ static void fallback_logger(char *ident, char *prio) int level = LOG_NOTICE; char buf[256]; + prctl(PR_SET_NAME, "finitlog", 0, 0, 0); openlog(ident, LOG_NOWAIT | LOG_PID, facility); while ((fgets(buf, sizeof(buf), stdin))) syslog(level, "%s", buf); closelog(); } -#endif /* * Redirect output to syslog using the command line logit tool @@ -206,19 +206,20 @@ static int lredirect(svc_t *svc) /* Reset signals */ sig_unblock(); + if (!whichp(LOGIT_PATH)) { + logit(LOG_INFO, LOGIT_PATH " missing, using syslog for %s instead", svc->name); + fallback_logger(tag, prio); + _exit(0); + } + if (svc->log.file[0] == '/') { -#ifdef LOGIT_ENABLED char sz[20], num[3]; snprintf(sz, sizeof(sz), "%d", logfile_size_max); snprintf(num, sizeof(num), "%d", logfile_count_max); - execlp(FINIT_LIBPATH_ "/logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); -#else - logit(LOG_INFO, "logit disabled, logging %s to syslog instead", svc->name); - fallback_logger(tag, prio); - _exit(0); -#endif + execlp(LOGIT_PATH, "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); + _exit(1); } if (svc->log.ident[0]) @@ -226,15 +227,8 @@ static int lredirect(svc_t *svc) if (svc->log.prio[0]) prio = svc->log.prio; -#ifdef LOGIT_ENABLED - execlp(FINIT_LIBPATH_ "/logit", "logit", "-t", tag, "-p", prio, NULL); -#else - if (whichp("logger")) - execlp("logger", "logger", "-t", tag, "-p", prio, NULL); - - fallback_logger(tag, prio); -#endif - _exit(0); + execlp(LOGIT_PATH, "logit", "-t", tag, "-p", prio, NULL); + _exit(1); } dup2(fd, STDOUT_FILENO); From e941a510c16f09045d4bcf1e1a21e6637971f2db Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 18 Feb 2021 19:37:00 +0100 Subject: [PATCH 09/17] doc: document new cmdline options Signed-off-by: Joachim Wiberg --- doc/cmdline.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/cmdline.md b/doc/cmdline.md index 280cd032..7e0b5dad 100644 --- a/doc/cmdline.md +++ b/doc/cmdline.md @@ -4,9 +4,22 @@ Tips & Tricks with the kernel cmdline This document summarizes the different boot parameters that can be passed on the Linux kernel command line. Not limited to Finit. +The `bool` setting is one of `on, off, true false, 1, 0`. + + * `debug`__ Enable kernel debug. Debug messages are printed to the console - until Finit starts up, the syslog daemon then empties `/dev/kmsg` + until Finit starts up. + +* `finit.debug[=bool]` + Enable finit debug. This is operated independently of the kerne + debug setting. New as of Finit v4. + +* `finit.show_status[=bool]` + Control finit boot progress, including banner. + +* `finit.status_style=` + Set Finit boot progress style, when enabled. * `init=/bin/sh` Bypass system default init and tell kernel to start a shell. Note, From 346320f6697aaac7722afb9c5d3967263bc6d46e Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 19 Feb 2021 18:12:00 +0100 Subject: [PATCH 10/17] doc: update text on conditions to reflect new world --- doc/conditions.md | 55 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/doc/conditions.md b/doc/conditions.md index 9076b194..23df8f50 100644 --- a/doc/conditions.md +++ b/doc/conditions.md @@ -17,8 +17,8 @@ Conditions are a new addition to Finit, introduced in v3, with the intention of providing a mechanism for common synchronization problems. For example: -- *"do not start this service until another process has started"*, or -- *"do not starty until basic network access is available"* +- *"wait for service A to start before starting service B"*, or +- *"wait for basic network access to be available"* Conditions are similar in syntax to declaring runlevels per service. They are specified within angle brackets `<>` and can be applied to any @@ -30,14 +30,12 @@ service to run. ### Example -```shell -service [2345] /sbin/netd -- Network monitor -``` + service [2345] /sbin/netd -- Network monitor In this example the Network monitor daemon `netd` is not started until -both the `pid/sbin/setupd` *and* `pid/sbin/zebra` conditions are -satisfied. An `svc` condition is satisfied by the corresponding -service's PID file being created. +both the `pid/setupd` *and* `pid/zebra` conditions are satisfied. A +`pid/` condition is satisfied by the corresponding service's PID file +being created. **NOTE:** Conditions also stop services when a condition is no longer asserted. I.e., if the Zebra process above stops or restarts, netd @@ -56,28 +54,29 @@ on this, see [Internals](#internals).) Thus, after a reconfiguration it is up to the "owner" of the condition to convey the new (or possibly unchanged) state of it. +> **Note:** For `pid/` conditions it is expected that services "touch" +> or recreate their PID file on `SIGHUP`. + Built-in Conditions ------------------- -Finit is distributed with a `pidfile` and `netlink` plugin. If enabled, -the `pidfile` plugin watches `/var/run/` for PID files created by the -monitored services, and sets a corresponding condition in the `pid/` -namespace. Similarily, the `netlink` plugin provides basic conditions -for when an interface is brought up/down and when a default route -(gateway) is set, in the `net/` namespace. +Finit comes with a `pidfile` and `netlink` plugin. When enabled, the +`pidfile` plugin watches `/var/run/` (recursively) for PID files created +by the monitored services, and sets a corresponding condition in the +`pid/` namespace. Similarily, the `netlink` plugin provides basic +conditions for when an interface is brought up/down and when a default +route (gateway) is set, in the `net/` namespace. With the example listed above, finit does not start the `/sbin/netd` daemon until `setupd` and `zebra` has started *and* created their PID files. Which they do when they have completed their initial set up and are ready to receive signals. -The full path to the dependency is needed by finit to match the PID file -to a monitored process. Finit expects monitored services to touch their -PID files, i.e. update the mtime, when they reload their configuration -files after a `SIGHUP`. Some services do not support `SIGHUP` and are -instead restarted, which is a crude but effective way to have the PID -file touched (re-created). +Finit expects monitored services to touch their PID files, i.e. update +the mtime, when they reload their configuration files after a `SIGHUP`. +Some services do not support `SIGHUP` and are instead restarted, which +is a crude but effective way to have the PID file touched (re-created). Built-in conditions: @@ -107,9 +106,9 @@ the daemon and the empty string. | /usr/bin/dbus-daemon | pid/dbus-daemon | | :222 dropbear -p 222 | pid/dropbear:222 | -The condition is asserted when `pidfile.so` receives an inotify event for a file -matching `/run/*.pid`, `/run/**/*.pid`, or `/run/**/pid`, which contains the -PID of the service Finit has started. +The condition is asserted when `pidfile.so` receives an inotify event +for a file matching `/run/*.pid`, `/run/**/*.pid`, or `/run/**/pid`, +which contains the PID of the service Finit has started. When Finit configuration files are changed and the `initctl reload` command is called, it is expected of services to touch their PID files @@ -140,10 +139,10 @@ conditions that are not satisfied. Listed as `off` below. ```shell ~ # initctl cond show -PID Service Status Condition (+ on, ~ flux, - off) -=============================================================================== -1419 /sbin/netd on <+pid/sbin/setupd,+pid/sbin/zebra> -0 /sbin/udhcpc off <-net/vlan1/exist> +PID IDENT STATUS CONDITION (+ ON, ~ FLUX, - OFF) +======================================================================= +1419 /sbin/netd on <+pid/setupd,+pid/zebra> +0 /sbin/udhcpc off <-net/vlan1/exist> ``` Here we can see that `netd` is allowed to run since both its conditions @@ -165,7 +164,7 @@ from `initctl cond show` again. The client will likely have failed to start, but at least the condition is now satisfied. There is also the `initctl cond dump` command, which dumps all known -conditions and their current status. +conditions, their current status, and their origin. Internals From 7b561044a9f27cbc3791fe5e1156346ee7c068f1 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 19 Feb 2021 19:03:00 +0100 Subject: [PATCH 11/17] doc: slight refactor of signals document --- doc/signals.md | 69 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/doc/signals.md b/doc/signals.md index 2c84dca9..c90d88a1 100644 --- a/doc/signals.md +++ b/doc/signals.md @@ -6,31 +6,50 @@ Though Finit comes with its own tools for (poweroff, halt, reboot), for compatibility with the existing BusyBox toolset the following signals have been adopted: -* `SIGHUP` - Same effect as `finit q`, `init q`, or `initctl reload`, reloads all - *.conf files in `/etc/finit.d/` -* `SIGUSR1` - Calls shutdown hooks, including `HOOK_SHUTDOWN`, stopping all running - processes, and unmounts all file systems. Then tells kernel to halt. - Most people these days want `SIGUSR2` though. +`SIGHUP` +-------- - SysV init and systemd use this to re-open their FIFO/D-Bus. -* `SIGUSR2` - Like SIGUSR1, but tell kernel to power-off the system, if ACPI - or similar exists to actually do this. If the kernel fails - power-off, Finit falls back to halt. +Same effect as `finit q`, `init q`, or `initctl reload`, reloads all +*.conf files in `/etc/finit.d/` + +`SIGUSR1` +--------- + +Calls shutdown hooks, including `HOOK_SHUTDOWN`, stopping all running +processes, and unmounts all file systems. Then tells kernel to halt. +Most people these days want `SIGUSR2` though. + +SysV init and systemd use this to re-open their FIFO/D-Bus. + +`SIGUSR2` +--------- + +Like SIGUSR1, but tell kernel to power-off the system, if ACPI or +similar exists to actually do this. If the kernel fails power-off, +Finit falls back to halt. - SysV init N/A, systemd dumps its internal state to log. -* `SIGTERM` - Like `SIGUSR1`, but tell kernel to reboot the system when done. +SysV init N/A, systemd dumps its internal state to log. + +`SIGTERM` +--------- + +Like `SIGUSR1`, but tell kernel to reboot the system when done. - SysV init N/A, systemd rexecutes itself. -* `SIGINT` - Sent from kernel when the CTRL-ALT-DEL key combo is pressed. - SysV init and systemd default to reboot with `shutdown -r`. - Finit currently forwards this to `SIGTERM`. -* `SIGPWR` - Sent from a power daemon, like `powstatd(8)`, on changes to the - UPS status. Traditionally SysV init read /etc/powerstatus and - acted on "OK", "FAIL", or "LOW" and then removed the file. - Finit currently forwards this to `SIGUSR2`. +SysV init N/A, systemd rexecutes itself. + +`SIGINT` +-------- + +Sent from kernel when the CTRL-ALT-DEL key combo is pressed. SysV init +and systemd default to reboot with `shutdown -r`. + +Finit currently forwards this to `SIGTERM`. + +`SIGPWR` +-------- + +Sent from a power daemon, like `powstatd(8)`, on changes to the +UPS status. Traditionally SysV init read /etc/powerstatus and +acted on "OK", "FAIL", or "LOW" and then removed the file. +Finit currently forwards this to `SIGUSR2`. + From 97a0baaf93e5dabe65a1906131d378435bf653f4 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 20 Feb 2021 22:42:00 +0100 Subject: [PATCH 12/17] Fix regression introduced in 13893b9 The refactor in 13893b9 caused a regression in composing service arguments. This patch should rectify that. Signed-off-by: Joachim Wiberg --- src/service.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/service.c b/src/service.c index 6bbd9486..10d34055 100644 --- a/src/service.c +++ b/src/service.c @@ -358,7 +358,7 @@ static int service_start(svc_t *svc) if (!svc_is_sysv(svc)) { for (i = 0; i < MAX_NUM_SVC_ARGS; i++) { if (strlen(svc->args[i]) == 0) - continue; + break; args[i] = svc->args[i]; } } else { @@ -394,7 +394,7 @@ static int service_start(svc_t *svc) } else if (debug) { char buf[CMD_SIZE] = ""; - for (i = 1; i < MAX_NUM_SVC_ARGS; i++) { + for (i = 0; i < MAX_NUM_SVC_ARGS; i++) { if (strlen(svc->args[i]) == 0) continue; if (buf[0]) From 27a4c8cd9a6fc69cdfc131fbb3f4220ce01adebf Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 21 Feb 2021 01:06:02 +0100 Subject: [PATCH 13/17] Follow-up to 70ca64a, handle missing bool argument to cmdnline opts Signed-off-by: Joachim Wiberg --- src/conf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/conf.c b/src/conf.c index dc1b10f0..7eab5133 100644 --- a/src/conf.c +++ b/src/conf.c @@ -77,11 +77,14 @@ static void hide_args(int argc, char *argv[]) int get_bool(char *ptr, int default_value) { + if (!ptr) + goto fallback; + if (string_compare(ptr, "true") || string_compare(ptr, "on") || string_compare(ptr, "1")) return 1; if (string_compare(ptr, "false") || string_compare(ptr, "off") || string_compare(ptr, "0")) return 0; - +fallback: return default_value; } From 1cb37a3ee78a99af1c823460e741d3ed26526a70 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 21 Feb 2021 01:12:26 +0100 Subject: [PATCH 14/17] plugins: update conditions for udevd and udevadm commands On systems with udevd, or systemd-udevd, we must wait for udevd to start before calling udevadm. This patch updates service and runtask stanzas to the new condition system naming and adds 'log' to the udevadm cmds. Signed-off-by: Joachim Wiberg --- plugins/hotplug.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/hotplug.c b/plugins/hotplug.c index 2385d14a..333a0346 100644 --- a/plugins/hotplug.c +++ b/plugins/hotplug.c @@ -43,19 +43,19 @@ static void setup(void *arg) path = which("/lib/systemd/systemd-udevd"); if (path) { /* Register udevd as a monitored service */ - snprintf(cmd, sizeof(cmd), "[S12345789] pid:udevd %s " + snprintf(cmd, sizeof(cmd), "[S12345789] pid:udevd name:udevd %s " "-- Device event managing daemon", path); if (service_register(SVC_TYPE_SERVICE, cmd, global_rlimit, NULL)) { _pe("Failed registering %s", path); } else { - snprintf(cmd, sizeof(cmd), ":1 [S] " + snprintf(cmd, sizeof(cmd), ":1 [S] log " "udevadm trigger -c add -t devices " - "-- Requesting device events", path); + "-- Requesting device events"); service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); - snprintf(cmd, sizeof(cmd), ":2 [S] " + snprintf(cmd, sizeof(cmd), ":2 [S] log " "udevadm trigger -c add -t subsystems " - "-- Requesting subsystem events", path); + "-- Requesting subsystem events"); service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); } From e80edbb40178d758b6657a71f2028d1a6bccea4b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 21 Feb 2021 10:47:48 +0100 Subject: [PATCH 15/17] If Finit API call fails (NACK), return non-zero to client Signed-off-by: Joachim Wiberg --- src/client.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client.c b/src/client.c index b1412fd9..5dfc919e 100644 --- a/src/client.c +++ b/src/client.c @@ -80,7 +80,10 @@ int client_send(struct init_request *rq, ssize_t len) if (read(sd, rq, len) != len) goto error; - result = 0; + if (rq->cmd == INIT_CMD_NACK) + result = 1; + else + result = 0; goto exit; error: perror("Failed communicating with finit"); From 649a269526148793f203bc409fa80ba769b807b5 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 21 Feb 2021 10:49:13 +0100 Subject: [PATCH 16/17] Add reboot/halt/poweroff/suspend commands to Finit API Signed-off-by: Joachim Wiberg --- src/api.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/finit.h | 4 ++++ src/reboot.c | 5 ----- src/util.h | 5 +++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/api.c b/src/api.c index f3bc15c7..e1365c22 100644 --- a/src/api.c +++ b/src/api.c @@ -182,6 +182,49 @@ static svc_t *do_find_byc(char *buf, size_t len) return NULL; } +static int do_reboot(int cmd, char *buf, size_t len) +{ + int rc = 1; + + switch (cmd) { + case INIT_CMD_REBOOT: + _d("reboot"); + halt = SHUT_REBOOT; + service_runlevel(6); + break; + + case INIT_CMD_HALT: + _d("halt"); + halt = SHUT_HALT; + service_runlevel(0); + break; + + case INIT_CMD_POWEROFF: + _d("poweroff"); + halt = SHUT_OFF; + service_runlevel(0); + break; + + case INIT_CMD_SUSPEND: + _d("suspend"); + sync(); + rc = reboot(RB_SW_SUSPEND); + if (rc) { + if (errno == EINVAL) + snprintf(buf, len, "Kernel does not support suspend."); + else + snprintf(buf, len, "Failed: %s", strerror(errno)); + } + break; + + default: + rc = 255; + break; + } + + return rc; +} + typedef struct { char *event; void (*cb)(void); @@ -302,6 +345,13 @@ static void api_cb(uev_t *w, void *arg, int events) rq.sleeptime = prevlevel; break; + case INIT_CMD_REBOOT: + case INIT_CMD_HALT: + case INIT_CMD_POWEROFF: + case INIT_CMD_SUSPEND: + result = do_reboot(rq.cmd, rq.data, sizeof(rq.data)); + break; + case INIT_CMD_ACK: _d("Client failed reading ACK"); goto leave; diff --git a/src/finit.h b/src/finit.h index bb9b8686..7a29eb9e 100644 --- a/src/finit.h +++ b/src/finit.h @@ -74,6 +74,10 @@ #define INIT_CMD_UNUSED2 14 /* Unused, was INIT_CMD_QUERY_INETD */ #define INIT_CMD_UNUSED1 15 /* Unused, was INIT_CMD_EMIT */ #define INIT_CMD_GET_RUNLEVEL 16 +#define INIT_CMD_REBOOT 20 +#define INIT_CMD_HALT 21 +#define INIT_CMD_POWEROFF 22 +#define INIT_CMD_SUSPEND 23 #define INIT_CMD_WDOG_HELLO 128 /* Watchdog register and hello */ #define INIT_CMD_SVC_ITER 129 #define INIT_CMD_SVC_QUERY 130 diff --git a/src/reboot.c b/src/reboot.c index aac059ba..340471d4 100644 --- a/src/reboot.c +++ b/src/reboot.c @@ -27,13 +27,8 @@ #include #include #include -#include #include "util.h" -#ifndef RB_SW_SUSPEND /* Since Linux 2.5.18, but not in GLIBC until 2.19 */ -#define RB_SW_SUSPEND 0xd000fce2 -#endif - typedef enum { CMD_UNKNOWN = -1, CMD_REBOOT = 0, diff --git a/src/util.h b/src/util.h index eb7aaa34..e65cd8c2 100644 --- a/src/util.h +++ b/src/util.h @@ -28,9 +28,14 @@ #ifdef HAVE_TERMIOS_H /* for screen_width() */ #include #include +#include #include #endif +#ifndef RB_SW_SUSPEND /* Since Linux 2.5.18, but not in GLIBC until 2.19 */ +#define RB_SW_SUSPEND 0xd000fce2 +#endif + extern int screen_rows; extern int screen_cols; extern char *prognm; From 7e422036270931f09bec7d40694ef1fd385e0b08 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 21 Feb 2021 10:50:10 +0100 Subject: [PATCH 17/17] Fix #147: refactor reboot as multi-call to initctl This patch drops the stand-alone reboot binary and folds it into the initctl tool, which now becomes a multi-call binary. With this change the reboot/shutdown/poweroff/halt/suspend commands also no longer default to sending signals to PID 1, instead it now uses the initctl <--> finit UNIX domain socket API. Signals are only used as a fallback in case of non-working domain socket. The -f,--force mode of operation still works, where `reboot(2)` is called directly. Signed-off-by: Joachim Wiberg --- ChangeLog.md | 5 ++++ src/Makefile.am | 80 +++++++++++++++++++++---------------------------- src/initctl.c | 52 +++++++++++++++++++++++++++----- src/reboot.c | 46 ++++++++++++++++++++-------- 4 files changed, 117 insertions(+), 66 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 9201934e..dfac459e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -12,6 +12,11 @@ to service conditions. There are other significant changes as well, so make sure to read the whole changelog when upgrading. ### Changes +* The stand-alone `reboot` tool has been replaced with a symlink to + `initctl`, like its siblings: halt, shutdown, poweroff, and suspend. + Calling `reboot` & C:o now defaults to the corresponding `initctl cmd` + with a fallback to sending signals as per traditional SysV init. The + `-f` (force) flag remains, where `reboot(2)` is called directly * Introducing Finit progress 𝓜𝓸𝓭𝓮𝓻𝓷 * Removed built-in inetd super server. If you need this functionality, use an external inetd, like xinetd, instead. A pull request for a diff --git a/src/Makefile.am b/src/Makefile.am index fd84585d..491efa6c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ else AM_LDFLAGS = -export-dynamic endif -sbin_PROGRAMS = finit initctl reboot +sbin_PROGRAMS = finit initctl pkglibexec_SCRIPTS = cgreaper.sh if WATCHDOGD pkglibexec_PROGRAMS = watchdogd @@ -49,19 +49,15 @@ else finit_LDADD += -ldl endif -initctl_SOURCES = initctl.c client.c client.h \ - serv.c serv.h svc.h \ +initctl_SOURCES = initctl.c client.c client.h \ + reboot.c serv.c serv.h svc.h \ cond.c cond.h util.c util.h initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99 initctl_CFLAGS += $(lite_CFLAGS) initctl_LDADD = $(lite_LIBS) -reboot_SOURCES = reboot.c util.c util.h -reboot_CFLAGS = $(lite_CFLAGS) -reboot_LDADD = $(lite_LIBS) - INIT_LNKS = init -REBOOT_LNKS = shutdown halt poweroff suspend +REBOOT_LNKS = reboot shutdown halt poweroff suspend if WATCHDOGD watchdogd_SOURCES = watchdog.c watchdog.h @@ -72,46 +68,38 @@ endif # Hook in install to add finit and reboot symlink(s) install-exec-hook: @$(INSTALL_DATA) $(srcdir)/rescue.conf $(DESTDIR)$(pkglibdir) - @for file in $(INIT_LNKS); do \ - lnk=$(DESTDIR)$(sbindir)/$$file; \ - if [ -e $$lnk ]; then \ - echo "Skipping $$lnk exists."; \ - else \ - $(LN_S) finit $$lnk; \ - fi; \ + @for file in $(INIT_LNKS); do \ + lnk=$(DESTDIR)$(sbindir)/$$file; \ + if [ -e $$lnk ]; then \ + echo "Skipping $$lnk exists."; \ + else \ + $(LN_S) finit $$lnk; \ + fi; \ + done + @for file in $(REBOOT_LNKS); do \ + lnk=$(DESTDIR)$(sbindir)/$$file; \ + if [ -e $$lnk ]; then \ + echo "Skipping $$lnk exists."; \ + else \ + $(LN_S) initctl $$lnk; \ + fi; \ done - @if [ -L $(DESTDIR)$(sbindir)/reboot ]; then \ - echo "Non-finit reboot, skipping install."; \ - else \ - for file in $(REBOOT_LNKS); do \ - lnk=$(DESTDIR)$(sbindir)/$$file; \ - if [ -e $$lnk ]; then \ - echo "Skipping $$lnk exists."; \ - else \ - $(LN_S) reboot $$lnk; \ - fi; \ - done; \ - fi uninstall-hook: @$(RM) $(DESTDIR)$(pkglibdir)/rescue.conf - @for file in $(INIT_LNKS); do \ - lnk=`readlink $(DESTDIR)$(sbindir)/$$file`; \ - if [ "$$lnk" != "finit" ]; then \ - echo "Skipping non-finit $$file."; \ - continue; \ - fi; \ - $(RM) $(DESTDIR)$(sbindir)/$$file; \ + @for file in $(INIT_LNKS); do \ + lnk=$(DESTDIR)$(sbindir)/$$file; \ + dst=`readlink $$lnk`; \ + if [ "$$dst" != "finit" ]; then \ + continue; \ + fi; \ + $(RM) $(DESTDIR)$(sbindir)/$$file; \ + done + @for file in $(REBOOT_LNKS); do \ + lnk=$(DESTDIR)$(sbindir)/$$file; \ + dst=`readlink $$lnk`; \ + if [ "$$dst" != "initctl" ]; then \ + continue; \ + fi; \ + $(RM) $$lnk; \ done - @if [ -L $(DESTDIR)$(sbindir)/reboot ]; then \ - echo "Non-finit reboot, skipping uninstall."; \ - else \ - for file in $(REBOOT_LNKS); do \ - lnk=$(DESTDIR)$(sbindir)/$$file; \ - dst=`readlink $$lnk`; \ - if [ "$$dst" != "reboot" ]; then \ - continue; \ - fi; \ - $(RM) $$lnk; \ - done; \ - fi diff --git a/src/initctl.c b/src/initctl.c index 2d81606a..fd12d5dd 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -54,6 +54,9 @@ int verbose = 0; int runlevel = 0; int iw, pw; +extern int reboot_main(int argc, char *argv[]); + + /* figure ut width of IDENT and PID columns */ static void col_widths(void) { @@ -349,17 +352,30 @@ static int do_cond(char *cmd) return do_cond_show(NULL); } -static int do_signal(int signo, const char *msg) +static int do_cmd(int cmd) { - if (kill(1, signo)) - err(1, "Failed signalling init to %s", msg); + struct init_request rq = { + .magic = INIT_MAGIC, + .cmd = cmd, + }; + + if (client_send(&rq, sizeof(rq))) { + if (rq.cmd == INIT_CMD_NACK) + puts(rq.data); + + return 1; + } + + /* Wait here for systemd to shutdown/reboot */ + sleep(5); return 0; } -static int do_halt (char *arg) { return do_signal(SIGUSR1, "halt"); } -static int do_poweroff(char *arg) { return do_signal(SIGUSR2, "power off"); } -static int do_reboot (char *arg) { return do_signal(SIGTERM, "reboot"); } +int do_reboot (char *arg) { return do_cmd(INIT_CMD_REBOOT); } +int do_halt (char *arg) { return do_cmd(INIT_CMD_HALT); } +int do_poweroff(char *arg) { return do_cmd(INIT_CMD_POWEROFF); } +int do_suspend (char *arg) { return do_cmd(INIT_CMD_SUSPEND); } int utmp_show(char *file) { @@ -605,6 +621,22 @@ static int show_cgroup(char *arg) return 0; } +static int transform(char *nm) +{ + char *names[] = { + "reboot", "shutdown", "poweroff", "halt", "suspend", + NULL + }; + size_t i; + + for (i = 0; names[i]; i++) { + if (!strcmp(nm, names[i])) + return 1; + } + + return 0; +} + static int usage(int rc) { fprintf(stderr, @@ -644,7 +676,8 @@ static int usage(int rc) " runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n" " reboot Reboot system\n" " halt Halt system\n" - " poweroff Halt and power off system\n", prognm); + " poweroff Halt and power off system\n" + " suspend Suspend system\n", prognm); if (has_utmp()) fprintf(stderr, @@ -693,6 +726,7 @@ int main(int argc, char *argv[]) { "reboot", do_reboot }, { "halt", do_halt }, { "poweroff", do_poweroff }, + { "suspend", do_suspend }, { "utmp", do_utmp }, { NULL, NULL } @@ -706,7 +740,9 @@ int main(int argc, char *argv[]) { NULL, 0, NULL, 0 } }; - progname(argv[0]); + if (transform(progname(argv[0]))) + return reboot_main(argc, argv); + while ((c = getopt_long(argc, argv, "bh?ntv", long_options, NULL)) != EOF) { switch(c) { case 'b': diff --git a/src/reboot.c b/src/reboot.c index 340471d4..ce397b96 100644 --- a/src/reboot.c +++ b/src/reboot.c @@ -22,6 +22,7 @@ */ #include +#include #include #include #include @@ -40,6 +41,13 @@ typedef enum { static cmd_t cmd = CMD_UNKNOWN; static char *msg = NULL; +/* initctl API */ +extern int do_reboot (char *arg); +extern int do_halt (char *arg); +extern int do_poweroff(char *arg); +extern int do_suspend (char *arg); + + static void transform(char *nm) { if (!nm) @@ -80,6 +88,16 @@ static void transform(char *nm) } } +/* + * fallback in case of initctl API failure + */ +static void do_kill(int signo, char *msg) +{ + if (kill(1, signo)) + err(1, "Failed signalling init to %s", msg); + do_sleep(5); +} + static void sig(int signo) { (void)signo; /* NOP */ @@ -100,7 +118,7 @@ static int usage(int rc) return rc; } -int main(int argc, char *argv[]) +int reboot_main(int argc, char *argv[]) { int c, force = 0; struct option long_options[] = { @@ -113,7 +131,7 @@ int main(int argc, char *argv[]) }; /* Initial command taken from program name */ - transform(progname(argv[0])); + transform(prognm); while ((c = getopt_long(argc, argv, "h?fHPpr", long_options, NULL)) != EOF) { switch(c) { @@ -165,6 +183,8 @@ int main(int argc, char *argv[]) case CMD_SUSPEND: c = reboot(RB_SW_SUSPEND); + if (c && errno == EINVAL) + errx(1, "Kernel does not support suspend."); break; case CMD_UNKNOWN: @@ -173,22 +193,25 @@ int main(int argc, char *argv[]) } if (c) - warn("Failed forced %s", msg); - else - return 0; + err(1, "Failed forced %s", msg); + + return 0; } switch (cmd) { case CMD_REBOOT: - c = kill(1, SIGTERM); + if (do_reboot(NULL)) + do_kill(SIGTERM, msg); break; case CMD_HALT: - c = kill(1, SIGUSR1); + if (do_halt(NULL)) + do_kill(SIGUSR1, msg); break; case CMD_POWEROFF: - c = kill(1, SIGUSR2); + if (do_poweroff(NULL)) + do_kill(SIGUSR2, msg); break; case CMD_SUSPEND: @@ -196,16 +219,15 @@ int main(int argc, char *argv[]) * Only initctl supports suspend, we avoid adding * another signal to finit for compat reasons. */ + do_suspend(NULL); + break; + case CMD_UNKNOWN: errx(1, "Invalid command"); break; } - if (c) - err(1, "Failed signalling init to %s", msg); - do_sleep(5); - return 0; }