]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: tweak handling of unknown options
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Thu, 14 May 2026 16:44:29 +0000 (18:44 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Fri, 15 May 2026 07:26:48 +0000 (09:26 +0200)
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.

src/core/main.c

index 9fc2ace1c56f28c004c1ad082a7148a037011c5a..3e67186f33b4c441de7dd4e09a6f0e8d88e7657a 100644 (file)
@@ -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)