core: tweak handling of unknown options
099663ff8cadded "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,cd57038a30made 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.
This commit is contained in:
+10
-4
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user