From 74d31947757479de5f3b4ecaed94c2c24f7e89a7 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:14:26 +0100 Subject: [PATCH 01/21] Follow-up to f4a0b99: track modifications of service env: files Add support for monitoring environment files for services declared with the `env:[-]/path/to/file` option. The default path to such files has been chosen to follow Debian and Buildroot /etc/default/* Signed-off-by: Joachim Wiberg --- configure.ac | 12 ++++++++++++ doc/config.md | 28 ++++++++++++++++++++++++++++ src/conf.c | 20 ++++++++++++++------ src/service.c | 2 +- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 6a1c7f85..c664e77e 100644 --- a/configure.ac +++ b/configure.ac @@ -109,6 +109,10 @@ AC_ARG_WITH(rc-local, AS_HELP_STRING([--with-rc-local=FILE], [SysV init /etc/rc.local file, default /etc/rc.local]), [rclocal=$withval], [rclocal=$sysconfdir/rc.local]) +AC_ARG_WITH(sysconfig, + AS_HELP_STRING([--with-sysconfig=DIR], [Enviroment config directory, default /etc/default]), + [sysconfig=$withval], [sysconfig=yes]) + AC_ARG_WITH(hostname, AS_HELP_STRING([--with-hostname=NAME], [If /etc/hostname is missing, default: noname]), [hostname=$withval], [hostname=noname]) @@ -165,6 +169,13 @@ AS_IF([test "x$with_rc_local" != "xno"], [ AC_EXPAND_DIR(rclocal_path, "$rclocal") AC_DEFINE_UNQUOTED(FINIT_RC_LOCAL, "$rclocal_path", [Compat SysV /etc/rc.local file to run last at boot.]) +AS_IF([test "x$with_sysconfig" != "xno"], [ + AS_IF([test "x$sysconfig" = "xyes"], [ + sysconfig=$sysconfdir/default + AC_EXPAND_DIR(sysconfig_path, "$sysconfig") + AC_DEFINE_UNQUOTED(FINIT_SYSCONFIG, "$sysconfig_path", + [System environment, default /etc/default])])]) + AS_IF([test "x$with_heading" != "xno"], [ AS_IF([test "x$heading" = "xyes"], [ heading="$PACKAGE_NAME v$PACKAGE_VERSION"])]) @@ -221,6 +232,7 @@ cat < examples in the `contrib/` directory, which can be used as a base. +Service Environment +------------------- + +Finit supports sourcing environment variables from `/etc/default/*`, or +similar `--with-sysconfig=DIR`. This is a common pattern from SysV init +scripts, where the start-stop script is a generic script for the given +service, `foo`, and the options for the service are sourced from the +file `/etc/default/foo`. Like this: + +* `/etc/default/foo`: + + FOO_OPTIONS=--extra-arg="bar" -s -x + +* `/etc/finit.conf`: + + service [2345] env:-/etc/default/foo foo -n $FOO_OPTIONS -- Example foo daemon + +Here the service `foo` is started with `-n`, to make sure it runs in the +foreground, and the with the options found in the environment file. With +the `ps` command we can see that the process is started with: + + foo -n --extra-arg=bar -s -x + +> **Note:** the leading `-` determines if Finit should treat a missing +> environment file as blocking the start of the service or not. When +> `-` is used, a missing environment file does *not* block the start. + + Service Wrapper Scripts ----------------------- diff --git a/src/conf.c b/src/conf.c index e866db7e..32c6b612 100644 --- a/src/conf.c +++ b/src/conf.c @@ -772,13 +772,13 @@ static void drop_changes(void) static int do_change(char *dir, char *name, uint32_t mask) { - char path[strlen(dir) + strlen(name) + 2]; + char fn[strlen(dir) + strlen(name) + 2]; struct conf_change *node; - snprintf(path, sizeof(path), "%s%s", dir, name); - _d("path: %s", path); + snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + _d("path: %s mask: %08x", fn, mask); - node = conf_find(path); + node = conf_find(fn); if (mask & (IN_DELETE | IN_MOVED_FROM)) { drop_change(node); return 0; @@ -793,13 +793,13 @@ static int do_change(char *dir, char *name, uint32_t mask) if (!node) return 1; - node->name = strdup(path); + node->name = strdup(fn); if (!node->name) { free(node); return 1; } - _d("Event registered for %s, mask 0x%x", path, mask); + _d("Event registered for %s, mask 0x%x", fn, mask); TAILQ_INSERT_HEAD(&conf_change_list, node, link); return 0; @@ -881,6 +881,14 @@ int conf_monitor(void) rc += iwatch_add(&iw_conf, FINIT_RCSD "/enabled/", IN_ONLYDIR | IN_DONT_FOLLOW); rc += iwatch_add(&iw_conf, FINIT_CONF, 0); + /* + * Systems with /etc/default, or similar, can also monitor changes + * in env files sourced by .conf files (above) + */ +#ifdef FINIT_SYSCONFIG + rc += iwatch_add(&iw_conf, FINIT_SYSCONFIG, IN_ONLYDIR); +#endif + return rc + conf_reload(); } diff --git a/src/service.c b/src/service.c index 7ba261e4..116f1528 100644 --- a/src/service.c +++ b/src/service.c @@ -1116,7 +1116,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) memcpy(svc->rlimit, rlimit, sizeof(svc->rlimit)); /* New, recently modified or unchanged ... used on reload. */ - if (file && conf_changed(file)) + if ((file && conf_changed(file)) || conf_changed(svc_getenv(svc))) svc_mark_dirty(svc); else svc_mark_clean(svc); From dfd41a1b3771eea42f5ce2f4be5f6e505eb94b65 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:17:30 +0100 Subject: [PATCH 02/21] configure: minor refactor, simplify to reduce duplication Signed-off-by: Joachim Wiberg --- configure.ac | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index c664e77e..3e971302 100644 --- a/configure.ac +++ b/configure.ac @@ -95,19 +95,19 @@ AC_PLUGIN([netlink], [yes], [Basic netlink plugin for IFUP/IFDN and GW eve # Check for extra arguments or packages AC_ARG_WITH(heading, AS_HELP_STRING([--with-heading=GREET], [Display GREET instead of Finit version at boot]), - [heading=$withval], [heading="$PACKAGE_NAME v$PACKAGE_VERSION"]) + [heading=$withval], [heading=yes]) AC_ARG_WITH(config, AS_HELP_STRING([--with-config=CONFIG], [Finit bootstrap config file, default /etc/finit.conf]), - [conf=$withval], [conf=$sysconfdir/finit.conf]) + [conf=$withval], [conf=yes]) AC_ARG_WITH(rcsd, AS_HELP_STRING([--with-rcsd=DIR], [Finit rcS.d/ directory, default /etc/finit.d]), - [rcsd=$withval], [rcsd=$sysconfdir/finit.d]) + [rcsd=$withval], [rcsd=yes]) AC_ARG_WITH(rc-local, AS_HELP_STRING([--with-rc-local=FILE], [SysV init /etc/rc.local file, default /etc/rc.local]), - [rclocal=$withval], [rclocal=$sysconfdir/rc.local]) + [rclocal=$withval], [rclocal=yes]) AC_ARG_WITH(sysconfig, AS_HELP_STRING([--with-sysconfig=DIR], [Enviroment config directory, default /etc/default]), @@ -115,15 +115,15 @@ AC_ARG_WITH(sysconfig, AC_ARG_WITH(hostname, AS_HELP_STRING([--with-hostname=NAME], [If /etc/hostname is missing, default: noname]), - [hostname=$withval], [hostname=noname]) + [hostname=$withval], [hostname=yes]) AC_ARG_WITH(runlevel, AS_HELP_STRING([--with-runlevel=N], [Runlevel to switch to after bootstrap, default: 2]), - [runlevel=$withval], [runlevel=2]) + [runlevel=$withval], [runlevel=yes]) AC_ARG_WITH(random-seed, AS_HELP_STRING([--with-random-seed=FILE], [Save a random seed for /dev/urandom across reboots, default /var/lib/misc/random-seed]), - [random_seed=$withval], [random_seed=/var/lib/misc/random-seed]) + [random_seed=$withval], [random_seed=yes]) ### Enable features ########################################################################### @@ -188,7 +188,8 @@ AC_DEFINE_UNQUOTED(DEFHOST, "$hostname", [For /etc/hostname]) AS_IF([test "x$with_runlevel" != "xno"], [ AS_IF([test "x$runlevel" = "xyes"], [ - AC_DEFINE_UNQUOTED(RUNLEVEL, 2, [Default runlevel to start after S])]) + runlevel=2 + AC_DEFINE_UNQUOTED(RUNLEVEL, $runlevel, [Default runlevel to start after S])]) AC_DEFINE_UNQUOTED(RUNLEVEL, $runlevel, [Runlevel to start after S])]) AS_IF([test "x$with_random_seed" != "xno"], [ From f8a989439b6e280b394d7465c0429d04f83faf72 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:18:03 +0100 Subject: [PATCH 03/21] Minor, insert '/' only if pasting components require it Signed-off-by: Joachim Wiberg --- plugins/pidfile.c | 4 ++-- src/exec.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/pidfile.c b/plugins/pidfile.c index 4377d736..c7f20458 100644 --- a/plugins/pidfile.c +++ b/plugins/pidfile.c @@ -68,7 +68,7 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask) svc_t *svc; _d("Got dir: %s, name: %s, mask: %08x", dir, name, mask); - snprintf(fn, sizeof(fn), "%s/%s", dir, name); + snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); if (fnmatch("*\\.pid", fn, 0) && fnmatch("*/pid", fn, 0)) return; @@ -131,7 +131,7 @@ static void pidfile_handle_dir(struct iwatch *iw, char *dir, char *name, int mas char path[strlen(dir) + strlen(name) + 2]; struct iwatch_path *iwp; - snprintf(path, sizeof(path), "%s/%s", dir, name); + snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); _d("path: %s", path); iwp = iwatch_find_by_path(iw, path); diff --git a/src/exec.c b/src/exec.c index eba38738..874816d5 100644 --- a/src/exec.c +++ b/src/exec.c @@ -454,7 +454,7 @@ int run_parts(char *dir, char *cmd) int status; int exit_status; - snprintf(path, sizeof(path), "%s/%s", dir, name); + snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); if (stat(path, &st)) { _d("Failed stat(%s): %s", path, strerror(errno)); continue; From 064d124e19806b1074581cc0dd58c00f2315cc31 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:38:46 +0100 Subject: [PATCH 04/21] Refactor, add new helper fn paste() to concat directory compoents Signed-off-by: Joachim Wiberg --- plugins/pidfile.c | 6 +++--- src/conf.c | 2 +- src/exec.c | 6 +++--- src/helpers.h | 11 +++++++++++ src/pid.h | 2 +- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/plugins/pidfile.c b/plugins/pidfile.c index c7f20458..19a4b907 100644 --- a/plugins/pidfile.c +++ b/plugins/pidfile.c @@ -67,8 +67,8 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask) char fn[PATH_MAX]; svc_t *svc; - _d("Got dir: %s, name: %s, mask: %08x", dir, name, mask); - snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(fn, sizeof(fn), dir, name); + _d("path: %s, mask: %08x", fn, mask); if (fnmatch("*\\.pid", fn, 0) && fnmatch("*/pid", fn, 0)) return; @@ -131,7 +131,7 @@ static void pidfile_handle_dir(struct iwatch *iw, char *dir, char *name, int mas char path[strlen(dir) + strlen(name) + 2]; struct iwatch_path *iwp; - snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(path, sizeof(path), dir, name); _d("path: %s", path); iwp = iwatch_find_by_path(iw, path); diff --git a/src/conf.c b/src/conf.c index 32c6b612..ae7def33 100644 --- a/src/conf.c +++ b/src/conf.c @@ -775,7 +775,7 @@ static int do_change(char *dir, char *name, uint32_t mask) char fn[strlen(dir) + strlen(name) + 2]; struct conf_change *node; - snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(fn, sizeof(fn), dir, name); _d("path: %s mask: %08x", fn, mask); node = conf_find(fn); diff --git a/src/exec.c b/src/exec.c index 874816d5..155eaf2d 100644 --- a/src/exec.c +++ b/src/exec.c @@ -441,20 +441,20 @@ int run_parts(char *dir, char *cmd) } for (i = 0; i < num; i++) { + const char *name = e[i]->d_name; + char path[strlen(dir) + strlen(name) + 2]; struct stat st; - char path[512]; char *argv[4] = { "sh", "-c", path, NULL }; - char *name = e[i]->d_name; pid_t pid = 0; int status; int exit_status; - snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(path, sizeof(path), dir, name); if (stat(path, &st)) { _d("Failed stat(%s): %s", path, strerror(errno)); continue; diff --git a/src/helpers.h b/src/helpers.h index e4fc1595..7c734d9f 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -103,6 +103,17 @@ static inline int create(char *path, mode_t mode, uid_t uid, gid_t gid) return 0; } +/* paste dir/file into buf */ +static inline int paste(char *buf, size_t len, const char *dir, const char *file) +{ + if (!dir) + dir = ""; + if (!file) + file = ""; + + return snprintf(buf, len, "%s%s%s", dir, fisslashdir(dir) ? "" : "/", file); +} + int ismnt (char *file, char *dir, char *mode); int fismnt (char *dir); diff --git a/src/pid.h b/src/pid.h index b0ca3325..6bf96ce2 100644 --- a/src/pid.h +++ b/src/pid.h @@ -62,7 +62,7 @@ static inline char *pid_runpath(const char *file, char *path, size_t len) else if (!strncmp(file, "/run/", 5)) file += 5; - rc = snprintf(path, len, "%s/%s", prefix, file); + rc = paste(path, len, prefix, file); if (rc < 0 || (size_t)rc >= len) _e("File path '%s' truncated, should end with '%s'", path, file); From da2dde879e096bbd3c06d548cc232ebb96eb5c9b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:54:22 +0100 Subject: [PATCH 05/21] contrib: debian: update to use env: files for service arguments Signed-off-by: Joachim Wiberg --- contrib/debian/finit.d/available/Makefile.am | 2 +- contrib/debian/finit.d/available/acpid.conf | 2 +- contrib/debian/finit.d/available/modules.conf | 12 ++++++------ contrib/debian/finit.d/available/ntpclient.conf | 2 -- contrib/debian/finit.d/available/rsyslogd.conf | 2 +- contrib/debian/finit.d/available/sntpd.conf | 2 ++ contrib/debian/finit.d/available/sshd.conf | 2 +- contrib/debian/finit.d/available/syslogd.conf | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 contrib/debian/finit.d/available/ntpclient.conf create mode 100644 contrib/debian/finit.d/available/sntpd.conf diff --git a/contrib/debian/finit.d/available/Makefile.am b/contrib/debian/finit.d/available/Makefile.am index e971a708..bb8e5299 100644 --- a/contrib/debian/finit.d/available/Makefile.am +++ b/contrib/debian/finit.d/available/Makefile.am @@ -1,5 +1,5 @@ docsdir := @docdir@/contrib/debian/finit.d/available docs_DATA = acpid.conf console-setup.conf getty.conf keyboard-setup.conf \ - lightdm.conf modules.conf ntpclient.conf rsyslogd.conf \ + lightdm.conf modules.conf rsyslogd.conf sntpd.conf \ sshd.conf syslogd.conf xdm.conf EXTRA_DIST = $(docs_DATA) diff --git a/contrib/debian/finit.d/available/acpid.conf b/contrib/debian/finit.d/available/acpid.conf index a854b71f..8a255dac 100644 --- a/contrib/debian/finit.d/available/acpid.conf +++ b/contrib/debian/finit.d/available/acpid.conf @@ -1,3 +1,3 @@ # Handle PWR button to shutdown/reboot -- useful in Qemu (virt-manager) # Depends on rsyslogd having started. Redirect any output to log. -service [2345] log acpid -f -- ACPI daemon +service [2345] env:-/etc/default/acpid log acpid -f $OPTIONS -- ACPI daemon diff --git a/contrib/debian/finit.d/available/modules.conf b/contrib/debian/finit.d/available/modules.conf index f5ac914a..f45caec2 100644 --- a/contrib/debian/finit.d/available/modules.conf +++ b/contrib/debian/finit.d/available/modules.conf @@ -1,8 +1,8 @@ ## Linux kernel modules to load #module fbcon -module button -module evdev -module loop -module psmouse -module mousedev -module 8139cp +#module button +#module evdev +#module loop +#module psmouse +#module mousedev +#module 8139cp diff --git a/contrib/debian/finit.d/available/ntpclient.conf b/contrib/debian/finit.d/available/ntpclient.conf deleted file mode 100644 index ed058a67..00000000 --- a/contrib/debian/finit.d/available/ntpclient.conf +++ /dev/null @@ -1,2 +0,0 @@ -# Simple NTP client (SNTP) -service [2345] ntpclient -n -L pool.ntp.org -- SNTP client diff --git a/contrib/debian/finit.d/available/rsyslogd.conf b/contrib/debian/finit.d/available/rsyslogd.conf index f58beaed..5ca6e9f8 100644 --- a/contrib/debian/finit.d/available/rsyslogd.conf +++ b/contrib/debian/finit.d/available/rsyslogd.conf @@ -1,2 +1,2 @@ # Start rsyslogd as soon as possible, should always run -service [S12345] rsyslogd -n -- Reliable and extended syslog daemon +service [S12345] env:-/etc/default/rsyslog rsyslogd -n $RSYSLOGD_OPTIONS -- Reliable syslog daemon diff --git a/contrib/debian/finit.d/available/sntpd.conf b/contrib/debian/finit.d/available/sntpd.conf new file mode 100644 index 00000000..7b5a9fa7 --- /dev/null +++ b/contrib/debian/finit.d/available/sntpd.conf @@ -0,0 +1,2 @@ +# Simple NTP client (SNTP) +service [2345] env:-/etc/default/sntpd sntpd -n $SNTPD_OPTIONS -- SNTP client diff --git a/contrib/debian/finit.d/available/sshd.conf b/contrib/debian/finit.d/available/sshd.conf index 0bc6a63f..62c310ab 100644 --- a/contrib/debian/finit.d/available/sshd.conf +++ b/contrib/debian/finit.d/available/sshd.conf @@ -1,2 +1,2 @@ # Start SSH daemon as soon as basic networking comes up -service [2345] /usr/sbin/sshd -D -- OpenSSH daemon +service [2345] env:-/etc/default/ssh /usr/sbin/sshd -D $SSHD_OPTS -- OpenSSH daemon diff --git a/contrib/debian/finit.d/available/syslogd.conf b/contrib/debian/finit.d/available/syslogd.conf index e2feb808..6896ed10 100644 --- a/contrib/debian/finit.d/available/syslogd.conf +++ b/contrib/debian/finit.d/available/syslogd.conf @@ -1,2 +1,2 @@ # Start sysklogd as soon as possible, should always run if enabled -service [S123456789] syslogd -sF -r 10M:10 -- System logging daemon +service [S123456789] env:-/etc/default/syslogd syslogd -F $SYSLOGD_OPTIONS -- System logging daemon From ea9f88b9fbea524de34e6a8e2d76297d5fddaeb7 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 18:15:05 +0100 Subject: [PATCH 06/21] contrib: alpine: update to use env: files for service arguments - Check for env in /etc/conf.d on Alpine - Update all services with their respective env: file - Disable default modules, noew modprobe plugin should handle this - Add watchdog service - Rename syslogd -> syslog, in case we want to integrate sysklogd Signed-off-by: Joachim Wiberg --- contrib/alpine/build.sh | 12 ++++++------ contrib/alpine/finit.d/available/Makefile.am | 5 +++-- contrib/alpine/finit.d/available/crond.conf | 2 +- contrib/alpine/finit.d/available/dropbear.conf | 2 +- contrib/alpine/finit.d/available/klogd.conf | 2 +- contrib/alpine/finit.d/available/modules.conf | 8 ++++---- contrib/alpine/finit.d/available/ntpd.conf | 4 ++-- contrib/alpine/finit.d/available/syslog.conf | 2 ++ contrib/alpine/finit.d/available/syslogd.conf | 2 -- contrib/alpine/finit.d/available/watchdog.conf | 1 + 10 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 contrib/alpine/finit.d/available/syslog.conf delete mode 100644 contrib/alpine/finit.d/available/syslogd.conf create mode 100644 contrib/alpine/finit.d/available/watchdog.conf diff --git a/contrib/alpine/build.sh b/contrib/alpine/build.sh index 744c0699..ea20792c 100755 --- a/contrib/alpine/build.sh +++ b/contrib/alpine/build.sh @@ -14,12 +14,12 @@ fi # The plugins are optional, but you may need D-Bus and X11 if you want # to run X-Window, the other configure flags are however required. PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig ./configure \ - --prefix=/usr --exec-prefix= \ - --sysconfdir=/etc --localstatedir=/var \ - --enable-dbus-plugin --enable-x11-common-plugin \ - --enable-alsa-utils-plugin \ - --enable-watchdog \ - --with-heading="Alpine Linux 3.13" --with-hostname=alpine + --prefix=/usr --exec-prefix= \ + --sysconfdir=/etc --localstatedir=/var \ + --enable-dbus-plugin --enable-x11-common-plugin \ + --enable-alsa-utils-plugin --enable-watchdog \ + --with-sysconfig=/etc/conf.d --with-hostname=alpine \ + --with-heading="Alpine Linux 3.13" echo echo "*** Building ..." diff --git a/contrib/alpine/finit.d/available/Makefile.am b/contrib/alpine/finit.d/available/Makefile.am index f712b81c..4ec27e04 100644 --- a/contrib/alpine/finit.d/available/Makefile.am +++ b/contrib/alpine/finit.d/available/Makefile.am @@ -1,4 +1,5 @@ docsdir := @docdir@/contrib/alpine/finit.d/available -docs_DATA = acpid.conf crond.conf dropbear.conf getty.conf keymap.conf \ - lxdm.conf modules.conf ntpd.conf klogd.conf syslogd.conf +docs_DATA = acpid.conf crond.conf dropbear.conf getty.conf keymap.conf \ + lxdm.conf modules.conf ntpd.conf klogd.conf syslog.conf \ + watchdog.conf EXTRA_DIST = $(docs_DATA) diff --git a/contrib/alpine/finit.d/available/crond.conf b/contrib/alpine/finit.d/available/crond.conf index 1ee0538a..687dc403 100644 --- a/contrib/alpine/finit.d/available/crond.conf +++ b/contrib/alpine/finit.d/available/crond.conf @@ -1 +1 @@ -service [2345] crond -f -S -c /etc/crontabs -- Cron daemon +service [2345] env:-/etc/conf.d/crond crond -f -S $CRON_OPTS -- Cron daemon diff --git a/contrib/alpine/finit.d/available/dropbear.conf b/contrib/alpine/finit.d/available/dropbear.conf index e8cd1212..11db0b73 100644 --- a/contrib/alpine/finit.d/available/dropbear.conf +++ b/contrib/alpine/finit.d/available/dropbear.conf @@ -1,2 +1,2 @@ # Start SSH daemon as soon as basic networking comes up -service [2345] dropbear -R -F -- Dropbear SSH daemon +service [2345] env:-/etc/conf.d/dropbear dropbear -R -F $DROPBEAR_OPTS -- Dropbear SSH daemon diff --git a/contrib/alpine/finit.d/available/klogd.conf b/contrib/alpine/finit.d/available/klogd.conf index 232f45c5..ad02d302 100644 --- a/contrib/alpine/finit.d/available/klogd.conf +++ b/contrib/alpine/finit.d/available/klogd.conf @@ -1,2 +1,2 @@ # Start klogd as soon as possible, after syslogd, should always run -service [S12345] klogd -n -- Kernel log daemon +service [S12345] env:-/etc/conf.d/klogd klogd -n $KLOGD_OPTS -- Kernel log daemon diff --git a/contrib/alpine/finit.d/available/modules.conf b/contrib/alpine/finit.d/available/modules.conf index 1f1ec5b3..7f3d85f0 100644 --- a/contrib/alpine/finit.d/available/modules.conf +++ b/contrib/alpine/finit.d/available/modules.conf @@ -1,6 +1,6 @@ ## Linux kernel modules to load #module fbcon -module button -module evdev -module mousedev -module 8139cp +#module button +#module evdev +#module mousedev +#module 8139cp diff --git a/contrib/alpine/finit.d/available/ntpd.conf b/contrib/alpine/finit.d/available/ntpd.conf index 1968b000..bf8f2d20 100644 --- a/contrib/alpine/finit.d/available/ntpd.conf +++ b/contrib/alpine/finit.d/available/ntpd.conf @@ -1,2 +1,2 @@ -# Silly Busybox ntpd has no option to use syslog when running in foreground -service [2345] log ntpd -n -N -p pool.ntp.org -- NTP daemon +# Busybox ntpd has no option to use syslog when running in foreground +service [2345] log env:-/etc/conf.d/ntpd ntpd -n $NTPD_OPTS -- NTP daemon diff --git a/contrib/alpine/finit.d/available/syslog.conf b/contrib/alpine/finit.d/available/syslog.conf new file mode 100644 index 00000000..c2916ce4 --- /dev/null +++ b/contrib/alpine/finit.d/available/syslog.conf @@ -0,0 +1,2 @@ +# Start syslogd as soon as possible, should always run +service [S12345] env:-/etc/conf.d/syslog syslogd -n $SYSLOGD_OPTS -- Syslog daemon diff --git a/contrib/alpine/finit.d/available/syslogd.conf b/contrib/alpine/finit.d/available/syslogd.conf deleted file mode 100644 index 95fc2e39..00000000 --- a/contrib/alpine/finit.d/available/syslogd.conf +++ /dev/null @@ -1,2 +0,0 @@ -# Start syslogd as soon as possible, should always run -service [S12345] syslogd -n -- Syslog daemon diff --git a/contrib/alpine/finit.d/available/watchdog.conf b/contrib/alpine/finit.d/available/watchdog.conf new file mode 100644 index 00000000..269b711f --- /dev/null +++ b/contrib/alpine/finit.d/available/watchdog.conf @@ -0,0 +1 @@ +service [S123456789] env:-/etc/conf.d/watchdog watchdog $WATCHDOG_OPTS $WATCHDOG_DEV -- System watchdog From 45a374b50a77add8226d152d1d3616d03f030194 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 18:28:11 +0100 Subject: [PATCH 07/21] configure: allow custom directory for --with-sysconfig Signed-off-by: Joachim Wiberg --- configure.ac | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 3e971302..6de89ca9 100644 --- a/configure.ac +++ b/configure.ac @@ -171,10 +171,10 @@ AC_DEFINE_UNQUOTED(FINIT_RC_LOCAL, "$rclocal_path", [Compat SysV /etc/rc.local f AS_IF([test "x$with_sysconfig" != "xno"], [ AS_IF([test "x$sysconfig" = "xyes"], [ - sysconfig=$sysconfdir/default - AC_EXPAND_DIR(sysconfig_path, "$sysconfig") - AC_DEFINE_UNQUOTED(FINIT_SYSCONFIG, "$sysconfig_path", - [System environment, default /etc/default])])]) + sysconfig=$sysconfdir/default]) + AC_EXPAND_DIR(sysconfig_path, "$sysconfig") + AC_DEFINE_UNQUOTED(FINIT_SYSCONFIG, "$sysconfig_path", + [System environment, default /etc/default])]) AS_IF([test "x$with_heading" != "xno"], [ AS_IF([test "x$heading" = "xyes"], [ From 80e4f025d3b5ef9d9b2f89dc00128b0d909037b5 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 09:09:34 +0100 Subject: [PATCH 08/21] flog: new helper function for debugging to file Signed-off-by: Joachim Wiberg --- src/log.c | 22 ++++++++++++++++++++++ src/log.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/log.c b/src/log.c index dc574c9e..cc603a52 100644 --- a/src/log.c +++ b/src/log.c @@ -130,6 +130,28 @@ done: va_end(ap); } +/* + * Log to file, intended for debug only. + */ +void flog(char *file, const char *fmt, ...) +{ + char fn[80]; + va_list ap; + FILE *fp; + + snprintf(fn, sizeof(fn), "/tmp/%s.log", file); + fp = fopen(fn, "a"); + if (!fp) + return; + + va_start(ap, fmt); + vfprintf(fp, fmt, ap); + va_end(ap); + + fclose(fp); +} + + /** * Local Variables: * indent-tabs-mode: t diff --git a/src/log.h b/src/log.h index 6ade9a9b..1a371219 100644 --- a/src/log.h +++ b/src/log.h @@ -51,5 +51,6 @@ void log_exit (void); void log_debug (void); void logit (int prio, const char *fmt, ...); +void flog (char *file, const char *fmt, ...); #endif /* FINIT_LOG_H_ */ From 6b7255fdeb51682abc1dc044b43e7e3409e228e3 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 09:11:56 +0100 Subject: [PATCH 09/21] Fix regression introduced in 7ef25ab, global rlimits not initialized Services that are instantiated by plugins, and thus don't have a .conf file, tried to use uninitialized rlimits. This caused the kernel to immediately kill those services for violating their own rlimits. Signed-off-by: Joachim Wiberg --- src/conf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/conf.c b/src/conf.c index ae7def33..ffc674df 100644 --- a/src/conf.c +++ b/src/conf.c @@ -902,6 +902,14 @@ int conf_init(uev_ctx_t *ctx) /* default hostname */ hostname = strdup(DEFHOST); + /* + * Get current global limits, which may be overridden from both + * finit.conf, for Finit and its services like getty+watchdogd, + * and *.conf in finit.d/, for each service(s) listed there. + */ + for (int i = 0; i < RLIMIT_NLIMITS; i++) + getrlimit(i, &global_rlimit[i]); + /* prepare /etc watcher */ fd = iwatch_init(&iw_conf); if (fd < 0) From e3b27d47bc80f4574c635fce296c85a8c0914a88 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 09:52:42 +0100 Subject: [PATCH 10/21] Follow-up to dfd41a1: fix default finit.conf path regression Signed-off-by: Joachim Wiberg --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 6de89ca9..53334075 100644 --- a/configure.ac +++ b/configure.ac @@ -152,7 +152,7 @@ AS_IF([test "x$enable_logrotate" != "xno"], [ ### With features ############################################################################## AS_IF([test "x$with_config" != "xno"], [ - AS_IF([test "x$config" = "xyes"], [ + AS_IF([test "x$conf" = "xyes"], [ conf=$sysconfdir/finit.conf])]) AC_EXPAND_DIR(conf_path, "$conf") AC_DEFINE_UNQUOTED(FINIT_CONF, "$conf_path", [Main Finit configuration file, default /etc/finit.conf]) From 60d0ca1075bbfbb34627f3954d1b705a562e18ac Mon Sep 17 00:00:00 2001 From: Andreas Egeberg Date: Wed, 24 Feb 2021 08:53:54 +1300 Subject: [PATCH 11/21] Added Edit command Command Edit opens file /etc/finit.d/available/.conf in the editor referenced by Linux environment variable $EDITOR If $EDITOR is not set it will try to open emacs and if emacs is not found it will try to open the file in vim. Signed-off-by: Andreas Egeberg --- src/initctl.c | 4 +++- src/serv.c | 31 +++++++++++++++++++++++++++++++ src/serv.h | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/initctl.c b/src/initctl.c index fd12d5dd..db2325ed 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -659,7 +659,8 @@ static int usage(int rc) " disable Disable .conf in /etc/finit.d/[enabled/]\n" " touch Mark .conf in /etc/finit.d/ for reload\n" " reload Reload *.conf in /etc/finit.d/ (activates changes)\n" -// " reload [:ID] Reload (SIGHUP) service by job# or name\n" +// " reload [:ID] Reload (SIGHUP) service by job# or name\n" + " edit Edit .conf in /etc/finit.d/available/\n" "\n" " cond show Show condition status\n" " cond dump Dump all conditions and their status\n" @@ -709,6 +710,7 @@ int main(int argc, char *argv[]) { "enable", serv_enable }, { "disable", serv_disable }, { "touch", serv_touch }, + { "edit", serv_edit }, { "reload", do_reload }, { "cond", do_cond }, diff --git a/src/serv.c b/src/serv.c index 71874810..9e31514b 100644 --- a/src/serv.c +++ b/src/serv.c @@ -27,6 +27,7 @@ #include /* Definition of AT_* constants */ #include #include +#include #include #include "util.h" @@ -199,6 +200,36 @@ int serv_touch(char *arg) return 0; } +int serv_edit(char *arg) +{ + char corr[40]; + char path[256]; + + if (!arg || !arg[0]) + return serv_list(NULL); + + if (!strstr(arg, ".conf")) { + snprintf(corr, sizeof(corr), "%s.conf", arg); + arg = corr; + } + + pushd(FINIT_RCSD); + if (mkdir("available", 0755) && EEXIST != errno) + err(1, "Failed creating %s/available directory", FINIT_RCSD); + + snprintf(path, sizeof(path), "%s/%s", available, arg); + if (!fexist(path)) + return serv_list(NULL); + + if (systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) != 0) + if (systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) != 0) + if (systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) != 0) + if (systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) != 0) + systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); + + return 0; +} + /** * Local Variables: * indent-tabs-mode: t diff --git a/src/serv.h b/src/serv.h index e52c42c3..f5e41753 100644 --- a/src/serv.h +++ b/src/serv.h @@ -28,6 +28,7 @@ int serv_list (char *arg); int serv_enable (char *arg); int serv_disable (char *arg); int serv_touch (char *arg); +int serv_edit (char *arg); #endif /* FINIT_SERV_H_ */ From bf2272c6b349e050dc365ab70d623c7eed03e196 Mon Sep 17 00:00:00 2001 From: Andreas Egeberg Date: Fri, 26 Feb 2021 10:51:30 +0100 Subject: [PATCH 12/21] Beautify code --- src/serv.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/serv.c b/src/serv.c index 9e31514b..fbc45283 100644 --- a/src/serv.c +++ b/src/serv.c @@ -221,13 +221,11 @@ int serv_edit(char *arg) if (!fexist(path)) return serv_list(NULL); - if (systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) != 0) - if (systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) != 0) - if (systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) != 0) - if (systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) != 0) - systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); - - return 0; + return systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) || \ + systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) || \ + systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) || \ + systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) || \ + systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); } /** From a579e0e8ae78f77cd25b3fa0ee33703e8cfb899d Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 10:55:55 +0100 Subject: [PATCH 13/21] travis-ci: bump libuEv and libite versions, for systemf() Signed-off-by: Joachim Wiberg --- .travis.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 74c98092..ea1eeae5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,12 +35,12 @@ addons: branch_pattern: dev install: - - wget https://github.com/troglobit/libuev/releases/download/v2.2.0/libuev-2.2.0.tar.xz - - wget https://github.com/troglobit/libite/releases/download/v2.0.1/libite-2.0.1.tar.xz - - tar xf libuev-2.2.0.tar.xz - - tar xf libite-2.0.1.tar.xz - - (cd libuev-2.2.0 && ./configure --prefix=/tmp && make && make install-strip) - - (cd libite-2.0.1 && ./configure --prefix=/tmp && make && make install-strip) + - wget https://github.com/troglobit/libuev/releases/download/v2.3.2/libuev-2.3.2.tar.xz + - wget https://github.com/troglobit/libite/releases/download/v2.3.1/libite-2.3.1.tar.gz + - tar xf libuev-2.3.2.tar.xz + - tar xf libite-2.3.1.tar.gz + - (cd libuev-2.3.2 && ./configure --prefix=/tmp && make && make install-strip) + - (cd libite-2.3.1 && ./configure --prefix=/tmp && make && make install-strip) # Custom build script for the time being, no "make test" yet script: From 6b1ba374387a20abf6660d236a034ceb2595c641 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 10:57:03 +0100 Subject: [PATCH 14/21] doc: bump required versions of libuEv and libite Signed-off-by: Joachim Wiberg --- doc/build.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/build.md b/doc/build.md index d38f8ace..3ca69bc4 100644 --- a/doc/build.md +++ b/doc/build.md @@ -29,8 +29,8 @@ optional plugins to enable. It depends on two external libraries: The configure script checks for all dependencies, including the correct version of the above mentioned libraries. Currently requried versions: -- libite v2.0.1 -- libuEv v2.1.0 +- libite v2.3.1 +- libuEv v2.3.2 Configure From 7439fa0c2a228b8ca16b7842c9a94d8aab7d9e41 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 11:08:03 +0100 Subject: [PATCH 15/21] initctl: fix logic in edit command fallback handling Signed-off-by: Joachim Wiberg --- src/serv.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/serv.c b/src/serv.c index fbc45283..a54f022f 100644 --- a/src/serv.c +++ b/src/serv.c @@ -221,11 +221,11 @@ int serv_edit(char *arg) if (!fexist(path)) return serv_list(NULL); - return systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) || \ - systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) || \ - systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) || \ - systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) || \ - systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); + return !systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) || \ + !systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) || \ + !systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) || \ + !systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) || \ + !systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); } /** From 6cf8c7ade4212094ef5c55e18bd8efd1db3037c6 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 11:45:04 +0100 Subject: [PATCH 16/21] initctl: minor refactor, simplify edit command - Gamble a bit by redirecting stderr to /dev/null instead of command -v - Drop vi from VISUAL/EDITOR expansion alternatives, tested later anyway Signed-off-by: Joachim Wiberg --- src/serv.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/serv.c b/src/serv.c index a54f022f..a78ca674 100644 --- a/src/serv.c +++ b/src/serv.c @@ -202,6 +202,13 @@ int serv_touch(char *arg) int serv_edit(char *arg) { + char *editor[] = { + "sensible-editor", + "editor", + "${VISUAL:-${EDITOR}}", + "mg", + "vi" + }; char corr[40]; char path[256]; @@ -221,11 +228,13 @@ int serv_edit(char *arg) if (!fexist(path)) return serv_list(NULL); - return !systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) || \ - !systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) || \ - !systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) || \ - !systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) || \ - !systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); + for (size_t i = 0; i < NELEMS(editor); i++) { + if (systemf("%s %s 2>/dev/null", editor[i], path)) + continue; + return 0; + } + + return 1; } /** From 001e859a97654566728acc4b3b13762fbc766d4d Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 12:47:47 +0100 Subject: [PATCH 17/21] initctl: add create command, no template yet Signed-off-by: Joachim Wiberg --- src/initctl.c | 4 +++- src/serv.c | 20 +++++++++++++++++--- src/serv.h | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/initctl.c b/src/initctl.c index 1ade2b0b..59d86fb5 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -655,7 +655,8 @@ static int usage(int rc) " help This help text\n" " version Show Finit version\n" "\n" - " ls|list List all .conf in /etc/finit.d/\n" + " ls | list List all .conf in /etc/finit.d/\n" + " create Create .conf in /etc/finit.d/available/\n" " enable Enable .conf in /etc/finit.d/available/\n" " disable Disable .conf in /etc/finit.d/[enabled/]\n" " touch Mark .conf in /etc/finit.d/ for reload\n" @@ -712,6 +713,7 @@ int main(int argc, char *argv[]) { "disable", serv_disable }, { "touch", serv_touch }, { "edit", serv_edit }, + { "create", serv_creat }, { "reload", do_reload }, { "cond", do_cond }, diff --git a/src/serv.c b/src/serv.c index a78ca674..a07dd7e2 100644 --- a/src/serv.c +++ b/src/serv.c @@ -200,7 +200,7 @@ int serv_touch(char *arg) return 0; } -int serv_edit(char *arg) +static int do_edit(char *arg, int creat) { char *editor[] = { "sensible-editor", @@ -225,8 +225,12 @@ int serv_edit(char *arg) err(1, "Failed creating %s/available directory", FINIT_RCSD); snprintf(path, sizeof(path), "%s/%s", available, arg); - if (!fexist(path)) - return serv_list(NULL); + if (!fexist(path)) { + if (!creat) + return serv_list(NULL); + + /* XXX: fill with template/commented-out examples */ + } for (size_t i = 0; i < NELEMS(editor); i++) { if (systemf("%s %s 2>/dev/null", editor[i], path)) @@ -237,6 +241,16 @@ int serv_edit(char *arg) return 1; } +int serv_edit(char *arg) +{ + return do_edit(arg, 0); +} + +int serv_creat(char *arg) +{ + return do_edit(arg, 1); +} + /** * Local Variables: * indent-tabs-mode: t diff --git a/src/serv.h b/src/serv.h index f5e41753..be9febd4 100644 --- a/src/serv.h +++ b/src/serv.h @@ -29,6 +29,7 @@ int serv_enable (char *arg); int serv_disable (char *arg); int serv_touch (char *arg); int serv_edit (char *arg); +int serv_creat (char *arg); #endif /* FINIT_SERV_H_ */ From 41319a8cf83ab9edc60aaf05a6f56a3500a9b01e Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 14:54:34 +0100 Subject: [PATCH 18/21] Work around forbidden chars in wordexp(): | < > & ; Signed-off-by: Joachim Wiberg --- src/service.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/service.c b/src/service.c index 116f1528..c5e53c58 100644 --- a/src/service.c +++ b/src/service.c @@ -493,10 +493,25 @@ static int service_start(svc_t *svc) } for (i = 0; i < MAX_NUM_SVC_ARGS; i++) { - if (strlen(svc->args[i]) == 0) + char *arg = svc->args[i]; + size_t len = strlen(arg); + char str[len + 2] = {0}; + char ch = *arg; + + if (len == 0) break; - if ((rc = wordexp(svc->args[i], &we, WRDE_APPEND))) + /* + * Escape forbidden characters in wordexp() + * but allowed in Finit run/task stanzas, + * + * XXX: escapes only leading characters ... + */ + if (strchr("|<>&:", ch)) + str[0] = '\\'; + strlcat(str, arg, sizeof(str)); + + if ((rc = wordexp(str, &we, WRDE_APPEND))) goto nomem; } From 5e37f94775a231a44e3aaab9480172e423a2a6e2 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 15:06:02 +0100 Subject: [PATCH 19/21] Improve error messages for failing wordexp() Signed-off-by: Joachim Wiberg --- src/service.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/service.c b/src/service.c index c5e53c58..6cefdc7c 100644 --- a/src/service.c +++ b/src/service.c @@ -486,8 +486,8 @@ static int service_start(svc_t *svc) int rc; if ((rc = wordexp(svc->cmd, &we, 0))) { + _e("%s: failed wordexp(%s): %d", svc->cmd, svc->cmd, rc); nomem: - _e("%s: failed wordexp(): %d", svc->cmd, rc); wordfree(&we); _exit(1); } @@ -495,7 +495,7 @@ static int service_start(svc_t *svc) for (i = 0; i < MAX_NUM_SVC_ARGS; i++) { char *arg = svc->args[i]; size_t len = strlen(arg); - char str[len + 2] = {0}; + char str[len + 2]; char ch = *arg; if (len == 0) @@ -508,11 +508,15 @@ static int service_start(svc_t *svc) * XXX: escapes only leading characters ... */ if (strchr("|<>&:", ch)) - str[0] = '\\'; + sprintf(str, "\\"); + else + str[0] = 0; strlcat(str, arg, sizeof(str)); - if ((rc = wordexp(str, &we, WRDE_APPEND))) + if ((rc = wordexp(str, &we, WRDE_APPEND))) { + _e("%s: failed wordexp(%s): %d", svc->cmd, str, rc); goto nomem; + } } if (we.we_wordc > MAX_NUM_SVC_ARGS) { From 6d4bc670e68bae9d30add66dfe34ff52a0b911d7 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 16:21:53 +0100 Subject: [PATCH 20/21] Relocate paste() from helpers.h to util.h, for sharing with initctl Signed-off-by: Joachim Wiberg --- src/exec.c | 1 + src/helpers.h | 11 ----------- src/pid.h | 1 + src/util.h | 12 ++++++++++++ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/exec.c b/src/exec.c index 155eaf2d..14558500 100644 --- a/src/exec.c +++ b/src/exec.c @@ -38,6 +38,7 @@ #include "conf.h" #include "helpers.h" #include "sig.h" +#include "util.h" #include "utmp-api.h" #define NUM_ARGS 16 diff --git a/src/helpers.h b/src/helpers.h index 7c734d9f..e4fc1595 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -103,17 +103,6 @@ static inline int create(char *path, mode_t mode, uid_t uid, gid_t gid) return 0; } -/* paste dir/file into buf */ -static inline int paste(char *buf, size_t len, const char *dir, const char *file) -{ - if (!dir) - dir = ""; - if (!file) - file = ""; - - return snprintf(buf, len, "%s%s%s", dir, fisslashdir(dir) ? "" : "/", file); -} - int ismnt (char *file, char *dir, char *mode); int fismnt (char *dir); diff --git a/src/pid.h b/src/pid.h index 6bf96ce2..7f0bd0cb 100644 --- a/src/pid.h +++ b/src/pid.h @@ -26,6 +26,7 @@ #define FINIT_PID_H_ #include "svc.h" +#include "util.h" int pid_alive (pid_t pid); char *pid_get_name (pid_t pid, char *name, size_t len); diff --git a/src/util.h b/src/util.h index e65cd8c2..84c32379 100644 --- a/src/util.h +++ b/src/util.h @@ -31,6 +31,7 @@ #include #include #endif +#include #ifndef RB_SW_SUSPEND /* Since Linux 2.5.18, but not in GLIBC until 2.19 */ #define RB_SW_SUSPEND 0xd000fce2 @@ -64,6 +65,17 @@ static inline char *strterm(char *str, size_t len) return str; } +/* paste dir/file into buf */ +static inline int paste(char *buf, size_t len, const char *dir, const char *file) +{ + if (!dir) + dir = ""; + if (!file) + file = ""; + + return snprintf(buf, len, "%s%s%s", dir, fisslashdir(dir) ? "" : "/", file); +} + #endif /* FINIT_UTIL_H_ */ /** From 01cd9ec3588f504313b6e49a4718f427a21a26d3 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 16:42:43 +0100 Subject: [PATCH 21/21] contrib: debian: rsyslogd provides syslogd cond Signed-off-by: Joachim Wiberg --- contrib/debian/finit.d/available/acpid.conf | 4 ++-- contrib/debian/finit.d/available/rsyslogd.conf | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/contrib/debian/finit.d/available/acpid.conf b/contrib/debian/finit.d/available/acpid.conf index 8a255dac..b23bf099 100644 --- a/contrib/debian/finit.d/available/acpid.conf +++ b/contrib/debian/finit.d/available/acpid.conf @@ -1,3 +1,3 @@ # Handle PWR button to shutdown/reboot -- useful in Qemu (virt-manager) -# Depends on rsyslogd having started. Redirect any output to log. -service [2345] env:-/etc/default/acpid log acpid -f $OPTIONS -- ACPI daemon +# Depends on a syslog daemon having started. Redirect any output to log. +service [2345] env:-/etc/default/acpid log acpid -f $OPTIONS -- ACPI daemon diff --git a/contrib/debian/finit.d/available/rsyslogd.conf b/contrib/debian/finit.d/available/rsyslogd.conf index 5ca6e9f8..80e72cda 100644 --- a/contrib/debian/finit.d/available/rsyslogd.conf +++ b/contrib/debian/finit.d/available/rsyslogd.conf @@ -1,2 +1,3 @@ # Start rsyslogd as soon as possible, should always run -service [S12345] env:-/etc/default/rsyslog rsyslogd -n $RSYSLOGD_OPTIONS -- Reliable syslog daemon +# Provides pid/syslogd condition +service [S12345] name:syslogd env:-/etc/default/rsyslog rsyslogd -n $RSYSLOGD_OPTIONS -- Reliable syslog daemon