]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgls: print error messages when --unit and --user-unit are used together
authorjouyouyun <yanbowen@uniontech.com>
Tue, 4 Nov 2025 08:10:31 +0000 (16:10 +0800)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 6 Nov 2025 21:26:42 +0000 (21:26 +0000)
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.

(cherry picked from commit 2b8c7adbecb929f131dc05d8b88babd87cc0ab22)

src/cgls/cgls.c

index 8349233fa62afd67e24bb207498c2c2f4cd48ac0..9e023af0312a4dfc7709b3585fdb70cede6008c0 100644 (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();