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.
This commit is contained in:
@@ -579,6 +579,9 @@ global:
|
||||
|
||||
sd_bus_error_move;
|
||||
|
||||
sd_bus_set_close_on_exit;
|
||||
sd_bus_get_close_on_exit;
|
||||
|
||||
sd_device_ref;
|
||||
sd_device_unref;
|
||||
|
||||
|
||||
@@ -211,6 +211,7 @@ struct sd_bus {
|
||||
bool accept_fd:1;
|
||||
bool attach_timestamp:1;
|
||||
bool connected_signal:1;
|
||||
bool close_on_exit:1;
|
||||
|
||||
int use_memfd;
|
||||
|
||||
|
||||
@@ -232,18 +232,22 @@ _public_ int sd_bus_new(sd_bus **ret) {
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
b = new0(sd_bus, 1);
|
||||
b = new(sd_bus, 1);
|
||||
if (!b)
|
||||
return -ENOMEM;
|
||||
|
||||
b->n_ref = REFCNT_INIT;
|
||||
b->input_fd = b->output_fd = -1;
|
||||
b->inotify_fd = -1;
|
||||
b->message_version = 1;
|
||||
b->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
|
||||
b->accept_fd = true;
|
||||
b->original_pid = getpid_cached();
|
||||
b->n_groups = (size_t) -1;
|
||||
*b = (sd_bus) {
|
||||
.n_ref = REFCNT_INIT,
|
||||
.input_fd = -1,
|
||||
.output_fd = -1,
|
||||
.inotify_fd = -1,
|
||||
.message_version = 1,
|
||||
.creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME,
|
||||
.accept_fd = true,
|
||||
.original_pid = getpid_cached(),
|
||||
.n_groups = (size_t) -1,
|
||||
.close_on_exit = true,
|
||||
};
|
||||
|
||||
assert_se(pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0);
|
||||
|
||||
@@ -3409,8 +3413,10 @@ static int quit_callback(sd_event_source *event, void *userdata) {
|
||||
|
||||
assert(event);
|
||||
|
||||
sd_bus_flush(bus);
|
||||
sd_bus_close(bus);
|
||||
if (bus->close_on_exit) {
|
||||
sd_bus_flush(bus);
|
||||
sd_bus_close(bus);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -4135,3 +4141,18 @@ _public_ int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret) {
|
||||
*ret = bus->method_call_timeout = BUS_DEFAULT_TIMEOUT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_bus_set_close_on_exit(sd_bus *bus, int b) {
|
||||
assert_return(bus, -EINVAL);
|
||||
assert_return(bus = bus_resolve(bus), -ENOPKG);
|
||||
|
||||
bus->close_on_exit = b;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_bus_get_close_on_exit(sd_bus *bus) {
|
||||
assert_return(bus, -EINVAL);
|
||||
assert_return(bus = bus_resolve(bus), -ENOPKG);
|
||||
|
||||
return bus->close_on_exit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user