diff options
author | Paweł Szewczyk <p.szewczyk@samsung.com> | 2019-07-26 16:38:21 +0200 |
---|---|---|
committer | Yunmi Ha <yunmi.ha@samsung.com> | 2019-08-13 17:05:38 +0900 |
commit | b8e764834187c627f441c110fa4703d7bf6aedd0 (patch) | |
tree | 1464dcfbf099ab7e6bb390b1649fcc03a4c100e2 | |
parent | f4052c0d569d11eff756b88e9a8375ff403a29cd (diff) | |
download | activationd-b8e764834187c627f441c110fa4703d7bf6aedd0.tar.gz activationd-b8e764834187c627f441c110fa4703d7bf6aedd0.tar.bz2 activationd-b8e764834187c627f441c110fa4703d7bf6aedd0.zip |
Switch to libsyscommon in unit control API
Change-Id: I9e73046906fd9733f4c42fdba39d6a42cb652a60
Signed-off-by: Paweł Szewczyk <p.szewczyk@samsung.com>
-rw-r--r-- | include/unit_control_event.h | 6 | ||||
-rw-r--r-- | src/decision_makers/unit_control_dm.c | 92 | ||||
-rw-r--r-- | src/listeners/unit_control_api.c | 92 |
3 files changed, 77 insertions, 113 deletions
diff --git a/include/unit_control_event.h b/include/unit_control_event.h index b133ae4..401fa8b 100644 --- a/include/unit_control_event.h +++ b/include/unit_control_event.h @@ -20,7 +20,7 @@ #define UNIT_CONTROL_EVENT_H #include <time.h> -#include <systemd/sd-bus.h> +#include <gio/gio.h> #include "event.h" #define UC_EVENT_ID "unit_control" @@ -30,7 +30,7 @@ struct unit_control_event { struct epc_event event; - sd_bus_message *m; + GDBusMethodInvocation *m; int jobs_cnt; int err_code; char *method; @@ -39,7 +39,7 @@ struct unit_control_event { }; struct unit_control_event_data { - sd_bus_message *m; + GDBusMethodInvocation *m; int refs; int err_code; char *method; diff --git a/src/decision_makers/unit_control_dm.c b/src/decision_makers/unit_control_dm.c index 5811bd6..67e1a55 100644 --- a/src/decision_makers/unit_control_dm.c +++ b/src/decision_makers/unit_control_dm.c @@ -18,7 +18,7 @@ #include <malloc.h> #include <errno.h> #include <string.h> -#include <systemd/sd-bus.h> +#include <libsyscommon/dbus-system.h> #include "decision_made_event.h" #include "action.h" @@ -57,77 +57,51 @@ static int unit_control_event_match(struct epc_event_handler *handler, return 0; } -static int list_unit_by_pattern(const char *pattern, sd_bus_message **reply) +static int list_unit_by_pattern(const char *pattern, GVariant **reply) { assert(pattern); - __attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL; - __attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL; + GVariant *result; int ret; - sd_bus *bus = NULL; char *prefix = NULL; char *joined = NULL; - char *patterns[2] = {}; - char *unit_states[] = { "loaded", NULL }; - ret = epc_acquire_systemd_bus(&bus); - if (ret < 0) { - log_error_errno(ret, "failed to acquire the default system bus connection: %m"); - return -EINVAL; - } - - ret = sd_bus_message_new_method_call( - bus, - &m, + patterns[0] = (char *)pattern; + result = dbus_handle_method_sync_with_reply_var( SYSTEMD_SERVICE, SYSTEMD_OBJ, SYSTEMD_MANAGER_INTERFACE, - "ListUnitsByPatterns"); + "ListUnitsByPatterns", + g_variant_new("(asas)", (char **)unit_states, (char **)patterns)); if (ret < 0) { log_error_errno(ret, "failed to issue ListUnitsByPatterns() call - %m"); return ret; } - ret = sd_bus_message_append_strv(m, (char **)unit_states); - if (ret < 0) { - log_error_errno(ret, "failed to to setup arg 1 for ListUnitsByPatterns() call - %m"); - return ret; - } - - patterns[0] = (char *)pattern; - ret = sd_bus_message_append_strv(m, patterns); - if (ret < 0) { - log_error_errno(ret, "failed to to setup arg 2 for ListUnitsByPatterns() call - %m"); - goto cleanup; - } - - ret = sd_bus_call(bus, m, 0, &error, reply); - if (ret < 0) { - log_error("failed to issue ListUnitsByPatterns() call: %s\n", error.message); - goto cleanup; - } + *reply = result; return 0; + cleanup: free(prefix); free(joined); return ret; } -static int parse_unit_info(sd_bus_message *m, char **name) +static int parse_unit_info(GVariant *m, char **name) { assert(m); char *id; int ret; - ret = sd_bus_message_read( + g_variant_get( m, "(ssssssouso)", &id, @@ -142,16 +116,11 @@ static int parse_unit_info(sd_bus_message *m, char **name) NULL ); - if (ret < 0) { - log_error_errno(ret, " failed to fetch message contents - %m"); - return ret; - } - *name = id; return ret; } -static int parse_unit_list(sd_bus_message *reply, struct list_head *units) +static int parse_unit_list(GVariant *reply, struct list_head *units) { assert(reply); assert(units); @@ -159,19 +128,20 @@ static int parse_unit_list(sd_bus_message *reply, struct list_head *units) int ret = 0; char *name; struct unit_info *u; + GVariantIter iter; + GVariant *child; - ret = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); - if (ret < 0) { - log_error_errno(ret, "failed to parse ListUnitsByPatterns() response - %m"); - return ret; - } + g_variant_get(reply, "a(ssssssouso)", &iter); - while ((ret = parse_unit_info(reply, &name)) > 0) { + while ((child = g_variant_iter_next_value(&iter))) { + parse_unit_list(child, &name); u = calloc(1, sizeof(*u)); + if (!u) { log_error("Could not allocate unit_info object"); return -ENOMEM; } + u->unit = strdup(name); if (!u->unit) return -ENOMEM; @@ -184,19 +154,12 @@ static int parse_unit_list(sd_bus_message *reply, struct list_head *units) return ret; } - ret = sd_bus_message_exit_container(reply); - if (ret < 0) { - log_error_errno(ret, "failed to clanup parsing unit info - %m"); - return ret; - } - return ret; } static void unit_control_executed(struct decision_made_event *dm_ev, int result) { struct unit_control_event *event = to_unit_control_event(dm_ev->reason); - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; int ret; if (result < 0) @@ -205,12 +168,10 @@ static void unit_control_executed(struct decision_made_event *dm_ev, int result) event->jobs_cnt--; if (event->jobs_cnt == 0) { if (event->err_code < 0) { - sd_bus_error_set_errno(&error, event->err_code); - ret = sd_bus_reply_method_error(event->m, &error); + g_dbus_method_invocation_return_value(event->m, g_variant_new("(s)", "error")); } else { - ret = sd_bus_reply_method_return(event->m, "s", "ok"); + g_dbus_method_invocation_return_value(event->m, g_variant_new("(s)", "ok")); } - sd_bus_message_unref(event->m); } epc_event_unref(&event->event); @@ -286,12 +247,11 @@ static int unit_control_query_and_execute(const char *pattern, const char *comma assert(command); assert(ev); - __attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL; int ret, error_code = 0; struct list_head unit_list; struct unit_info *u, *next; struct unit_control_event *event = to_unit_control_event(ev); - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + GVariant *reply; INIT_LIST_HEAD(&unit_list); @@ -316,8 +276,7 @@ static int unit_control_query_and_execute(const char *pattern, const char *comma /* nothing more to do */ if (event->jobs_cnt == 0) { - sd_bus_error_set_errno(&error, ENOENT); - return sd_bus_reply_method_error(event->m, &error); + g_dbus_method_invocation_return_value(event->m, g_variant_new("(s)", "error")); } return 0; @@ -418,7 +377,6 @@ static int unit_control_make_decision(struct epc_event_handler *handler) bool matched = false; int error_code = 0; struct whitelist_entry *w; - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_free_ char *service_name = NULL; /* cannot use start with wildcards */ @@ -458,9 +416,7 @@ static int unit_control_make_decision(struct epc_event_handler *handler) cleanup: if (error_code != 0) { - sd_bus_error_set_errno(&error, error_code); - sd_bus_reply_method_error(ev->m, &error); - sd_bus_message_unref(ev->m); + g_dbus_method_invocation_return_value(ev->m, g_variant_new("(s)", "error")); } epc_event_unref(event); return 0; diff --git a/src/listeners/unit_control_api.c b/src/listeners/unit_control_api.c index 94d2aa9..79faad6 100644 --- a/src/listeners/unit_control_api.c +++ b/src/listeners/unit_control_api.c @@ -22,23 +22,21 @@ #include "event_processor.h" #include "systemd_dbus.h" #include "unit_control_event.h" +#include <libsyscommon/dbus-system.h> #define ACTD_DBUS_API_INTERFACE_NAME "org.tizen.activationd" #define ACTD_DBUS_API_SERVICE_NAME "org.tizen.activationd" #define ACTD_DBUS_API_OBJECT_PATH "/org/tizen/activationd" -static int method_generic_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error, const char *method) +static int method_generic_callback(GVariant *param, GDBusMethodInvocation *invocation, + void *userdata, const char *method) { - _cleanup_(sd_bus_error_free) sd_bus_error error_buf = SD_BUS_ERROR_NULL; int r; const char *s; struct unit_control_event_data uc_ev_data = {}; struct epc_event *ev; struct timespec ts; - if (!ret_error) - ret_error = &error_buf; - if (strcmp(method, "start") && strcmp(method, "stop") && strcmp(method, "restart")) { @@ -46,13 +44,15 @@ static int method_generic_callback(sd_bus_message *m, void *userdata, sd_bus_err goto fail; } - /* Read parameters */ - r = sd_bus_message_read(m, "s", &s); - if (r < 0) { - fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r)); + if (!g_variant_is_of_type(param, "(s)")) { + log_error("Invalid parameter type passed to %s method. " + "Expected: (s), got: %s\n", method, g_variant_get_type_string(param)); + r = -EINVAL; goto fail; } + g_variant_get(param, "(s)", &s); + if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { log_error_errno(errno, "Unable to get timestamp : %m"); r = -errno; @@ -74,8 +74,8 @@ static int method_generic_callback(sd_bus_message *m, void *userdata, sd_bus_err } /*we'll use it later for the response */ - sd_bus_message_ref(m); - uc_ev_data.m = m; + /* XXX ref(invocation) */ + uc_ev_data.m = invocation; r = epc_event_create(UC_EVENT_ID, &uc_ev_data, &ev); if (r) { @@ -91,43 +91,53 @@ static int method_generic_callback(sd_bus_message *m, void *userdata, sd_bus_err r = -EINVAL; goto fail; } - return 1; + + return NULL; + fail: - if (uc_ev_data.m) - sd_bus_message_unref(uc_ev_data.m); free(uc_ev_data.method); free(uc_ev_data.unit); - return sd_bus_error_set_errno(ret_error, -r); + return g_variant_new("(s)", "error"); } -static int method_start(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) +static GVariant *method_start(GDBusConnection *conn, + const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, + GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) { - return method_generic_callback(m, userdata, ret_error, "start"); + return method_generic_callback(param, invocation, user_data, "start"); } -static int method_stop(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) +static GVariant *method_stop(GDBusConnection *conn, + const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, + GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) { - return method_generic_callback(m, userdata, ret_error, "stop"); + return method_generic_callback(param, invocation, user_data, "stop"); } -static int method_restart(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) +static GVariant *method_restart(GDBusConnection *conn, + const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, + GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) { - return method_generic_callback(m, userdata, ret_error, "restart"); + return method_generic_callback(param, invocation, user_data, "restart"); } -static const sd_bus_vtable unit_control_api_vtable[] = { - SD_BUS_VTABLE_START(0), - SD_BUS_METHOD("Start", "s", "s", method_start, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Stop", "s", "s", method_stop, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Restart", "s", "s", method_restart, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_VTABLE_END +static const dbus_method_s unit_control_api_vtable[] = { + {"Start", "s", "s", method_start}, + {"Stop", "s", "s", method_stop}, + {"Restart", "s", "s", method_restart}, }; +static const dbus_interface_u unit_control_api_interface = { + .oh = NULL, + .name = ACTD_DBUS_API_INTERFACE_NAME, + .methods = unit_control_api_vtable, + .nr_methods = ARRAY_SIZE(unit_control_api_vtable), +}; struct unit_control_api { struct epc_module module; - sd_bus *bus; + dbus_handle_h bus; }; #define to_unit_control_api(MOD) \ @@ -135,44 +145,42 @@ struct unit_control_api { static void cleanup_listener(struct unit_control_api *l) { - if (l->bus) - sd_bus_release_name(l->bus, ACTD_DBUS_API_INTERFACE_NAME); } +static void actd_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + log_debug("Name acquired"); +} + + static int unit_control_api_init(struct epc_module *module, struct epc_config *config, sd_event* loop) { struct unit_control_api *listener = to_unit_control_api(module); int ret = 0; - sd_bus_slot *slot = NULL; - - listener->bus = NULL; - ret = epc_acquire_systemd_bus(&listener->bus); - if (ret < 0) { + listener->bus = dbus_handle_get_connection(G_BUS_TYPE_SYSTEM, 0); + if (!listener->bus < 0) { log_error_errno(ret, "Failed to acquire the default system bus connection: %m"); return -EINVAL; } - ret = sd_bus_add_object_vtable(listener->bus, - &slot, + ret = dbus_handle_register_dbus_object(listener->bus, ACTD_DBUS_API_OBJECT_PATH, - ACTD_DBUS_API_INTERFACE_NAME, - unit_control_api_vtable, - listener); - + &unit_control_api_interface); if (ret < 0) { fprintf(stderr, "Failed to register api calls: %s\n", strerror(-ret)); goto cleanup; } /* Take a well-known service name so that clients can find us */ - ret = sd_bus_request_name(listener->bus, ACTD_DBUS_API_SERVICE_NAME, 0); + ret = dbus_handle_request_bus_name(listener->bus, ACTD_DBUS_API_SERVICE_NAME, actd_dbus_name_acquired, NULL); if (ret < 0) { fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-ret)); goto cleanup; } + return 0; cleanup: |