ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
803fb7
From 5532bedf8ef456f02fdec62d46bc1be65cdfde30 Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Tue, 28 Apr 2015 12:21:31 +0200
803fb7
Subject: [PATCH] run: synchronously wait until the scope unit we create is
803fb7
 started
803fb7
803fb7
Otherwise it might happen that by the time PID 1 adds our process to the
803fb7
scope unit the process might already have died, if the process is
803fb7
short-running (such as an invocation to /bin/true).
803fb7
803fb7
https://bugs.freedesktop.org/show_bug.cgi?id=86520
803fb7
803fb7
Cherry-picked from: de158ed22db60e3a6654557fa4aa72f7248550af
803fb7
Resolves: #1272368
803fb7
---
803fb7
 src/libsystemd/sd-bus/bus-util.c | 10 ++++++++++
803fb7
 src/libsystemd/sd-bus/bus-util.h |  1 +
803fb7
 src/run/run.c                    | 42 +++++++++++++++++++++++++++++++++-------
803fb7
 3 files changed, 46 insertions(+), 7 deletions(-)
803fb7
803fb7
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
803fb7
index e48abf55a..6d5615082 100644
803fb7
--- a/src/libsystemd/sd-bus/bus-util.c
803fb7
+++ b/src/libsystemd/sd-bus/bus-util.c
803fb7
@@ -1854,6 +1854,16 @@ int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
803fb7
         return set_put_strdup(d->jobs, path);
803fb7
 }
803fb7
 
803fb7
+int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
803fb7
+        int r;
803fb7
+
803fb7
+        r = bus_wait_for_jobs_add(d, path);
803fb7
+        if (r < 0)
803fb7
+                return log_oom();
803fb7
+
803fb7
+        return bus_wait_for_jobs(d, quiet);
803fb7
+}
803fb7
+
803fb7
 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes) {
803fb7
         const char *type, *path, *source;
803fb7
         int r;
803fb7
diff --git a/src/libsystemd/sd-bus/bus-util.h b/src/libsystemd/sd-bus/bus-util.h
803fb7
index 21db98228..8c8846c6e 100644
803fb7
--- a/src/libsystemd/sd-bus/bus-util.h
803fb7
+++ b/src/libsystemd/sd-bus/bus-util.h
803fb7
@@ -209,6 +209,7 @@ int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret);
803fb7
 void bus_wait_for_jobs_free(BusWaitForJobs *d);
803fb7
 int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path);
803fb7
 int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet);
803fb7
+int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet);
803fb7
 
803fb7
 DEFINE_TRIVIAL_CLEANUP_FUNC(BusWaitForJobs*, bus_wait_for_jobs_free);
803fb7
 
803fb7
diff --git a/src/run/run.c b/src/run/run.c
803fb7
index 0e5bde23d..dd1338f3b 100644
803fb7
--- a/src/run/run.c
803fb7
+++ b/src/run/run.c
803fb7
@@ -806,14 +806,20 @@ static int start_transient_scope(
803fb7
                 char **argv) {
803fb7
 
803fb7
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
803fb7
+        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
803fb7
         _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
803fb7
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
803fb7
         _cleanup_free_ char *scope = NULL;
803fb7
+        const char *object = NULL;
803fb7
         int r;
803fb7
 
803fb7
         assert(bus);
803fb7
         assert(argv);
803fb7
 
803fb7
+        r = bus_wait_for_jobs_new(bus, &w);
803fb7
+        if (r < 0)
803fb7
+                return log_oom();
803fb7
+
803fb7
         if (arg_unit) {
803fb7
                 scope = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
803fb7
                 if (!scope)
803fb7
@@ -854,7 +860,7 @@ static int start_transient_scope(
803fb7
         if (r < 0)
803fb7
                 return bus_log_create_error(r);
803fb7
 
803fb7
-        r = sd_bus_call(bus, m, 0, &error, NULL);
803fb7
+        r = sd_bus_call(bus, m, 0, &error, &reply);
803fb7
         if (r < 0) {
803fb7
                 log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
803fb7
                 return r;
803fb7
@@ -914,8 +920,16 @@ static int start_transient_scope(
803fb7
         if (!env)
803fb7
                 return log_oom();
803fb7
 
803fb7
+        r = sd_bus_message_read(reply, "o", &object);
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        r = bus_wait_for_jobs_one(w, object, arg_quiet);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
         if (!arg_quiet)
803fb7
-                log_info("Running as unit %s.", scope);
803fb7
+                log_info("Running scope as unit %s.", scope);
803fb7
 
803fb7
         execvpe(argv[0], argv, env);
803fb7
 
803fb7
@@ -927,13 +941,19 @@ static int start_transient_timer(
803fb7
                 char **argv) {
803fb7
 
803fb7
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
803fb7
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
803fb7
+        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
803fb7
         _cleanup_free_ char *timer = NULL, *service = NULL;
803fb7
+        const char *object = NULL;
803fb7
         int r;
803fb7
 
803fb7
         assert(bus);
803fb7
         assert(argv);
803fb7
 
803fb7
+        r = bus_wait_for_jobs_new(bus, &w);
803fb7
+        if (r < 0)
803fb7
+                return log_oom();
803fb7
+
803fb7
         if (arg_unit) {
803fb7
                 switch(unit_name_to_type(arg_unit)) {
803fb7
 
803fb7
@@ -1034,15 +1054,23 @@ static int start_transient_timer(
803fb7
         if (r < 0)
803fb7
                 return bus_log_create_error(r);
803fb7
 
803fb7
-        r = sd_bus_call(bus, m, 0, &error, NULL);
803fb7
+        r = sd_bus_call(bus, m, 0, &error, &reply);
803fb7
         if (r < 0) {
803fb7
                 log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
803fb7
                 return r;
803fb7
         }
803fb7
 
803fb7
-        log_info("Running as unit %s.", timer);
803fb7
+        r = sd_bus_message_read(reply, "o", &object);
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        r = bus_wait_for_jobs_one(w, object, arg_quiet);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
+        log_info("Running timer as unit %s.", timer);
803fb7
         if (argv[0])
803fb7
-                log_info("Will run as unit %s.", service);
803fb7
+                log_info("Will run service as unit %s.", service);
803fb7
 
803fb7
         return 0;
803fb7
 }