X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Futils-systemd.c;h=9187acc9c330b1eb2149e4d290a857cf1e450844;hb=50ab763bc31cb9e32bcb7e08e1f2be502fc50a5f;hp=830fec6bda6d9ae52fc3d40dabd4df28a979311a;hpb=b6afa1aa893544b459cb767cc5a2ad8d2148228c;p=src%2Fapp-framework-main.git diff --git a/src/utils-systemd.c b/src/utils-systemd.c index 830fec6..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) @@ -57,8 +58,10 @@ static const char sdbi_unit[] = "org.freedesktop.systemd1.Unit"; static const char sdbi_service[] = "org.freedesktop.systemd1.Service"; static const char sdbm_reload[] = "Reload"; static const char sdbm_start_unit[] = "StartUnit"; +static const char sdbm_restart_unit[] = "RestartUnit"; static const char sdbm_stop_unit[] = "StopUnit"; static const char sdbm_start[] = "Start"; +static const char sdbm_restart[] = "Restart"; static const char sdbm_stop[] = "Stop"; static const char sdbm_get_unit[] = "GetUnit"; static const char sdbm_get_unit_by_pid[] = "GetUnitByPID"; @@ -95,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; @@ -123,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 @@ -289,6 +301,17 @@ static int unit_start(struct sd_bus *bus, const char *dpath) return rc; } +static int unit_restart(struct sd_bus *bus, const char *dpath) +{ + int rc; + struct sd_bus_message *ret = NULL; + sd_bus_error err = SD_BUS_ERROR_NULL; + + rc = sd_bus_call_method(bus, sdb_destination, dpath, sdbi_unit, sdbm_restart, &err, &ret, "s", "replace"); + sd_bus_message_unref(ret); + return rc; +} + static int unit_stop(struct sd_bus *bus, const char *dpath) { int rc; @@ -311,6 +334,17 @@ static int unit_start_name(struct sd_bus *bus, const char *name) return rc; } +static int unit_restart_name(struct sd_bus *bus, const char *name) +{ + int rc; + struct sd_bus_message *ret = NULL; + sd_bus_error err = SD_BUS_ERROR_NULL; + + rc = sd_bus_call_method(bus, sdb_destination, sdb_path, sdbi_manager, sdbm_restart_unit, &err, &ret, "ss", name, "replace"); + sd_bus_message_unref(ret); + return rc; +} + static int unit_stop_name(struct sd_bus *bus, const char *name) { int rc; @@ -377,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... */ @@ -459,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) @@ -474,16 +508,25 @@ 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); } +int systemd_unit_restart_dpath(int isuser, const char *dpath) +{ + int rc; + struct sd_bus *bus; + + rc = systemd_get_bus(isuser, &bus); + return rc < 0 ? rc : unit_restart(bus, dpath); +} + 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); } @@ -492,18 +535,29 @@ 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; } +int systemd_unit_restart_name(int isuser, const char *name) +{ + int rc; + struct sd_bus *bus; + + rc = systemd_get_bus(isuser, &bus); + if (rc >= 0) + rc = unit_restart_name(bus, name); + return rc; +} + 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; @@ -515,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) @@ -533,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); } @@ -542,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); }