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,
109 GVariant *reply = NULL;
110 gchar *objpath = NULL;
116 ctx = dbus_handle_new_signal_ctx();
120 _I("Starting: %s %s", method, name);
122 /* synchronous signal subscription */
123 ret = subscribe_dbus_signal_ctx(NULL,
125 SYSTEMD_DBUS_SERVICE,
127 SYSTEMD_DBUS_IFACE_MANAGER,
135 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
137 SYSTEMD_DBUS_MANAGER_IFACE,
139 g_variant_new("(ss)", name, "replace"));
140 if (!reply || !dh_get_param_from_var(reply, "(o)", &objpath)) {
141 _E("fail (%s): no message", method);
146 uinfo.job_id = objpath;
147 uinfo.unit_name = name;
148 ctx->user_data = &uinfo;
151 ret = dbus_handle_signal_ctx_add_timeout(ctx, timeout_msec);
153 _E("Failed to set timeout, %d", ret);
157 /* run loop and wait signal callback */
158 quit_reason = dbus_handle_signal_ctx_wait(ctx);
159 if (quit_reason != CTX_QUIT_NORMAL) {
161 _E("Failed to receive JobRemoved signal %d", quit_reason);
165 _I("Finished: %s %s", method, name);
169 g_variant_unref(reply);
172 dbus_handle_free_signal_ctx(ctx);
177 static int _systemd_control_unit_async(const char *method, const char *name)
179 GVariant *reply = NULL;
180 gchar *objpath = NULL;
183 _I("Starting: %s %s", method, name);
185 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
187 SYSTEMD_DBUS_MANAGER_IFACE,
189 g_variant_new("(ss)", name, "replace"));
191 if (!reply || !dh_get_param_from_var(reply, "(o)", &objpath)) {
192 _E("fail (%s): no message", method);
197 _I("Finished: %s %s", method, name);
200 g_variant_unref(reply);
206 static int _change_suffix(const char *name,
212 unsigned int len = 0;
215 if (!name || !suffix || !new_name) {
216 _E("Wrong param name:%s, suffix:%s, new_name:%s (null)",
217 name ? : "(null)", suffix ? : "(null)", new_name ? "not" : "");
221 ext = strrchr(name, '.');
223 _E("Wrong file name %s", name);
227 /* if ext is same as suffix */
228 if (ext && strcmp(ext, suffix) == 0) {
229 *new_name = strdup(name);
233 /* otherwise, make new unit name */
240 if ((len + strlen(suffix)) >= UNIT_NAME_MAX) {
241 _E("Name is too long:%d", (len + strlen(suffix)));
242 return -ENAMETOOLONG;
245 buf = (char *)malloc(sizeof(char) * (len + strlen(suffix) + 1));
247 _E("Failed to alloc mem");
251 ret = snprintf(buf, len + 1, "%s", name);
254 _E("Failed to snprintf %d", ret);
257 ret = snprintf(buf + len, strlen(suffix) + 1, "%s", suffix);
260 _E("Failed to snprintf %d", ret);
274 _systemd_control_unit
276 Start or Stop systemd unit.
277 1) Wait for started/stopped
278 - Send StartUnit/StopUnit Method call(sync)
279 reply:(o):/org/freedesktop/systemd1/job/[jobid]
280 - Wait JobRemoved signal from systemd
281 (uoss):(uint32 [jobid], objectpath '/org/freedesktop/systemd1/job/[jobid]', '[unit name]', '[result]')
283 - Send StartUnit/StopUnit Method call(sync)
285 @param name: unit name
286 @param suffix: (nullable): change extension of unit name, or %NULL
287 @param timeout_msec: the timeout in milliseconds, -1 to use the default
288 @param method: method name, "StartUnit" or "StopUnit"
290 Returns: the exit status
292 static int _systemd_control_unit(const char *name,
298 char *new_name = NULL;
301 if (!name || !method) {
302 _E("Wrong param name %s, method %s", name ? : "(null)", method ? : "(null)");
305 if (timeout_msec < -1) {
306 _E("wrong timeout. timeout(>=0 or -1)");
311 ret = _change_suffix(name, suffix, &new_name);
316 if (strlen(name) > UNIT_NAME_MAX) {
317 _E("Invalid name length %d(>%d)", strlen(name), UNIT_NAME_MAX);
323 ret = _systemd_control_unit_wait(method, name, timeout_msec);
325 ret = _systemd_control_unit_async(method, name);
333 int systemd_start_unit_wait_started(const char *name, const char *suffix, int timeout_msec)
335 return _systemd_control_unit(name, suffix, timeout_msec, "StartUnit", TRUE);
338 int systemd_stop_unit_wait_stopped(const char *name, const char *suffix, int timeout_msec)
340 return _systemd_control_unit(name, suffix, timeout_msec, "StopUnit", TRUE);
343 int systemd_start_unit_async(const char *name, const char *suffix)
345 return _systemd_control_unit(name, suffix, -1, "StartUnit", FALSE);
348 int systemd_stop_unit_async(const char *name, const char *suffix)
350 return _systemd_control_unit(name, suffix, -1, "StopUnit", FALSE);
353 #define SYSTEMD_UNIT_ESCAPE_CHAR ".-"
355 static char *systemd_get_unit_dbus_path(const char *unit)
359 size_t p, k, prefix_len, unit_len;
360 size_t path_len, len, escape;
364 unit_len = strlen(unit);
366 for (escape = 0, p = 0; p < unit_len; escape++) {
367 k = strcspn(unit + p, SYSTEMD_UNIT_ESCAPE_CHAR);
368 if (p + k >= unit_len)
373 prefix_len = strlen(SYSTEMD_DBUS_UNIT_PATH);
374 /* assume we try to get object path of foo-bar.service then
375 * the object path will be
376 * "/org/freedesktop/systemd1/unit/foo_2dbar_2eservice\n". In
377 * this case we can find two escape characters, one of escape
378 * char('-') is changed to three of char("_2d"). So the total
380 /* (PREFIX) + (unit - escape + 3*escape) + NULL */
381 path_len = prefix_len + (unit_len - escape)
382 + (escape * 3 * sizeof(char)) + 1;
383 path = (char *)calloc(path_len, sizeof(char));
387 strncpy(path, SYSTEMD_DBUS_UNIT_PATH, prefix_len);
388 for (i = 0, p = 0; i <= escape; i++) {
389 k = strcspn(unit + p, SYSTEMD_UNIT_ESCAPE_CHAR);
390 strncpy(path + prefix_len, unit + p, k);
391 if (k < strlen(unit + p)) {
392 len = path_len - (prefix_len + k);
393 snprintf(path + prefix_len + k, len,
394 "_%x", *(unit + p + k) & 0xff);
403 GVariant *systemd_get_manager_property(const char *property)
405 GVariant *reply = NULL;
406 GVariant *val = NULL;
411 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
413 DBUS_IFACE_DBUS_PROPERTIES,
415 g_variant_new("(ss)", SYSTEMD_DBUS_MANAGER_IFACE, property));
416 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
417 _E("Failed to get variant");
419 g_variant_unref(reply);
424 GVariant *systemd_get_unit_property(const char *unit, const char *property)
427 GVariant *reply = NULL;
428 GVariant *val = NULL;
430 if (!unit || !property)
433 escaped = systemd_get_unit_dbus_path(unit);
435 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
437 DBUS_IFACE_DBUS_PROPERTIES,
439 g_variant_new("(ss)", SYSTEMD_DBUS_UNIT_IFACE, property));
441 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
442 _E("Failed to get variant");
444 g_variant_unref(reply);
450 GVariant *systemd_get_service_property(const char *unit, const char *property)
453 GVariant *reply = NULL;
454 GVariant *val = NULL;
456 if (!unit || !property)
459 escaped = systemd_get_unit_dbus_path(unit);
461 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
463 DBUS_IFACE_DBUS_PROPERTIES,
465 g_variant_new("(ss)", SYSTEMD_DBUS_SERVICE_IFACE, property));
466 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
467 _E("Failed to get variant");
469 g_variant_unref(reply);