summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-11-11 22:00:48 +0100
committerLennart Poettering <lennart@poettering.net>2013-11-12 00:12:43 +0100
commit76b543756ef69ce69784d571aefe8de65eaeb331 (patch)
tree092b117494d53fe09a9de227009d60d94d781659 /src
parentafc6adb5ec7e73bc13156c43f52fb015cd80cc68 (diff)
bus: introduce concept of a default bus for each thread and make use of it everywhere
We want to emphasize bus connections as per-thread communication primitives, hence introduce a concept of a per-thread default bus, and make use of it everywhere.
Diffstat (limited to 'src')
-rw-r--r--src/hostname/hostnamed.c2
-rw-r--r--src/journal/journal-gatewayd.c2
-rw-r--r--src/libsystemd-bus/bus-internal.h3
-rw-r--r--src/libsystemd-bus/bus-util.c4
-rw-r--r--src/libsystemd-bus/libsystemd-bus.sym3
-rw-r--r--src/libsystemd-bus/sd-bus.c74
-rw-r--r--src/libsystemd-bus/sd-event.c10
-rw-r--r--src/locale/localed.c2
-rw-r--r--src/login/inhibit.c2
-rw-r--r--src/login/logind.c2
-rw-r--r--src/machine/machined.c2
-rw-r--r--src/nspawn/nspawn.c2
-rw-r--r--src/systemd/sd-bus.h4
-rw-r--r--src/timedate/timedated.c2
14 files changed, 94 insertions, 20 deletions
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index d2f0b022e5..4363b09edb 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -579,7 +579,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
assert(event);
assert(_bus);
- r = sd_bus_open_system(&bus);
+ r = sd_bus_default_system(&bus);
if (r < 0) {
log_error("Failed to get system bus connection: %s", strerror(-r));
return r;
diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c
index c51afbfeb9..384137b2bd 100644
--- a/src/journal/journal-gatewayd.c
+++ b/src/journal/journal-gatewayd.c
@@ -746,7 +746,7 @@ static int get_virtualization(char **v) {
char *b;
int r;
- r = sd_bus_open_system(&bus);
+ r = sd_bus_default_system(&bus);
if (r < 0)
return r;
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index cd9f37edd7..4f9d941c63 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -249,6 +249,9 @@ struct sd_bus {
sd_event *event;
sd_bus_message *current;
+
+ sd_bus **default_bus_ptr;
+ pid_t tid;
};
#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
diff --git a/src/libsystemd-bus/bus-util.c b/src/libsystemd-bus/bus-util.c
index d277e1e0f0..33bdf38171 100644
--- a/src/libsystemd-bus/bus-util.c
+++ b/src/libsystemd-bus/bus-util.c
@@ -944,9 +944,9 @@ int bus_open_transport(BusTransport transport, const char *host, bool user, sd_b
case BUS_TRANSPORT_LOCAL:
if (user)
- r = sd_bus_open_user(bus);
+ r = sd_bus_default_user(bus);
else
- r = sd_bus_open_system(bus);
+ r = sd_bus_default_system(bus);
break;
diff --git a/src/libsystemd-bus/libsystemd-bus.sym b/src/libsystemd-bus/libsystemd-bus.sym
index 796a395a17..b6ad84b98b 100644
--- a/src/libsystemd-bus/libsystemd-bus.sym
+++ b/src/libsystemd-bus/libsystemd-bus.sym
@@ -12,6 +12,8 @@ global:
/* Same order as in sd-bus.h should be used */
/* Connections */
+ sd_bus_default_user;
+ sd_bus_default_system;
sd_bus_open_user;
sd_bus_open_system;
sd_bus_open_system_remote;
@@ -49,6 +51,7 @@ global:
sd_bus_wait;
sd_bus_flush;
sd_bus_get_current;
+ sd_bus_get_tid;
sd_bus_attach_event;
sd_bus_detach_event;
sd_bus_add_filter;
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index fdfbbeb2be..3ecda17fce 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -2370,16 +2370,21 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
int r;
assert_return(bus, -EINVAL);
- assert_return(event, -EINVAL);
assert_return(!bus->event, -EBUSY);
assert(!bus->input_io_event_source);
assert(!bus->output_io_event_source);
assert(!bus->time_event_source);
- bus->event = sd_event_ref(event);
+ if (event)
+ bus->event = sd_event_ref(event);
+ else {
+ r = sd_event_default(&bus->event);
+ if (r < 0)
+ return r;
+ }
- r = sd_event_add_io(event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
+ r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
if (r < 0)
goto fail;
@@ -2388,7 +2393,7 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
goto fail;
if (bus->output_fd != bus->input_fd) {
- r = sd_event_add_io(event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
+ r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
if (r < 0)
goto fail;
@@ -2401,7 +2406,7 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
if (r < 0)
goto fail;
- r = sd_event_add_monotonic(event, 0, 0, time_callback, bus, &bus->time_event_source);
+ r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
if (r < 0)
goto fail;
@@ -2409,7 +2414,7 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
if (r < 0)
goto fail;
- r = sd_event_add_quit(event, quit_callback, bus, &bus->quit_event_source);
+ r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
if (r < 0)
goto fail;
@@ -2442,8 +2447,63 @@ _public_ int sd_bus_detach_event(sd_bus *bus) {
return 0;
}
-sd_bus_message* sd_bus_get_current(sd_bus *bus) {
+_public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
assert_return(bus, NULL);
return bus->current;
}
+
+static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
+ sd_bus *b = NULL;
+ int r;
+
+ assert(bus_open);
+ assert(default_bus);
+
+ if (!ret)
+ return !!*default_bus;
+
+ if (*default_bus) {
+ *ret = sd_bus_ref(*default_bus);
+ return 0;
+ }
+
+ r = bus_open(&b);
+ if (r < 0)
+ return r;
+
+ b->default_bus_ptr = default_bus;
+ b->tid = gettid();
+ *default_bus = b;
+
+ *ret = b;
+ return 1;
+}
+
+_public_ int sd_bus_default_system(sd_bus **ret) {
+ static __thread sd_bus *default_system_bus = NULL;
+
+ return bus_default(sd_bus_open_system, &default_system_bus, ret);
+}
+
+_public_ int sd_bus_default_user(sd_bus **ret) {
+ static __thread sd_bus *default_user_bus = NULL;
+
+ return bus_default(sd_bus_open_user, &default_user_bus, ret);
+}
+
+_public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
+ assert_return(b, -EINVAL);
+ assert_return(tid, -EINVAL);
+ assert_return(!bus_pid_changed(b), -ECHILD);
+
+ if (b->tid != 0) {
+ *tid = b->tid;
+ return 0;
+ }
+
+ if (b->event)
+ return sd_event_get_tid(b->event, tid);
+
+ return -ENXIO;
+}
diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index 8025d24bb7..0996316986 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -1906,8 +1906,12 @@ _public_ int sd_event_default(sd_event **ret) {
_public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
assert_return(e, -EINVAL);
assert_return(tid, -EINVAL);
- assert_return(e->tid != 0, -ENXIO);
+ assert_return(!event_pid_changed(e), -ECHILD);
- *tid = e->tid;
- return 0;
+ if (e->tid != 0) {
+ *tid = e->tid;
+ return 0;
+ }
+
+ return -ENXIO;
}
diff --git a/src/locale/localed.c b/src/locale/localed.c
index 32d52a90a2..78250d6483 100644
--- a/src/locale/localed.c
+++ b/src/locale/localed.c
@@ -1061,7 +1061,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
assert(event);
assert(_bus);
- r = sd_bus_open_system(&bus);
+ r = sd_bus_default_system(&bus);
if (r < 0) {
log_error("Failed to get system bus connection: %s", strerror(-r));
return r;
diff --git a/src/login/inhibit.c b/src/login/inhibit.c
index 10de54b221..48c2ec401f 100644
--- a/src/login/inhibit.c
+++ b/src/login/inhibit.c
@@ -233,7 +233,7 @@ int main(int argc, char *argv[]) {
if (r == 0)
return EXIT_SUCCESS;
- r = sd_bus_open_system(&bus);
+ r = sd_bus_default_system(&bus);
if (r < 0) {
log_error("Failed to connect to bus: %s", strerror(-r));
return EXIT_FAILURE;
diff --git a/src/login/logind.c b/src/login/logind.c
index b54689e1d5..1d16eaa7b8 100644
--- a/src/login/logind.c
+++ b/src/login/logind.c
@@ -605,7 +605,7 @@ static int manager_connect_bus(Manager *m) {
assert(m);
assert(!m->bus);
- r = sd_bus_open_system(&m->bus);
+ r = sd_bus_default_system(&m->bus);
if (r < 0) {
log_error("Failed to connect to system bus: %s", strerror(-r));
return r;
diff --git a/src/machine/machined.c b/src/machine/machined.c
index ee6a6d6aeb..01b2caa322 100644
--- a/src/machine/machined.c
+++ b/src/machine/machined.c
@@ -129,7 +129,7 @@ static int manager_connect_bus(Manager *m) {
assert(m);
assert(!m->bus);
- r = sd_bus_open_system(&m->bus);
+ r = sd_bus_default_system(&m->bus);
if (r < 0) {
log_error("Failed to connect to system bus: %s", strerror(-r));
return r;
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 06d627c0a9..2778cd8411 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -965,7 +965,7 @@ static int terminate_machine(pid_t pid) {
const char *path;
int r;
- r = sd_bus_open_system(&bus);
+ r = sd_bus_default_system(&bus);
if (r < 0) {
log_error("Failed to open system bus: %s", strerror(-r));
return r;
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index b31cfaab6f..ef5984f45b 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -57,6 +57,9 @@ typedef int (*sd_bus_node_enumerator_t) (sd_bus *bus, const char *path, char ***
/* Connections */
+int sd_bus_default_user(sd_bus **ret);
+int sd_bus_default_system(sd_bus **ret);
+
int sd_bus_open_user(sd_bus **ret);
int sd_bus_open_system(sd_bus **ret);
int sd_bus_open_system_remote(const char *host, sd_bus **ret);
@@ -101,6 +104,7 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec);
int sd_bus_flush(sd_bus *bus);
sd_bus_message* sd_bus_get_current(sd_bus *bus);
+int sd_bus_get_tid(sd_bus *bus, pid_t *tid);
int sd_bus_attach_event(sd_bus *bus, sd_event *e, int priority);
int sd_bus_detach_event(sd_bus *bus);
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index 8fb7f6e806..0ae93d852d 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -795,7 +795,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
assert(event);
assert(_bus);
- r = sd_bus_open_system(&bus);
+ r = sd_bus_default_system(&bus);
if (r < 0) {
log_error("Failed to get system bus connection: %s", strerror(-r));
return r;