From f2162bc7cb174f59357b416701f84123289f1a4a Mon Sep 17 00:00:00 2001 From: "taemin.yeom" Date: Mon, 8 Mar 2021 17:23:27 +0900 Subject: [PATCH 01/16] Restore gdbus APIs based on va_list Change-Id: I9c275d6c652ac4253d3058fa3c022340ca55c6cd Signed-off-by: taemin.yeom --- src/libgdbus/dbus-system.c | 176 +++++++++++++++++++++++++++++++++++++++++++++ src/libgdbus/dbus-system.h | 24 +++++++ 2 files changed, 200 insertions(+) diff --git a/src/libgdbus/dbus-system.c b/src/libgdbus/dbus-system.c index 2b3fbb5..45b0874 100644 --- a/src/libgdbus/dbus-system.c +++ b/src/libgdbus/dbus-system.c @@ -1852,6 +1852,110 @@ int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char return 0; } +int dbus_handle_method_sync_pairs(const char *dest, + const char *path, + const char *iface, + const char *method, + int num, + va_list args) +{ + GError *err = NULL; + GVariant * reply = NULL; + char *key, *value; + int ret = 0; + GVariant *var; + GVariantBuilder *builder; + dbus_handle_s *dh = NULL; + + if (!dest || !path || !iface || !method) { + _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method); + return -1; + } + + dh = _dbus_handle_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + return -1; + } + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{ss}")); + + for (int i = 0 ; i < num ; i = i + 2) { + key = va_arg(args, char *); + value = va_arg(args, char *); + _I("key(%s), value(%s)", key, value); + g_variant_builder_add(builder, "{ss}", key, value); + } + + var = g_variant_new("(a{ss})", builder); + g_variant_builder_unref(builder); + + reply = g_dbus_connection_call_sync(dh->conn, + dest, path, iface, method, + var, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err); + if (!reply || err) { + _E("failed to g_dbus_connection_call_sync"); + return -1; + } + + if (g_strcmp0("(i)", g_variant_get_type_string(reply)) == 0) + g_variant_get(reply, "(i)", &ret); + else + ret = -ENOMSG; + + g_variant_unref(reply); + + return ret; +} + +int dbus_handle_method_async_pairs(const char *dest, + const char *path, + const char *iface, + const char *method, + int num, + va_list args) +{ + char *key, *value; + GVariant *var; + GVariantBuilder *builder; + dbus_handle_s *dh = NULL; + + if (!dest || !path || !iface || !method) { + _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method); + return -1; + } + + dh = _dbus_handle_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + return -1; + } + + // dict + builder = g_variant_builder_new(G_VARIANT_TYPE("a{ss}")); + + for (int i = 0 ; i < num ; i = i + 2) { + key = va_arg(args, char *); + value = va_arg(args, char *); + _I("key(%s), value(%s)", key, value); + g_variant_builder_add(builder, "{ss}", key, value); + } + + var = g_variant_new("(a{ss})", builder); + g_variant_builder_unref(builder); + + g_dbus_connection_call(dh->conn, + dest, path, iface, method, + var, NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + NULL, + NULL); + + return 0; +} + gint* dbus_handle_get_unix_fd_list(GDBusMethodInvocation *invocation, int *size) { GUnixFDList *fd_list = NULL; @@ -2117,6 +2221,78 @@ err: return ret; } +int dbus_handle_method_async_pairs_with_reply(const char *dest, + const char *path, + const char *iface, + const char *method, + int num, + va_list args, + dbus_pending_cb cb, + int timeout_msec, + void *data) +{ + dbus_handle_s *dh = NULL; + pending_call_data *pdata = NULL; + GVariantBuilder *builder; + char *key, *value; + GVariant *param; + int ret = 0; + + if (!dest || !path || !iface || !method) { + _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method); + return -1; + } + + if (timeout_msec < -1) { + _E("wrong timeout %d", timeout_msec); + return -1; + } + + dh = _dbus_handle_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + return -EPERM; + } + + // dict + builder = g_variant_builder_new(G_VARIANT_TYPE("a{ss}")); + + for (int i = 0 ; i < num ; i = i + 2) { + key = va_arg(args, char *); + value = va_arg(args, char *); + _I("key(%s), value(%s)", key, value); + g_variant_builder_add(builder, "{ss}", key, value); + } + + param = g_variant_new("(a{ss})", builder); + g_variant_builder_unref(builder); + + if (cb) { + pdata = (pending_call_data*)malloc(sizeof(pending_call_data)); + if (!pdata) { + ret = -ENOMEM; + goto err; + } + + pdata->func = cb; + pdata->data = data; + } + g_dbus_connection_call(dh->conn, + dest, path, iface, method, + param, NULL, + G_DBUS_CALL_FLAGS_NONE, + timeout_msec, + NULL, + (GAsyncReadyCallback)_cb_pending, + pdata); + + return ret; +err: + if (param) + g_variant_unref(param); + return ret; +} + int dbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) { GError *err = NULL; diff --git a/src/libgdbus/dbus-system.h b/src/libgdbus/dbus-system.h index 0b831da..c707d19 100644 --- a/src/libgdbus/dbus-system.h +++ b/src/libgdbus/dbus-system.h @@ -137,6 +137,30 @@ int dbus_handle_method_sync_with_reply_var_timeout (const char *dest, GVariant **reply, int timeout); +int dbus_handle_method_sync_pairs (const char *dest, + const char *path, + const char *interface, + const char *method, + int num, + va_list args); + +int dbus_handle_method_async_pairs (const char *dest, + const char *path, + const char *interface, + const char *method, + int num, + va_list args); + +int dbus_handle_method_async_pairs_with_reply (const char *dest, + const char *path, + const char *iface, + const char *method, + int num, + va_list args, + dbus_pending_cb cb, + int timeout_msec, + void *data); + gint* dbus_handle_get_unix_fd_list (GDBusMethodInvocation *invocation, int *size); -- 2.7.4 From 41bb90025fbe75dbc13f6bfc926109e1a337f591 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Tue, 9 Mar 2021 13:24:11 +0900 Subject: [PATCH 02/16] Rearrange header and source files Change-Id: I7cb9725a5be1ac4ead7cac73d50203f413f669cf Signed-off-by: Hyotaek Shim --- CMakeLists.txt | 12 ++--- .../{dbus-system-iface.h => dbus-iface-system.h} | 0 src/libgdbus/{dbus-system.c => libgdbus.c} | 2 +- src/libgdbus/{dbus-system.h => libgdbus.h} | 2 +- .../dbus-systemd.c => libsystemd/libsystemd.c} | 40 +++++++++++++- .../dbus-systemd.h => libsystemd/libsystemd.h} | 3 +- src/libsystemd/systemd-state.c | 61 ---------------------- src/libsystemd/systemd-state.h | 35 ------------- 8 files changed, 48 insertions(+), 107 deletions(-) rename src/libgdbus/{dbus-system-iface.h => dbus-iface-system.h} (100%) rename src/libgdbus/{dbus-system.c => libgdbus.c} (99%) rename src/libgdbus/{dbus-system.h => libgdbus.h} (99%) rename src/{libgdbus/dbus-systemd.c => libsystemd/libsystemd.c} (93%) rename src/{libgdbus/dbus-systemd.h => libsystemd/libsystemd.h} (95%) delete mode 100644 src/libsystemd/systemd-state.c delete mode 100644 src/libsystemd/systemd-state.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d314ee5..e16516d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,16 +12,14 @@ SET(INCLUDEDIR "${PREFIX}/include") SET(VERSION 4.1) SET(libsyscommon_SRCS - src/libgdbus/dbus-system.c - src/libgdbus/dbus-systemd.c - src/libsystemd/systemd-state.c + src/libgdbus/libgdbus.c + src/libsystemd/libsystemd.c src/libcommon/ini-parser.c ) SET(HEADERS - src/libgdbus/dbus-system.h - src/libgdbus/dbus-system-iface.h - src/libgdbus/dbus-systemd.h - src/libsystemd/systemd-state.h + src/libgdbus/libgdbus.h + src/libgdbus/dbus-iface-system.h + src/libsystemd/libsystemd.h src/libcommon/list.h src/libcommon/ini-parser.h src/libcommon/file.h diff --git a/src/libgdbus/dbus-system-iface.h b/src/libgdbus/dbus-iface-system.h similarity index 100% rename from src/libgdbus/dbus-system-iface.h rename to src/libgdbus/dbus-iface-system.h diff --git a/src/libgdbus/dbus-system.c b/src/libgdbus/libgdbus.c similarity index 99% rename from src/libgdbus/dbus-system.c rename to src/libgdbus/libgdbus.c index 45b0874..499a449 100644 --- a/src/libgdbus/dbus-system.c +++ b/src/libgdbus/libgdbus.c @@ -24,7 +24,7 @@ #include #include "shared/log.h" -#include "dbus-system.h" +#include "libgdbus.h" /* 10 seconds */ #define DBUS_REPLY_TIMEOUT (10000) diff --git a/src/libgdbus/dbus-system.h b/src/libgdbus/libgdbus.h similarity index 99% rename from src/libgdbus/dbus-system.h rename to src/libgdbus/libgdbus.h index c707d19..99fdc3e 100644 --- a/src/libgdbus/dbus-system.h +++ b/src/libgdbus/libgdbus.h @@ -26,7 +26,7 @@ #include #include -#include "dbus-system-iface.h" +#include "dbus-iface-system.h" typedef struct { const unsigned char *data; diff --git a/src/libgdbus/dbus-systemd.c b/src/libsystemd/libsystemd.c similarity index 93% rename from src/libgdbus/dbus-systemd.c rename to src/libsystemd/libsystemd.c index 14c8a72..a463118 100644 --- a/src/libgdbus/dbus-systemd.c +++ b/src/libsystemd/libsystemd.c @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include "shared/log.h" @@ -483,3 +483,41 @@ GVariant *systemd_get_service_property(const char *unit, const char *property) free(escaped); return val; } + +#define SYSTEMD_DBUS_METHOD_SYSTEM_STATE "SystemState" +#define SYSTEMD_STATE_RUNNING "running" +#define SYSTEMD_STATE_DEGRADED "degraded" + +int check_system_boot_finished(void) +{ + char *state = NULL; + int ret = 0; + size_t len; + GVariant *reply = NULL; + + reply = systemd_get_manager_property(SYSTEMD_DBUS_METHOD_SYSTEM_STATE); + if (!reply) { + _E("Failed to get system state: No reply"); + goto err; + } + if (!g_variant_get_safe(reply, "s", &state)) { + _E("Failed to get system state(%s)", g_variant_get_type_string(reply)); + goto err; + } + + _I("System state=%s", state); + + len = strlen(state) + 1; + if (!strncmp(state, SYSTEMD_STATE_RUNNING, len) || + !strncmp(state, SYSTEMD_STATE_DEGRADED, len)) + ret = 1; + else + ret = 0; + +err: + if (reply) + g_variant_unref(reply); + free(state); + + return ret; +} diff --git a/src/libgdbus/dbus-systemd.h b/src/libsystemd/libsystemd.h similarity index 95% rename from src/libgdbus/dbus-systemd.h rename to src/libsystemd/libsystemd.h index 4d15200..79c9f9b 100644 --- a/src/libgdbus/dbus-systemd.h +++ b/src/libsystemd/libsystemd.h @@ -20,7 +20,7 @@ #ifndef __DBUS_SYSTEMD_H__ #define __DBUS_SYSTEMD_H__ -#include "dbus-system.h" +#include #ifdef __cplusplus extern "C" { @@ -49,6 +49,7 @@ GVariant *systemd_get_unit_property (const char *unit, GVariant *systemd_get_service_property (const char *unit, const char *property); +int check_system_boot_finished(void); #ifdef __cplusplus } diff --git a/src/libsystemd/systemd-state.c b/src/libsystemd/systemd-state.c deleted file mode 100644 index 1c25575..0000000 --- a/src/libsystemd/systemd-state.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * libsyscommon - * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include "shared/log.h" - -#define SYSTEMD_DBUS_METHOD_SYSTEM_STATE "SystemState" -#define SYSTEMD_STATE_RUNNING "running" -#define SYSTEMD_STATE_DEGRADED "degraded" - -int check_system_boot_finished(void) -{ - char *state = NULL; - int ret = 0; - size_t len; - GVariant *reply = NULL; - - reply = systemd_get_manager_property(SYSTEMD_DBUS_METHOD_SYSTEM_STATE); - if (!reply) { - _E("Failed to get system state: No reply"); - goto err; - } - if (!g_variant_get_safe(reply, "s", &state)) { - _E("Failed to get system state(%s)", g_variant_get_type_string(reply)); - goto err; - } - - _I("System state=%s", state); - - len = strlen(state) + 1; - if (!strncmp(state, SYSTEMD_STATE_RUNNING, len) || - !strncmp(state, SYSTEMD_STATE_DEGRADED, len)) - ret = 1; - else - ret = 0; - -err: - if (reply) - g_variant_unref(reply); - free(state); - - return ret; -} diff --git a/src/libsystemd/systemd-state.h b/src/libsystemd/systemd-state.h deleted file mode 100644 index 54b4217..0000000 --- a/src/libsystemd/systemd-state.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * libsyscommon - * - * Copyright (c) 2019 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef __SYSTEMD_STATE_H__ -#define __SYSTEMD_STATE_H__ - -#include "dbus-systemd.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int check_system_boot_finished(void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif __SYSTEMD_STATE_H__ -- 2.7.4 From dfd0397af377e125ef1dd7bcf27a99b4f981ed4d Mon Sep 17 00:00:00 2001 From: "taemin.yeom" Date: Fri, 12 Mar 2021 11:02:08 +0900 Subject: [PATCH 03/16] Add GVariant NULL check Change-Id: I7278251402d76f4785b15b8fdc6724d80e59f642 Signed-off-by: taemin.yeom --- src/libgdbus/libgdbus.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libgdbus/libgdbus.h b/src/libgdbus/libgdbus.h index 99fdc3e..91fb1f3 100644 --- a/src/libgdbus/libgdbus.h +++ b/src/libgdbus/libgdbus.h @@ -66,7 +66,7 @@ typedef struct { gchar *sec_label; } GDBusCredentials; -#define g_variant_get_safe(gvar, signature, ...) ((g_strcmp0(signature, g_variant_get_type_string(gvar)) == 0) ? g_variant_get(gvar, signature, __VA_ARGS__), TRUE : FALSE) +#define g_variant_get_safe(gvar, signature, ...) ((gvar && (g_strcmp0(signature, g_variant_get_type_string(gvar)) == 0)) ? g_variant_get(gvar, signature, __VA_ARGS__), TRUE : FALSE) #define dbus_handle_new_g_variant_tuple() g_variant_new_tuple(NULL, 0) -- 2.7.4 From 5e55b268d818cd9ec315630b4bc8f4b2e4936301 Mon Sep 17 00:00:00 2001 From: "taemin.yeom" Date: Thu, 11 Mar 2021 18:17:04 +0900 Subject: [PATCH 04/16] Improve variable naming style Change-Id: I5236467f2efbc7768f49028b09e0a758967fd979 Signed-off-by: taemin.yeom --- src/libgdbus/libgdbus.c | 128 ++++++++++++++++++++++---------------------- src/libgdbus/libgdbus.h | 6 +-- src/libsystemd/libsystemd.c | 18 +++---- 3 files changed, 76 insertions(+), 76 deletions(-) diff --git a/src/libgdbus/libgdbus.c b/src/libgdbus/libgdbus.c index 499a449..3679337 100644 --- a/src/libgdbus/libgdbus.c +++ b/src/libgdbus/libgdbus.c @@ -998,21 +998,21 @@ static void _method_call_handler(GDBusConnection *conn, { dbus_interface_s *iface_s = (dbus_interface_s *)user_data; const dbus_method_s *methods; - GVariant *result = NULL; + GVariant *reply = NULL; /* todo: ghash ? */ methods = _dbus_handle_lookup_method(iface_s->list_methods, name); if (methods) { - result = methods->func(conn, sender, path, iface, name, param, invocation, get_dh_from_oh(iface_s->oh)); + reply = methods->func(conn, sender, path, iface, name, param, invocation, get_dh_from_oh(iface_s->oh)); /* async, maybe they will reply...maybe.. */ - if (!result) + if (!reply) return; } else { _E("no methods"); } - g_dbus_method_invocation_return_value(invocation, result); + g_dbus_method_invocation_return_value(invocation, reply); } static GDBusInterfaceVTable path_vtable = {_method_call_handler}; @@ -1356,7 +1356,7 @@ int dbus_handle_register_dbus_object_all(dbus_handle_h handle) dcl_dbus_handle(); dbus_object_handle_s *oh = NULL; dbus_interface_s *ih = NULL; - int ret = 0; + int ret_dbus = 0; if (!dh) { dh = _dbus_handle_get_default_connection(); @@ -1405,9 +1405,9 @@ int dbus_handle_register_dbus_object_all(dbus_handle_h handle) if (ih && ih->list_methods) _D("method list len %d", g_list_length(ih->list_methods));*/ - ret = _dbus_handle_register_dbus_object(dh, oh->path, ih); - if (ret <= 0) - _E("failed to register dbus object%d", ret); + ret_dbus = _dbus_handle_register_dbus_object(dh, oh->path, ih); + if (ret_dbus <= 0) + _E("failed to register dbus object%d", ret_dbus); } } @@ -1753,11 +1753,11 @@ int dbus_handle_method_sync_with_reply_var(const char *dest, const char *iface, const char *method, GVariant *param, - GVariant **reply) + GVariant **out_reply) { GError *err = NULL; - GVariant *ret = NULL; - int err_val = 0; + GVariant *reply = NULL; + int ret = 0; dbus_handle_s *dh = NULL; if (!dest || !path || !iface || !method) { @@ -1775,41 +1775,41 @@ int dbus_handle_method_sync_with_reply_var(const char *dest, return -ECOMM; } - ret = g_dbus_connection_call_sync(dh->conn, + reply = g_dbus_connection_call_sync(dh->conn, dest, path, iface, method, param, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err); - if (!ret || err) { + if (!reply || err) { if (err) { _E("failed to g_dbus_connection_call_sync:%s", err->message); if (g_error_matches(err, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED)) - err_val = -EPERM; + ret = -EPERM; else - err_val = -ECOMM; + ret = -ECOMM; g_error_free(err); - return err_val; + return ret; } else { _E("failed to g_dbus_connection_call_sync"); } return -ECOMM; } - if(reply) - *reply = ret; + if(out_reply) + *out_reply = reply; else - g_variant_unref(ret); + g_variant_unref(reply); - return err_val; + return ret; } int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char *path, - const char *iface, const char *method, GVariant *param, GVariant **reply, int timeout) + const char *iface, const char *method, GVariant *param, GVariant **out_reply, int timeout) { GError *err = NULL; - GVariant *ret; + GVariant *reply; dbus_handle_s *dh = NULL; if (!dest || !path || !iface || !method) { @@ -1827,14 +1827,14 @@ int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char return -ECOMM; } - ret = g_dbus_connection_call_sync(dh->conn, + reply = g_dbus_connection_call_sync(dh->conn, dest, path, iface, method, param, NULL, G_DBUS_CALL_FLAGS_NONE, timeout, NULL, &err); - if (!ret || err) { + if (!reply || err) { if (err) { _E("failed to g_dbus_connection_call_sync:%s", err->message); g_error_free(err); @@ -1844,10 +1844,10 @@ int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char return -ECOMM; } - if(reply) - *reply = ret; + if(out_reply) + *out_reply = reply; else - g_variant_unref(ret); + g_variant_unref(reply); return 0; } @@ -1860,7 +1860,7 @@ int dbus_handle_method_sync_pairs(const char *dest, va_list args) { GError *err = NULL; - GVariant * reply = NULL; + GVariant *reply = NULL; char *key, *value; int ret = 0; GVariant *var; @@ -1984,14 +1984,14 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, const char *iface, const char *method, GVariant *param, - GVariant **reply, + GVariant **out_reply, int *in_fdlist, int in_size, int **out_fdlist, int *out_size) { - int err_val = 0; - GVariant *ret = NULL; + int ret = 0; + GVariant *reply = NULL; GError *err = NULL; dbus_handle_s *dh = NULL; GUnixFDList *g_infdlist = NULL; @@ -2017,7 +2017,7 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, g_infdlist = g_unix_fd_list_new_from_array(in_fdlist, in_size); if (!g_infdlist) { _E("failed to g_unix_fd_list_new_from_array\n"); - err_val = -EAGAIN; + ret = -EAGAIN; goto out; } //g_infdlist = g_unix_fd_list_new(); @@ -2025,14 +2025,14 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, } /* send message */ - ret = g_dbus_connection_call_with_unix_fd_list_sync(dh->conn, + reply = g_dbus_connection_call_with_unix_fd_list_sync(dh->conn, dest, path, iface, method, param, NULL, G_DBUS_CALL_FLAGS_NONE, -1, g_infdlist, &g_outfdlist, NULL, &err); - if (!ret || err) { + if (!reply || err) { if (err) { _E("failed to g_dbus_connection_call_with_unix_fd_list_sync:%s", err->message); g_error_free(err); @@ -2043,14 +2043,14 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, if (g_infdlist) g_object_unref(g_infdlist); } - err_val = -EAGAIN; + ret = -EAGAIN; goto out; } - if(reply) - *reply = ret; + if(out_reply) + *out_reply = reply; else - g_variant_unref(ret); + g_variant_unref(reply); /* copy fds to out array */ if (g_outfdlist) { @@ -2062,7 +2062,7 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, out: if (g_outfdlist) g_object_unref(g_outfdlist); - return err_val; + return ret; } int dbus_handle_method_sync_var(const char *dest, @@ -2296,7 +2296,7 @@ err: int dbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) { GError *err = NULL; - GVariant *vret = NULL; + GVariant *reply = NULL; pid_t pid = 0; if (!conn) { @@ -2308,21 +2308,21 @@ int dbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) return -1; } - vret = g_dbus_connection_call_sync(conn, + reply = g_dbus_connection_call_sync(conn, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetConnectionUnixProcessID", g_variant_new("(s)", sender), NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err); - if (!vret || err) { + if (!reply || err) { _E("failed to g_dbus_connection_call_sync:%s", err->message); g_error_free(err); return -1; } - g_variant_get(vret, "(u)", &pid); - g_variant_unref(vret); + g_variant_get(reply, "(u)", &pid); + g_variant_unref(reply); return pid; } @@ -2350,7 +2350,7 @@ int dbus_handle_get_sender_pid(dbus_handle_h handle, const char * sender) int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, GDBusCredentials *creds) { dcl_dbus_handle(); - GVariant *vret = NULL; + GVariant *reply = NULL; GError *err = NULL; GVariantIter *iter = NULL; char * item; @@ -2363,7 +2363,7 @@ int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, G return -1; } } - vret = g_dbus_connection_call_sync(dh->conn, + reply = g_dbus_connection_call_sync(dh->conn, DBUS_BUS_NAME, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, @@ -2374,12 +2374,12 @@ int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, G DBUS_REPLY_TIMEOUT, NULL, &err); - if (!vret || err) { + if (!reply || err) { _E("failed to g_dbus_connection_call_sync:%s", err->message); return -1; } - g_variant_get(vret, "(a{sv})", &iter); + g_variant_get(reply, "(a{sv})", &iter); while (g_variant_iter_loop(iter, "{sv}", &item, &sub)) { if (!g_strcmp0(item, "UnixUserID")) { @@ -2396,8 +2396,8 @@ int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, G if (iter) g_variant_iter_free(iter); - if (vret) - g_variant_unref(vret); + if (reply) + g_variant_unref(reply); return 0; } @@ -2451,7 +2451,7 @@ void dbus_handle_unwatch_name(guint id) int _get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size) { - int fd, ret; + int fd, ret_file; char buf[PATH_MAX + 1]; char *filename; @@ -2462,9 +2462,9 @@ int _get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size) return -1; } - ret = read(fd, buf, PATH_MAX); + ret_file = read(fd, buf, PATH_MAX); close(fd); - if (ret < 0) + if (ret_file < 0) return -1; buf[PATH_MAX] = '\0'; @@ -2490,7 +2490,7 @@ char **dbus_handle_get_owner_list(dbus_handle_h handle, const char *bus_name) { dcl_dbus_handle(); GError *err = NULL; - GVariant *vret = NULL; + GVariant *reply = NULL; GVariantIter *iter = NULL; gchar **strv = NULL; gchar *str = NULL; @@ -2509,7 +2509,7 @@ char **dbus_handle_get_owner_list(dbus_handle_h handle, const char *bus_name) } } - vret = g_dbus_connection_call_sync(dh->conn, + reply = g_dbus_connection_call_sync(dh->conn, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", @@ -2520,13 +2520,13 @@ char **dbus_handle_get_owner_list(dbus_handle_h handle, const char *bus_name) DBUS_REPLY_TIMEOUT, NULL, &err); - if (!vret || err) { + if (!reply || err) { _E("failed to g_dbus_connection_call_sync:%s", err->message); g_error_free(err); return NULL; } - g_variant_get(vret, "(as)", &iter); + g_variant_get(reply, "(as)", &iter); strv = g_new(gchar *, g_variant_iter_n_children(iter) + 1); i = 0; @@ -2535,7 +2535,7 @@ char **dbus_handle_get_owner_list(dbus_handle_h handle, const char *bus_name) strv[i] = NULL; g_variant_iter_free(iter); - g_variant_unref(vret); + g_variant_unref(reply); return strv; } @@ -2575,7 +2575,7 @@ void dbus_handle_check_owner_name(dbus_handle_h handle, const char *owner_name) int check_systemd_active(void) { int ret = FALSE; - GVariant *msg = NULL; + GVariant *reply = NULL; GVariant *var = NULL; char *state; @@ -2586,13 +2586,13 @@ int check_systemd_active(void) "org.freedesktop.DBus.Properties", "Get", g_variant_new("(ss)", "org.freedesktop.systemd1.Unit", "ActiveState"), - &msg); + &reply); if (ret < 0) return ret; - if (!msg) + if (!reply) return -EBADMSG; - if (!g_variant_get_safe(msg, "(v)", &var)) { + if (!g_variant_get_safe(reply, "(v)", &var)) { _E("reply is not variant type"); ret = -EBADMSG; goto out; @@ -2610,8 +2610,8 @@ int check_systemd_active(void) out: if (var) g_variant_unref(var); - if (msg) - g_variant_unref(msg); + if (reply) + g_variant_unref(reply); return ret; } diff --git a/src/libgdbus/libgdbus.h b/src/libgdbus/libgdbus.h index 91fb1f3..0e26bb8 100644 --- a/src/libgdbus/libgdbus.h +++ b/src/libgdbus/libgdbus.h @@ -127,14 +127,14 @@ int dbus_handle_method_sync_with_reply_var (const char *dest, const char *iface, const char *method, GVariant *param, - GVariant **reply); + GVariant **out_reply); int dbus_handle_method_sync_with_reply_var_timeout (const char *dest, const char *path, const char *iface, const char *method, GVariant *param, - GVariant **reply, + GVariant **out_reply, int timeout); int dbus_handle_method_sync_pairs (const char *dest, @@ -169,7 +169,7 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, const char *iface, const char *method, GVariant *param, - GVariant **reply, + GVariant **out_reply, int *in_fdlist, int in_size, int **out_fdlist, diff --git a/src/libsystemd/libsystemd.c b/src/libsystemd/libsystemd.c index a463118..a045616 100644 --- a/src/libsystemd/libsystemd.c +++ b/src/libsystemd/libsystemd.c @@ -411,18 +411,18 @@ GVariant *systemd_get_manager_property(const char *property) { GVariant *reply = NULL; GVariant *val = NULL; - int ret = 0; + int ret_dbus = 0; if (!property) return NULL; - ret = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret_dbus = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, SYSTEMD_DBUS_PATH, DBUS_IFACE_DBUS_PROPERTIES, "Get", g_variant_new("(ss)", SYSTEMD_DBUS_MANAGER_IFACE, property), &reply); - if (!reply || !g_variant_get_safe(reply, "(v)", &val) || ret < 0) + if (ret_dbus < 0 || !g_variant_get_safe(reply, "(v)", &val)) _E("Failed to get variant"); if (reply) g_variant_unref(reply); @@ -435,21 +435,21 @@ GVariant *systemd_get_unit_property(const char *unit, const char *property) char *escaped; GVariant *reply = NULL; GVariant *val = NULL; - int ret = 0; + int ret_dbus = 0; if (!unit || !property) return NULL; escaped = systemd_get_unit_dbus_path(unit); - ret = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret_dbus = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, escaped, DBUS_IFACE_DBUS_PROPERTIES, "Get", g_variant_new("(ss)", SYSTEMD_DBUS_UNIT_IFACE, property), &reply); - if (!reply || !g_variant_get_safe(reply, "(v)", &val) || ret < 0) + if (ret_dbus < 0 || !g_variant_get_safe(reply, "(v)", &val)) _E("Failed to get variant"); if (reply) g_variant_unref(reply); @@ -463,20 +463,20 @@ GVariant *systemd_get_service_property(const char *unit, const char *property) char *escaped; GVariant *reply = NULL; GVariant *val = NULL; - int ret = 0; + int ret_dbus = 0; if (!unit || !property) return NULL; escaped = systemd_get_unit_dbus_path(unit); - ret = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret_dbus = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, escaped, DBUS_IFACE_DBUS_PROPERTIES, "Get", g_variant_new("(ss)", SYSTEMD_DBUS_SERVICE_IFACE, property), &reply); - if (!reply || !g_variant_get_safe(reply, "(v)", &val) || ret < 0) + if (ret_dbus < 0 || !g_variant_get_safe(reply, "(v)", &val)) _E("Failed to get variant"); if (reply) g_variant_unref(reply); -- 2.7.4 From e2a628e2e58cd35758846667e4d8ae4379c5dcd3 Mon Sep 17 00:00:00 2001 From: "taemin.yeom" Date: Fri, 19 Mar 2021 14:14:10 +0900 Subject: [PATCH 05/16] Change dbus function name Change-Id: I67b2b5d555db01ecbd32078380662e90f1dc3b6f Signed-off-by: taemin.yeom --- src/libgdbus/libgdbus.c | 692 +++++++++++++++++++++++++++++--------------- src/libgdbus/libgdbus.h | 155 +++++----- src/libsystemd/libsystemd.c | 20 +- 3 files changed, 553 insertions(+), 314 deletions(-) diff --git a/src/libgdbus/libgdbus.c b/src/libgdbus/libgdbus.c index 3679337..9295582 100644 --- a/src/libgdbus/libgdbus.c +++ b/src/libgdbus/libgdbus.c @@ -32,7 +32,7 @@ static GBusType g_default_bus_type = G_BUS_TYPE_SYSTEM; pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; -void dbus_handle_set_default_bus_type(GBusType bus_type) +void gdbus_set_default_bus_type(GBusType bus_type) { if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) return ; @@ -42,7 +42,7 @@ void dbus_handle_set_default_bus_type(GBusType bus_type) pthread_mutex_unlock(&g_mutex); } -GBusType dbus_handle_get_default_bus_type(void) +GBusType gdbus_get_default_bus_type(void) { GBusType type; @@ -88,33 +88,33 @@ typedef struct { /* global shared bus : system, session */ static dbus_handle_s g_dh[2]; -static dbus_handle_s *_dbus_handle_get_connection(GBusType bus_type); +static dbus_handle_s *_gdbus_get_connection(GBusType bus_type); -dbus_handle_s * _dbus_handle_get_default_connection(void) +dbus_handle_s * _gdbus_get_default_connection(void) { - return _dbus_handle_get_connection(dbus_handle_get_default_bus_type()); + return _gdbus_get_connection(gdbus_get_default_bus_type()); } -#define dbus_handle_lock(handle) do {\ +#define gdbus_lock(handle) do {\ assert(handle);\ pthread_mutex_lock(&((handle)->mutex));\ } while (0); -#define dbus_handle_unlock(handle) do {\ +#define gdbus_unlock(handle) do {\ assert(handle);\ pthread_mutex_unlock(&(handle)->mutex);\ } while (0); -#define dcl_dbus_handle() dbus_handle_s *dh = (dbus_handle_s *)handle; -#define dcl_dbus_handle_null_check() dbus_handle_s *dh = (dbus_handle_s *)handle;\ +#define dcl_gdbus() dbus_handle_s *dh = (dbus_handle_s *)handle; +#define dcl_gdbus_null_check() dbus_handle_s *dh = (dbus_handle_s *)handle;\ if (!dh) {\ _E("dbus handle is null\n");\ return 0;\ } -dbus_object_handle_s * _dbus_handle_lookup_object(GList *list_obj, const char *obj_path); -dbus_interface_s * _dbus_handle_lookup_interface(GList *list_iface, const char *iface_name); -dbus_method_s * _dbus_handle_lookup_method(GList *list_methods, const char *method_name); +dbus_object_handle_s * _gdbus_lookup_object(GList *list_obj, const char *obj_path); +dbus_interface_s * _gdbus_lookup_interface(GList *list_iface, const char *iface_name); +dbus_method_s * _gdbus_lookup_method(GList *list_methods, const char *method_name); dbus_interface_s *_iface_u_to_s(const dbus_interface_u *iface_u) { @@ -196,7 +196,7 @@ static GDBusConnection * _get_bus_private(GBusType bus_type) } /* ref cout is 1 */ -static dbus_handle_s *_dbus_handle_get_connection(GBusType bus_type) +static dbus_handle_s *_gdbus_get_connection(GBusType bus_type) { int ibus = bus_type - 1; dbus_handle_s *dh = NULL; @@ -207,25 +207,25 @@ static dbus_handle_s *_dbus_handle_get_connection(GBusType bus_type) } dh = &g_dh[ibus]; - dbus_handle_lock(dh); + gdbus_lock(dh); if (!dh->conn) { dh->conn = _get_bus(bus_type); if (!dh->conn) { - dbus_handle_unlock(dh); + gdbus_unlock(dh); return NULL; } dh->priv = FALSE; dh->bus_type = bus_type; } - dbus_handle_unlock(dh); + gdbus_unlock(dh); return dh; } /* ref cout is 1 */ -static dbus_handle_s *_dbus_handle_get_connection_private(GBusType bus_type) +static dbus_handle_s *_gdbus_get_connection_private(GBusType bus_type) { dbus_handle_s * dh; @@ -240,7 +240,7 @@ static dbus_handle_s *_dbus_handle_get_connection_private(GBusType bus_type) return NULL; } - dbus_handle_lock(dh); + gdbus_lock(dh); if (!dh->conn) { dh->conn = _get_bus_private(bus_type); @@ -249,18 +249,18 @@ static dbus_handle_s *_dbus_handle_get_connection_private(GBusType bus_type) goto err; } - dbus_handle_unlock(dh); + gdbus_unlock(dh); return dh; err: if (dh) { - dbus_handle_unlock(dh); + gdbus_unlock(dh); free(dh); } return NULL; } -dbus_handle_h dbus_handle_get_connection(GBusType bus_type, gboolean priv) +dbus_handle_h gdbus_get_connection(GBusType bus_type, gboolean priv) { dbus_handle_s *dh = NULL; int i; @@ -273,10 +273,10 @@ dbus_handle_h dbus_handle_get_connection(GBusType bus_type, gboolean priv) for (i = 0 ; i < 3; ++i) { /* private */ if (priv) - dh = _dbus_handle_get_connection_private(bus_type); + dh = _gdbus_get_connection_private(bus_type); /* shared */ else - dh = _dbus_handle_get_connection(bus_type); + dh = _gdbus_get_connection(bus_type); if (dh) break; @@ -286,7 +286,7 @@ dbus_handle_h dbus_handle_get_connection(GBusType bus_type, gboolean priv) return dh; } -static void _dbus_handle_add_bus_name(dbus_handle_s *handle, const char *name, guint id) +static void _gdbus_add_bus_name(dbus_handle_s *handle, const char *name, guint id) { dbus_name *dn = NULL; int locked = 0; @@ -313,7 +313,7 @@ static void _dbus_handle_add_bus_name(dbus_handle_s *handle, const char *name, g // todo : delete lock ? if (locked != EBUSY) - dbus_handle_unlock(handle); + gdbus_unlock(handle); } static gint _compare_dbus_name(gconstpointer a, gconstpointer b) @@ -324,7 +324,7 @@ static gint _compare_dbus_name(gconstpointer a, gconstpointer b) return strcmp(bus_name, (const char *)b); } -dbus_name * _dbus_handle_lookup_dbus_name(GList *list_name, const char *bus_name) +dbus_name * _gdbus_lookup_dbus_name(GList *list_name, const char *bus_name) { if (!list_name || !bus_name) return NULL; @@ -339,9 +339,9 @@ dbus_name * _dbus_handle_lookup_dbus_name(GList *list_name, const char *bus_name #define dh_to_ds(x) ((dbus_handle_s*)x) /* remove dbus_name from dbus handle */ -static void _dbus_handle_remove_bus_name(dbus_handle_s *handle, const char *bus_name) +static void _gdbus_remove_bus_name(dbus_handle_s *handle, const char *bus_name) { - dcl_dbus_handle(); + dcl_gdbus(); dbus_name *dn = NULL; if (!bus_name) { @@ -349,15 +349,15 @@ static void _dbus_handle_remove_bus_name(dbus_handle_s *handle, const char *bus_ return ; } if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return ; } } - dbus_handle_lock(dh); - dn = _dbus_handle_lookup_dbus_name(dh->list_names, bus_name); + gdbus_lock(dh); + dn = _gdbus_lookup_dbus_name(dh->list_names, bus_name); if (!dn) { _E("failed to find dbus name %s", bus_name); goto out; @@ -365,7 +365,7 @@ static void _dbus_handle_remove_bus_name(dbus_handle_s *handle, const char *bus_ dh->list_names = g_list_remove(dh->list_names, dn); free(dn); out: - dbus_handle_unlock(dh); + gdbus_unlock(dh); } //extern void booting_done(void); @@ -395,15 +395,15 @@ static void _name_lost(GDBusConnection *connection, const gchar *name, gpointer _E("%s:%d:dbus handle is null\n", __func__, __LINE__); return ; } - _dbus_handle_remove_bus_name(dh, name); + _gdbus_remove_bus_name(dh, name); } -int dbus_handle_request_bus_name(dbus_handle_h handle, +int gdbus_request_name(dbus_handle_h handle, const char *bus_name, GBusNameAcquiredCallback acquired_handler, GBusNameLostCallback lost_handler) { - dcl_dbus_handle(); + dcl_gdbus(); int id = -1; GList *item = NULL; @@ -414,14 +414,14 @@ int dbus_handle_request_bus_name(dbus_handle_h handle, /* get shared connection */ if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } - dbus_handle_lock(dh); + gdbus_lock(dh); if (!dh->conn) { _E("failed to register name: connection is null\n"); goto out; @@ -447,17 +447,17 @@ int dbus_handle_request_bus_name(dbus_handle_h handle, goto out; } - _dbus_handle_add_bus_name(dh, bus_name, id); + _gdbus_add_bus_name(dh, bus_name, id); out: - dbus_handle_unlock(dh); + gdbus_unlock(dh); return id; } /* !! _name_lost handler callback is disabled by g_bus_unown_name : ubuntu */ -int dbus_handle_release_bus_name(dbus_handle_h handle, const char *bus_name) +int gdbus_release_name(dbus_handle_h handle, const char *bus_name) { - dcl_dbus_handle(); + dcl_gdbus(); dbus_name *dn = NULL; if (!bus_name) { @@ -466,14 +466,14 @@ int dbus_handle_release_bus_name(dbus_handle_h handle, const char *bus_name) } if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } - dn = _dbus_handle_lookup_dbus_name(dh->list_names, bus_name); + dn = _gdbus_lookup_dbus_name(dh->list_names, bus_name); if (!dn) { _E("failed to find bus_name %s on dbus handle", bus_name); return -1; @@ -483,25 +483,89 @@ int dbus_handle_release_bus_name(dbus_handle_h handle, const char *bus_name) /* _name_lost handler is disabled by g_bus_unown_name : ubuntu */ g_bus_unown_name(dn->id); - dbus_handle_lock(dh); + gdbus_lock(dh); dh->list_names = g_list_remove(dh->list_names, dn); free(dn); - dbus_handle_unlock(dh); + gdbus_unlock(dh); return 0; } -int dbus_handle_free_connection(dbus_handle_h handle) +void gdbus_unwatch_name(guint id) { - dcl_dbus_handle(); + if (id == 0) { + _E("wrong id %d", id); + return; + } + g_bus_unwatch_name(id); +} + +// g_strfreev(strv) +char **gdbus_get_owner_list(dbus_handle_h handle, const char *bus_name) +{ + dcl_gdbus(); + GError *err = NULL; + GVariant *reply = NULL; + GVariantIter *iter = NULL; + gchar **strv = NULL; + gchar *str = NULL; + int i = 0; + + if (!bus_name) { + _E("wrong parameter bus_name is null"); + return NULL; + } + + if (!dh) { + dh = _gdbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); + return NULL; + } + } + + reply = g_dbus_connection_call_sync(dh->conn, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "ListQueuedOwners", + g_variant_new("(s)", bus_name), + NULL, + G_DBUS_CALL_FLAGS_NONE, + DBUS_REPLY_TIMEOUT, + NULL, + &err); + if (!reply || err) { + _E("failed to g_dbus_connection_call_sync:%s", err->message); + g_error_free(err); + return NULL; + } + + g_variant_get(reply, "(as)", &iter); + strv = g_new(gchar *, g_variant_iter_n_children(iter) + 1); + + i = 0; + while (g_variant_iter_loop(iter, "s", &str)) + strv[i++] = g_strdup(str); + strv[i] = NULL; + + g_variant_iter_free(iter); + g_variant_unref(reply); + + return strv; +} + +int gdbus_free_connection(dbus_handle_h handle) +{ + dcl_gdbus(); dbus_handle_s *pdh = NULL; GError *err = NULL; GList *item = NULL; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -514,7 +578,7 @@ int dbus_handle_free_connection(dbus_handle_h handle) pdh = dh; /* disable dbus handler */ - dbus_handle_lock(dh); + gdbus_lock(dh); if (!pdh->conn) { _E("conn is null"); free(pdh); @@ -591,7 +655,7 @@ int dbus_handle_free_connection(dbus_handle_h handle) g_object_unref(pdh->conn); - dbus_handle_unlock(dh); + gdbus_unlock(dh); if (dh->priv) free(dh); @@ -824,7 +888,7 @@ static gint _compare_dbus_method(gconstpointer a, gconstpointer b) return strcmp(pa->member, (const char*)b); } -dbus_object_handle_s * _dbus_handle_lookup_object(GList *list_obj, const char *obj_path) +dbus_object_handle_s * _gdbus_lookup_object(GList *list_obj, const char *obj_path) { if (!list_obj || !obj_path) return NULL; @@ -836,7 +900,7 @@ dbus_object_handle_s * _dbus_handle_lookup_object(GList *list_obj, const char *o return (dbus_object_handle_s *)item->data; } -dbus_interface_s * _dbus_handle_lookup_interface(GList *list_iface, const char *iface_name) +dbus_interface_s * _gdbus_lookup_interface(GList *list_iface, const char *iface_name) { if (!list_iface || !iface_name) return NULL; @@ -848,7 +912,7 @@ dbus_interface_s * _dbus_handle_lookup_interface(GList *list_iface, const char * return (dbus_interface_s *)item->data; } -dbus_interface_s * _dbus_handle_lookup_interface_by_id(GList *list_iface, guint id) +dbus_interface_s * _gdbus_lookup_interface_by_id(GList *list_iface, guint id) { if (!list_iface || !id) return NULL; @@ -860,7 +924,7 @@ dbus_interface_s * _dbus_handle_lookup_interface_by_id(GList *list_iface, guint return (dbus_interface_s *)item->data; } -dbus_method_s * _dbus_handle_lookup_method(GList *list_methods, const char *method_name) +dbus_method_s * _gdbus_lookup_method(GList *list_methods, const char *method_name) { if (!list_methods || !method_name) return NULL; @@ -908,7 +972,7 @@ static void _free_func_object(gpointer data) } } -static int _dbus_handle_attach_object(dbus_handle_s *dh, const char *obj_path, dbus_interface_s *iface) +static int _gdbus_attach_object(dbus_handle_s *dh, const char *obj_path, dbus_interface_s *iface) { dbus_object_handle_s *oh = NULL; @@ -919,7 +983,7 @@ static int _dbus_handle_attach_object(dbus_handle_s *dh, const char *obj_path, d /* find object handle */ if (dh->list_object) - oh = _dbus_handle_lookup_object(dh->list_object, obj_path); + oh = _gdbus_lookup_object(dh->list_object, obj_path); if (!oh) { oh = (dbus_object_handle_s*)calloc(1, sizeof(dbus_object_handle_s)); @@ -1001,7 +1065,7 @@ static void _method_call_handler(GDBusConnection *conn, GVariant *reply = NULL; /* todo: ghash ? */ - methods = _dbus_handle_lookup_method(iface_s->list_methods, name); + methods = _gdbus_lookup_method(iface_s->list_methods, name); if (methods) { reply = methods->func(conn, sender, path, iface, name, param, invocation, get_dh_from_oh(iface_s->oh)); @@ -1020,11 +1084,11 @@ static GDBusInterfaceVTable path_vtable = {_method_call_handler}; /* before register object, attach object into dbus handle -_dbus_handle_attach_object() +_gdbus_attach_object() */ -static int _dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, dbus_interface_s *iface) +static int _gdbus_register_object(dbus_handle_h handle, const char *obj_path, dbus_interface_s *iface) { - dcl_dbus_handle(); + dcl_gdbus(); int ret = 0; char *buf = NULL; GError *err = NULL; @@ -1036,9 +1100,9 @@ static int _dbus_handle_register_dbus_object(dbus_handle_h handle, const char *o return -1; } if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -1123,13 +1187,13 @@ register same interface at once if interface is constructed by multiple methods, also it is not possible to make methods struct at once, -use dbus_handle_add_dbus_object(), dbus_handle_register_dbus_object_all(). +use gdbus_add_object(), gdbus_register_object_all(). return reg_id */ -int dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface_u) +int gdbus_register_object(dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface_u) { - dcl_dbus_handle(); + dcl_gdbus(); int ret = 0; dbus_interface_s *iface = NULL; @@ -1138,9 +1202,9 @@ int dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, return -1; } if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -1151,9 +1215,9 @@ int dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, /* check registered interface */ if (dh->list_object) { - dbus_object_handle_s *oh = _dbus_handle_lookup_object(dh->list_object, obj_path); + dbus_object_handle_s *oh = _gdbus_lookup_object(dh->list_object, obj_path); if (oh) { - dbus_interface_s *ih = _dbus_handle_lookup_interface(oh->list_ifaces, iface_u->name); + dbus_interface_s *ih = _gdbus_lookup_interface(oh->list_ifaces, iface_u->name); if (ih) { _E("path %s, interface %s already registered", obj_path, iface_u->name); return -1; @@ -1168,13 +1232,13 @@ int dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, } /* attach interface before register object */ - ret = _dbus_handle_attach_object(dh, obj_path, iface); + ret = _gdbus_attach_object(dh, obj_path, iface); if (ret < 0) { _E("failed to attach object"); goto err; } - ret = _dbus_handle_register_dbus_object(dh, obj_path, iface); + ret = _gdbus_register_object(dh, obj_path, iface); if (ret <= 0) { _E("failed to register dbus object%d", ret); goto err; @@ -1183,9 +1247,68 @@ err: return ret; } +int gdbus_unregister_object(dbus_handle_h handle, const char *obj_path) +{ + dcl_gdbus(); + dbus_object_handle_s *oh = NULL; + int ret = 0; + + if (!obj_path) + return -1; + + if (!dh) { + dh = _gdbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); + return -1; + } + } + if (!dh->list_object) { + _E("list_object is empty"); + return 0; + } + + oh = _gdbus_lookup_object(dh->list_object, obj_path); + if (!oh) { + _E("no object with name %s", obj_path); + return -1; + } + + /* unregister every interface of object*/ + for (GList *item = g_list_first(oh->list_ifaces); item != NULL; item = g_list_next(item)) { + dbus_interface_s *ih = item->data; + if (!ih) { + _E("this is error"); + assert(0); + } + + /* remove ih from list_ifaces */ + if (!ih->reg_id) { + item = g_list_previous(item); + + /* remove and free link */ + oh->list_ifaces = g_list_remove(oh->list_ifaces, ih); + + /* free list_methods */ + g_list_free(ih->list_methods); + + /* free data */ + free(ih); + continue; + } + + /* unregister object by id */ + ret = g_dbus_connection_unregister_object(dh->conn, ih->reg_id); + if (!ret) + _E("failed to unregister object %s, interface %s, regid %d", oh->path, ih->name, ih->reg_id); + } + + return 0; +} + int dbus_handle_unregister_dbus_object(dbus_handle_h handle, const char *obj_path) { - dcl_dbus_handle(); + dcl_gdbus(); dbus_object_handle_s *oh = NULL; int ret = 0; @@ -1193,9 +1316,9 @@ int dbus_handle_unregister_dbus_object(dbus_handle_h handle, const char *obj_pat return -1; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -1204,7 +1327,7 @@ int dbus_handle_unregister_dbus_object(dbus_handle_h handle, const char *obj_pat return 0; } - oh = _dbus_handle_lookup_object(dh->list_object, obj_path); + oh = _gdbus_lookup_object(dh->list_object, obj_path); if (!oh) { _E("no object with name %s", obj_path); return -1; @@ -1244,13 +1367,116 @@ int dbus_handle_unregister_dbus_object(dbus_handle_h handle, const char *obj_pat /* add object temporarily. -dbus_handle_register_dbus_object_all register every objects on connection. +gdbus_register_object_all register every objects on connection. return registered method count */ +int gdbus_add_object(dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface_u) +{ + dcl_gdbus(); + dbus_object_handle_s *oh = NULL; + dbus_interface_s *ih = NULL; + int cnt; + + if (!obj_path || !iface_u) { + _E("wrong parameter path %s, iface_u %p\n", obj_path, iface_u); + return -1; + } + if (iface_u && (!iface_u->name || !iface_u->methods)) { + _E("wrong parameter path %s, iface_u %p\n", obj_path, iface_u); + return -1; + } + + cnt = iface_u->nr_methods; + + if (!dh) { + dh = _gdbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); + return -1; + } + } + + if (!dh->conn) { + _E("failed to register method. connection is null\n"); + return -1; + } + + /* if there are no object list, just add */ + if (!dh->list_object) { + if (_gdbus_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { + _E("failed to attach object"); + return -1; + } + goto out; + } + + oh = _gdbus_lookup_object(dh->list_object, obj_path); + /* if there are no matched object, just add */ + if (!oh) { + if (_gdbus_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { + _E("failed to attach object"); + return -1; + } + goto out; + } + + /* this is an error, interface must have one or more item ? */ + if (!oh->list_ifaces) { + _E("error. list_ifaces is null\n"); + assert(0); + goto out; + } + + ih = _gdbus_lookup_interface(oh->list_ifaces, iface_u->name); + /* if there are no matched interface, just add */ + if (!ih) { + if (_gdbus_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { + _E("failed to attach object"); + return -1; + } + goto out; + } + + /* todo: + 1. unregister interface + 2. update interface and methods + 3. register interface + */ + if (ih->reg_id) { + _E("interface already registered, ignore new interface"); + return -1; + } + + /* attach new methods */ + cnt = 0; + for (int i = 0; i < iface_u->nr_methods; ++i) { + GList *item = g_list_find_custom(g_list_first(ih->list_methods), iface_u->methods[i].member, _compare_dbus_method); + if (!item) { + //_D("attached %s", iface_u->methods[i].member); + ih->list_methods = g_list_prepend(ih->list_methods, (void*)(iface_u->methods + i)); + ++cnt; + } + } + + if (cnt) + ih->modified = TRUE; + +out: + /*todo: delete debugging log */ + //if (dh && dh->list_object) + // _D("obj list len %d", g_list_length(dh->list_object)); + //if (oh && oh->list_ifaces) + // _D("iface list len %d", g_list_length(oh->list_ifaces)); + //if (ih && ih->list_methods) + // _D("method list len %d", g_list_length(ih->list_methods)); + + return cnt; +} + int dbus_handle_add_dbus_object(dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface_u) { - dcl_dbus_handle(); + dcl_gdbus(); dbus_object_handle_s *oh = NULL; dbus_interface_s *ih = NULL; int cnt; @@ -1267,9 +1493,9 @@ int dbus_handle_add_dbus_object(dbus_handle_h handle, const char *obj_path, cons cnt = iface_u->nr_methods; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -1281,17 +1507,17 @@ int dbus_handle_add_dbus_object(dbus_handle_h handle, const char *obj_path, cons /* if there are no object list, just add */ if (!dh->list_object) { - if (_dbus_handle_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { + if (_gdbus_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { _E("failed to attach object"); return -1; } goto out; } - oh = _dbus_handle_lookup_object(dh->list_object, obj_path); + oh = _gdbus_lookup_object(dh->list_object, obj_path); /* if there are no matched object, just add */ if (!oh) { - if (_dbus_handle_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { + if (_gdbus_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { _E("failed to attach object"); return -1; } @@ -1305,10 +1531,10 @@ int dbus_handle_add_dbus_object(dbus_handle_h handle, const char *obj_path, cons goto out; } - ih = _dbus_handle_lookup_interface(oh->list_ifaces, iface_u->name); + ih = _gdbus_lookup_interface(oh->list_ifaces, iface_u->name); /* if there are no matched interface, just add */ if (!ih) { - if (_dbus_handle_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { + if (_gdbus_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) { _E("failed to attach object"); return -1; } @@ -1351,17 +1577,85 @@ out: return cnt; } +int gdbus_register_object_all(dbus_handle_h handle) +{ + dcl_gdbus(); + dbus_object_handle_s *oh = NULL; + dbus_interface_s *ih = NULL; + int ret_dbus = 0; + + if (!dh) { + dh = _gdbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); + return -1; + } + } + if (!dh->conn) { + _E("connection is null\n"); + return -1; + } + + if (!dh->list_object) { + _E("obj list is empty"); + return -1; + } + + /*if (dh && dh->list_object) + _D("obj list len %d", g_list_length(dh->list_object));*/ + + for (GList *item = g_list_first(dh->list_object); item != NULL; item = g_list_next(item)) { + oh = (dbus_object_handle_s *)item->data; + + if (!oh) { + _E("something wrong"); + assert(0); + } + if (!oh->list_ifaces) { + _E("path %s: list_ifaces are null", oh->path); + goto err; + } + + //_D("iface list len %d", g_list_length(oh->list_ifaces)); + + for (GList *li = g_list_first(oh->list_ifaces); li != NULL; li = g_list_next(li)) { + ih = (dbus_interface_s *)li->data; + + /* if there are no modification, goto next */ + if (!ih->modified) + continue; + + /* todo: if already registered interface, unregister first */ + + /*_E("interface %s:", ih->name); + if (ih && ih->list_methods) + _D("method list len %d", g_list_length(ih->list_methods));*/ + + ret_dbus = _gdbus_register_object(dh, oh->path, ih); + if (ret_dbus <= 0) + _E("failed to register dbus object%d", ret_dbus); + + } + } + return 0; +err: + + // todo: delete all updates + + return -1; +} + int dbus_handle_register_dbus_object_all(dbus_handle_h handle) { - dcl_dbus_handle(); + dcl_gdbus(); dbus_object_handle_s *oh = NULL; dbus_interface_s *ih = NULL; int ret_dbus = 0; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -1405,7 +1699,7 @@ int dbus_handle_register_dbus_object_all(dbus_handle_h handle) if (ih && ih->list_methods) _D("method list len %d", g_list_length(ih->list_methods));*/ - ret_dbus = _dbus_handle_register_dbus_object(dh, oh->path, ih); + ret_dbus = _gdbus_register_object(dh, oh->path, ih); if (ret_dbus <= 0) _E("failed to register dbus object%d", ret_dbus); @@ -1419,17 +1713,17 @@ err: return -1; } -guint subscribe_dbus_signal(dbus_handle_h handle, const char *path, +guint gdbus_signal_subscribe(dbus_handle_h handle, const char *path, const char *iface, const char *name, GDBusSignalCallback cb, void *data, destroy_notified free_func) { - dcl_dbus_handle(); + dcl_gdbus(); if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return 0; } } @@ -1441,13 +1735,13 @@ guint subscribe_dbus_signal(dbus_handle_h handle, const char *path, return g_dbus_connection_signal_subscribe(dh->conn, NULL, iface, name, path, NULL, G_DBUS_SIGNAL_FLAGS_NONE, cb, data, free_func); } -void unsubscribe_dbus_signal(dbus_handle_h handle, guint id) +void gdbus_signal_unsubscribe(dbus_handle_h handle, guint id) { - dcl_dbus_handle(); + dcl_gdbus(); if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return; } } @@ -1484,7 +1778,7 @@ static void _signal_reply_sync_cb(GDBusConnection *conn, g_main_loop_quit(ctx->loop); } -sig_ctx *dbus_handle_new_signal_ctx(void) +sig_ctx *gdbus_signal_ctx_new(void) { sig_ctx *ctx; @@ -1517,7 +1811,7 @@ sig_ctx *dbus_handle_new_signal_ctx(void) return ctx; } -void dbus_handle_free_signal_ctx(sig_ctx *ctx) +void gdbus_signal_ctx_free(sig_ctx *ctx) { if (!ctx) return ; @@ -1527,7 +1821,7 @@ void dbus_handle_free_signal_ctx(sig_ctx *ctx) ctx->param = NULL; } if (ctx->sig_id) { - unsubscribe_dbus_signal(NULL, ctx->sig_id); + gdbus_signal_unsubscribe(NULL, ctx->sig_id); ctx->sig_id = 0; } if (ctx->timeout_src) { @@ -1559,7 +1853,7 @@ static gboolean _cb_ctx_timeout(gpointer user_data) /* if cb return FALSE, source will be destroyed */ ctx->timeout_src = NULL; - unsubscribe_dbus_signal(NULL, ctx->sig_id); + gdbus_signal_unsubscribe(NULL, ctx->sig_id); ctx->sig_id = 0; g_main_loop_quit(ctx->loop); @@ -1569,7 +1863,7 @@ static gboolean _cb_ctx_timeout(gpointer user_data) #define CTX_MAX_TIMEOUT 25000 -int dbus_handle_signal_ctx_add_timeout(sig_ctx *ctx, int timeout_msec) +int gdbus_signal_ctx_add_timeout(sig_ctx *ctx, int timeout_msec) { GSource *src = NULL; @@ -1594,12 +1888,12 @@ int dbus_handle_signal_ctx_add_timeout(sig_ctx *ctx, int timeout_msec) return 0; } -guint subscribe_dbus_signal_ctx(dbus_handle_h handle, sig_ctx *ctx, +guint gdbus_signal_ctx_subscribe(dbus_handle_h handle, sig_ctx *ctx, const char *sender, const char *path, const char *iface, const char *name, GDBusSignalCallback _cb) { - dcl_dbus_handle(); + dcl_gdbus(); if (!ctx) { _E("wrong param ctx is null"); @@ -1607,10 +1901,10 @@ guint subscribe_dbus_signal_ctx(dbus_handle_h handle, sig_ctx *ctx, } if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { _E("failed to get default connection, bustype:%d", - (int)dbus_handle_get_default_bus_type()); + (int)gdbus_get_default_bus_type()); return 0; } } @@ -1637,7 +1931,7 @@ guint subscribe_dbus_signal_ctx(dbus_handle_h handle, sig_ctx *ctx, return ctx->sig_id; } -int dbus_handle_signal_ctx_wait(sig_ctx *ctx) +int gdbus_signal_ctx_wait(sig_ctx *ctx) { if (!ctx || !ctx->loop) return -EINVAL; @@ -1669,7 +1963,7 @@ int _check_type_string_is_container(const char *signature) return TRUE; } -int dbus_handle_emit_dbus_signal(const char *dest, +int gdbus_signal_emit(const char *dest, const char *path, const char *iface, const char *name, @@ -1679,9 +1973,9 @@ int dbus_handle_emit_dbus_signal(const char *dest, GError *err = NULL; gboolean ret = 0; - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } @@ -1694,7 +1988,7 @@ int dbus_handle_emit_dbus_signal(const char *dest, return ret; } -int dbus_handle_emit_dbus_signal_sync(const char *dest, +int gdbus_signal_emit_sync(const char *dest, const char *path, const char *iface, const char *name, @@ -1704,9 +1998,9 @@ int dbus_handle_emit_dbus_signal_sync(const char *dest, GError *err = NULL; gboolean ret = 0; - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } @@ -1725,16 +2019,16 @@ int dbus_handle_emit_dbus_signal_sync(const char *dest, return ret; } -int dbus_handle_flush_sync(dbus_handle_h handle) +int gdbus_flush_sync(dbus_handle_h handle) { - dcl_dbus_handle(); + dcl_gdbus(); GError *err = NULL; gboolean ret = 0; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return 0; } } @@ -1748,7 +2042,7 @@ int dbus_handle_flush_sync(dbus_handle_h handle) return ret; } -int dbus_handle_method_sync_with_reply_var(const char *dest, +int gdbus_call_sync_with_reply(const char *dest, const char *path, const char *iface, const char *method, @@ -1767,9 +2061,9 @@ int dbus_handle_method_sync_with_reply_var(const char *dest, return -EINVAL; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); if (param) g_variant_unref(param); return -ECOMM; @@ -1805,7 +2099,7 @@ int dbus_handle_method_sync_with_reply_var(const char *dest, return ret; } -int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char *path, +int gdbus_call_sync_with_reply_timeout(const char *dest, const char *path, const char *iface, const char *method, GVariant *param, GVariant **out_reply, int timeout) { GError *err = NULL; @@ -1819,9 +2113,9 @@ int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char return -EINVAL; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); if (param) g_variant_unref(param); return -ECOMM; @@ -1852,7 +2146,7 @@ int dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char return 0; } -int dbus_handle_method_sync_pairs(const char *dest, +int gdbus_call_pairs_sync_with_reply_int(const char *dest, const char *path, const char *iface, const char *method, @@ -1872,9 +2166,9 @@ int dbus_handle_method_sync_pairs(const char *dest, return -1; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } @@ -1908,7 +2202,7 @@ int dbus_handle_method_sync_pairs(const char *dest, return ret; } -int dbus_handle_method_async_pairs(const char *dest, +int gdbus_call_pairs_async(const char *dest, const char *path, const char *iface, const char *method, @@ -1925,9 +2219,9 @@ int dbus_handle_method_async_pairs(const char *dest, return -1; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } @@ -1956,7 +2250,7 @@ int dbus_handle_method_async_pairs(const char *dest, return 0; } -gint* dbus_handle_get_unix_fd_list(GDBusMethodInvocation *invocation, int *size) +gint* gdbus_get_unix_fd_list(GDBusMethodInvocation *invocation, int *size) { GUnixFDList *fd_list = NULL; int length = 0; @@ -1979,7 +2273,7 @@ gint* dbus_handle_get_unix_fd_list(GDBusMethodInvocation *invocation, int *size) return g_unix_fd_list_steal_fds(fd_list, NULL); } -int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, +int gdbus_call_unix_fd_list_sync_with_reply(const char *dest, const char *path, const char *iface, const char *method, @@ -2006,9 +2300,9 @@ int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, return -EINVAL; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -EAGAIN; } @@ -2065,7 +2359,7 @@ out: return ret; } -int dbus_handle_method_sync_var(const char *dest, +int gdbus_call_sync_with_reply_int(const char *dest, const char *path, const char *iface, const char *method, @@ -2082,7 +2376,7 @@ int dbus_handle_method_sync_var(const char *dest, return -EINVAL; } - ret = dbus_handle_method_sync_with_reply_var(dest, path, iface, method, param, &reply); + ret = gdbus_call_sync_with_reply(dest, path, iface, method, param, &reply); if (ret < 0) return ret; @@ -2106,7 +2400,7 @@ int dbus_handle_method_sync_var(const char *dest, return ret; } -int dbus_handle_method_async_var(const char *dest, +int gdbus_call_async(const char *dest, const char *path, const char *iface, const char *method, @@ -2119,9 +2413,9 @@ int dbus_handle_method_async_var(const char *dest, return -1; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } @@ -2166,7 +2460,7 @@ out: free(data); } -int dbus_handle_method_async_with_reply_var(const char *dest, +int gdbus_call_async_with_reply(const char *dest, const char *path, const char *iface, const char *method, @@ -2189,9 +2483,9 @@ int dbus_handle_method_async_with_reply_var(const char *dest, return -1; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -EPERM; } @@ -2221,7 +2515,7 @@ err: return ret; } -int dbus_handle_method_async_pairs_with_reply(const char *dest, +int gdbus_call_pairs_async_with_reply(const char *dest, const char *path, const char *iface, const char *method, @@ -2248,9 +2542,9 @@ int dbus_handle_method_async_pairs_with_reply(const char *dest, return -1; } - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -EPERM; } @@ -2293,7 +2587,7 @@ err: return ret; } -int dbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) +int gdbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) { GError *err = NULL; GVariant *reply = NULL; @@ -2327,14 +2621,14 @@ int dbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) return pid; } -int dbus_handle_get_sender_pid(dbus_handle_h handle, const char * sender) +int gdbus_get_sender_pid(dbus_handle_h handle, const char * sender) { - dcl_dbus_handle(); + dcl_gdbus(); if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -2344,12 +2638,12 @@ int dbus_handle_get_sender_pid(dbus_handle_h handle, const char * sender) return -1; } - return dbus_connection_get_sender_pid(dh->conn, sender); + return gdbus_connection_get_sender_pid(dh->conn, sender); } -int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, GDBusCredentials *creds) +int gdbus_get_sender_credentials(dbus_handle_h handle, const char *name, GDBusCredentials *creds) { - dcl_dbus_handle(); + dcl_gdbus(); GVariant *reply = NULL; GError *err = NULL; GVariantIter *iter = NULL; @@ -2357,9 +2651,9 @@ int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, G GVariant *sub; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return -1; } } @@ -2408,7 +2702,7 @@ void _destroy_notify_watch_name(gpointer data) // free(data); } -int dbus_handle_watch_name(const char *name, +int gdbus_watch_name(const char *name, GBusNameAppearedCallback name_appeared_handler, GBusNameVanishedCallback name_vanished_handler, void *user_data, @@ -2425,7 +2719,7 @@ int dbus_handle_watch_name(const char *name, return -1; } - id = g_bus_watch_name(dbus_handle_get_default_bus_type(), + id = g_bus_watch_name(gdbus_get_default_bus_type(), name, G_BUS_NAME_WATCHER_FLAGS_NONE, name_appeared_handler, @@ -2440,15 +2734,6 @@ int dbus_handle_watch_name(const char *name, return id; } -void dbus_handle_unwatch_name(guint id) -{ - if (id == 0) { - _E("wrong id %d", id); - return; - } - g_bus_unwatch_name(id); -} - int _get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size) { int fd, ret_file; @@ -2485,85 +2770,30 @@ int _get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size) return 0; } -// g_strfreev(strv) -char **dbus_handle_get_owner_list(dbus_handle_h handle, const char *bus_name) -{ - dcl_dbus_handle(); - GError *err = NULL; - GVariant *reply = NULL; - GVariantIter *iter = NULL; - gchar **strv = NULL; - gchar *str = NULL; - int i = 0; - - if (!bus_name) { - _E("wrong parameter bus_name is null"); - return NULL; - } - - if (!dh) { - dh = _dbus_handle_get_default_connection(); - if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); - return NULL; - } - } - - reply = g_dbus_connection_call_sync(dh->conn, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "ListQueuedOwners", - g_variant_new("(s)", bus_name), - NULL, - G_DBUS_CALL_FLAGS_NONE, - DBUS_REPLY_TIMEOUT, - NULL, - &err); - if (!reply || err) { - _E("failed to g_dbus_connection_call_sync:%s", err->message); - g_error_free(err); - return NULL; - } - - g_variant_get(reply, "(as)", &iter); - strv = g_new(gchar *, g_variant_iter_n_children(iter) + 1); - - i = 0; - while (g_variant_iter_loop(iter, "s", &str)) - strv[i++] = g_strdup(str); - strv[i] = NULL; - - g_variant_iter_free(iter); - g_variant_unref(reply); - - return strv; -} - -void dbus_handle_check_owner_name(dbus_handle_h handle, const char *owner_name) +void gdbus_check_name_owner(dbus_handle_h handle, const char *owner_name) { - dcl_dbus_handle(); + dcl_gdbus(); char exe_name[PATH_MAX]; int pid; char **strv = NULL; int i; if (!dh) { - dh = _dbus_handle_get_default_connection(); + dh = _gdbus_get_default_connection(); if (!dh) { - _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type()); + _E("failed to get default connection, bustype:%d", (int)gdbus_get_default_bus_type()); return ; } } - strv = dbus_handle_get_owner_list(dh, owner_name); + strv = gdbus_get_owner_list(dh, owner_name); if (!strv) { _E("failed to get owner list of %s", owner_name); return ; } for (i = 0; strv[i] != NULL; ++i) { - pid = dbus_handle_get_sender_pid(dh, strv[i]); + pid = gdbus_get_sender_pid(dh, strv[i]); if (_get_cmdline_name(pid, exe_name, PATH_MAX) != 0) break; _I("%s(%d)", exe_name, pid); @@ -2581,7 +2811,7 @@ int check_systemd_active(void) _I("%s %s", "org.freedesktop.systemd1.Unit", "ActiveState"); - ret = dbus_handle_method_sync_with_reply_var("org.freedesktop.systemd1", + ret = gdbus_call_sync_with_reply("org.freedesktop.systemd1", "/org/freedesktop/systemd1/unit/default_2etarget", "org.freedesktop.DBus.Properties", "Get", @@ -2616,7 +2846,7 @@ out: return ret; } -GVariant *dbus_handle_make_simple_array(const char *sig, int *param) +GVariant *gdbus_make_simple_array(const char *sig, int *param) { GVariantBuilder *builder = NULL; GVariant *var = NULL; diff --git a/src/libgdbus/libgdbus.h b/src/libgdbus/libgdbus.h index 0e26bb8..6828b76 100644 --- a/src/libgdbus/libgdbus.h +++ b/src/libgdbus/libgdbus.h @@ -68,7 +68,7 @@ typedef struct { #define g_variant_get_safe(gvar, signature, ...) ((gvar && (g_strcmp0(signature, g_variant_get_type_string(gvar)) == 0)) ? g_variant_get(gvar, signature, __VA_ARGS__), TRUE : FALSE) -#define dbus_handle_new_g_variant_tuple() g_variant_new_tuple(NULL, 0) +#define gdbus_new_g_variant_tuple() g_variant_new_tuple(NULL, 0) typedef void (*destroy_notified)(void *data); @@ -79,18 +79,44 @@ typedef struct { void *data; } pending_call_data; -dbus_handle_h dbus_handle_get_connection (GBusType bus_type, +dbus_handle_h gdbus_get_connection (GBusType bus_type, gboolean priv); -int dbus_handle_request_bus_name (dbus_handle_h handle, +int gdbus_request_name (dbus_handle_h handle, const char *bus_name, GBusNameAcquiredCallback acquired_handler, GBusNameLostCallback lost_handler); -int dbus_handle_register_dbus_object (dbus_handle_h handle, +int gdbus_release_name (dbus_handle_h handle, + const char *bus_name); + +int gdbus_watch_name (const char *name, + GBusNameAppearedCallback name_appeared_handler, + GBusNameVanishedCallback name_vanished_handler, + void *user_data, + GDestroyNotify user_data_free_func); + +void gdbus_unwatch_name (guint id); + +char** gdbus_get_owner_list (dbus_handle_h handle, + const char *bus_name); + +void gdbus_check_name_owner (dbus_handle_h handle, + const char *owner_name); + +int gdbus_register_object (dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface); +int gdbus_unregister_object (dbus_handle_h handle, + const char *obj_path); + +int gdbus_add_object (dbus_handle_h handle, + const char *obj_path, + const dbus_interface_u *iface_u); + +int gdbus_register_object_all (dbus_handle_h handle); + int dbus_handle_unregister_dbus_object (dbus_handle_h handle, const char *obj_path); @@ -100,20 +126,35 @@ int dbus_handle_add_dbus_object (dbus_handle_h handle, int dbus_handle_register_dbus_object_all (dbus_handle_h handle); -int dbus_handle_method_sync_var (const char *dest, +int gdbus_call_sync_with_reply_int (const char *dest, const char *path, const char *iface, const char *method, GVariant *param, int *output); -int dbus_handle_method_async_var (const char *dest, +int gdbus_call_sync_with_reply (const char *dest, + const char *path, + const char *iface, + const char *method, + GVariant *param, + GVariant **out_reply); + +int gdbus_call_sync_with_reply_timeout (const char *dest, + const char *path, + const char *iface, + const char *method, + GVariant *param, + GVariant **out_reply, + int timeout); + +int gdbus_call_async (const char *dest, const char *path, const char *iface, const char *method, GVariant *param); -int dbus_handle_method_async_with_reply_var (const char *dest, +int gdbus_call_async_with_reply (const char *dest, const char *path, const char *iface, const char *method, @@ -122,36 +163,32 @@ int dbus_handle_method_async_with_reply_var (const char *dest, int timeout_msec, void *data); -int dbus_handle_method_sync_with_reply_var (const char *dest, - const char *path, - const char *iface, - const char *method, - GVariant *param, - GVariant **out_reply); - -int dbus_handle_method_sync_with_reply_var_timeout (const char *dest, +int gdbus_call_unix_fd_list_sync_with_reply (const char *dest, const char *path, const char *iface, const char *method, GVariant *param, GVariant **out_reply, - int timeout); + int *in_fdlist, + int in_size, + int **out_fdlist, + int *out_size); -int dbus_handle_method_sync_pairs (const char *dest, +int gdbus_call_pairs_sync_with_reply_int (const char *dest, const char *path, const char *interface, const char *method, int num, va_list args); -int dbus_handle_method_async_pairs (const char *dest, +int gdbus_call_pairs_async (const char *dest, const char *path, const char *interface, const char *method, int num, va_list args); -int dbus_handle_method_async_pairs_with_reply (const char *dest, +int gdbus_call_pairs_async_with_reply (const char *dest, const char *path, const char *iface, const char *method, @@ -161,45 +198,28 @@ int dbus_handle_method_async_pairs_with_reply (const char *dest, int timeout_msec, void *data); -gint* dbus_handle_get_unix_fd_list (GDBusMethodInvocation *invocation, - int *size); - -int dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest, - const char *path, - const char *iface, - const char *method, - GVariant *param, - GVariant **out_reply, - int *in_fdlist, - int in_size, - int **out_fdlist, - int *out_size); - - -guint subscribe_dbus_signal (dbus_handle_h handle, - const char *path, - const char *iface, - const char *name, - GDBusSignalCallback cb, - void *data, - destroy_notified free_func); - -void unsubscribe_dbus_signal (dbus_handle_h handle, - guint id); - -int dbus_handle_emit_dbus_signal (const char *dest, +int gdbus_signal_emit (const char *dest, const char *path, const char *iface, const char *name, GVariant *param); -int dbus_handle_emit_dbus_signal_sync(const char *dest, +int gdbus_signal_emit_sync(const char *dest, const char *path, const char *iface, const char *name, GVariant *param); -int dbus_handle_flush_sync(dbus_handle_h handle); +guint gdbus_signal_subscribe (dbus_handle_h handle, + const char *path, + const char *iface, + const char *name, + GDBusSignalCallback cb, + void *data, + destroy_notified free_func); + +void gdbus_signal_unsubscribe (dbus_handle_h handle, + guint id); enum ctx_quit_reason {CTX_QUIT_UNKNOWN, CTX_QUIT_NORMAL, CTX_QUIT_TIMEOUT}; @@ -213,11 +233,7 @@ typedef struct { void *user_data; } sig_ctx; -sig_ctx *dbus_handle_new_signal_ctx (void); - -void dbus_handle_free_signal_ctx (sig_ctx *ctx); - -guint subscribe_dbus_signal_ctx (dbus_handle_h handle, +guint gdbus_signal_ctx_subscribe (dbus_handle_h handle, sig_ctx *ctx, const char *sender, const char *path, @@ -225,42 +241,35 @@ guint subscribe_dbus_signal_ctx (dbus_handle_h handle, const char *name, GDBusSignalCallback cb); -int dbus_handle_signal_ctx_wait (sig_ctx *ctx); +sig_ctx *gdbus_signal_ctx_new (void); -int dbus_handle_signal_ctx_add_timeout (sig_ctx *ctx, - int timeout); +void gdbus_signal_ctx_free (sig_ctx *ctx); +int gdbus_signal_ctx_wait (sig_ctx *ctx); +int gdbus_signal_ctx_add_timeout (sig_ctx *ctx, + int timeout); +int gdbus_flush_sync(dbus_handle_h handle); -int dbus_connection_get_sender_pid (GDBusConnection *conn, +int gdbus_connection_get_sender_pid (GDBusConnection *conn, const char * sender); -int dbus_handle_get_sender_pid (dbus_handle_h handle, +int gdbus_get_sender_pid (dbus_handle_h handle, const char * sender); -int dbus_handle_get_sender_credentials (dbus_handle_h handle, +int gdbus_get_sender_credentials (dbus_handle_h handle, const char *name, GDBusCredentials *creds); -int dbus_handle_watch_name (const char *name, - GBusNameAppearedCallback name_appeared_handler, - GBusNameVanishedCallback name_vanished_handler, - void *user_data, - GDestroyNotify user_data_free_func); - -void dbus_handle_unwatch_name (guint id); - -char** dbus_handle_get_owner_list (dbus_handle_h handle, - const char *bus_name); +gint* gdbus_get_unix_fd_list (GDBusMethodInvocation *invocation, + int *size); -void dbus_handle_check_owner_name (dbus_handle_h handle, - const char *owner_name); +GVariant *gdbus_make_simple_array (const char *sig, + int *param); int check_systemd_active (void); -GVariant *dbus_handle_make_simple_array (const char *sig, - int *param); #endif diff --git a/src/libsystemd/libsystemd.c b/src/libsystemd/libsystemd.c index a045616..8b8295c 100644 --- a/src/libsystemd/libsystemd.c +++ b/src/libsystemd/libsystemd.c @@ -113,14 +113,14 @@ static int _systemd_control_unit_wait(const char *method, unitinfo uinfo; int quit_reason; - ctx = dbus_handle_new_signal_ctx(); + ctx = gdbus_signal_ctx_new(); if (!ctx) return -ENOMEM; _I("Starting: %s %s", method, name); /* synchronous signal subscription */ - ret = subscribe_dbus_signal_ctx(NULL, + ret = gdbus_signal_ctx_subscribe(NULL, ctx, SYSTEMD_DBUS_SERVICE, SYSTEMD_DBUS_PATH, @@ -132,7 +132,7 @@ static int _systemd_control_unit_wait(const char *method, goto finish; } - ret = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret = gdbus_call_sync_with_reply(SYSTEMD_DBUS_DEST, SYSTEMD_DBUS_PATH, SYSTEMD_DBUS_MANAGER_IFACE, method, @@ -152,14 +152,14 @@ static int _systemd_control_unit_wait(const char *method, ctx->user_data = &uinfo; /* set timeout */ - ret = dbus_handle_signal_ctx_add_timeout(ctx, timeout_msec); + ret = gdbus_signal_ctx_add_timeout(ctx, timeout_msec); if (ret < 0) { _E("Failed to set timeout, %d", ret); goto finish; } /* run loop and wait signal callback */ - quit_reason = dbus_handle_signal_ctx_wait(ctx); + quit_reason = gdbus_signal_ctx_wait(ctx); if (quit_reason != CTX_QUIT_NORMAL) { ret = -1; _E("Failed to receive JobRemoved signal %d", quit_reason); @@ -173,7 +173,7 @@ finish: g_variant_unref(reply); g_free(objpath); - dbus_handle_free_signal_ctx(ctx); + gdbus_signal_ctx_free(ctx); return ret; } @@ -186,7 +186,7 @@ static int _systemd_control_unit_async(const char *method, const char *name) _I("Starting: %s %s", method, name); - ret = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret = gdbus_call_sync_with_reply(SYSTEMD_DBUS_DEST, SYSTEMD_DBUS_PATH, SYSTEMD_DBUS_MANAGER_IFACE, method, @@ -416,7 +416,7 @@ GVariant *systemd_get_manager_property(const char *property) if (!property) return NULL; - ret_dbus = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret_dbus = gdbus_call_sync_with_reply(SYSTEMD_DBUS_DEST, SYSTEMD_DBUS_PATH, DBUS_IFACE_DBUS_PROPERTIES, "Get", @@ -442,7 +442,7 @@ GVariant *systemd_get_unit_property(const char *unit, const char *property) escaped = systemd_get_unit_dbus_path(unit); - ret_dbus = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret_dbus = gdbus_call_sync_with_reply(SYSTEMD_DBUS_DEST, escaped, DBUS_IFACE_DBUS_PROPERTIES, "Get", @@ -470,7 +470,7 @@ GVariant *systemd_get_service_property(const char *unit, const char *property) escaped = systemd_get_unit_dbus_path(unit); - ret_dbus = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST, + ret_dbus = gdbus_call_sync_with_reply(SYSTEMD_DBUS_DEST, escaped, DBUS_IFACE_DBUS_PROPERTIES, "Get", -- 2.7.4 From e67938efffc0ed9a0732c26fa25dd185eb3ff21e Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 19 Mar 2021 17:40:06 +0900 Subject: [PATCH 06/16] Fix including libgdbus header libsystemd Change-Id: I427757673726c8829a777d8667dcd096eb481a49 Signed-off-by: Hyotaek Shim --- src/libsystemd/libsystemd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsystemd/libsystemd.h b/src/libsystemd/libsystemd.h index 79c9f9b..1f807d3 100644 --- a/src/libsystemd/libsystemd.h +++ b/src/libsystemd/libsystemd.h @@ -20,7 +20,7 @@ #ifndef __DBUS_SYSTEMD_H__ #define __DBUS_SYSTEMD_H__ -#include +#include #ifdef __cplusplus extern "C" { -- 2.7.4 From e2af1e6325af742bd3ed90af862734958b6622e1 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 22 Apr 2021 10:01:48 +0900 Subject: [PATCH 07/16] Remove with_tizen build macro Change-Id: I565075d4b1dbc99f73fd3bcc7afe5eb944bce854 Signed-off-by: Hyotaek Shim --- packaging/libsyscommon.spec | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index 3b6869f..80e7db4 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -5,9 +5,7 @@ Release: 0%{?release_flags} License: Apache-2.0 Group: System/Libraries Source: %{name}-%{version}.tar.gz -%if 0%{?_with_tizen} Source1001: %{name}.manifest -%endif BuildRequires: pkgconfig(glib-2.0) >= 2.44 BuildRequires: pkgconfig(gio-2.0) >= 2.44 @@ -36,9 +34,7 @@ Development header files for system common library. %prep %setup -q -%if 0%{?_with_tizen} cp %{SOURCE1001} . -%endif %build %cmake . @@ -58,17 +54,13 @@ touch debugsources.list %files %defattr(-,root,root,-) -%if 0%{?_with_tizen} %manifest %{name}.manifest -%endif %license LICENSE.Apache-2.0 %{_libdir}/libsyscommon.so.* %files -n libsyscommon-devel %defattr(-,root,root,-) -%if 0%{?_with_tizen} %manifest %{name}.manifest -%endif %license LICENSE.Apache-2.0 %{_libdir}/libsyscommon.so %{_includedir}/libsyscommon/*.h -- 2.7.4 From 3ff9715bba83e550b1ef5cb26b9df14414a89e45 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 11 Jun 2021 15:09:02 +0900 Subject: [PATCH 08/16] libcommon: fix indentation Change-Id: Ib0af8672bfa8843050db917540c3ddfe91efbcff Signed-off-by: Youngjae Cho --- src/libcommon/file.h | 112 ++++++++++++++++++++++----------------------- src/libcommon/ini-parser.h | 8 ++-- src/libcommon/list.h | 42 ++++++++--------- 3 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/libcommon/file.h b/src/libcommon/file.h index 1cd4dec..4d256a0 100644 --- a/src/libcommon/file.h +++ b/src/libcommon/file.h @@ -28,102 +28,102 @@ static inline int sys_read_buf(char *file, char *buf, int len) { - int fd, r; + int fd, r; - if (!file || !buf || len < 0) - return -EINVAL; + if (!file || !buf || len < 0) + return -EINVAL; - fd = open(file, O_RDONLY); - if (fd == -1) - return -ENOENT; + fd = open(file, O_RDONLY); + if (fd == -1) + return -ENOENT; - r = read(fd, buf, len); - close(fd); - if ((r >= 0) && (r < len)) - buf[r] = '\0'; - else - return -EIO; + r = read(fd, buf, len); + close(fd); + if ((r >= 0) && (r < len)) + buf[r] = '\0'; + else + return -EIO; - return 0; + return 0; } static inline int sys_write_buf(char *file, char *buf) { - int fd, r; + int fd, r; - if (!file || !buf) - return -EINVAL; + if (!file || !buf) + return -EINVAL; - fd = open(file, O_WRONLY); - if (fd == -1) - return -EPERM; + fd = open(file, O_WRONLY); + if (fd == -1) + return -EPERM; - r = write(fd, buf, strlen(buf)); - close(fd); - if (r < 0) - return -EIO; + r = write(fd, buf, strlen(buf)); + close(fd); + if (r < 0) + return -EIO; - return 0; + return 0; } static inline int sys_get_int(char *fname, int *val) { - char buf[SHARED_H_BUF_MAX]; - int r; + char buf[SHARED_H_BUF_MAX]; + int r; - if (!fname || !val) - return -EINVAL; + if (!fname || !val) + return -EINVAL; - r = sys_read_buf(fname, buf, sizeof(buf)); - if (r < 0) - return r; + r = sys_read_buf(fname, buf, sizeof(buf)); + if (r < 0) + return r; - *val = atoi(buf); - return 0; + *val = atoi(buf); + return 0; } static inline int sys_get_str(char *fname, char *str, int len) { - int r; + int r; - if (!fname || !str || len < 0) - return -EINVAL; + if (!fname || !str || len < 0) + return -EINVAL; - r = sys_read_buf(fname, str, len); - if (r < 0) - return r; + r = sys_read_buf(fname, str, len); + if (r < 0) + return r; - return 0; + return 0; } static inline int sys_set_int(char *fname, int val) { - char buf[SHARED_H_BUF_MAX]; - int r; + char buf[SHARED_H_BUF_MAX]; + int r; - if (!fname) - return -EINVAL; + if (!fname) + return -EINVAL; - snprintf(buf, sizeof(buf), "%d", val); - r = sys_write_buf(fname, buf); - if (r < 0) - return r; + snprintf(buf, sizeof(buf), "%d", val); + r = sys_write_buf(fname, buf); + if (r < 0) + return r; - return 0; + return 0; } static inline int sys_set_str(char *fname, char *val) { - int r; + int r; - if (!fname || !val) - return -EINVAL; + if (!fname || !val) + return -EINVAL; - r = sys_write_buf(fname, val); - if (r < 0) - return r; + r = sys_write_buf(fname, val); + if (r < 0) + return r; - return 0; + return 0; } #endif /* __LIBCOMMON_FILE_H__ */ diff --git a/src/libcommon/ini-parser.h b/src/libcommon/ini-parser.h index b65c2b1..f30096c 100644 --- a/src/libcommon/ini-parser.h +++ b/src/libcommon/ini-parser.h @@ -24,9 +24,9 @@ #define SET_CONF(a, b) (a = (b > 0.0 ? b : a)) struct parse_result { - char *section; - char *name; - char *value; + char *section; + char *name; + char *value; }; /** @@ -37,6 +37,6 @@ struct parse_result { * @return 0 on success, negative if failed */ int config_parse(const char *file_name, int cb(struct parse_result *result, - void *user_data), void *user_data); + void *user_data), void *user_data); #endif diff --git a/src/libcommon/list.h b/src/libcommon/list.h index 0809b04..ab27843 100644 --- a/src/libcommon/list.h +++ b/src/libcommon/list.h @@ -22,39 +22,39 @@ #include #define SYS_G_LIST_PREPEND(a, b) \ - a = g_list_prepend(a, (gpointer)b) + a = g_list_prepend(a, (gpointer)b) #define SYS_G_LIST_APPEND(a, b) \ - a = g_list_append(a, (gpointer)b) + a = g_list_append(a, (gpointer)b) #define SYS_G_LIST_REMOVE(a, b) \ - a = g_list_remove(a, (gpointer)b) + a = g_list_remove(a, (gpointer)b) #define SYS_G_LIST_REMOVE_LIST(a, b) \ - a = g_list_delete_link(a, b) + a = g_list_delete_link(a, b) #define SYS_G_LIST_LENGTH(a) \ - g_list_length(a) + g_list_length(a) #define SYS_G_LIST_NTH(a, b) \ - g_list_nth_data(a, b) + g_list_nth_data(a, b) #define SYS_G_LIST_FIND(a, b) \ - g_list_find(a, (gpointer)b) + g_list_find(a, (gpointer)b) #define SYS_G_LIST_FREE_LIST(a) \ - g_list_free(a) + g_list_free(a) #define SYS_G_LIST_FOREACH(head, elem, node) \ - for (elem = head, node = NULL; \ - elem && ((node = elem->data) != NULL); \ - elem = elem->next, node = NULL) + for (elem = head, node = NULL; \ + elem && ((node = elem->data) != NULL); \ + elem = elem->next, node = NULL) #define SYS_G_LIST_FOREACH_SAFE(head, elem, elem_next, node) \ - for (elem = head, elem_next = g_list_next(elem), node = NULL; \ - elem && ((node = elem->data) != NULL); \ - elem = elem_next, elem_next = g_list_next(elem), node = NULL) + for (elem = head, elem_next = g_list_next(elem), node = NULL; \ + elem && ((node = elem->data) != NULL); \ + elem = elem_next, elem_next = g_list_next(elem), node = NULL) #define SYS_G_LIST_REVERSE_FOREACH(head, elem, node) \ - for (elem = g_list_last(head), node = NULL; \ - elem && ((node = elem->data) != NULL); \ - elem = g_list_previous(elem), node = NULL) + for (elem = g_list_last(head), node = NULL; \ + elem && ((node = elem->data) != NULL); \ + elem = g_list_previous(elem), node = NULL) #define SYS_G_LIST_REVERSE_FOREACH_SAFE(head, elem, elem_next, node) \ - for (elem = g_list_last(head), elem_next = g_list_previous(elem), node = NULL; \ - elem && ((node = elem->data) != NULL); \ - elem = elem_next, elem_next = g_list_previous(elem), node = NULL) + for (elem = g_list_last(head), elem_next = g_list_previous(elem), node = NULL; \ + elem && ((node = elem->data) != NULL); \ + elem = elem_next, elem_next = g_list_previous(elem), node = NULL) #define SYS_G_LIST_NEXT(a) \ - g_list_next(a) + g_list_next(a) #endif -- 2.7.4 From 5f52d2f736a267fd679407765d8e72d39f858a87 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 11 Jun 2021 15:12:50 +0900 Subject: [PATCH 09/16] libcommon: bring file IO from deviced Change-Id: I0068a42487b31881ff2c8f91de785ae8362984d4 Signed-off-by: Youngjae Cho --- CMakeLists.txt | 1 + src/libcommon/file.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/libcommon/file.h | 113 +++---------------------------------------- 3 files changed, 140 insertions(+), 107 deletions(-) create mode 100644 src/libcommon/file.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e16516d..2c1231a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ SET(libsyscommon_SRCS src/libgdbus/libgdbus.c src/libsystemd/libsystemd.c src/libcommon/ini-parser.c + src/libcommon/file.c ) SET(HEADERS src/libgdbus/libgdbus.h diff --git a/src/libcommon/file.c b/src/libcommon/file.c new file mode 100644 index 0000000..b3ca5f1 --- /dev/null +++ b/src/libcommon/file.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include "file.h" + +#define SHARED_H_BUF_MAX 255 + +int sys_read_buf(char *file, char *buf, int len) +{ + int fd, r; + + if (!file || !buf || len < 0) + return -EINVAL; + + fd = open(file, O_RDONLY); + if (fd == -1) + return -errno; + + r = read(fd, buf, len); + if ((r >= 0) && (r < len)) + buf[r] = '\0'; + else { + buf[0] = '\0'; + r = -EIO; + } + + close(fd); + + return r; +} + +int sys_write_buf(char *file, char *buf) +{ + int fd, r; + + if (!file || !buf) + return -EINVAL; + + fd = open(file, O_WRONLY); + if (fd == -1) + return -errno; + + r = write(fd, buf, strlen(buf)); + if (r < 0) + r = -EIO; + + close(fd); + + return 0; +} + +int sys_get_int(char *fname, int *val) +{ + char buf[SHARED_H_BUF_MAX]; + int r; + + if (!fname || !val) + return -EINVAL; + + r = sys_read_buf(fname, buf, sizeof(buf)); + if (r > 0) { + *val = atoi(buf); + } else { + *val = -1; + r = -EIO; + } + + return r; +} + +int sys_get_str(char *fname, char *str, int len) +{ + int r; + + if (!fname || !str || len < 0) + return -EINVAL; + + r = sys_read_buf(fname, str, len); + if (r < 0) + return r; + + return 0; +} + +int sys_set_int(char *fname, int val) +{ + char buf[SHARED_H_BUF_MAX]; + int r; + + if (!fname) + return -EINVAL; + + snprintf(buf, sizeof(buf), "%d", val); + r = sys_write_buf(fname, buf); + if (r < 0) + return r; + + return 0; +} + +int sys_set_str(char *fname, char *val) +{ + int r; + + if (!fname || !val) + return -EINVAL; + + r = sys_write_buf(fname, val); + if (r < 0) + return r; + + return 0; +} diff --git a/src/libcommon/file.h b/src/libcommon/file.h index 4d256a0..1250da8 100644 --- a/src/libcommon/file.h +++ b/src/libcommon/file.h @@ -18,112 +18,11 @@ #ifndef __LIBCOMMON_FILE_H__ #define __LIBCOMMON_FILE_H__ -#include -#include -#include -#include -#include - -#define SHARED_H_BUF_MAX 255 - -static inline int sys_read_buf(char *file, char *buf, int len) -{ - int fd, r; - - if (!file || !buf || len < 0) - return -EINVAL; - - fd = open(file, O_RDONLY); - if (fd == -1) - return -ENOENT; - - r = read(fd, buf, len); - close(fd); - if ((r >= 0) && (r < len)) - buf[r] = '\0'; - else - return -EIO; - - return 0; -} - -static inline int sys_write_buf(char *file, char *buf) -{ - int fd, r; - - if (!file || !buf) - return -EINVAL; - - fd = open(file, O_WRONLY); - if (fd == -1) - return -EPERM; - - r = write(fd, buf, strlen(buf)); - close(fd); - if (r < 0) - return -EIO; - - return 0; -} - -static inline int sys_get_int(char *fname, int *val) -{ - char buf[SHARED_H_BUF_MAX]; - int r; - - if (!fname || !val) - return -EINVAL; - - r = sys_read_buf(fname, buf, sizeof(buf)); - if (r < 0) - return r; - - *val = atoi(buf); - return 0; -} - -static inline int sys_get_str(char *fname, char *str, int len) -{ - int r; - - if (!fname || !str || len < 0) - return -EINVAL; - - r = sys_read_buf(fname, str, len); - if (r < 0) - return r; - - return 0; -} - -static inline int sys_set_int(char *fname, int val) -{ - char buf[SHARED_H_BUF_MAX]; - int r; - - if (!fname) - return -EINVAL; - - snprintf(buf, sizeof(buf), "%d", val); - r = sys_write_buf(fname, buf); - if (r < 0) - return r; - - return 0; -} - -static inline int sys_set_str(char *fname, char *val) -{ - int r; - - if (!fname || !val) - return -EINVAL; - - r = sys_write_buf(fname, val); - if (r < 0) - return r; - - return 0; -} +int sys_read_buf(char *file, char *buf, int len); +int sys_write_buf(char *file, char *buf); +int sys_get_int(char *fname, int *val); +int sys_get_str(char *fname, char *str, int len); +int sys_set_int(char *fname, int val); +int sys_set_str(char *fname, char *val); #endif /* __LIBCOMMON_FILE_H__ */ -- 2.7.4 From f3b24d981fc721ccaebfd15dc9b160d270979cf1 Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Mon, 5 Jul 2021 20:47:06 +0900 Subject: [PATCH 10/16] libcommon: add is_container function Change-Id: I5ba0109e996b3f4e8ff939859499a3de8b623c90 Signed-off-by: Yunmi Ha --- CMakeLists.txt | 2 ++ src/libcommon/common.c | 29 +++++++++++++++++++++++++++++ src/libcommon/common.h | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/libcommon/common.c create mode 100644 src/libcommon/common.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c1231a..049a45b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ SET(libsyscommon_SRCS src/libsystemd/libsystemd.c src/libcommon/ini-parser.c src/libcommon/file.c + src/libcommon/common.c ) SET(HEADERS src/libgdbus/libgdbus.h @@ -24,6 +25,7 @@ SET(HEADERS src/libcommon/list.h src/libcommon/ini-parser.h src/libcommon/file.h + src/libcommon/common.h ) # CHECK PKG diff --git a/src/libcommon/common.c b/src/libcommon/common.c new file mode 100644 index 0000000..fd3a11b --- /dev/null +++ b/src/libcommon/common.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "common.h" + +#define CONTAINER_FILE_PATH "/run/systemd/container" + +bool is_container() +{ + if (access(CONTAINER_FILE_PATH, F_OK) == 0) + return true; + + return false; +} diff --git a/src/libcommon/common.h b/src/libcommon/common.h new file mode 100644 index 0000000..43a1fae --- /dev/null +++ b/src/libcommon/common.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LIBCOMMON_COMMON_H__ +#define __LIBCOMMON_COMMON_H__ + +#include + +bool is_container(); + +#endif /* __LIBCOMMON_COMMON_H__ */ -- 2.7.4 From dc635f86f30269a21e7440279a74265e3989ed4b Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 1 Oct 2021 18:10:18 +0900 Subject: [PATCH 11/16] tests: add test running automatically on building If the test fails, the whole build will fail. Change-Id: Ibb88e60d13bcec09567630d051372fbcfbbb79da Signed-off-by: Youngjae Cho --- CMakeLists.txt | 2 + packaging/libsyscommon.spec | 1 + tests/CMakeLists.txt | 20 ++++ tests/libcommon/CMakeLists.txt | 16 ++++ tests/libcommon/test-common.c | 208 +++++++++++++++++++++++++++++++++++++++++ tests/libcommon/test.conf | 8 ++ tests/test-main.c | 50 ++++++++++ tests/test-main.h | 10 ++ tests/test-mock.c | 65 +++++++++++++ tests/test-mock.h | 8 ++ 10 files changed, 388 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/libcommon/CMakeLists.txt create mode 100644 tests/libcommon/test-common.c create mode 100644 tests/libcommon/test.conf create mode 100644 tests/test-main.c create mode 100644 tests/test-main.h create mode 100644 tests/test-mock.c create mode 100644 tests/test-mock.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 049a45b..bebdb2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,3 +68,5 @@ INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_I FOREACH(hfile ${HEADERS}) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${hfile} DESTINATION include/${PROJECT_NAME}) ENDFOREACH(hfile) + +ADD_SUBDIRECTORY(tests) diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index 80e7db4..4ec8ea2 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -11,6 +11,7 @@ BuildRequires: pkgconfig(glib-2.0) >= 2.44 BuildRequires: pkgconfig(gio-2.0) >= 2.44 BuildRequires: pkgconfig(gio-unix-2.0) BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(cmocka) BuildRequires: cmake Requires: /bin/cp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..bfd44ea --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,20 @@ +SET(TEST_DRIVER "test-driver") + +FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") + +SET(TEST_SUITE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +ADD_DEFINITIONS("-DTEST_SUITE_DIRECTORY=\"${TEST_SUITE_DIRECTORY}\"") + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED + cmocka) + +ADD_EXECUTABLE(${TEST_DRIVER} ${SRCS}) +TARGET_LINK_LIBRARIES(${TEST_DRIVER} cmocka dl) +SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "-Wl,--export-dynamic") + +ADD_CUSTOM_TARGET(run-test ALL "./${TEST_DRIVER}") +ADD_DEPENDENCIES(run-test ${TEST_DRIVER}) + +# add test suites +ADD_SUBDIRECTORY(libcommon) diff --git a/tests/libcommon/CMakeLists.txt b/tests/libcommon/CMakeLists.txt new file mode 100644 index 0000000..9fe27d3 --- /dev/null +++ b/tests/libcommon/CMakeLists.txt @@ -0,0 +1,16 @@ +SET(TEST_SUITE "test-driver-libcommon") + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) + +FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") +FILE(GLOB ORIG_SRCS "${CMAKE_SOURCE_DIR}/src/libcommon/*.c") + +ADD_LIBRARY(${TEST_SUITE} MODULE ${SRCS} ${ORIG_SRCS}) +SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${TEST_SUITE_DIRECTORY}) +ADD_DEPENDENCIES(${TEST_DRIVER} ${TEST_SUITE}) + +SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open") +SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") +SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LINK_FLAGS ${WRAP_FLAGS}) + +TARGET_LINK_LIBRARIES(${TEST_SUITE} cmocka glib-2.0) diff --git a/tests/libcommon/test-common.c b/tests/libcommon/test-common.c new file mode 100644 index 0000000..76e770c --- /dev/null +++ b/tests/libcommon/test-common.c @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "libcommon/file.h" +#include "libcommon/list.h" +#include "libcommon/ini-parser.h" +#include "../test-main.h" +#include "../test-mock.h" + +#define MOCK_FILE_PATH "testfile" +#define MOCK_FILE_LENGTH 100 + +static int setup(void **state) +{ + FILE *fp; + char cwd[128] = {0, }; + char path[256] = {0, }; + char str[MOCK_FILE_LENGTH]; + + memset(str, 't', MOCK_FILE_LENGTH); + + snprintf(path, sizeof(path), "%s/%s", getcwd(cwd, sizeof(cwd)), MOCK_FILE_PATH); + + fp = fopen(path, "w+"); + + if (!fp) { + printf("Failed to setup mockfile, %m\n"); + return -1; + } + + fwrite(str, 1, MOCK_FILE_LENGTH, fp); + fclose(fp); + + return 0; +} + +static int teardown(void **state) +{ + char cwd[128] = {0, }; + char path[256] = {0, }; + + snprintf(path, sizeof(path), "%s/%s", getcwd(cwd, sizeof(cwd)), MOCK_FILE_PATH); + + unlink(path); + + return 0; +} + +static void test_sys_get_str_p1(void **state) +{ + char buffer[MOCK_FILE_LENGTH * 2]; + int retval; + + /* remove possible null character */ + for (int i = 0; i < sizeof(buffer); ++i) + buffer[i] = 'x'; + + /* mocking open() syscall */ + mock_open(MOCK_FILE_PATH, O_RDONLY); + + /* this is example of how mock works */ + retval = sys_get_str("the pathname will be replaced to mocked one", + buffer, sizeof(buffer)); + + assert_int_equal(retval, 0); +} + +/* test small buffer */ +static void test_sys_get_str_n1(void **state) +{ + char buffer[MOCK_FILE_LENGTH / 2]; + int retval; + + /* remove possible null character */ + for (int i = 0; i < sizeof(buffer); ++i) + buffer[i] = 'x'; + + /* mocking open() syscall */ + mock_open(MOCK_FILE_PATH, O_RDONLY); + + /* this is example of how mock works */ + retval = sys_get_str("the pathname will be replaced to mocked one", + buffer, sizeof(buffer)); + + assert_int_equal(retval, -EIO); + assert_int_equal(buffer[0], 0); +} + +/* test no-exist file */ +static void test_sys_get_str_n2(void **state) +{ + char buffer[MOCK_FILE_LENGTH * 2]; + int retval; + + mock_open(MOCK_FILE_PATH, O_RDONLY); + + retval = sys_get_str("the pathname will be replaced to mocked one", + buffer, sizeof(buffer)); + + assert_int_equal(retval, -ENOENT); +} + +static void test_list_append_p(void **state) +{ + GList *head = NULL; + GList *elem, *elem2; + int i; + int value; + int length; + + for ( i = 0; i < 10; ++i) + SYS_G_LIST_APPEND(head, (intptr_t) i); + + length = SYS_G_LIST_LENGTH(head); + assert_int_equal(length, 10); + + i = 0; + SYS_G_LIST_FOREACH(head, elem, elem2) { + value = (int) elem2; + assert_int_equal(value, i++); + } +} + +static void test_list_prepend_p(void **state) +{ + GList *head = NULL; + GList *elem, *elem2; + int i; + int value; + int length; + + for ( i = 0; i < 10; ++i) + SYS_G_LIST_PREPEND(head, (intptr_t) i); + + length = SYS_G_LIST_LENGTH(head); + assert_int_equal(length, 10); + + i = 10; + SYS_G_LIST_FOREACH(head, elem, elem2) { + value = (int) elem2; + assert_int_equal(value, --i); + } +} + +static int check_user_data = 0x443; +static bool checked1; +static bool checked2; +static bool checked3; +static bool garbage; +static int test_parser(struct parse_result *result, void *data) +{ + assert_int_equal(data, check_user_data); + + if (MATCH(result->section, "Section1") && + MATCH(result->name, "Key1") && + MATCH(result->value, "Value1")) + checked1 = true; + else if (MATCH(result->section, "Section2") && + MATCH(result->name, "Key2") && + MATCH(result->value, "Value2")) + checked2 = true; + else if (MATCH(result->section, "Section2") && + MATCH(result->name, "Key3") && + MATCH(result->value, "Value3")) + checked3 = true; + else + garbage = true; + + return 0; +} + +static void test_config_parse_p(void **state) +{ + char cwd[128]; + char path[256]; + int retval; + + /* load predefined test.conf file */ + retval = snprintf(path, sizeof(path), "%s/libcommon/test.conf", getcwd(cwd, sizeof(cwd))); + if (retval >= sizeof(path)) + path[sizeof(path) - 1] = '\0'; + + config_parse(path, test_parser, (void *)(intptr_t) check_user_data); + + assert_true(checked1); + assert_true(checked2); + assert_true(checked3); + assert_false(garbage); +} + +int run_test_suite(void) +{ + const struct CMUnitTest testsuite[] = { + cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown), + cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown), + cmocka_unit_test(test_sys_get_str_n2), + cmocka_unit_test(test_list_append_p), + cmocka_unit_test(test_list_prepend_p), + cmocka_unit_test(test_config_parse_p), + }; + + return cmocka_run_group_tests(testsuite, NULL, NULL); +} diff --git a/tests/libcommon/test.conf b/tests/libcommon/test.conf new file mode 100644 index 0000000..c915ae1 --- /dev/null +++ b/tests/libcommon/test.conf @@ -0,0 +1,8 @@ +[Section1] +# this is comment +Key1=Value1 + +[Section2] +# this is comment +Key2=Value2 +Key3=Value3 diff --git a/tests/test-main.c b/tests/test-main.c new file mode 100644 index 0000000..0451498 --- /dev/null +++ b/tests/test-main.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +#include "test-main.h" + +int main(int argc, char *argv[]) +{ + int fail = 0; + DIR *dir; + struct dirent *ent; + + dir = opendir(TEST_SUITE_DIRECTORY); + if (!dir) { + printf("Failed to opendir, %m\n"); + return 1; + } + + while ((ent = readdir(dir))) { + void *handle; + char sopath[512] = {0, }; + int (*run_test_suite) (void); + + if (!strstr(ent->d_name, ".so")) + continue; + + snprintf(sopath, sizeof(sopath), "%s/%s", TEST_SUITE_DIRECTORY, ent->d_name); + + handle = dlopen(sopath, RTLD_LAZY); + if (!handle) { + printf("Failed to dlopen, %s\n", dlerror()); + continue; + } + + run_test_suite = dlsym(handle, "run_test_suite"); + if (!run_test_suite) { + printf("Failed to dlsym, %s\n", dlerror()); + dlclose(handle); + continue; + } + + fail += run_test_suite(); + } + + closedir(dir); + + return fail; +} diff --git a/tests/test-main.h b/tests/test-main.h new file mode 100644 index 0000000..4b04a85 --- /dev/null +++ b/tests/test-main.h @@ -0,0 +1,10 @@ +#ifndef __TEST_MAIN_H__ +#define __TEST_MAIN_H__ + +#include +#include +#include +#include + +#endif //__TEST_MAIN_H__ + diff --git a/tests/test-mock.c b/tests/test-mock.c new file mode 100644 index 0000000..4e7f260 --- /dev/null +++ b/tests/test-mock.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include "test-main.h" +#include "test-mock.h" + +//static FILE *__fp = NULL; + +int __wrap_open(const char *pathname, int flags) +{ + const char *mock_pathname; + int mock_flags; + + mock_pathname = mock_ptr_type(char *); + if (mock_pathname == NULL) { + errno = -ENOENT; + return -1; + } + + mock_flags = mock_type(int); + + return open(mock_pathname, mock_flags); +} + +int __wrap_open64(const char *pathname, int flags) +{ + return __wrap_open(pathname, flags); +} + + +void mock_open(const char *pathname, int flags) +{ + will_return(__wrap_open, pathname); + will_return(__wrap_open, flags); +} + +/* +FILE *__wrap_fopen(const char *pathname, const char *mode) +{ + const char *mock_pathname = mock_ptr_type(char *); + const char *mock_mode = mock_ptr_type(char *); + + __fp = fopen(mock_pathname, mock_mode); + + return __fp; +} + +FILE *__wrap_fopen64(const char *pathname, const char *mode) +{ + return __wrap_fopen(pathname, mode); + +} + +int __wrap_fclose(FILE *stream) +{ + return 0; +} + +int __wrap_fclose64(FILE *stream) +{ + return __wrap_fclose(stream); +} +*/ diff --git a/tests/test-mock.h b/tests/test-mock.h new file mode 100644 index 0000000..22c7196 --- /dev/null +++ b/tests/test-mock.h @@ -0,0 +1,8 @@ +#ifndef __TEST_MOCK_H__ +#define __TEST_MOCK_H__ + +#include + +void mock_open(const char *pathname, int flags); + +#endif //__TEST_MOCK_H__ -- 2.7.4 From 24159abc46b34ca5dad1541398b1beb5e3533440 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 8 Nov 2021 17:18:17 -0800 Subject: [PATCH 12/16] Fix svace issue Change-Id: I920fbb2f16b8af7cf164c58a18a1eb6ff2f1db9a Signed-off-by: Youngjae Cho --- tests/test-main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test-main.c b/tests/test-main.c index 0451498..5af3e94 100644 --- a/tests/test-main.c +++ b/tests/test-main.c @@ -42,6 +42,7 @@ int main(int argc, char *argv[]) } fail += run_test_suite(); + dlclose(handle); } closedir(dir); -- 2.7.4 From d01a2dc1a7f1db20d806b19e3a1c956e390fa26e Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 19 Oct 2021 14:26:02 +0900 Subject: [PATCH 13/16] Modify dbus object path Change-Id: I6377613b93197d45e7873004767a22ddbedb68a1 Signed-off-by: Unsung Lee --- src/libgdbus/dbus-iface-system.h | 3 ++- src/libgdbus/libgdbus.c | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libgdbus/dbus-iface-system.h b/src/libgdbus/dbus-iface-system.h index e65384a..8b8c27b 100644 --- a/src/libgdbus/dbus-iface-system.h +++ b/src/libgdbus/dbus-iface-system.h @@ -39,6 +39,7 @@ #define DBUS_BUS_NAME "org.freedesktop.DBus" #define DBUS_OBJECT_PATH "/org/freedesktop/DBus" #define DBUS_INTERFACE_NAME DBUS_BUS_NAME +#define DBUS_INTERFACE_PROPERTIES "org.freedesktop.DBus.Properties" /******************************************************************************* * @@ -338,4 +339,4 @@ /***********************************************/ -#endif \ No newline at end of file +#endif diff --git a/src/libgdbus/libgdbus.c b/src/libgdbus/libgdbus.c index 9295582..9eaa937 100644 --- a/src/libgdbus/libgdbus.c +++ b/src/libgdbus/libgdbus.c @@ -525,9 +525,9 @@ char **gdbus_get_owner_list(dbus_handle_h handle, const char *bus_name) } reply = g_dbus_connection_call_sync(dh->conn, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", + DBUS_BUS_NAME, + DBUS_OBJECT_PATH, + DBUS_INTERFACE_NAME, "ListQueuedOwners", g_variant_new("(s)", bus_name), NULL, @@ -2603,7 +2603,8 @@ int gdbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender) } reply = g_dbus_connection_call_sync(conn, - "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetConnectionUnixProcessID", + DBUS_BUS_NAME, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, + "GetConnectionUnixProcessID", g_variant_new("(s)", sender), NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, @@ -2813,7 +2814,7 @@ int check_systemd_active(void) ret = gdbus_call_sync_with_reply("org.freedesktop.systemd1", "/org/freedesktop/systemd1/unit/default_2etarget", - "org.freedesktop.DBus.Properties", + DBUS_INTERFACE_PROPERTIES, "Get", g_variant_new("(ss)", "org.freedesktop.systemd1.Unit", "ActiveState"), &reply); -- 2.7.4 From ccd9bb816cfb02a9ed6e21a726dba6d96526eff8 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 9 Dec 2021 17:14:37 +0900 Subject: [PATCH 14/16] Change interface DEVICED_INTERFACE_POWER Change-Id: I3c605793fb70e4948c8a95dbb2d4c1bf4b75b39f Signed-off-by: Youngjae Cho --- src/libgdbus/dbus-iface-system.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libgdbus/dbus-iface-system.h b/src/libgdbus/dbus-iface-system.h index 8b8c27b..a5b838e 100644 --- a/src/libgdbus/dbus-iface-system.h +++ b/src/libgdbus/dbus-iface-system.h @@ -69,7 +69,7 @@ #define DEVICED_INTERFACE_PASS DEVICED_INTERFACE_NAME".pass" /* Power service: set resetkey disable operations about power */ #define DEVICED_PATH_POWER DEVICED_OBJECT_PATH"/Power" -#define DEVICED_INTERFACE_POWER DEVICED_INTERFACE_NAME".power" +#define DEVICED_INTERFACE_POWER DEVICED_INTERFACE_NAME".Power" /* Storage service: get storage size operatioins about storage */ #define DEVICED_PATH_STORAGE DEVICED_OBJECT_PATH"/Storage" #define DEVICED_INTERFACE_STORAGE DEVICED_INTERFACE_NAME".storage" -- 2.7.4 From b953f57f3f6a1aa73d025e83f5d0422a0fd765a0 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 20 Dec 2021 16:46:29 +0900 Subject: [PATCH 15/16] file: add libsys_parse_cmdline_scanf() libsys_parse_cmdline_scanf() scans tokens of file /proc/cmdline. The parameters and return value have same semantics to those of scanf() family. Change-Id: Iac5838fe36b0ed2de36eb0412a13691ba0a50c3b Signed-off-by: Youngjae Cho --- src/libcommon/file.c | 27 +++++++++++++++++++++++++++ src/libcommon/file.h | 3 +++ 2 files changed, 30 insertions(+) diff --git a/src/libcommon/file.c b/src/libcommon/file.c index b3ca5f1..2b0c6ba 100644 --- a/src/libcommon/file.c +++ b/src/libcommon/file.c @@ -131,3 +131,30 @@ int sys_set_str(char *fname, char *val) return 0; } + +int libsys_parse_cmdline_scanf(const char *format, ...) +{ + FILE *fp = NULL; + char *token = NULL; + size_t len = 0; + va_list ap; + int ret = 0; + + fp = fopen("/proc/cmdline", "r"); + if (!fp) + return 0; + + va_start(ap, format); + + while (getdelim(&token, &len, ' ', fp) != EOF) { + ret = vsscanf(token, format, ap); + if (ret > 0) + break; + } + + free(token); + va_end(ap); + fclose(fp); + + return ret; +} diff --git a/src/libcommon/file.h b/src/libcommon/file.h index 1250da8..31df8fa 100644 --- a/src/libcommon/file.h +++ b/src/libcommon/file.h @@ -18,11 +18,14 @@ #ifndef __LIBCOMMON_FILE_H__ #define __LIBCOMMON_FILE_H__ +#include + int sys_read_buf(char *file, char *buf, int len); int sys_write_buf(char *file, char *buf); int sys_get_int(char *fname, int *val); int sys_get_str(char *fname, char *str, int len); int sys_set_int(char *fname, int val); int sys_set_str(char *fname, char *val); +int libsys_parse_cmdline_scanf(const char *format, ...); #endif /* __LIBCOMMON_FILE_H__ */ -- 2.7.4 From 0134007e65c5a07440380ce536a45746ac224482 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 23 Dec 2021 16:33:30 +0900 Subject: [PATCH 16/16] ini-parser: add libsys_config_parse_by_section() The original function, config_parse(), parses configuration file line by line. Due to this, a parser, which is registered by the config_parse(), can hardly handle out-of-ordered section/property or multiple sections with same name. The libsys_config_parse_by_section() now parses configuration file by section. Therefore a parser can now find whole properties of a section when the section is conveyed to the parser. Change-Id: I08f09e2f6aefabea497393edb3c1f88dbe2d8850 Signed-off-by: Youngjae Cho --- src/libcommon/ini-parser.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++ src/libcommon/ini-parser.h | 28 +++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/src/libcommon/ini-parser.c b/src/libcommon/ini-parser.c index f3ed8c7..a82192c 100644 --- a/src/libcommon/ini-parser.c +++ b/src/libcommon/ini-parser.c @@ -129,3 +129,95 @@ error: return ret; } +/* scan format after trimming leading/trailing whitespace, comment */ +static int sscanf_trim_whitespace_comment(const char *str, const char *format, ...) +{ + char trimmed_str[1024] = { 0, }; + va_list ap; + int ret; + + str += strspn(str, " \t"); + ret = sscanf(str, "%1023[^ \t\n\r#]", trimmed_str); + if (ret != 1) + return 0; + + va_start(ap, format); + ret = vsscanf(trimmed_str, format, ap); + va_end(ap); + + return ret; +} + +static void free_data(gpointer data) +{ + free(data); +} + +int libsys_config_parse_by_section(const char *fname, int cb(const struct parse_result *, void *), void *user_data) +{ + FILE *fp = NULL; + char *line = NULL; + size_t len = 0; + size_t n_read; + int ret = 0; + int retval; + struct parse_result result = { 0, }; + struct section_property *prop; + char tmp_sectname[128]; + char tmp_key[128]; + char tmp_value[128]; + + if (!fname || !cb) + return -EINVAL; + + fp = fopen(fname, "r"); + if (!fp) { + _E("Failed to open file '%s'.", fname); + return -errno; + } + + while (getline(&line, &len, fp) != EOF) { + // find section + retval = sscanf_trim_whitespace_comment(line, "[%127[^]]]", tmp_sectname); + if (retval != 1) + continue; + + result.section = strndup(tmp_sectname, sizeof(tmp_sectname)); + + // parse properties within the section + while((n_read = getline(&line, &len, fp)) != EOF) { + retval = sscanf_trim_whitespace_comment(line, "[%127[^]]]", tmp_sectname); + if (retval == 1) { // found next section. stop parsing properties + fseek(fp, -n_read, SEEK_CUR); + break; + } + + retval = sscanf_trim_whitespace_comment(line, "%127[^=]=%127s", tmp_key, tmp_value); + if (retval == 2) { // found property + prop = calloc(1, sizeof(struct section_property)); + if (!prop) + continue; + strncpy(prop->key, tmp_key, sizeof(prop->key) - 1); + strncpy(prop->value, tmp_value, sizeof(prop->value) - 1); + result.props = g_list_append(result.props, prop); + } + } + + ret = cb(&result, user_data); + + free(result.section); + result.section = NULL; + + g_list_free_full(result.props, free_data); + result.props = NULL; + + if (ret != 0) + break; + } + + if (fp) + fclose(fp); + free(line); + + return ret; +} diff --git a/src/libcommon/ini-parser.h b/src/libcommon/ini-parser.h index f30096c..7af9f42 100644 --- a/src/libcommon/ini-parser.h +++ b/src/libcommon/ini-parser.h @@ -20,13 +20,28 @@ #ifndef __INI_PARSER_H__ #define __INI_PARSER_H__ +#include + #define MATCH(a, b) (!strncmp(a, b, strlen(a))) #define SET_CONF(a, b) (a = (b > 0.0 ? b : a)) struct parse_result { char *section; - char *name; - char *value; + union { + // config_parse() + struct { + char *name; + char *value; + }; + + // config_parse_by_section() + GList *props; + }; +}; + +struct section_property { + char key[128]; + char value[128]; }; /** @@ -39,4 +54,13 @@ struct parse_result { int config_parse(const char *file_name, int cb(struct parse_result *result, void *user_data), void *user_data); + +/** + * @brief Parse config file and call callback\n + * @param[in] file_name conf file. + * @param[in] cb cb is called when conf file is parsed by section + * @param[in] user_data user data is passed to cb. + * @return 0 on success, negative if failed + */ +int libsys_config_parse_by_section(const char *file_name, int cb(const struct parse_result *, void *), void *user_data); #endif -- 2.7.4