teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0258-run-synchronously-wait-until-the-scope-unit-we-creat.patch

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