4 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <libgdbus/dbus-system.h>
26 #include "shared/log.h"
28 #define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
29 #define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1"
30 #define SYSTEMD_DBUS_UNIT_PATH "/org/freedesktop/systemd1/unit/"
32 #define SYSTEMD_DBUS_MANAGER_IFACE "org.freedesktop.systemd1.Manager"
33 #define SYSTEMD_DBUS_UNIT_IFACE "org.freedesktop.systemd1.Unit"
34 #define SYSTEMD_DBUS_SERVICE_IFACE "org.freedesktop.systemd1.Service"
35 #define SYSTEMD_DBUS_TARGET_IFACE "org.freedesktop.systemd1.Target"
37 #define DBUS_IFACE_DBUS_PROPERTIES "org.freedesktop.DBus.Properties"
39 #define SUFFIX_SERVICE ".service"
40 #define SUFFIX_SOCKET ".socket"
41 #define SUFFIX_BUSNAME ".busname"
42 #define SUFFIX_TARGET ".target"
43 #define SUFFIX_DEVICE ".device"
44 #define SUFFIX_MOUNT ".mount"
45 #define SUFFIX_SWAP ".swap"
46 #define SUFFIX_TIMER ".timer"
47 #define SUFFIX_PATH ".path"
48 #define SUFFIX_SLICE ".slice"
49 #define SUFFIX_SCOPE ".scope"
51 #define UNIT_NAME_MAX 256
55 const char *unit_name;
58 static void _cb_JobRemoved(GDBusConnection *conn,
68 gchar *unit_name = NULL;
69 unitinfo *uinfo = NULL;
72 _E("User data ctx is null");
76 uinfo = ctx->user_data;
78 _E("User_data uinfo is null");
81 if (!dh_get_param_from_var(param, "(uoss)", NULL, &job_id, &unit_name, NULL)) {
82 _E("Failed to get param");
85 if (strcmp(uinfo->job_id, job_id) || strcmp(uinfo->unit_name, unit_name)) {
86 _E("Not matched: job_id:%s, unit_name:%s", job_id, unit_name);
90 /* otherwise, if matched signal, quit loop */
92 ctx->quit_reason = CTX_QUIT_NORMAL;
93 if (ctx->timeout_src) {
94 g_source_destroy(ctx->timeout_src);
95 ctx->timeout_src = NULL;
98 g_main_loop_quit(ctx->loop);
105 static int _systemd_control_unit_wait(const char *method, const char *name, int timeout_msec)
107 GVariant *reply = NULL;
108 gchar *objpath = NULL;
114 ctx = dbus_handle_new_signal_ctx();
118 _I("Starting: %s %s", method, name);
120 /* synchronous signal subscription */
121 ret = subscribe_dbus_signal_ctx(NULL, ctx, SYSTEMD_DBUS_SERVICE, SYSTEMD_DBUS_PATH, SYSTEMD_DBUS_IFACE_MANAGER, "JobRemoved", _cb_JobRemoved);
127 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
129 SYSTEMD_DBUS_MANAGER_IFACE,
131 g_variant_new("(ss)", name, "replace"));
132 if (!reply || !dh_get_param_from_var(reply, "(o)", &objpath)) {
133 _E("fail (%s): no message", method);
138 uinfo.job_id = objpath;
139 uinfo.unit_name = name;
140 ctx->user_data = &uinfo;
143 ret = dbus_handle_signal_ctx_add_timeout(ctx, timeout_msec);
145 _E("Failed to set timeout, %d", ret);
149 /* run loop and wait signal callback */
150 quit_reason = dbus_handle_signal_ctx_wait(ctx);
151 if (quit_reason != CTX_QUIT_NORMAL) {
153 _E("Failed to receive JobRemoved signal %d", quit_reason);
157 _I("Finished: %s %s", method, name);
161 g_variant_unref(reply);
164 dbus_handle_free_signal_ctx(ctx);
169 static int _systemd_control_unit_async(const char *method, const char *name)
171 GVariant *reply = NULL;
172 gchar *objpath = NULL;
175 _I("Starting: %s %s", method, name);
177 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
179 SYSTEMD_DBUS_MANAGER_IFACE,
181 g_variant_new("(ss)", name, "replace"));
183 if (!reply || !dh_get_param_from_var(reply, "(o)", &objpath)) {
184 _E("fail (%s): no message", method);
189 _I("Finished: %s %s", method, name);
192 g_variant_unref(reply);
198 static int _change_suffix(const char *name, const char *suffix, char **new_name)
202 unsigned int len = 0;
205 if (!name || !suffix || !new_name) {
206 _E("Wrong param name:%s, suffix:%s, new_name:%s", name, suffix, new_name);
210 ext = strrchr(name, '.');
212 _E("Wrong file name %s", name);
216 /* if ext is same as suffix */
217 if (ext && strcmp(ext, suffix) == 0) {
218 *new_name = strdup(name);
222 /* otherwise, make new unit name */
229 if ((len + strlen(suffix)) >= UNIT_NAME_MAX) {
230 _E("Name is too long:%d", (len + strlen(suffix)));
231 return -ENAMETOOLONG;
234 buf = (char *)malloc(sizeof(char) * (len + strlen(suffix) + 1));
236 _E("Failed to alloc mem");
240 ret = snprintf(buf, len + 1, "%s", name);
243 _E("Failed to snprintf %d", ret);
246 ret = snprintf(buf + len, strlen(suffix) + 1, "%s", suffix);
249 _E("Failed to snprintf %d", ret);
263 _systemd_control_unit
265 Start or Stop systemd unit.
266 1) Wait for started/stopped
267 - Send StartUnit/StopUnit Method call(sync)
268 reply:(o):/org/freedesktop/systemd1/job/[jobid]
269 - Wait JobRemoved signal from systemd
270 (uoss):(uint32 [jobid], objectpath '/org/freedesktop/systemd1/job/[jobid]', '[unit name]', '[result]')
272 - Send StartUnit/StopUnit Method call(sync)
274 @param name: unit name
275 @param suffix: (nullable): change extension of unit name, or %NULL
276 @param timeout_msec: the timeout in milliseconds, -1 to use the default
277 @param method: method name, "StartUnit" or "StopUnit"
279 Returns: the exit status
281 static int _systemd_control_unit(const char *name, const char *suffix,
282 int timeout_msec, const char *method, int wait)
284 char *new_name = NULL;
287 if (!name || !method) {
288 _E("Wrong param name %s, method %s", name, method);
291 if (timeout_msec < -1) {
292 _E("wrong timeout. timeout(>=0 or -1)");
297 ret = _change_suffix(name, suffix, &new_name);
302 if (strlen(name) > UNIT_NAME_MAX) {
303 _E("Invalid name length %d(>%d)", strlen(name), UNIT_NAME_MAX);
309 ret = _systemd_control_unit_wait(method, name, timeout_msec);
311 ret = _systemd_control_unit_async(method, name);
319 int systemd_start_unit_wait_started(const char *name, const char *suffix, int timeout_msec)
321 return _systemd_control_unit(name, suffix, timeout_msec, "StartUnit", TRUE);
324 int systemd_stop_unit_wait_stopped(const char *name, const char *suffix, int timeout_msec)
326 return _systemd_control_unit(name, suffix, timeout_msec, "StopUnit", TRUE);
329 int systemd_start_unit_async(const char *name, const char *suffix)
331 return _systemd_control_unit(name, suffix, -1, "StartUnit", FALSE);
334 int systemd_stop_unit_async(const char *name, const char *suffix)
336 return _systemd_control_unit(name, suffix, -1, "StopUnit", FALSE);
339 #define SYSTEMD_UNIT_ESCAPE_CHAR ".-"
341 static char *systemd_get_unit_dbus_path(const char *unit)
345 size_t p, k, prefix_len, unit_len;
346 size_t path_len, len, escape;
350 unit_len = strlen(unit);
352 for (escape = 0, p = 0; p < unit_len; escape++) {
353 k = strcspn(unit + p, SYSTEMD_UNIT_ESCAPE_CHAR);
354 if (p + k >= unit_len)
359 prefix_len = strlen(SYSTEMD_DBUS_UNIT_PATH);
360 /* assume we try to get object path of foo-bar.service then
361 * the object path will be
362 * "/org/freedesktop/systemd1/unit/foo_2dbar_2eservice\n". In
363 * this case we can find two escape characters, one of escape
364 * char('-') is changed to three of char("_2d"). So the total
366 /* (PREFIX) + (unit - escape + 3*escape) + NULL */
367 path_len = prefix_len + (unit_len - escape)
368 + (escape * 3 * sizeof(char)) + 1;
369 path = (char *)calloc(path_len, sizeof(char));
373 strncpy(path, SYSTEMD_DBUS_UNIT_PATH, prefix_len);
374 for (i = 0, p = 0; i <= escape; i++) {
375 k = strcspn(unit + p, SYSTEMD_UNIT_ESCAPE_CHAR);
376 strncpy(path + prefix_len, unit + p, k);
377 if (k < strlen(unit + p)) {
378 len = path_len - (prefix_len + k);
379 snprintf(path + prefix_len + k, len,
380 "_%x", *(unit + p + k) & 0xff);
389 GVariant *systemd_get_manager_property(const char *property)
391 GVariant *reply = NULL;
392 GVariant *val = NULL;
397 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
399 DBUS_IFACE_DBUS_PROPERTIES,
401 g_variant_new("(ss)", SYSTEMD_DBUS_MANAGER_IFACE, property));
402 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
403 _E("Failed to get variant");
405 g_variant_unref(reply);
410 GVariant *systemd_get_unit_property(const char *unit,
411 const char *property)
414 GVariant *reply = NULL;
415 GVariant *val = NULL;
417 if (!unit || !property)
420 escaped = systemd_get_unit_dbus_path(unit);
422 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
424 DBUS_IFACE_DBUS_PROPERTIES,
426 g_variant_new("(ss)", SYSTEMD_DBUS_UNIT_IFACE, property));
428 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
429 _E("Failed to get variant");
431 g_variant_unref(reply);
437 GVariant *systemd_get_service_property(const char *unit,
438 const char *property)
441 GVariant *reply = NULL;
442 GVariant *val = NULL;
444 if (!unit || !property)
447 escaped = systemd_get_unit_dbus_path(unit);
449 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
451 DBUS_IFACE_DBUS_PROPERTIES,
453 g_variant_new("(ss)", SYSTEMD_DBUS_SERVICE_IFACE, property));
454 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
455 _E("Failed to get variant");
457 g_variant_unref(reply);