X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Futils-systemd.c;h=9187acc9c330b1eb2149e4d290a857cf1e450844;hb=50ab763bc31cb9e32bcb7e08e1f2be502fc50a5f;hp=df9b89f9f9f489b28d69cca2833a6a9c9a8d6da4;hpb=1a673de307982e7f2abfb362d2927e8e5e16be55;p=src%2Fapp-framework-main.git diff --git a/src/utils-systemd.c b/src/utils-systemd.c index df9b89f..9187acc 100644 --- a/src/utils-systemd.c +++ b/src/utils-systemd.c @@ -1,5 +1,5 @@ /* - Copyright 2017 IoT.bzh + Copyright (C) 2017-2019 IoT.bzh author: José Bollo @@ -34,6 +34,7 @@ struct sd_bus; struct sd_bus_message; typedef struct { const char *name; const char *message; } sd_bus_error; +# define sd_bus_unref(...) ((void)0) # define sd_bus_default_user(p) ((*(p)=NULL),(-ENOTSUP)) # define sd_bus_default_system(p) ((*(p)=NULL),(-ENOTSUP)) # define sd_bus_call_method(...) (-ENOTSUP) @@ -97,11 +98,12 @@ static int errno2sderr(int rc) return rc < 0 ? -errno : rc; } -/* Returns in 'ret' either the system bus (if isuser==0) +/* + * Returns in 'ret' either the system bus (if isuser==0) * or the user bus (if isuser!=0). * Returns 0 in case of success or -1 in case of error */ -static int get_bus(int isuser, struct sd_bus **ret) +int systemd_get_bus(int isuser, struct sd_bus **ret) { int rc; struct sd_bus *bus; @@ -125,6 +127,14 @@ error: return sderr2errno(rc); } +void systemd_set_bus(int isuser, struct sd_bus *bus) +{ + struct sd_bus **target = isuser ? &usrbus : &sysbus; + if (*target) + sd_bus_unref(*target); + *target = bus; +} + #if 0 /******************************************************************** * routines for escaping unit names to compute dbus path of units @@ -401,7 +411,7 @@ int systemd_daemon_reload(int isuser) struct sd_bus_message *ret = NULL; sd_bus_error err = SD_BUS_ERROR_NULL; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); if (rc >= 0) { /* TODO: asynchronous bind... */ /* TODO: more diagnostic... */ @@ -483,14 +493,14 @@ char *systemd_unit_dpath_by_name(int isuser, const char *name, int load) { struct sd_bus *bus; - return get_bus(isuser, &bus) < 0 ? NULL : get_unit_dpath(bus, name, load); + return systemd_get_bus(isuser, &bus) < 0 ? NULL : get_unit_dpath(bus, name, load); } char *systemd_unit_dpath_by_pid(int isuser, unsigned pid) { struct sd_bus *bus; - return get_bus(isuser, &bus) < 0 ? NULL : get_unit_dpath_by_pid(bus, pid); + return systemd_get_bus(isuser, &bus) < 0 ? NULL : get_unit_dpath_by_pid(bus, pid); } int systemd_unit_start_dpath(int isuser, const char *dpath) @@ -498,7 +508,7 @@ int systemd_unit_start_dpath(int isuser, const char *dpath) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); return rc < 0 ? rc : unit_start(bus, dpath); } @@ -507,7 +517,7 @@ int systemd_unit_restart_dpath(int isuser, const char *dpath) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); return rc < 0 ? rc : unit_restart(bus, dpath); } @@ -516,7 +526,7 @@ int systemd_unit_stop_dpath(int isuser, const char *dpath) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); return rc < 0 ? rc : unit_stop(bus, dpath); } @@ -525,7 +535,7 @@ int systemd_unit_start_name(int isuser, const char *name) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); if (rc >= 0) rc = unit_start_name(bus, name); return rc; @@ -536,7 +546,7 @@ int systemd_unit_restart_name(int isuser, const char *name) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); if (rc >= 0) rc = unit_restart_name(bus, name); return rc; @@ -547,7 +557,7 @@ int systemd_unit_stop_name(int isuser, const char *name) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); if (rc >= 0) rc = unit_stop_name(bus, name); return rc; @@ -559,7 +569,7 @@ int systemd_unit_stop_pid(int isuser, unsigned pid) struct sd_bus *bus; char *dpath; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); if (rc >= 0) { dpath = get_unit_dpath_by_pid(bus, pid); if (!dpath) @@ -577,7 +587,7 @@ int systemd_unit_pid_of_dpath(int isuser, const char *dpath) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); return rc < 0 ? rc : unit_pid(bus, dpath); } @@ -586,7 +596,7 @@ const char *systemd_unit_state_of_dpath(int isuser, const char *dpath) int rc; struct sd_bus *bus; - rc = get_bus(isuser, &bus); + rc = systemd_get_bus(isuser, &bus); return rc < 0 ? NULL : unit_state(bus, dpath); }