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", name, suffix, new_name);
220 ext = strrchr(name, '.');
222 _E("Wrong file name %s", name);
226 /* if ext is same as suffix */
227 if (ext && strcmp(ext, suffix) == 0) {
228 *new_name = strdup(name);
232 /* otherwise, make new unit name */
239 if ((len + strlen(suffix)) >= UNIT_NAME_MAX) {
240 _E("Name is too long:%d", (len + strlen(suffix)));
241 return -ENAMETOOLONG;
244 buf = (char *)malloc(sizeof(char) * (len + strlen(suffix) + 1));
246 _E("Failed to alloc mem");
250 ret = snprintf(buf, len + 1, "%s", name);
253 _E("Failed to snprintf %d", ret);
256 ret = snprintf(buf + len, strlen(suffix) + 1, "%s", suffix);
259 _E("Failed to snprintf %d", ret);
273 _systemd_control_unit
275 Start or Stop systemd unit.
276 1) Wait for started/stopped
277 - Send StartUnit/StopUnit Method call(sync)
278 reply:(o):/org/freedesktop/systemd1/job/[jobid]
279 - Wait JobRemoved signal from systemd
280 (uoss):(uint32 [jobid], objectpath '/org/freedesktop/systemd1/job/[jobid]', '[unit name]', '[result]')
282 - Send StartUnit/StopUnit Method call(sync)
284 @param name: unit name
285 @param suffix: (nullable): change extension of unit name, or %NULL
286 @param timeout_msec: the timeout in milliseconds, -1 to use the default
287 @param method: method name, "StartUnit" or "StopUnit"
289 Returns: the exit status
291 static int _systemd_control_unit(const char *name,
297 char *new_name = NULL;
300 if (!name || !method) {
301 _E("Wrong param name %s, method %s", name, method);
304 if (timeout_msec < -1) {
305 _E("wrong timeout. timeout(>=0 or -1)");
310 ret = _change_suffix(name, suffix, &new_name);
315 if (strlen(name) > UNIT_NAME_MAX) {
316 _E("Invalid name length %d(>%d)", strlen(name), UNIT_NAME_MAX);
322 ret = _systemd_control_unit_wait(method, name, timeout_msec);
324 ret = _systemd_control_unit_async(method, name);
332 int systemd_start_unit_wait_started(const char *name, const char *suffix, int timeout_msec)
334 return _systemd_control_unit(name, suffix, timeout_msec, "StartUnit", TRUE);
337 int systemd_stop_unit_wait_stopped(const char *name, const char *suffix, int timeout_msec)
339 return _systemd_control_unit(name, suffix, timeout_msec, "StopUnit", TRUE);
342 int systemd_start_unit_async(const char *name, const char *suffix)
344 return _systemd_control_unit(name, suffix, -1, "StartUnit", FALSE);
347 int systemd_stop_unit_async(const char *name, const char *suffix)
349 return _systemd_control_unit(name, suffix, -1, "StopUnit", FALSE);
352 #define SYSTEMD_UNIT_ESCAPE_CHAR ".-"
354 static char *systemd_get_unit_dbus_path(const char *unit)
358 size_t p, k, prefix_len, unit_len;
359 size_t path_len, len, escape;
363 unit_len = strlen(unit);
365 for (escape = 0, p = 0; p < unit_len; escape++) {
366 k = strcspn(unit + p, SYSTEMD_UNIT_ESCAPE_CHAR);
367 if (p + k >= unit_len)
372 prefix_len = strlen(SYSTEMD_DBUS_UNIT_PATH);
373 /* assume we try to get object path of foo-bar.service then
374 * the object path will be
375 * "/org/freedesktop/systemd1/unit/foo_2dbar_2eservice\n". In
376 * this case we can find two escape characters, one of escape
377 * char('-') is changed to three of char("_2d"). So the total
379 /* (PREFIX) + (unit - escape + 3*escape) + NULL */
380 path_len = prefix_len + (unit_len - escape)
381 + (escape * 3 * sizeof(char)) + 1;
382 path = (char *)calloc(path_len, sizeof(char));
386 strncpy(path, SYSTEMD_DBUS_UNIT_PATH, prefix_len);
387 for (i = 0, p = 0; i <= escape; i++) {
388 k = strcspn(unit + p, SYSTEMD_UNIT_ESCAPE_CHAR);
389 strncpy(path + prefix_len, unit + p, k);
390 if (k < strlen(unit + p)) {
391 len = path_len - (prefix_len + k);
392 snprintf(path + prefix_len + k, len,
393 "_%x", *(unit + p + k) & 0xff);
402 GVariant *systemd_get_manager_property(const char *property)
404 GVariant *reply = NULL;
405 GVariant *val = NULL;
410 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
412 DBUS_IFACE_DBUS_PROPERTIES,
414 g_variant_new("(ss)", SYSTEMD_DBUS_MANAGER_IFACE, property));
415 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
416 _E("Failed to get variant");
418 g_variant_unref(reply);
423 GVariant *systemd_get_unit_property(const char *unit, const char *property)
426 GVariant *reply = NULL;
427 GVariant *val = NULL;
429 if (!unit || !property)
432 escaped = systemd_get_unit_dbus_path(unit);
434 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
436 DBUS_IFACE_DBUS_PROPERTIES,
438 g_variant_new("(ss)", SYSTEMD_DBUS_UNIT_IFACE, property));
440 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
441 _E("Failed to get variant");
443 g_variant_unref(reply);
449 GVariant *systemd_get_service_property(const char *unit, const char *property)
452 GVariant *reply = NULL;
453 GVariant *val = NULL;
455 if (!unit || !property)
458 escaped = systemd_get_unit_dbus_path(unit);
460 reply = dbus_handle_method_sync_with_reply_var(SYSTEMD_DBUS_DEST,
462 DBUS_IFACE_DBUS_PROPERTIES,
464 g_variant_new("(ss)", SYSTEMD_DBUS_SERVICE_IFACE, property));
465 if (!reply || !dh_get_param_from_var(reply, "(v)", &val))
466 _E("Failed to get variant");
468 g_variant_unref(reply);