]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homectl: split out helper parse_capability_set_field
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 10 Dec 2025 14:35:39 +0000 (15:35 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 13 Jan 2026 16:59:54 +0000 (17:59 +0100)
src/home/homectl.c

index 9df62d6dd7b4ed751d349b9cb7fd3c35cff3bdfe..e176f2918618b7184833227392a3fe60ac1a8a89 100644 (file)
@@ -3694,6 +3694,38 @@ static int parse_group_field(
         }
 }
 
+static int parse_capability_set_field(
+                sd_json_variant **identity,
+                uint64_t *capability_set,
+                const char *field,
+                const char *arg) {
+
+        _cleanup_strv_free_ char **l = NULL;
+        int r;
+
+        assert(identity);
+        assert(capability_set);
+        assert(field);
+        assert(arg);
+
+        r = parse_capability_set(arg, CAP_MASK_UNSET, capability_set);
+        if (r == 0)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capabilities in capability string '%s'.", arg);
+        if (r < 0)
+                return log_error_errno(r, "Failed to parse capability string '%s': %m", arg);
+
+        if (*capability_set == CAP_MASK_UNSET)
+                return drop_from_identity(field);
+
+        if (capability_set_to_strv(*capability_set, &l) < 0)
+                return log_oom();
+
+        r = sd_json_variant_set_field_strv(identity, field, l);
+        if (r < 0)
+                return log_error_errno(r, "Failed to set %s field: %m", field);
+        return 0;
+}
+
 static int help(int argc, char *argv[], void *userdata) {
         _cleanup_free_ char *link = NULL;
         int r;
@@ -4727,43 +4759,20 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_CAPABILITY_AMBIENT_SET:
-                case ARG_CAPABILITY_BOUNDING_SET: {
-                        _cleanup_strv_free_ char **l = NULL;
-                        uint64_t *which;
-                        const char *field;
-
-                        if (c == ARG_CAPABILITY_AMBIENT_SET) {
-                                which = &arg_capability_ambient_set;
-                                field = "capabilityAmbientSet";
-                        } else {
-                                assert(c == ARG_CAPABILITY_BOUNDING_SET);
-                                which = &arg_capability_bounding_set;
-                                field = "capabilityBoundingSet";
-                        }
-
-                        r = parse_capability_set(optarg, CAP_MASK_UNSET, which);
-                        if (r == 0)
-                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capabilities in capability string '%s'.", optarg);
+                        r = parse_capability_set_field(match_identity ?: &arg_identity_extra,
+                                                       &arg_capability_ambient_set,
+                                                       "capabilityAmbientSet", optarg);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse capability string '%s': %m", optarg);
-
-                        if (*which == CAP_MASK_UNSET) {
-                                r = drop_from_identity(field);
-                                if (r < 0)
-                                        return r;
-
-                                break;
-                        }
-
-                        if (capability_set_to_strv(*which, &l) < 0)
-                                return log_oom();
+                                return r;
+                        break;
 
-                        r = sd_json_variant_set_field_strv(match_identity ?: &arg_identity_extra, field, l);
+                case ARG_CAPABILITY_BOUNDING_SET:
+                        r = parse_capability_set_field(match_identity ?: &arg_identity_extra,
+                                                       &arg_capability_bounding_set,
+                                                       "capabilityBoundingSet", optarg);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to set %s field: %m", field);
-
+                                return r;
                         break;
-                }
 
                 case ARG_PROMPT_NEW_USER:
                         arg_prompt_new_user = true;