X Tutup
Skip to content

Commit c4e4803

Browse files
committed
sd-bus: make "close+flush-on-exit" optional when using sd-event with sd-bus
This adds a new pair of API calls sd_bus_set_close_on_exit() and sd_bus_get_close_on_exit(). They control whether an sd_bus object attached to a an sd-event loop shall automatically be flushed/closed when the event loop goes down. Usually that's a good thing, except for very few cases where the bus connection is longer living than the event loop it is attached on. Specifically, this is the case for nspawn, where we run the event loop only while the container is up, but afterwards still want to be able to use the bus connection.
1 parent e5c3629 commit c4e4803

File tree

7 files changed

+145
-11
lines changed

7 files changed

+145
-11
lines changed

man/rules/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ manpages = [
289289
'sd_bus_release_name_async',
290290
'sd_bus_request_name_async'],
291291
''],
292+
['sd_bus_set_close_on_exit', '3', ['sd_bus_get_close_on_exit'], ''],
292293
['sd_bus_set_connected_signal', '3', ['sd_bus_get_connected_signal'], ''],
293294
['sd_bus_set_description',
294295
'3',

man/sd-bus.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<citerefentry><refentrytitle>sd_bus_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8585
<citerefentry><refentrytitle>sd_bus_set_sender</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8686
<citerefentry><refentrytitle>sd_bus_set_watch_bind</refentrytitle><manvolnum>3</manvolnum></citerefentry>
87+
<citerefentry><refentrytitle>sd_bus_set_close_on_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>
8788
<citerefentry><refentrytitle>sd_bus_slot_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8889
<citerefentry><refentrytitle>sd_bus_slot_set_destroy_callback</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8990
<citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>,

man/sd_bus_set_close_on_exit.xml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version='1.0'?>
2+
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3+
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4+
5+
<!--
6+
SPDX-License-Identifier: LGPL-2.1+
7+
-->
8+
9+
<refentry id="sd_bus_set_close_on_exit"
10+
xmlns:xi="http://www.w3.org/2001/XInclude">
11+
12+
<refentryinfo>
13+
<title>sd_bus_set_close_on_exit</title>
14+
<productname>systemd</productname>
15+
</refentryinfo>
16+
17+
<refmeta>
18+
<refentrytitle>sd_bus_set_close_on_exit</refentrytitle>
19+
<manvolnum>3</manvolnum>
20+
</refmeta>
21+
22+
<refnamediv>
23+
<refname>sd_bus_set_close_on_exit</refname>
24+
<refname>sd_bus_get_close_on_exit</refname>
25+
26+
<refpurpose>Control whether to close the bus connection during the event loop exit phase</refpurpose>
27+
</refnamediv>
28+
29+
<refsynopsisdiv>
30+
<funcsynopsis>
31+
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
32+
33+
<funcprototype>
34+
<funcdef>int <function>sd_bus_set_close_on_exit</function></funcdef>
35+
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
36+
<paramdef>int <parameter>b</parameter></paramdef>
37+
</funcprototype>
38+
39+
<funcprototype>
40+
<funcdef>int <function>sd_bus_get_close_on_exit</function></funcdef>
41+
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
42+
</funcprototype>
43+
44+
</funcsynopsis>
45+
</refsynopsisdiv>
46+
47+
<refsect1>
48+
<title>Description</title>
49+
50+
<para><function>sd_bus_set_close_on_exit()</function> may be used to enable or disable whether the bus connection
51+
is automatically flushed (as in
52+
<citerefentry><refentrytitle>sd_bus_flush</refentrytitle><manvolnum>3</manvolnum></citerefentry>) and closed (as in
53+
<citerefentry><refentrytitle>sd_bus_close</refentrytitle><manvolnum>3</manvolnum></citerefentry>) during the exit
54+
phase of the event loop. This logic only applies to bus connections that are attached to an
55+
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry> event loop, see
56+
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>. By default
57+
this mechanism is enabled and makes sure that any pending messages that have not been written to the bus connection
58+
are written out when the event loop is shutting down. In some cases this behaviour is not desirable, for example
59+
when the bus connection shall remain usable until after the event loop exited. If <parameter>b</parameter> is
60+
true, the feature is enabled (which is the default), otherwise disabled.</para>
61+
62+
<para><function>sd_bus_get_close_on_exit()</function> may be used to query the current setting of this feature. It
63+
returns zero when the feature is disabled, and positive if enabled.</para>
64+
</refsect1>
65+
66+
<refsect1>
67+
<title>Return Value</title>
68+
69+
<para>On success, <function>sd_bus_set_close_on_exit()</function> returns 0 or a positive integer. On failure, it returns a negative errno-style
70+
error code.</para>
71+
72+
<para><function>sd_bus_get_close_on_exit()</function> returns 0 if the feature is currently turned off or a
73+
positive integer if it is on. On failure, it returns a negative errno-style error code.</para>
74+
</refsect1>
75+
76+
<refsect1>
77+
<title>Errors</title>
78+
79+
<para>Returned errors may indicate the following problems:</para>
80+
81+
<variablelist>
82+
<varlistentry>
83+
<term><constant>-ECHILD</constant></term>
84+
85+
<listitem><para>The bus connection has been created in a different process.</para></listitem>
86+
</varlistentry>
87+
</variablelist>
88+
</refsect1>
89+
90+
<xi:include href="libsystemd-pkgconfig.xml" />
91+
92+
<refsect1>
93+
<title>See Also</title>
94+
95+
<para>
96+
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
97+
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
98+
<citerefentry><refentrytitle>sd_bus_flush</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
99+
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
100+
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
101+
<citerefentry><refentrytitle>sd_event_add_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>
102+
</para>
103+
</refsect1>
104+
105+
</refentry>

src/libsystemd/libsystemd.sym

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@ global:
579579

580580
sd_bus_error_move;
581581

582+
sd_bus_set_close_on_exit;
583+
sd_bus_get_close_on_exit;
584+
582585
sd_device_ref;
583586
sd_device_unref;
584587

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ struct sd_bus {
211211
bool accept_fd:1;
212212
bool attach_timestamp:1;
213213
bool connected_signal:1;
214+
bool close_on_exit:1;
214215

215216
int use_memfd;
216217

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,22 @@ _public_ int sd_bus_new(sd_bus **ret) {
232232

233233
assert_return(ret, -EINVAL);
234234

235-
b = new0(sd_bus, 1);
235+
b = new(sd_bus, 1);
236236
if (!b)
237237
return -ENOMEM;
238238

239-
b->n_ref = REFCNT_INIT;
240-
b->input_fd = b->output_fd = -1;
241-
b->inotify_fd = -1;
242-
b->message_version = 1;
243-
b->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
244-
b->accept_fd = true;
245-
b->original_pid = getpid_cached();
246-
b->n_groups = (size_t) -1;
239+
*b = (sd_bus) {
240+
.n_ref = REFCNT_INIT,
241+
.input_fd = -1,
242+
.output_fd = -1,
243+
.inotify_fd = -1,
244+
.message_version = 1,
245+
.creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME,
246+
.accept_fd = true,
247+
.original_pid = getpid_cached(),
248+
.n_groups = (size_t) -1,
249+
.close_on_exit = true,
250+
};
247251

248252
assert_se(pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0);
249253

@@ -3409,8 +3413,10 @@ static int quit_callback(sd_event_source *event, void *userdata) {
34093413

34103414
assert(event);
34113415

3412-
sd_bus_flush(bus);
3413-
sd_bus_close(bus);
3416+
if (bus->close_on_exit) {
3417+
sd_bus_flush(bus);
3418+
sd_bus_close(bus);
3419+
}
34143420

34153421
return 1;
34163422
}
@@ -4135,3 +4141,18 @@ _public_ int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret) {
41354141
*ret = bus->method_call_timeout = BUS_DEFAULT_TIMEOUT;
41364142
return 0;
41374143
}
4144+
4145+
_public_ int sd_bus_set_close_on_exit(sd_bus *bus, int b) {
4146+
assert_return(bus, -EINVAL);
4147+
assert_return(bus = bus_resolve(bus), -ENOPKG);
4148+
4149+
bus->close_on_exit = b;
4150+
return 0;
4151+
}
4152+
4153+
_public_ int sd_bus_get_close_on_exit(sd_bus *bus) {
4154+
assert_return(bus, -EINVAL);
4155+
assert_return(bus = bus_resolve(bus), -ENOPKG);
4156+
4157+
return bus->close_on_exit;
4158+
}

src/systemd/sd-bus.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b);
154154
int sd_bus_get_allow_interactive_authorization(sd_bus *bus);
155155
int sd_bus_set_exit_on_disconnect(sd_bus *bus, int b);
156156
int sd_bus_get_exit_on_disconnect(sd_bus *bus);
157+
int sd_bus_set_close_on_exit(sd_bus *bus, int b);
158+
int sd_bus_get_close_on_exit(sd_bus *bus);
157159
int sd_bus_set_watch_bind(sd_bus *bus, int b);
158160
int sd_bus_get_watch_bind(sd_bus *bus);
159161
int sd_bus_set_connected_signal(sd_bus *bus, int b);

0 commit comments

Comments
 (0)
X Tutup