]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nsresource: make sd_json_dispatch_field table static
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Sep 2024 16:27:44 +0000 (01:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Sep 2024 16:34:57 +0000 (01:34 +0900)
This also adds missing error check of sd_json_dispatch().

Follow-up for 54452c7b2aa03536331fc1ec04439c89d0320b57.

src/shared/nsresource.c

index 4a2a9d2e41677e45d0c07c0a217edf327c875303..24ea2e2be5680f6dd7c3a8f0419b7d701671d20f 100644 (file)
@@ -250,6 +250,18 @@ int nsresource_add_cgroup(int userns_fd, int cgroup_fd) {
         return 1;
 }
 
+typedef struct InterfaceParams {
+        char *host_interface_name;
+        char *namespace_interface_name;
+} InterfaceParams;
+
+static void interface_params_done(InterfaceParams *p) {
+        assert(p);
+
+        free(p->host_interface_name);
+        free(p->namespace_interface_name);
+}
+
 int nsresource_add_netif(
                 int userns_fd,
                 int netns_fd,
@@ -313,20 +325,20 @@ int nsresource_add_netif(
         if (error_id)
                 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to add network to user namespace: %s", error_id);
 
-        _cleanup_free_ char *host_interface_name = NULL, *namespace_interface_name = NULL;
-        r = sd_json_dispatch(
-                        reply,
-                        (const sd_json_dispatch_field[]) {
-                                { "hostInterfaceName",      SD_JSON_VARIANT_STRING, sd_json_dispatch_string, PTR_TO_SIZE(&host_interface_name)      },
-                                { "namespaceInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, PTR_TO_SIZE(&namespace_interface_name) },
-                        },
-                        SD_JSON_ALLOW_EXTENSIONS,
-                        /* userdata= */ NULL);
+        static const sd_json_dispatch_field dispatch_table[] = {
+                { "hostInterfaceName",      SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(InterfaceParams, host_interface_name),      0 },
+                { "namespaceInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(InterfaceParams, namespace_interface_name), 0 },
+        };
+
+        _cleanup_(interface_params_done) InterfaceParams p = {};
+        r = sd_json_dispatch(reply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p);
+        if (r < 0)
+                return r;
 
         if (ret_host_ifname)
-                *ret_host_ifname = TAKE_PTR(host_interface_name);
+                *ret_host_ifname = TAKE_PTR(p.host_interface_name);
         if (ret_namespace_ifname)
-                *ret_namespace_ifname = TAKE_PTR(namespace_interface_name);
+                *ret_namespace_ifname = TAKE_PTR(p.namespace_interface_name);
 
         return 1;
 }