cgls: print error messages when --unit and --user-unit are used together

Mixing the `--unit` and `--user-unit` options will result in error messages.
During the parsing phase, only the `arg_show_unit` record of the last
occurrence of the option is used; all names are placed in the same `arg_names`,
thus mixing the two types of units in the query.

For example, `-u foo --user-unit bar` will also treat `foo` as a user unit and
query it in the user service.
This commit is contained in:
jouyouyun
2025-11-05 01:38:18 +09:00
committed by Yu Watanabe
parent a8c3ac6672
commit 2b8c7adbec
+8
View File
@@ -114,12 +114,20 @@ static int parse_argv(int argc, char *argv[]) {
break;
case 'u':
if (arg_show_unit == SHOW_UNIT_USER)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Cannot combine --unit with --user-unit.");
arg_show_unit = SHOW_UNIT_SYSTEM;
if (strv_push(&arg_names, optarg) < 0) /* push optarg if not empty */
return log_oom();
break;
case ARG_USER_UNIT:
if (arg_show_unit == SHOW_UNIT_SYSTEM)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Cannot combine --user-unit with --unit.");
arg_show_unit = SHOW_UNIT_USER;
if (strv_push(&arg_names, optarg) < 0) /* push optarg if not empty */
return log_oom();