anitazha / rpms / systemd

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