resource: Pass error result directly from resource creation accepted/tizen/unified/20220917.094318 accepted/tizen/unified/20220919.013232
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 15 Sep 2022 02:15:27 +0000 (11:15 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 15 Sep 2022 06:41:52 +0000 (15:41 +0900)
Previously, it was lost by returning NULL instead of the error code
that occurred during the resource creation process, but now the error
code is passed directly so that the caller can know the exact reason.

Change-Id: Iffc25c266186195d23ef0f61bba989ff5e36b4d7
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
include/util/resource.h
src/monitor/request-handler.c
src/util/resource.c

index 028e7c7..0855c80 100644 (file)
@@ -151,7 +151,7 @@ int add_resource_device(struct resource_device *resource_device);
 void remove_resource_device(struct resource_device *resource_device);
 
 /* Create/delete resource instance */
-struct resource *create_resource(int resource_type);
+int create_resource(struct resource **res, int resource_type);
 void delete_resource(struct resource *resource);
 
 /* Set flag of the resource to given flag mask */
index 5d2d8bd..f5a2138 100644 (file)
@@ -88,7 +88,7 @@ get_resource_by_id(struct request_client *client, int resource_id)
 static int handle_request_create_resource(struct request_client *client, char *args)
 {
        struct resource *res;
-       int resource_type;
+       int resource_type, ret;
 
        if (!client || !args) {
                _E("Invalid parameter\n");
@@ -101,10 +101,10 @@ static int handle_request_create_resource(struct request_client *client, char *a
         */
        resource_type = atoi(args);
 
-       res = create_resource(resource_type);
-       if (!res) {
+       ret = create_resource(&res, resource_type);
+       if (ret < 0) {
                _E("failed to create resource, res:type(%d)\n", resource_type);
-               return -EINVAL;
+               return ret;
        }
 
        register_resource_to_client(client, res);
index cfb9e94..2def817 100644 (file)
@@ -227,7 +227,7 @@ void init_resource_id(void)
        g_resource_id = clear_sign_bit(val);
 }
 
-struct resource *create_resource(int resource_type)
+int create_resource(struct resource **res, int resource_type)
 {
        const struct resource_driver *driver = NULL;
        struct resource *resource = NULL;
@@ -237,12 +237,12 @@ struct resource *create_resource(int resource_type)
        if (!driver) {
                _E("failed to find resource driver, res:type(%d)\n",
                                                resource_type);
-               return NULL;
+               return -EINVAL;
        }
 
        resource = calloc(1, sizeof(*resource));
        if (!resource)
-               return NULL;
+               return -ENOMEM;
 
        resource->id = clear_sign_bit(
                        (unsigned int)__sync_fetch_and_add(&g_resource_id, 1));
@@ -255,7 +255,7 @@ struct resource *create_resource(int resource_type)
                                        sizeof(*resource->attrs_value));
        if (!resource->attrs_value) {
                do_delete_resource(resource);
-               return NULL;
+               return -ENOMEM;
        }
 
        for (i = 0; i < resource->num_attrs; i++)
@@ -271,11 +271,13 @@ struct resource *create_resource(int resource_type)
                        _E("failed to initialize resource driver, res:type(%s)id(%d)\n",
                                                resource->name, resource->id);
                        do_delete_resource(resource);
-                       return NULL;
+                       return ret;
                }
        }
 
-       return resource;
+       *res = resource;
+
+       return 0;
 }
 
 int set_resource_flag(struct resource *resource, u_int64_t flag_mask)