diff options
author | Lennart Poettering <lennart@poettering.net> | 2019-03-06 18:55:13 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2019-03-13 17:38:43 +0100 |
commit | 9a1996455e4761ae78b204299086243eff97b9a6 (patch) | |
tree | 25b4c4a16f2f67f1da0c3068b32e9d4239a874b3 /src | |
parent | ef9fafbd9143a1d6278f65ce857735ad08c79f66 (diff) | |
download | systemd-9a1996455e4761ae78b204299086243eff97b9a6.tar.gz systemd-9a1996455e4761ae78b204299086243eff97b9a6.tar.bz2 systemd-9a1996455e4761ae78b204299086243eff97b9a6.zip |
systemctl: refactor code to watch unit ActiveState a bit
let's split out one function, to make this reusable
Diffstat (limited to 'src')
-rw-r--r-- | src/systemctl/systemctl.c | 87 |
1 files changed, 52 insertions, 35 deletions
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 23818a0db1..7814d8313c 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -2893,6 +2893,56 @@ static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error return 0; } +static int wait_context_watch( + WaitContext *wait_context, + sd_bus *bus, + const char *name) { + + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_free_ char *unit_path = NULL; + int r; + + assert(wait_context); + assert(name); + + log_debug("Watching for property changes of %s", name); + r = sd_bus_call_method( + bus, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "RefUnit", + &error, + NULL, + "s", name); + if (r < 0) + return log_error_errno(r, "Failed to add reference to unit %s: %s", name, bus_error_message(&error, r)); + + unit_path = unit_dbus_path_from_name(name); + if (!unit_path) + return log_oom(); + + r = set_ensure_allocated(&wait_context->unit_paths, &string_hash_ops); + if (r < 0) + return log_oom(); + + r = set_put_strdup(wait_context->unit_paths, unit_path); + if (r < 0) + return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path); + + r = sd_bus_match_signal_async(bus, + &wait_context->match, + NULL, + unit_path, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + on_properties_changed, NULL, wait_context); + if (r < 0) + return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); + + return 0; +} + static int start_unit_one( sd_bus *bus, const char *method, @@ -2912,38 +2962,9 @@ static int start_unit_one( assert(error); if (wait_context) { - _cleanup_free_ char *unit_path = NULL; - - log_debug("Watching for property changes of %s", name); - r = sd_bus_call_method( - bus, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", - "RefUnit", - error, - NULL, - "s", name); - if (r < 0) - return log_error_errno(r, "Failed to RefUnit %s: %s", name, bus_error_message(error, r)); - - unit_path = unit_dbus_path_from_name(name); - if (!unit_path) - return log_oom(); - - r = set_put_strdup(wait_context->unit_paths, unit_path); - if (r < 0) - return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path); - - r = sd_bus_match_signal_async(bus, - &wait_context->match, - NULL, - unit_path, - "org.freedesktop.DBus.Properties", - "PropertiesChanged", - on_properties_changed, NULL, wait_context); + r = wait_context_watch(wait_context, bus, name); if (r < 0) - return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); + return r; } log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", @@ -3164,10 +3185,6 @@ static int start_unit(int argc, char *argv[], void *userdata) { } if (arg_wait) { - wait_context.unit_paths = set_new(&string_hash_ops); - if (!wait_context.unit_paths) - return log_oom(); - r = sd_bus_call_method_async( bus, NULL, |