systemctl: refactor code to watch unit ActiveState a bit

let's split out one function, to make this reusable
This commit is contained in:
Lennart Poettering 2019-03-06 18:55:13 +01:00
parent ef9fafbd91
commit 9a1996455e

View file

@ -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);
r = wait_context_watch(wait_context, bus, 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);
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,