From: Zbigniew Jędrzejewski-Szmek Date: Thu, 14 May 2026 16:44:29 +0000 (+0200) Subject: core: tweak handling of unknown options X-Git-Tag: v261-rc1~157^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63fc9f1696a7d2032280f0bfd8729ecf2fe425e1;p=thirdparty%2Fsystemd.git core: tweak handling of unknown options 099663ff8c117303af369a4d412dafed0c5614c2 added "support" for -b/-s/-z ARG with a comment of > /* Just to eat away the sysvinit kernel cmdline args without getopt() > * error messages that we'll parse in parse_proc_cmdline_word() or > * ignore. */ And for PID1 those was valid. But when not running as PID1, those options would be parsed as valid but then help() would immediately return -EINVAL: $ build-old/systemd -b; echo $? 1 At the same time, when running as PID1, if we encounter an error, we shouldn't opine about the rest of the command line. So continuing with the loop and the checks after the loop were iffy. Later, cd57038a30aa9447bde3af7111ac8dc517b38bbf made a big refactoring, and the 'break' (i.e. continuation of the loop) was changed to 'return 0', making things even more confusing, since now we'd just silently stop in the middle of the command line if -b/-s/-z were encountered. So be more careful: when running as PID1, stop parsing on error and return from the function. We didn't parse the full command line, so the later checks are not useful. Silently ignore -b/-s/-z. When not running as PID1, explicitly say that -b/-s/-z are not supported, and propogate the error if parsing fails, e.g. with an unknown option. --- diff --git a/src/core/main.c b/src/core/main.c index 9fc2ace1c56..3e67186f33b 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1178,15 +1178,21 @@ static int parse_argv(int argc, char *argv[]) { log_set_max_level(LOG_DEBUG); break; - /* Just to eat away the sysvinit kernel cmdline args that we'll parse in + /* Eat the sysvinit kernel cmdline args that we'll parse in * parse_proc_cmdline_item() or ignore. */ OPTION_SHORT('b', NULL, /* help= */ NULL): {} OPTION_SHORT('s', NULL, /* help= */ NULL): {} OPTION_SHORT('z', "ARG", /* help= */ NULL): + if (getpid_cached() != 1) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Switch -%c is only supported when running as PID 1.", opts.opt->short_code); + /* In PID1, do nothing and continue with option parsing. */ + break; OPTION_ERROR: - if (getpid_cached() == 1) - return 0; - return -EINVAL; + if (getpid_cached() != 1) + return -EINVAL; + /* In PID1, silently terminate option parsing. */ + return 0; } if (option_parser_get_n_args(&opts) > 0 && getpid_cached() != 1)