From 2b8c7adbecb929f131dc05d8b88babd87cc0ab22 Mon Sep 17 00:00:00 2001 From: jouyouyun Date: Tue, 4 Nov 2025 16:10:31 +0800 Subject: [PATCH] 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. --- src/cgls/cgls.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 8349233fa62..9e023af0312 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -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(); -- 2.47.3