dumpsys: Remove code duplication 14/281114/2
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 12 Sep 2022 11:25:50 +0000 (13:25 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Tue, 13 Sep 2022 06:35:56 +0000 (08:35 +0200)
Change-Id: I8f248dffde4595b0462eb89f1681394c0792220e

src/client-api/CMakeLists.txt
src/client-api/common_client_api.c [new file with mode: 0644]
src/client-api/common_client_api.h [new file with mode: 0644]
src/client-api/dumpsys-system.c
src/client-api/dumpsys-user.c
src/shared/common.h
tests/system/utils/CMakeLists.txt
tests/unit/CMakeLists.txt

index 14484db..6d4d01b 100644 (file)
@@ -8,7 +8,7 @@ pkg_check_modules(dumpsys-system_pkgs REQUIRED
        gio-2.0
        gio-unix-2.0
        pkgmgr-info)
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/shared)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/shared ${CMAKE_SOURCE_DIR}/src/client-api)
 FOREACH(flag ${dumpsys-system_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
@@ -17,7 +17,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE -Wno-unused-function -
 
 SET(DCA_SRCS dumpsys-system.c)
 
-ADD_LIBRARY(libdumpsys-system SHARED dumpsys-system.c)
+ADD_LIBRARY(libdumpsys-system SHARED dumpsys-system.c common_client_api.c)
 SET_TARGET_PROPERTIES(libdumpsys-system PROPERTIES
        SOVERSION 1
        PUBLIC_HEADER dumpsys-system.h
@@ -25,7 +25,7 @@ SET_TARGET_PROPERTIES(libdumpsys-system PROPERTIES
 TARGET_LINK_LIBRARIES(libdumpsys-system PUBLIC ${dumpsys-system_pkgs_LIBRARIES})
 
 
-ADD_LIBRARY(libdumpsys-user SHARED dumpsys-user.c)
+ADD_LIBRARY(libdumpsys-user SHARED dumpsys-user.c common_client_api.c)
 SET_TARGET_PROPERTIES(libdumpsys-user PROPERTIES
        SOVERSION 1
        PUBLIC_HEADER dumpsys-user.h
diff --git a/src/client-api/common_client_api.c b/src/client-api/common_client_api.c
new file mode 100644 (file)
index 0000000..574ffe4
--- /dev/null
@@ -0,0 +1,118 @@
+
+#include "common_client_api.h"
+
+int get_args(GVariant *vargs, gchar ***argv)
+{
+       assert(vargs);
+       assert(argv);
+
+       gsize count = -1;
+
+       GVariant *array = g_variant_get_child_value(vargs, 0);
+       *argv = g_variant_dup_strv(array, &count);
+       g_variant_unref(array);
+
+       return count;
+}
+
+bool register_object(struct dumpsys_info *dumpsys_info,
+                     const GDBusInterfaceVTable *interface_vtable)
+{
+       assert(dumpsys_info);
+       assert(interface_vtable);
+
+       GError *error = NULL;
+       dumpsys_info->reg_id = g_dbus_connection_register_object(dumpsys_info->connection,
+                                                   DUMPSYS_PATH,
+                                                   dumpsys_info->introspection_data->interfaces[0],
+                                                   interface_vtable,
+                                                   NULL,
+                                                   NULL,
+                                                   &error);
+
+       if (!dumpsys_info->reg_id) {
+               LOGE("g_dbus_connection_register_object() error: %s", error ? error->message : "(none)");
+               if (error)
+                       g_error_free(error);
+               return false;
+       }
+       return true;
+}
+
+bool dbus_init(const GBusType bus_type,
+               struct dumpsys_info *dumpsys_info,
+               const gchar *introspection_xml,
+               const GDBusInterfaceVTable *interface_vtable)
+{
+       assert(dumpsys_info);
+       assert(introspection_xml);
+       assert(interface_vtable);
+
+       dumpsys_info->introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
+       if (dumpsys_info->introspection_data == NULL) {
+               LOGE("g_dbus_node_info_new_for_xml() error\n");
+               return false;
+       }
+
+       GError *error = NULL;
+
+       dumpsys_info->connection = g_bus_get_sync(bus_type, NULL, &error);
+
+       if (!dumpsys_info->connection || error != NULL) {
+               LOGE("connect to the bus error: %s\n", error ? error->message : "(none)");
+               if (error)
+                       g_error_free(error);
+               return false;
+       }
+
+       char name_with_prefix[DBUS_BUS_NAME_MAX_LEN];
+       int ret = snprintf(name_with_prefix, sizeof(name_with_prefix), "%s.%s", DUMPSYS_SERVICE_NAME_PREFIX, dumpsys_info->service_name);
+       if (ret < 0) {
+               LOGE("snprintf error: %m\n");
+               return false;
+       }
+
+       dumpsys_info->own_id = g_bus_own_name_on_connection(dumpsys_info->connection,
+                                                           name_with_prefix,
+                                                           G_BUS_NAME_OWNER_FLAGS_NONE,
+                                                           NULL, NULL, NULL, NULL);
+
+       return register_object(dumpsys_info, interface_vtable);
+}
+
+bool dbus_close(struct dumpsys_info *dumpsys_info)
+{
+       assert(dumpsys_info);
+
+       if (!dumpsys_info->connection)
+               return false;
+
+       bool res = g_dbus_connection_unregister_object(dumpsys_info->connection,
+                                                       dumpsys_info->reg_id);
+       g_bus_unown_name(dumpsys_info->own_id);
+       g_object_unref(dumpsys_info->connection);
+       g_dbus_node_info_unref(dumpsys_info->introspection_data);
+       dumpsys_info->connection = NULL;
+       dumpsys_info->reg_id = 0;
+       dumpsys_info->own_id = 0;
+       return res;
+}
+
+bool register_callback_pre(const char *name,
+                           struct dumpsys_info *dumpsys_info,
+                           bool (*get_name_cb)(char **name))
+{
+       assert(dumpsys_info);
+       if (name) {
+               dumpsys_info->service_name = strdup(name);
+               if (!dumpsys_info->service_name) {
+                       LOGE("strdup error: %m\n");
+                       return false;
+               }
+       } else if (!(get_name_cb)(&dumpsys_info->service_name)) {
+               LOGE("get_name error\n");
+               return false;
+       }
+
+       return true;
+}
diff --git a/src/client-api/common_client_api.h b/src/client-api/common_client_api.h
new file mode 100644 (file)
index 0000000..23b1e3d
--- /dev/null
@@ -0,0 +1,39 @@
+#pragma once
+
+#include <assert.h>
+#include <stdio.h>
+#include <gio/gio.h>
+#include <stdbool.h>
+#include <dlog.h>
+#include "common.h"
+
+#define DBUS_BUS_NAME_MAX_LEN 255
+
+#define DCA_ERROR 1
+#define DCA_ERROR_UNSPECIFIED_ERROR 1
+#define DCA_ERROR_GETEXEPATH 2
+#define DCA_ERROR_GETAPPID 3
+#define DCA_ERROR_UNIXFDLISTEMPTY 4
+
+struct dumpsys_info {
+        GDBusNodeInfo *introspection_data;
+        GDBusConnection *connection;
+        guint reg_id, own_id;
+        char *service_name;
+};
+
+int get_args(GVariant *vargs, gchar ***argv);
+
+bool register_object(struct dumpsys_info *dumpsys_info,
+                     const GDBusInterfaceVTable *interface_vtable);
+
+bool dbus_init(const GBusType bus_type,
+               struct dumpsys_info *dumpsys_info,
+               const gchar *introspection_xml,
+               const GDBusInterfaceVTable *interface_vtable);
+
+bool dbus_close(struct dumpsys_info *dumpsys_info);
+
+bool register_callback_pre(const char *name,
+                           struct dumpsys_info *dumpsys_info,
+                           bool (*get_name_cb)(char **name));
index ee8b18e..8946d48 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "dumpsys-system.h"
 #include "common.h"
+#include "common_client_api.h"
 
 #ifndef LOG_TAG
 #define LOG_TAG "DUMPSYS_SYSTEM"
 
 #include <dlog.h>
 
-#define DBUS_BUS_NAME_MAX_LEN 255
-
-#define DCA_ERROR 1
-#define DCA_ERROR_UNSPECIFIED_ERROR 1
-#define DCA_ERROR_GETEXEPATH 2
-#define DCA_ERROR_GETAPPID 3
-#define DCA_ERROR_UNIXFDLISTEMPTY 4
 
 static dumpsys_system_dump_cb dump_cb;
-static GDBusNodeInfo *introspection_data;
-static GDBusConnection *connection;
-static guint reg_id, own_id;
-static char *service_name;
+static struct dumpsys_info dumpsys_info;
 
 static const gchar introspection_xml[] =
 "<node>"
@@ -69,10 +60,10 @@ static bool get_name(char **name)
 {
        assert(name);
 
-       if (service_name == NULL)
+       if (dumpsys_info.service_name == NULL)
                return false;
 
-       *name = strdup(service_name);
+       *name = strdup(dumpsys_info.service_name);
        if (*name == NULL)  {
                LOGE("malloc error: %m\n");
                return false;
@@ -96,19 +87,6 @@ static void dump_thread_cb(GTask *task, gpointer source_object, gpointer task_da
        g_task_return_int(task, result);
 }
 
-static int get_args(GVariant *vargs, gchar ***argv)
-{
-       assert(vargs);
-       assert(argv);
-
-       gsize count = -1;
-
-       GVariant *array = g_variant_get_child_value(vargs, 0);
-       *argv = g_variant_dup_strv(array, &count);
-       g_variant_unref(array);
-       return count;
-}
-
 static void dump_handler(GDBusMethodInvocation *invocation)
 {
        assert(invocation);
@@ -165,13 +143,13 @@ static void dump_handler(GDBusMethodInvocation *invocation)
 }
 
 static void method_call_handler(GDBusConnection *conn,
-                                const gchar *sender,
-                                const gchar *object_path,
-                                const gchar *iface_name,
-                                const gchar *method_name,
-                                GVariant *parameters,
-                                GDBusMethodInvocation *invocation,
-                                gpointer user_data)
+                               const gchar *sender,
+                               const gchar *object_path,
+                               const gchar *iface_name,
+                               const gchar *method_name,
+                               GVariant *parameters,
+                               GDBusMethodInvocation *invocation,
+                               gpointer user_data)
 {
        if (g_strcmp0(method_name, METHOD_DUMP) == 0)
                dump_handler(invocation);
@@ -181,95 +159,14 @@ static const GDBusInterfaceVTable interface_vtable = {
        method_call_handler, NULL, NULL
 };
 
-static bool register_object(GDBusConnection *conn)
-{
-       GError *error = NULL;
-       reg_id = g_dbus_connection_register_object(conn,
-                                                   DUMPSYS_PATH,
-                                                   introspection_data->interfaces[0],
-                                                   &interface_vtable,
-                                                   NULL,
-                                                   NULL,
-                                                   &error);
-
-       if (!reg_id) {
-               LOGE("g_dbus_connection_register_object() error: %s\n", error ? error->message : "(none)");
-               if (error)
-                       g_error_free(error);
-               return false;
-       }
-       return true;
-}
-
-static bool dbus_init(const GBusType bus_type)
-{
-       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
-       if (introspection_data == NULL) {
-               LOGE("g_dbus_node_info_new_for_xml() error\n");
-               return false;
-       }
-
-       GError *error = NULL;
-
-       connection = g_bus_get_sync(bus_type, NULL, &error);
-
-       if (!connection || error != NULL) {
-               LOGE("connect to the bus error: %s\n", error ? error->message : "(none)");
-               if (error)
-                       g_error_free(error);
-               return false;
-       }
-
-       char name_with_prefix[DBUS_BUS_NAME_MAX_LEN];
-       int ret = snprintf(name_with_prefix, sizeof(name_with_prefix), "%s.%s", DUMPSYS_SERVICE_NAME_PREFIX, service_name);
-       if (ret < 0) {
-               LOGE("snprintf error: %m\n");
-               return false;
-       }
-
-       own_id = g_bus_own_name_on_connection(connection,
-                                             name_with_prefix,
-                                             G_BUS_NAME_OWNER_FLAGS_NONE,
-                                             NULL,
-                                             NULL,
-                                             NULL,
-                                             NULL);
-
-       return register_object(connection);
-}
-
-static bool dbus_close()
-{
-       if (!connection)
-               return false;
-
-       bool res = g_dbus_connection_unregister_object(connection, reg_id);
-       g_bus_unown_name(own_id);
-       g_object_unref(connection);
-       g_dbus_node_info_unref(introspection_data);
-       connection = NULL;
-       reg_id = 0;
-       own_id = 0;
-       return res;
-}
-
 static int register_callback(dumpsys_system_dump_cb callback, const char *name)
 {
        assert(callback);
-       if (name == NULL) {
-               if (!get_name(&service_name)) {
-                       LOGE("get_name error\n");
-                       return TIZEN_ERROR_INVALID_OPERATION;
-               }
-       } else {
-               service_name = strdup(name);
-               if (service_name == NULL) {
-                       LOGE("strdup error: %m\n");
-                       return TIZEN_ERROR_INVALID_OPERATION;
-               }
-       }
 
-       bool result = dbus_init(G_BUS_TYPE_SYSTEM);
+       bool result = register_callback_pre(name, &dumpsys_info, get_name);
+
+       if (result)
+               result = dbus_init(G_BUS_TYPE_SYSTEM, &dumpsys_info, introspection_xml, &interface_vtable);
        if (result)
                dump_cb = callback;
        return result ? TIZEN_ERROR_NONE : TIZEN_ERROR_IO_ERROR;
@@ -308,9 +205,9 @@ int API_FUNCTION dumpsys_system_unregister_dump_cb(void *handler)
 
        dump_cb = NULL;
 
-       if (service_name) {
-               free(service_name);
-               service_name = NULL;
+       if (dumpsys_info.service_name) {
+               free(dumpsys_info.service_name);
+               dumpsys_info.service_name = NULL;
        }
-       return dbus_close() ? TIZEN_ERROR_NONE : TIZEN_ERROR_IO_ERROR;
+       return dbus_close(&dumpsys_info) ? TIZEN_ERROR_NONE : TIZEN_ERROR_IO_ERROR;
 }
index 453491d..20c39fc 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "dumpsys-user.h"
 #include "common.h"
+#include "common_client_api.h"
 
 #ifndef LOG_TAG
 #define LOG_TAG "DUMPSYS_USER"
 #include <dlog.h>
 
 #define APPID_MAX 128
-#define DBUS_BUS_NAME_MAX_LEN 255
-
-#define DCA_ERROR 1
-#define DCA_ERROR_UNSPECIFIED_ERROR 1
-#define DCA_ERROR_GETEXEPATH 2
-#define DCA_ERROR_GETAPPID 3
-#define DCA_ERROR_UNIXFDLISTEMPTY 4
 
 static dumpsys_dump_cb dump_cb;
-static GDBusNodeInfo *introspection_data;
-static GDBusConnection *connection;
-static guint reg_id, own_id;
-static char *service_name;
+static struct dumpsys_info dumpsys_info;
 
 static const gchar introspection_xml[] =
 "<node>"
@@ -171,20 +162,6 @@ static bool get_name(char **name)
        return false;
 }
 
-static int get_args(GVariant *vargs, gchar ***argv)
-{
-       assert(vargs);
-       assert(argv);
-
-       gsize count = -1;
-
-       GVariant *array = g_variant_get_child_value(vargs, 0);
-       *argv = g_variant_dup_strv(array, &count);
-       g_variant_unref(array);
-
-       return count;
-}
-
 static void dump_handler(GDBusMethodInvocation *invocation)
 {
        assert(invocation);
@@ -240,13 +217,13 @@ static void dump_handler(GDBusMethodInvocation *invocation)
 }
 
 static void method_call_handler(GDBusConnection *conn,
-                                const gchar *sender,
-                                const gchar *object_path,
-                                const gchar *iface_name,
-                                const gchar *method_name,
-                                GVariant *parameters,
-                                GDBusMethodInvocation *invocation,
-                                gpointer user_data)
+                               const gchar *sender,
+                               const gchar *object_path,
+                               const gchar *iface_name,
+                               const gchar *method_name,
+                               GVariant *parameters,
+                               GDBusMethodInvocation *invocation,
+                               gpointer user_data)
 {
        if (g_strcmp0(method_name, METHOD_DUMP) == 0)
                dump_handler(invocation);
@@ -256,94 +233,15 @@ static const GDBusInterfaceVTable interface_vtable = {
        method_call_handler, NULL, NULL
 };
 
-static bool register_object(GDBusConnection *conn)
-{
-       GError *error = NULL;
-       reg_id = g_dbus_connection_register_object(conn,
-                                                   DUMPSYS_PATH,
-                                                   introspection_data->interfaces[0],
-                                                   &interface_vtable,
-                                                   NULL,
-                                                   NULL,
-                                                   &error);
-
-       if (!reg_id) {
-               LOGE("g_dbus_connection_register_object() error: %s", error ? error->message : "(none)");
-               if (error)
-                       g_error_free(error);
-               return false;
-       }
-       return true;
-}
-
-static bool dbus_init()
-{
-       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
-       if (introspection_data == NULL) {
-               LOGE("g_dbus_node_info_new_for_xml() error");
-               return false;
-       }
-
-       GError *error = NULL;
-
-       connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
-
-       if (!connection || error != NULL) {
-               LOGE("connect to the bus error: %s", error ? error->message : "(none)");
-               if (error)
-                       g_error_free(error);
-               return false;
-       }
-
-       char name_with_prefix[DBUS_BUS_NAME_MAX_LEN];
-       int ret = snprintf(name_with_prefix, sizeof(name_with_prefix), "%s.%s", DUMPSYS_SERVICE_NAME_PREFIX, service_name);
-       if (ret < 0) {
-               LOGE("snprintf error: %m");
-               return false;
-       }
-
-       own_id = g_bus_own_name_on_connection(connection,
-                                             name_with_prefix,
-                                             G_BUS_NAME_OWNER_FLAGS_NONE,
-                                             NULL,
-                                             NULL,
-                                             NULL,
-                                             NULL);
-
-       return register_object(connection);
-}
-
-static bool dbus_close()
-{
-       if (!connection)
-               return false;
-
-       bool res = g_dbus_connection_unregister_object(connection, reg_id);
-       g_bus_unown_name(own_id);
-       g_object_unref(connection);
-       g_dbus_node_info_unref(introspection_data);
-       connection = NULL;
-       reg_id = 0;
-       own_id = 0;
-       return res;
-}
-
 static int register_callback(dumpsys_dump_cb callback, const char *name)
 {
        assert(callback);
 
-       if (name) {
-               service_name = strdup(name);
-               if (!service_name) {
-                       LOGE("strdup error: %m\n");
-                       return TIZEN_ERROR_INVALID_OPERATION;
-               }
-       } else if (!get_name(&service_name)) {
-               LOGE("get_name error\n");
-               return TIZEN_ERROR_INVALID_OPERATION;
-       }
+       bool result = register_callback_pre(name, &dumpsys_info, get_name);
+
+       if (result)
+               result = dbus_init(G_BUS_TYPE_SYSTEM, &dumpsys_info, introspection_xml, &interface_vtable);
 
-       bool result = dbus_init();
        if (result)
                dump_cb = callback;
        return result ? TIZEN_ERROR_NONE : TIZEN_ERROR_IO_ERROR;
@@ -386,8 +284,8 @@ int API_FUNCTION dumpsys_unregister_dump_cb(void *handler)
        }
 
        dump_cb = NULL;
-       free(service_name);
-       return dbus_close() ? TIZEN_ERROR_NONE : TIZEN_ERROR_IO_ERROR;
+       free(dumpsys_info.service_name);
+       return dbus_close(&dumpsys_info) ? TIZEN_ERROR_NONE : TIZEN_ERROR_IO_ERROR;
 }
 
 int API_FUNCTION dumpsys_get_args_count(dumpsys_dump_h dump_handler, int *args_count)
index e7665a9..34d6ff9 100644 (file)
@@ -36,7 +36,6 @@
 
 #define DUMP_SUCCESS 1
 #define DUMP_FAIL 0
-
 #define ERR_TEXT(error) (error ? error->message : "unknown error")
 
 #endif
index e28646b..902a0a4 100644 (file)
@@ -21,7 +21,8 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE -Wno-unused-function -
 ADD_EXECUTABLE(test-app
                test-app.c
               ${CMAKE_SOURCE_DIR}/src/client-api/dumpsys-system.c
-              ${CMAKE_SOURCE_DIR}/src/client-api/dumpsys-user.c)
+              ${CMAKE_SOURCE_DIR}/src/client-api/dumpsys-user.c
+              ${CMAKE_SOURCE_DIR}/src/client-api/common_client_api.c)
 
 TARGET_LINK_LIBRARIES(test-app PUBLIC ${dumpsys-system_pkgs_LIBRARIES} -pie)
 
index 230619c..d72bbaf 100644 (file)
@@ -26,7 +26,7 @@ function(add_mocked_test)
         set(multiValueArgs SOURCE_DIRS)
         cmake_parse_arguments(T "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
 
-        add_executable(test_${T_NAME} test_${T_NAME}.c test_common.c)
+        add_executable(test_${T_NAME} test_${T_NAME}.c test_common.c ../../src/client-api/common_client_api.c)
 
         add_definitions(-DTEST)
         target_link_libraries(test_${T_NAME} ${dumpsys_service_pkgs_LIBRARIES} -pie -lcmocka -lgcov)