62b301
From 3022d49ba2276eb5634d84a89a078aa3c357b70a Mon Sep 17 00:00:00 2001
62b301
From: Yu Watanabe <watanabe.yu+github@gmail.com>
62b301
Date: Fri, 13 Jul 2018 17:38:47 +0900
62b301
Subject: [PATCH] sd-bus: make BUS_DEFAULT_TIMEOUT configurable
62b301
62b301
This adds sd_bus_{get,set}_method_call_timeout().
62b301
If the timeout is not set or set to 0, then the timeout value is
62b301
parsed from $SYSTEMD_BUS_TIMEOUT= environment variable. If the
62b301
environment variable is not set, then built-in timeout is used.
62b301
62b301
(cherry picked from commit 385b2eb262a99373f09d01b7f5571dd71a14dc98)
62b301
62b301
Resolves: #2039461
62b301
---
62b301
 doc/ENVIRONMENT.md                   |  5 ++++
62b301
 src/libsystemd/libsystemd.sym        |  7 ++++-
62b301
 src/libsystemd/sd-bus/bus-internal.h |  9 ++++---
62b301
 src/libsystemd/sd-bus/bus-message.c  |  7 +++--
62b301
 src/libsystemd/sd-bus/sd-bus.c       | 40 ++++++++++++++++++++++++++--
62b301
 src/systemd/sd-bus.h                 |  3 +++
62b301
 6 files changed, 62 insertions(+), 9 deletions(-)
62b301
62b301
diff --git a/doc/ENVIRONMENT.md b/doc/ENVIRONMENT.md
62b301
index 8d7ce6ae2c..d1a79eaa4f 100644
62b301
--- a/doc/ENVIRONMENT.md
62b301
+++ b/doc/ENVIRONMENT.md
62b301
@@ -50,6 +50,11 @@ All tools:
62b301
   this only controls use of Unicode emoji glyphs, and has no effect on other
62b301
   Unicode glyphs.
62b301
 
62b301
+* `$SYSTEMD_BUS_TIMEOUT=SECS` — specifies the maximum time to wait for method call
62b301
+  completion. If no time unit is specified, assumes seconds. The usual other units
62b301
+  are understood, too (us, ms, s, min, h, d, w, month, y). If it is not set or set
62b301
+  to 0, then the built-in default is used.
62b301
+
62b301
 systemctl:
62b301
 
62b301
 * `$SYSTEMCTL_FORCE_BUS=1` — if set, do not connect to PID1's private D-Bus
62b301
diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym
62b301
index f4a1426248..3b55fc6473 100644
62b301
--- a/src/libsystemd/libsystemd.sym
62b301
+++ b/src/libsystemd/libsystemd.sym
62b301
@@ -573,12 +573,17 @@ global:
62b301
         sd_event_source_disable_unref;
62b301
 } LIBSYSTEMD_238;
62b301
 
62b301
+LIBSYSTEMD_240 {
62b301
+        sd_bus_set_method_call_timeout;
62b301
+        sd_bus_get_method_call_timeout;
62b301
+} LIBSYSTEMD_239;
62b301
+
62b301
 LIBSYSTEMD_248 {
62b301
 global:
62b301
         sd_event_source_set_ratelimit;
62b301
         sd_event_source_get_ratelimit;
62b301
         sd_event_source_is_ratelimited;
62b301
-} LIBSYSTEMD_239;
62b301
+} LIBSYSTEMD_240;
62b301
 
62b301
 LIBSYSTEMD_250 {
62b301
 global:
62b301
diff --git a/src/libsystemd/sd-bus/bus-internal.h b/src/libsystemd/sd-bus/bus-internal.h
62b301
index 06bd7862cb..88415ae678 100644
62b301
--- a/src/libsystemd/sd-bus/bus-internal.h
62b301
+++ b/src/libsystemd/sd-bus/bus-internal.h
62b301
@@ -319,6 +319,9 @@ struct sd_bus {
62b301
 
62b301
         int *inotify_watches;
62b301
         size_t n_inotify_watches;
62b301
+
62b301
+        /* zero means use value specified by $SYSTEMD_BUS_TIMEOUT= environment variable or built-in default */
62b301
+        usec_t method_call_timeout;
62b301
 };
62b301
 
62b301
 /* For method calls we time-out at 25s, like in the D-Bus reference implementation */
62b301
@@ -340,8 +343,7 @@ struct sd_bus {
62b301
 
62b301
 #define BUS_CONTAINER_DEPTH 128
62b301
 
62b301
-/* Defined by the specification as maximum size of an array in
62b301
- * bytes */
62b301
+/* Defined by the specification as maximum size of an array in bytes */
62b301
 #define BUS_ARRAY_MAX_SIZE 67108864
62b301
 
62b301
 #define BUS_FDS_MAX 1024
62b301
@@ -392,8 +394,7 @@ void bus_close_io_fds(sd_bus *b);
62b301
              _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
62b301
 
62b301
 /* If we are invoking callbacks of a bus object, ensure unreffing the
62b301
- * bus from the callback doesn't destroy the object we are working
62b301
- * on */
62b301
+ * bus from the callback doesn't destroy the object we are working on */
62b301
 #define BUS_DONT_DESTROY(bus) \
62b301
         _cleanup_(sd_bus_unrefp) _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)
62b301
 
62b301
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
62b301
index 7fe8929f82..3ffe559b8d 100644
62b301
--- a/src/libsystemd/sd-bus/bus-message.c
62b301
+++ b/src/libsystemd/sd-bus/bus-message.c
62b301
@@ -5882,8 +5882,11 @@ int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) {
62b301
                 return r;
62b301
 
62b301
         timeout = (*m)->timeout;
62b301
-        if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED))
62b301
-                timeout = BUS_DEFAULT_TIMEOUT;
62b301
+        if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)) {
62b301
+                r = sd_bus_get_method_call_timeout(bus, &timeout);
62b301
+                if (r < 0)
62b301
+                        return r;
62b301
+        }
62b301
 
62b301
         r = sd_bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout);
62b301
         if (r < 0)
62b301
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
62b301
index c65e24b2d1..803f3f50d6 100644
62b301
--- a/src/libsystemd/sd-bus/sd-bus.c
62b301
+++ b/src/libsystemd/sd-bus/sd-bus.c
62b301
@@ -1651,8 +1651,11 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
62b301
                 return 0;
62b301
         }
62b301
 
62b301
-        if (timeout == 0)
62b301
-                timeout = BUS_DEFAULT_TIMEOUT;
62b301
+        if (timeout == 0) {
62b301
+                r = sd_bus_get_method_call_timeout(b, &timeout);
62b301
+                if (r < 0)
62b301
+                        return r;
62b301
+        }
62b301
 
62b301
         if (!m->sender && b->patch_sender) {
62b301
                 r = sd_bus_message_set_sender(m, b->patch_sender);
62b301
@@ -4141,3 +4144,36 @@ _public_ int sd_bus_enqueue_for_read(sd_bus *bus, sd_bus_message *m) {
62b301
         bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
62b301
         return 0;
62b301
 }
62b301
+
62b301
+_public_ int sd_bus_set_method_call_timeout(sd_bus *bus, uint64_t usec) {
62b301
+        assert_return(bus, -EINVAL);
62b301
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
62b301
+
62b301
+        bus->method_call_timeout = usec;
62b301
+        return 0;
62b301
+}
62b301
+
62b301
+_public_ int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret) {
62b301
+        const char *e;
62b301
+        usec_t usec;
62b301
+
62b301
+        assert_return(bus, -EINVAL);
62b301
+        assert_return(bus = bus_resolve(bus), -ENOPKG);
62b301
+        assert_return(ret, -EINVAL);
62b301
+
62b301
+        if (bus->method_call_timeout != 0) {
62b301
+                *ret = bus->method_call_timeout;
62b301
+                return 0;
62b301
+        }
62b301
+
62b301
+        e = secure_getenv("SYSTEMD_BUS_TIMEOUT");
62b301
+        if (e && parse_sec(e, &usec) >= 0 && usec != 0) {
62b301
+                /* Save the parsed value to avoid multiple parsing. To change the timeout value,
62b301
+                 * use sd_bus_set_method_call_timeout() instead of setenv(). */
62b301
+                *ret = bus->method_call_timeout = usec;
62b301
+                return 0;
62b301
+        }
62b301
+
62b301
+        *ret = bus->method_call_timeout = BUS_DEFAULT_TIMEOUT;
62b301
+        return 0;
62b301
+}
62b301
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
62b301
index 9ba757b13d..8b6f70bd68 100644
62b301
--- a/src/systemd/sd-bus.h
62b301
+++ b/src/systemd/sd-bus.h
62b301
@@ -207,6 +207,9 @@ sd_event *sd_bus_get_event(sd_bus *bus);
62b301
 int sd_bus_get_n_queued_read(sd_bus *bus, uint64_t *ret);
62b301
 int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret);
62b301
 
62b301
+int sd_bus_set_method_call_timeout(sd_bus *bus, uint64_t usec);
62b301
+int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret);
62b301
+
62b301
 int sd_bus_add_filter(sd_bus *bus, sd_bus_slot **slot, sd_bus_message_handler_t callback, void *userdata);
62b301
 int sd_bus_add_match(sd_bus *bus, sd_bus_slot **slot, const char *match, sd_bus_message_handler_t callback, void *userdata);
62b301
 int sd_bus_add_match_async(sd_bus *bus, sd_bus_slot **slot, const char *match, sd_bus_message_handler_t callback, sd_bus_message_handler_t install_callback, void *userdata);