From 66b62cbd455d8518eeb8871795b30c7da0fb0358 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 13 Nov 2026 16:00:25 +0100 Subject: [PATCH] service: don't take a command named like an option for one A run/task/service whose command starts like an option without a ':' lost its command, e.g. the example in finit.conf(5): run [2345] @joe:users logger "Hello world" was registered as the log option, running "Hello world". Same for pidof, pidproxy, and cgroupfs-mount. The option parser matches with MATCH_CMD(), a prefix match, also for the options with no trailing ':' to end the prefix: log, pid, cgroup, respawn, nowarn, and the rest. Match these against the whole token, allowing only the separator that starts a value: "log:", "pid:", "cgroup." and "cgroup:". Signed-off-by: Joachim Wiberg (cherry picked from commit f15e95d1cc2b84711c05576b9fbccc03c7266a34) Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 4 ++++ src/service.c | 40 +++++++++++++++++++++++++++++++--------- test/Makefile.am | 2 ++ test/option-lookalike.sh | 21 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100755 test/option-lookalike.sh diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 926d6b25..bfb06445 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -30,6 +30,10 @@ All relevant changes are documented in this file. - Fix `initctl stop` of a forking service before it had written its PID file. The PID file showing up later undid the stop and the service was reported running. Now the late daemon is stopped instead +- Fix a command whose name starts like a service option, e.g. `logger` + or `pidof`. It was taken as the `log` or `pid` option and the next + argument ran as the command, which also broke the `logger` example in + finit.conf(5) - Remove stale daemon-owned (`pid:!`) pidfiles after unclean exits. When a daemon dies via SIGKILL/OOM/segfault, or exits early during startup, its pidfile lingers and can prevent the next instance from diff --git a/src/service.c b/src/service.c index 0051c671..8601b712 100644 --- a/src/service.c +++ b/src/service.c @@ -1473,6 +1473,24 @@ static char *parse_args(char **line) return NULL; } +/* + * Match an option that takes a value, @opt on its own or followed by + * one of the characters in @sep, e.g. "log" and "log:/path". Returns + * the value, or NULL, so a command like logger or pidof is not taken + * for an option. + */ +static char *is_opt(char *tok, const char *opt, const char *sep) +{ + size_t len = strlen(opt); + + if (strncasecmp(tok, opt, len)) + return NULL; + if (tok[len] && !strchr(sep, tok[len])) + return NULL; + + return &tok[len]; +} + /* * log:/path/to/logfile,priority:facility.level,tag:ident */ @@ -1921,6 +1939,10 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) return errno = ENOENT; } + /* + * Options ending in ':' can be prefix matched, the rest must match + * the whole token or a command like logger is taken for one. + */ while (cmd) { char *arg; @@ -1935,19 +1957,19 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) cond = &cmd[1]; else if (cmd[0] == ':') /* :ID */ id = &cmd[1]; - else if (MATCH_CMD(cmd, "log", arg)) + else if (is_opt(cmd, "log", ":")) log = cmd; - else if (MATCH_CMD(cmd, "pid", arg)) + else if (is_opt(cmd, "pid", ":")) pid = cmd; else if (MATCH_CMD(cmd, "name:", arg)) name = cmd; else if (MATCH_CMD(cmd, "notify:", arg)) notify = arg; - else if (MATCH_CMD(cmd, "type:forking", arg)) + else if (!strcasecmp(cmd, "type:forking")) forking = 1; - else if (MATCH_CMD(cmd, "manual:yes", arg)) + else if (!strcasecmp(cmd, "manual:yes")) manual = 1; - else if (MATCH_CMD(cmd, "remain:yes", arg)) + else if (!strcasecmp(cmd, "remain:yes")) remain = 1; else if (MATCH_CMD(cmd, "restart:", arg)) { if (MATCH_CMD(arg, "always", arg)) @@ -1959,9 +1981,9 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) restart_tmo = atoi(arg) * 1000; else if (MATCH_CMD(cmd, "restart_sec:", arg)) restart_tmo = atoi(arg) * 1000; - else if (MATCH_CMD(cmd, "norestart", arg)) + else if (!strcasecmp(cmd, "norestart")) restart_max = 0; - else if (MATCH_CMD(cmd, "nowarn", arg)) + else if (!strcasecmp(cmd, "nowarn")) nowarn = 1; else if (MATCH_CMD(cmd, "oncrash:", arg)) { if (MATCH_CMD(arg, "reboot", arg)) @@ -1969,7 +1991,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) if (MATCH_CMD(arg, "script", arg)) oncrash_action = SVC_ONCRASH_SCRIPT; } - else if (MATCH_CMD(cmd, "respawn", arg)) + else if (!strcasecmp(cmd, "respawn")) respawn = 1; else if (MATCH_CMD(cmd, "halt:", arg)) halt = arg; @@ -1994,7 +2016,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) else if (MATCH_CMD(cmd, "tty:", arg)) ctty = arg; /* catch both cgroup: and cgroup. handled in parse_cgroup() */ - else if (MATCH_CMD(cmd, "cgroup", arg)) + else if ((arg = is_opt(cmd, "cgroup", ".:"))) cgroup = arg; else if (MATCH_CMD(cmd, "conflict:", arg)) conflict = arg; diff --git a/test/Makefile.am b/test/Makefile.am index 57dda818..95aba16d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -42,6 +42,7 @@ EXTRA_DIST += global-envs.sh EXTRA_DIST += initctl-status-subset.sh EXTRA_DIST += notify.sh EXTRA_DIST += pidfile.sh +EXTRA_DIST += option-lookalike.sh EXTRA_DIST += pidfile-stopped.sh EXTRA_DIST += stale-pidfile.sh EXTRA_DIST += pre-post-serv.sh @@ -89,6 +90,7 @@ TESTS += global-envs.sh TESTS += initctl-status-subset.sh TESTS += notify.sh TESTS += pidfile.sh +TESTS += option-lookalike.sh TESTS += pidfile-stopped.sh TESTS += stale-pidfile.sh TESTS += pre-post-serv.sh diff --git a/test/option-lookalike.sh b/test/option-lookalike.sh new file mode 100755 index 00000000..3f9406f3 --- /dev/null +++ b/test/option-lookalike.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Verify that a command which starts like a service option, e.g. pidof +# vs. pid, is still taken as the command. +set -eu + +TEST_DIR=$(dirname "$0") + +test_teardown() +{ + say "Running test teardown." + run "rm -f $FINIT_CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +say 'A command named like an option is the command' +run "echo 'task [2345] name:lookalike pidof serv -- Lookalike' > $FINIT_CONF" +run "initctl reload" + +retry "test \"\$(texec initctl -p status lookalike | sed -n 's/.*Command : //p')\" = 'pidof serv'"