X Tutup
Skip to content

Commit 385b2eb

Browse files
yuwatapoettering
authored andcommitted
sd-bus: make BUS_DEFAULT_TIMEOUT configurable
This adds sd_bus_{get,set}_method_call_timeout(). If the timeout is not set or set to 0, then the timeout value is parsed from $SYSTEMD_BUS_TIMEOUT= environment variable. If the environment variable is not set, then built-in timeout is used.
1 parent db9eee7 commit 385b2eb

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

doc/ENVIRONMENT.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ All tools:
3737
useful for debugging, in order to test generators and other code against
3838
specific kernel command lines.
3939

40+
* `$SYSTEMD_BUS_TIMEOUT=SECS` — specifies the maximum time to wait for method call
41+
completion. If no time unit is specified, assumes seconds. The usual other units
42+
are understood, too (us, ms, s, min, h, d, w, month, y). If it is not set or set
43+
to 0, then the built-in default is used.
44+
4045
systemctl:
4146

4247
* `$SYSTEMCTL_FORCE_BUS=1` — if set, do not connect to PID1's private D-Bus

src/libsystemd/libsystemd.sym

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,4 +573,6 @@ global:
573573

574574
LIBSYSTEMD_240 {
575575
sd_bus_message_readv;
576+
sd_bus_set_method_call_timeout;
577+
sd_bus_get_method_call_timeout;
576578
} LIBSYSTEMD_239;

src/libsystemd/sd-bus/bus-internal.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ struct sd_bus {
316316

317317
int *inotify_watches;
318318
size_t n_inotify_watches;
319+
320+
/* zero means use value specified by $SYSTEMD_BUS_TIMEOUT= environment variable or built-in default */
321+
usec_t method_call_timeout;
319322
};
320323

321324
/* For method calls we time-out at 25s, like in the D-Bus reference implementation */
@@ -333,8 +336,7 @@ struct sd_bus {
333336

334337
#define BUS_CONTAINER_DEPTH 128
335338

336-
/* Defined by the specification as maximum size of an array in
337-
* bytes */
339+
/* Defined by the specification as maximum size of an array in bytes */
338340
#define BUS_ARRAY_MAX_SIZE 67108864
339341

340342
#define BUS_FDS_MAX 1024
@@ -385,8 +387,7 @@ void bus_close_io_fds(sd_bus *b);
385387
_slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
386388

387389
/* If we are invoking callbacks of a bus object, ensure unreffing the
388-
* bus from the callback doesn't destroy the object we are working
389-
* on */
390+
* bus from the callback doesn't destroy the object we are working on */
390391
#define BUS_DONT_DESTROY(bus) \
391392
_cleanup_(sd_bus_unrefp) _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)
392393

src/libsystemd/sd-bus/bus-message.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5809,8 +5809,11 @@ int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) {
58095809
return r;
58105810

58115811
timeout = (*m)->timeout;
5812-
if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED))
5813-
timeout = BUS_DEFAULT_TIMEOUT;
5812+
if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)) {
5813+
r = sd_bus_get_method_call_timeout(bus, &timeout);
5814+
if (r < 0)
5815+
return r;
5816+
}
58145817

58155818
r = sd_bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout);
58165819
if (r < 0)

src/libsystemd/sd-bus/sd-bus.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,8 +1609,11 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
16091609
return 0;
16101610
}
16111611

1612-
if (timeout == 0)
1613-
timeout = BUS_DEFAULT_TIMEOUT;
1612+
if (timeout == 0) {
1613+
r = sd_bus_get_method_call_timeout(b, &timeout);
1614+
if (r < 0)
1615+
return r;
1616+
}
16141617

16151618
if (!m->sender && b->patch_sender) {
16161619
r = sd_bus_message_set_sender(m, b->patch_sender);
@@ -4073,3 +4076,36 @@ _public_ int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret) {
40734076
*ret = bus->wqueue_size;
40744077
return 0;
40754078
}
4079+
4080+
_public_ int sd_bus_set_method_call_timeout(sd_bus *bus, uint64_t usec) {
4081+
assert_return(bus, -EINVAL);
4082+
assert_return(bus = bus_resolve(bus), -ENOPKG);
4083+
4084+
bus->method_call_timeout = usec;
4085+
return 0;
4086+
}
4087+
4088+
_public_ int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret) {
4089+
const char *e;
4090+
usec_t usec;
4091+
4092+
assert_return(bus, -EINVAL);
4093+
assert_return(bus = bus_resolve(bus), -ENOPKG);
4094+
assert_return(ret, -EINVAL);
4095+
4096+
if (bus->method_call_timeout != 0) {
4097+
*ret = bus->method_call_timeout;
4098+
return 0;
4099+
}
4100+
4101+
e = secure_getenv("SYSTEMD_BUS_TIMEOUT");
4102+
if (e && parse_sec(e, &usec) >= 0 && usec != 0) {
4103+
/* Save the parsed value to avoid multiple parsing. To change the timeout value,
4104+
* use sd_bus_set_method_call_timeout() instead of setenv(). */
4105+
*ret = bus->method_call_timeout = usec;
4106+
return 0;
4107+
}
4108+
4109+
*ret = bus->method_call_timeout = BUS_DEFAULT_TIMEOUT;
4110+
return 0;
4111+
}

src/systemd/sd-bus.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ sd_event *sd_bus_get_event(sd_bus *bus);
205205
int sd_bus_get_n_queued_read(sd_bus *bus, uint64_t *ret);
206206
int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret);
207207

208+
int sd_bus_set_method_call_timeout(sd_bus *bus, uint64_t usec);
209+
int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret);
210+
208211
int sd_bus_add_filter(sd_bus *bus, sd_bus_slot **slot, sd_bus_message_handler_t callback, void *userdata);
209212
int sd_bus_add_match(sd_bus *bus, sd_bus_slot **slot, const char *match, sd_bus_message_handler_t callback, void *userdata);
210213
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);

0 commit comments

Comments
 (0)
X Tutup