From 5b1bad8d45319f45e5679d5d47be74e43ee344d3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 23 Dec 2016 22:56:39 +0100 Subject: run: fix race for "systemd-run --wait" D-Bus is inherently racy when a function returns an object path for a newly allocated object the client shall watch: as the object already exists before the client can subscribe to it, it might lose messages from it. Let's fix this, by explicitly querying unit properties right after subscribing to its property changes. Fixes: #4920 --- src/run/run.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/run/run.c b/src/run/run.c index 99f03465b0..3e3116f235 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -798,7 +798,7 @@ static void run_context_check_done(RunContext *c) { sd_event_exit(c->event, EXIT_SUCCESS); } -static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { +static int run_context_update(RunContext *c, const char *path) { static const struct bus_properties_map map[] = { { "ActiveState", "s", NULL, offsetof(RunContext, active_state) }, @@ -811,12 +811,11 @@ static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error {} }; - RunContext *c = userdata; int r; r = bus_map_all_properties(c->bus, "org.freedesktop.systemd1", - sd_bus_message_get_path(m), + path, map, c); if (r < 0) { @@ -828,6 +827,15 @@ static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error return 0; } +static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { + RunContext *c = userdata; + + assert(m); + assert(c); + + return run_context_update(c, sd_bus_message_get_path(m)); +} + static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) { RunContext *c = userdata; @@ -1027,6 +1035,10 @@ static int start_transient_service( r = sd_bus_attach_event(bus, c.event, 0); if (r < 0) return log_error_errno(r, "Failed to attach bus to event loop."); + + r = run_context_update(&c, path); + if (r < 0) + return r; } r = sd_event_loop(c.event); -- cgit v1.2.3-54-g00ecf