chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
bus: add minimal locking around the memfd cache
[elogind.git]
/
src
/
libsystemd-bus
/
sd-bus.c
diff --git
a/src/libsystemd-bus/sd-bus.c
b/src/libsystemd-bus/sd-bus.c
index 29487cbd317904f545b27b570c7ea5712695b55c..5e66a31162611ac2d7ae988840aa55950a458320 100644
(file)
--- a/
src/libsystemd-bus/sd-bus.c
+++ b/
src/libsystemd-bus/sd-bus.c
@@
-26,11
+26,14
@@
#include <netdb.h>
#include <sys/poll.h>
#include <byteswap.h>
#include <netdb.h>
#include <sys/poll.h>
#include <byteswap.h>
+#include <sys/mman.h>
+#include <pthread.h>
#include "util.h"
#include "macro.h"
#include "strv.h"
#include "set.h"
#include "util.h"
#include "macro.h"
#include "strv.h"
#include "set.h"
+#include "missing.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "sd-bus.h"
#include "bus-internal.h"
@@
-42,6
+45,18
@@
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
+static void bus_close_fds(sd_bus *b) {
+ assert(b);
+
+ if (b->input_fd >= 0)
+ close_nointr_nofail(b->input_fd);
+
+ if (b->output_fd >= 0 && b->output_fd != b->input_fd)
+ close_nointr_nofail(b->output_fd);
+
+ b->input_fd = b->output_fd = -1;
+}
+
static void bus_free(sd_bus *b) {
struct filter_callback *f;
struct object_callback *c;
static void bus_free(sd_bus *b) {
struct filter_callback *f;
struct object_callback *c;
@@
-49,12
+64,16
@@
static void bus_free(sd_bus *b) {
assert(b);
assert(b);
- sd_bus_close(b);
+ bus_close_fds(b);
+
+ if (b->kdbus_buffer)
+ munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
free(b->rbuffer);
free(b->unique_name);
free(b->auth_buffer);
free(b->address);
free(b->rbuffer);
free(b->unique_name);
free(b->auth_buffer);
free(b->address);
+ free(b->kernel);
free(b->exec_path);
strv_free(b->exec_argv);
free(b->exec_path);
strv_free(b->exec_argv);
@@
-84,9
+103,12
@@
static void bus_free(sd_bus *b) {
}
hashmap_free(b->object_callbacks);
}
hashmap_free(b->object_callbacks);
-
bus_match_free(&b->match_callbacks);
bus_match_free(&b->match_callbacks);
+ bus_kernel_flush_memfd(b);
+
+ assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
+
free(b);
}
free(b);
}
@@
-100,10
+122,13
@@
int sd_bus_new(sd_bus **ret) {
if (!r)
return -ENOMEM;
if (!r)
return -ENOMEM;
- r->n_ref =
1
;
+ r->n_ref =
REFCNT_INIT
;
r->input_fd = r->output_fd = -1;
r->message_version = 1;
r->negotiate_fds = true;
r->input_fd = r->output_fd = -1;
r->message_version = 1;
r->negotiate_fds = true;
+ r->original_pid = getpid();
+
+ assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
/* We guarantee that wqueue always has space for at least one
* entry */
/* We guarantee that wqueue always has space for at least one
* entry */
@@
-126,6
+151,8
@@
int sd_bus_set_address(sd_bus *bus, const char *address) {
return -EPERM;
if (!address)
return -EINVAL;
return -EPERM;
if (!address)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
a = strdup(address);
if (!a)
a = strdup(address);
if (!a)
@@
-146,6
+173,8
@@
int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
return -EINVAL;
if (output_fd < 0)
return -EINVAL;
return -EINVAL;
if (output_fd < 0)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
bus->input_fd = input_fd;
bus->output_fd = output_fd;
bus->input_fd = input_fd;
bus->output_fd = output_fd;
@@
-163,6
+192,8
@@
int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
return -EINVAL;
if (strv_isempty(argv))
return -EINVAL;
return -EINVAL;
if (strv_isempty(argv))
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
p = strdup(path);
if (!p)
p = strdup(path);
if (!p)
@@
-188,6
+219,8
@@
int sd_bus_set_bus_client(sd_bus *bus, int b) {
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
bus->bus_client = !!b;
return 0;
bus->bus_client = !!b;
return 0;
@@
-198,6
+231,8
@@
int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
bus->negotiate_fds = !!b;
return 0;
bus->negotiate_fds = !!b;
return 0;
@@
-210,6
+245,8
@@
int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
bus->is_server = !!b;
bus->server_id = server_id;
bus->is_server = !!b;
bus->server_id = server_id;
@@
-221,23
+258,25
@@
int sd_bus_set_anonymous(sd_bus *bus, int b) {
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
bus->anonymous_auth = !!b;
return 0;
}
bus->anonymous_auth = !!b;
return 0;
}
-static int hello_callback(sd_bus *bus,
int error,
sd_bus_message *reply, void *userdata) {
+static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
const char *s;
int r;
assert(bus);
assert(bus->state == BUS_HELLO);
const char *s;
int r;
assert(bus);
assert(bus->state == BUS_HELLO);
-
- if (error != 0)
- return -error;
-
assert(reply);
assert(reply);
+ r = bus_message_to_errno(reply);
+ if (r < 0)
+ return r;
+
r = sd_bus_message_read(reply, "s", &s);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "s", &s);
if (r < 0)
return r;
@@
-279,7
+318,7
@@
static int bus_send_hello(sd_bus *bus) {
int bus_start_running(sd_bus *bus) {
assert(bus);
int bus_start_running(sd_bus *bus) {
assert(bus);
- if (bus->bus_client) {
+ if (bus->bus_client
&& !bus->is_kernel
) {
bus->state = BUS_HELLO;
return 1;
}
bus->state = BUS_HELLO;
return 1;
}
@@
-809,6
+848,8
@@
int sd_bus_start(sd_bus *bus) {
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
bus->state = BUS_OPENING;
bus->state = BUS_OPENING;
@@
-840,7
+881,7
@@
int sd_bus_open_system(sd_bus **ret) {
if (r < 0)
return r;
if (r < 0)
return r;
- e = getenv("DBUS_SYSTEM_BUS_ADDRESS");
+ e =
secure_
getenv("DBUS_SYSTEM_BUS_ADDRESS");
if (e) {
r = sd_bus_set_address(b, e);
if (r < 0)
if (e) {
r = sd_bus_set_address(b, e);
if (r < 0)
@@
-878,13
+919,13
@@
int sd_bus_open_user(sd_bus **ret) {
if (r < 0)
return r;
if (r < 0)
return r;
- e = getenv("DBUS_SESSION_BUS_ADDRESS");
+ e =
secure_
getenv("DBUS_SESSION_BUS_ADDRESS");
if (e) {
r = sd_bus_set_address(b, e);
if (r < 0)
goto fail;
} else {
if (e) {
r = sd_bus_set_address(b, e);
if (r < 0)
goto fail;
} else {
- e = getenv("XDG_RUNTIME_DIR");
+ e =
secure_
getenv("XDG_RUNTIME_DIR");
if (!e) {
r = -ENOENT;
goto fail;
if (!e) {
r = -ENOENT;
goto fail;
@@
-918,22
+959,29
@@
fail:
void sd_bus_close(sd_bus *bus) {
if (!bus)
return;
void sd_bus_close(sd_bus *bus) {
if (!bus)
return;
+ if (bus->state == BUS_CLOSED)
+ return;
+ if (bus_pid_changed(bus))
+ return;
- if (bus->input_fd >= 0)
- close_nointr_nofail(bus->input_fd);
- if (
bus->output_fd >= 0 && bus->output_fd != bus->input_fd
)
-
close_nointr_nofail(bus->output_fd
);
+ bus->state = BUS_CLOSED;
+
+ if (
!bus->is_kernel
)
+
bus_close_fds(bus
);
- bus->input_fd = bus->output_fd = -1;
+ /* We'll leave the fd open in case this is a kernel bus, since
+ * there might still be memblocks around that reference this
+ * bus, and they might need to invoke the
+ * KDBUS_CMD_MSG_RELEASE ioctl on the fd when they are
+ * freed. */
}
sd_bus *sd_bus_ref(sd_bus *bus) {
if (!bus)
return NULL;
}
sd_bus *sd_bus_ref(sd_bus *bus) {
if (!bus)
return NULL;
- assert
(bus->n_ref > 0
);
+ assert
_se(REFCNT_INC(bus->n_ref) >= 2
);
- bus->n_ref++;
return bus;
}
return bus;
}
@@
-941,10
+989,7
@@
sd_bus *sd_bus_unref(sd_bus *bus) {
if (!bus)
return NULL;
if (!bus)
return NULL;
- assert(bus->n_ref > 0);
- bus->n_ref--;
-
- if (bus->n_ref <= 0)
+ if (REFCNT_DEC(bus->n_ref) <= 0)
bus_free(bus);
return NULL;
bus_free(bus);
return NULL;
@@
-953,8
+998,10
@@
sd_bus *sd_bus_unref(sd_bus *bus) {
int sd_bus_is_open(sd_bus *bus) {
if (!bus)
return -EINVAL;
int sd_bus_is_open(sd_bus *bus) {
if (!bus)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
- return
bus->state != BUS_UNSET && bus->input_fd >= 0
;
+ return
BUS_IS_OPEN(bus->state)
;
}
int sd_bus_can_send(sd_bus *bus, char type) {
}
int sd_bus_can_send(sd_bus *bus, char type) {
@@
-962,8
+1009,10
@@
int sd_bus_can_send(sd_bus *bus, char type) {
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->
output_fd < 0
)
+ if (bus->
state == BUS_UNSET
)
return -ENOTCONN;
return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (type == SD_BUS_TYPE_UNIX_FD) {
if (!bus->negotiate_fds)
if (type == SD_BUS_TYPE_UNIX_FD) {
if (!bus->negotiate_fds)
@@
-986,6
+1035,8
@@
int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
return -EINVAL;
if (!server_id)
return -EINVAL;
return -EINVAL;
if (!server_id)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = bus_ensure_running(bus);
if (r < 0)
r = bus_ensure_running(bus);
if (r < 0)
@@
-1013,9
+1064,6
@@
static int dispatch_wqueue(sd_bus *bus) {
assert(bus);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
assert(bus);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
- if (bus->output_fd < 0)
- return -ENOTCONN;
-
while (bus->wqueue_size > 0) {
if (bus->is_kernel)
while (bus->wqueue_size > 0) {
if (bus->is_kernel)
@@
-1060,9
+1108,6
@@
static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
assert(m);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
assert(m);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
- if (bus->input_fd < 0)
- return -ENOTCONN;
-
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
if (bus->rqueue_size > 0) {
/* Dispatch a queued message */
@@
-1098,12
+1143,12
@@
int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
- if (bus->output_fd < 0)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
if (!m)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (m->n_fds > 0) {
r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
if (m->n_fds > 0) {
r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
@@
-1211,9
+1256,7
@@
int sd_bus_send_with_reply(
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
- if (bus->output_fd < 0)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
if (!m)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
@@
-1223,6
+1266,8
@@
int sd_bus_send_with_reply(
return -EINVAL;
if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return -EINVAL;
return -EINVAL;
if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
if (r < 0)
r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
if (r < 0)
@@
-1278,6
+1323,8
@@
int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
return -EINVAL;
if (serial == 0)
return -EINVAL;
return -EINVAL;
if (serial == 0)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
c = hashmap_remove(bus->reply_callbacks, &serial);
if (!c)
c = hashmap_remove(bus->reply_callbacks, &serial);
if (!c)
@@
-1295,11
+1342,8
@@
int bus_ensure_running(sd_bus *bus) {
assert(bus);
assert(bus);
- if (bus->
input_fd < 0
)
+ if (bus->
state == BUS_UNSET || bus->state == BUS_CLOSED
)
return -ENOTCONN;
return -ENOTCONN;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
-
if (bus->state == BUS_RUNNING)
return 1;
if (bus->state == BUS_RUNNING)
return 1;
@@
-1332,9
+1376,7
@@
int sd_bus_send_with_reply_and_block(
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->output_fd < 0)
- return -ENOTCONN;
- if (bus->state == BUS_UNSET)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
if (!m)
return -EINVAL;
return -ENOTCONN;
if (!m)
return -EINVAL;
@@
-1344,6
+1386,8
@@
int sd_bus_send_with_reply_and_block(
return -EINVAL;
if (bus_error_is_dirty(error))
return -EINVAL;
return -EINVAL;
if (bus_error_is_dirty(error))
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = bus_ensure_running(bus);
if (r < 0)
r = bus_ensure_running(bus);
if (r < 0)
@@
-1450,10
+1494,12
@@
int sd_bus_send_with_reply_and_block(
int sd_bus_get_fd(sd_bus *bus) {
if (!bus)
return -EINVAL;
int sd_bus_get_fd(sd_bus *bus) {
if (!bus)
return -EINVAL;
- if (
bus->input_fd < 0
)
+ if (
!BUS_IS_OPEN(bus->state)
)
return -ENOTCONN;
if (bus->input_fd != bus->output_fd)
return -EPERM;
return -ENOTCONN;
if (bus->input_fd != bus->output_fd)
return -EPERM;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
return bus->input_fd;
}
return bus->input_fd;
}
@@
-1463,10
+1509,10
@@
int sd_bus_get_events(sd_bus *bus) {
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
- if (bus->input_fd < 0)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (bus->state == BUS_OPENING)
flags |= POLLOUT;
if (bus->state == BUS_OPENING)
flags |= POLLOUT;
@@
-1494,10
+1540,10
@@
int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
return -EINVAL;
if (!timeout_usec)
return -EINVAL;
return -EINVAL;
if (!timeout_usec)
return -EINVAL;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
- if (bus->input_fd < 0)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (bus->state == BUS_AUTHENTICATING) {
*timeout_usec = bus->auth_timeout;
if (bus->state == BUS_AUTHENTICATING) {
*timeout_usec = bus->auth_timeout;
@@
-1520,6
+1566,7
@@
int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
}
static int process_timeout(sd_bus *bus) {
}
static int process_timeout(sd_bus *bus) {
+ _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
struct reply_callback *c;
usec_t n;
int r;
struct reply_callback *c;
usec_t n;
int r;
@@
-1534,10
+1581,18
@@
static int process_timeout(sd_bus *bus) {
if (c->timeout > n)
return 0;
if (c->timeout > n)
return 0;
+ r = bus_message_new_synthetic_error(
+ bus,
+ c->serial,
+ &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.Timeout", "Timed out"),
+ &m);
+ if (r < 0)
+ return r;
+
assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
hashmap_remove(bus->reply_callbacks, &c->serial);
assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
hashmap_remove(bus->reply_callbacks, &c->serial);
- r = c->callback(bus,
ETIMEDOUT, NULL
, c->userdata);
+ r = c->callback(bus,
m
, c->userdata);
free(c);
return r < 0 ? r : 1;
free(c);
return r < 0 ? r : 1;
@@
-1583,7
+1638,11
@@
static int process_reply(sd_bus *bus, sd_bus_message *m) {
if (c->timeout != 0)
prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
if (c->timeout != 0)
prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
- r = c->callback(bus, 0, m, c->userdata);
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0)
+ return r;
+
+ r = c->callback(bus, m, c->userdata);
free(c);
return r;
free(c);
return r;
@@
-1610,7
+1669,11
@@
static int process_filter(sd_bus *bus, sd_bus_message *m) {
l->last_iteration = bus->iteration_counter;
l->last_iteration = bus->iteration_counter;
- r = l->callback(bus, 0, m, l->userdata);
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0)
+ return r;
+
+ r = l->callback(bus, m, l->userdata);
if (r != 0)
return r;
if (r != 0)
return r;
@@
-1630,7
+1693,7
@@
static int process_match(sd_bus *bus, sd_bus_message *m) {
do {
bus->match_callbacks_modified = false;
do {
bus->match_callbacks_modified = false;
- r = bus_match_run(bus, &bus->match_callbacks,
0,
m);
+ r = bus_match_run(bus, &bus->match_callbacks, m);
if (r != 0)
return r;
if (r != 0)
return r;
@@
-1719,7
+1782,11
@@
static int process_object(sd_bus *bus, sd_bus_message *m) {
c->last_iteration = bus->iteration_counter;
c->last_iteration = bus->iteration_counter;
- r = c->callback(bus, 0, m, c->userdata);
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0)
+ return r;
+
+ r = c->callback(bus, m, c->userdata);
if (r != 0)
return r;
if (r != 0)
return r;
@@
-1745,7
+1812,11
@@
static int process_object(sd_bus *bus, sd_bus_message *m) {
c->last_iteration = bus->iteration_counter;
c->last_iteration = bus->iteration_counter;
- r = c->callback(bus, 0, m, c->userdata);
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0)
+ return r;
+
+ r = c->callback(bus, m, c->userdata);
if (r != 0)
return r;
if (r != 0)
return r;
@@
-1823,13
+1894,9
@@
static int process_introspect(sd_bus *bus, sd_bus_message *m) {
if (p)
*p = 0;
if (p)
*p = 0;
- r = set_put(s, a);
- if (r < 0) {
- free(a);
-
- if (r != -EEXIST)
- return r;
- }
+ r = set_consume(s, a);
+ if (r < 0 && r != -EEXIST)
+ return r;
}
f = open_memstream(&introspection, &size);
}
f = open_memstream(&introspection, &size);
@@
-1929,6
+1996,10
@@
static int process_running(sd_bus *bus, sd_bus_message **ret) {
goto null_message;
if (ret) {
goto null_message;
if (ret) {
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0)
+ return r;
+
*ret = m;
m = NULL;
return 1;
*ret = m;
m = NULL;
return 1;
@@
-1968,8
+2039,8
@@
int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus
->input_fd < 0
)
- return -E
NOTCONN
;
+ if (bus
_pid_changed(bus)
)
+ return -E
CHILD
;
/* We don't allow recursively invoking sd_bus_process(). */
if (bus->processing)
/* We don't allow recursively invoking sd_bus_process(). */
if (bus->processing)
@@
-1978,6
+2049,7
@@
int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
switch (bus->state) {
case BUS_UNSET:
switch (bus->state) {
case BUS_UNSET:
+ case BUS_CLOSED:
return -ENOTCONN;
case BUS_OPENING:
return -ENOTCONN;
case BUS_OPENING:
@@
-2018,7
+2090,7
@@
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
assert(bus);
assert(bus);
- if (
bus->input_fd < 0
)
+ if (
!BUS_IS_OPEN(bus->state)
)
return -ENOTCONN;
e = sd_bus_get_events(bus);
return -ENOTCONN;
e = sd_bus_get_events(bus);
@@
-2064,10
+2136,11
@@
int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
- if (bus->input_fd < 0)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
+
if (bus->rqueue_size > 0)
return 0;
if (bus->rqueue_size > 0)
return 0;
@@
-2079,10
+2152,10
@@
int sd_bus_flush(sd_bus *bus) {
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
- if (bus->state == BUS_UNSET)
- return -ENOTCONN;
- if (bus->output_fd < 0)
+ if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = bus_ensure_running(bus);
if (r < 0)
r = bus_ensure_running(bus);
if (r < 0)
@@
-2112,6
+2185,8
@@
int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *user
return -EINVAL;
if (!callback)
return -EINVAL;
return -EINVAL;
if (!callback)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
f = new0(struct filter_callback, 1);
if (!f)
f = new0(struct filter_callback, 1);
if (!f)
@@
-2131,6
+2206,8
@@
int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *u
return -EINVAL;
if (!callback)
return -EINVAL;
return -EINVAL;
if (!callback)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
if (f->callback == callback && f->userdata == userdata) {
LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
if (f->callback == callback && f->userdata == userdata) {
@@
-2160,6
+2237,8
@@
static int bus_add_object(
return -EINVAL;
if (!callback)
return -EINVAL;
return -EINVAL;
if (!callback)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
if (r < 0)
r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
if (r < 0)
@@
-2205,6
+2284,8
@@
static int bus_remove_object(
return -EINVAL;
if (!callback)
return -EINVAL;
return -EINVAL;
if (!callback)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
c = hashmap_get(bus->object_callbacks, path);
if (!c)
c = hashmap_get(bus->object_callbacks, path);
if (!c)
@@
-2245,6
+2326,8
@@
int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t ca
return -EINVAL;
if (!match)
return -EINVAL;
return -EINVAL;
if (!match)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (bus->bus_client) {
r = bus_add_match_internal(bus, match);
if (bus->bus_client) {
r = bus_add_match_internal(bus, match);
@@
-2272,6
+2355,8
@@
int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t
return -EINVAL;
if (!match)
return -EINVAL;
return -EINVAL;
if (!match)
return -EINVAL;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (bus->bus_client)
r = bus_remove_match_internal(bus, match);
if (bus->bus_client)
r = bus_remove_match_internal(bus, match);
@@
-2299,6
+2384,10
@@
int sd_bus_emit_signal(
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
+ if (!BUS_IS_OPEN(bus->state))
+ return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = sd_bus_message_new_signal(bus, path, interface, member, &m);
if (r < 0)
r = sd_bus_message_new_signal(bus, path, interface, member, &m);
if (r < 0)
@@
-2329,6
+2418,10
@@
int sd_bus_call_method(
if (!bus)
return -EINVAL;
if (!bus)
return -EINVAL;
+ if (!BUS_IS_OPEN(bus->state))
+ return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
if (r < 0)
r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
if (r < 0)
@@
-2360,6
+2453,10
@@
int sd_bus_reply_method_return(
return -EPERM;
if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
return -EPERM;
if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
+ if (!BUS_IS_OPEN(bus->state))
+ return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return 0;
if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return 0;
@@
-2395,6
+2492,10
@@
int sd_bus_reply_method_error(
return -EINVAL;
if (!sd_bus_error_is_set(e))
return -EINVAL;
return -EINVAL;
if (!sd_bus_error_is_set(e))
return -EINVAL;
+ if (!BUS_IS_OPEN(bus->state))
+ return -ENOTCONN;
+ if (bus_pid_changed(bus))
+ return -ECHILD;
if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return 0;
if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return 0;
@@
-2405,3
+2506,12
@@
int sd_bus_reply_method_error(
return sd_bus_send(bus, m, NULL);
}
return sd_bus_send(bus, m, NULL);
}
+
+bool bus_pid_changed(sd_bus *bus) {
+ assert(bus);
+
+ /* We don't support people creating a bus connection and
+ * keeping it around over a fork(). Let's complain. */
+
+ return bus->original_pid != getpid();
+}