X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fsd-bus.c;h=6bd1eaa81b5ec82a75732a349608846bba1a6481;hb=441d56a12a90bbb5f6e58717a1e6b9bfe5c82efc;hp=9dfb6e444215012dfe363660424d54d3179f6dbb;hpb=dc74ce9b4a49ab31383d00b30406bc225b2cc8ed;p=elogind.git diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 9dfb6e444..6bd1eaa81 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -34,6 +34,7 @@ #include "strv.h" #include "set.h" #include "missing.h" +#include "def.h" #include "sd-bus.h" #include "bus-internal.h" @@ -141,6 +142,8 @@ static void bus_free(sd_bus *b) { free(b->address); free(b->kernel); free(b->machine); + free(b->fake_label); + free(b->cgroup_root); free(b->exec_path); strv_free(b->exec_argv); @@ -381,7 +384,7 @@ static int bus_send_hello(sd_bus *bus) { if (r < 0) return r; - return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial); + return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_cookie); } int bus_start_running(sd_bus *bus) { @@ -1045,12 +1048,7 @@ _public_ int sd_bus_open_system(sd_bus **ret) { if (e) r = sd_bus_set_address(b, e); else -#ifdef ENABLE_KDBUS - r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket"); -#else - r = sd_bus_set_address(b, "unix:path=/run/dbus/system_bus_socket"); -#endif - + r = sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_PATH); if (r < 0) goto fail; @@ -1101,13 +1099,13 @@ _public_ int sd_bus_open_user(sd_bus **ret) { } #ifdef ENABLE_KDBUS - asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee); + asprintf(&b->address, KERNEL_USER_BUS_FMT ";" UNIX_USER_BUS_FMT, (unsigned long) getuid(), ee); #else - b->address = strjoin("unix:path=", ee, "/bus", NULL); + asprintf(&b->address, UNIX_USER_BUS_FMT, ee); #endif } else { #ifdef ENABLE_KDBUS - asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid()); + asprintf(&b->address, KERNEL_USER_BUS_FMT, (unsigned long) getuid()); #else return -ECONNREFUSED; #endif @@ -1236,7 +1234,7 @@ _public_ void sd_bus_close(sd_bus *bus) { /* 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_FREE + * bus, and they might need to invoke the KDBUS_CMD_FREE * ioctl on the fd when they are freed. */ } @@ -1321,16 +1319,16 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) { if (m->sealed) { /* If we copy the same message to multiple - * destinations, avoid using the same serial + * destinations, avoid using the same cookie * numbers. */ - b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m)); + b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m)); return 0; } if (timeout == 0) timeout = BUS_DEFAULT_TIMEOUT; - return bus_message_seal(m, ++b->serial, timeout); + return bus_message_seal(m, ++b->cookie, timeout); } static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) { @@ -1360,14 +1358,33 @@ int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) { return bus_message_seal(m, 0xFFFFFFFFULL, 0); } -static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) { +static int bus_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) { + int r; + assert(bus); - assert(message); + assert(m); if (bus->is_kernel) - return bus_kernel_write_message(bus, message); + r = bus_kernel_write_message(bus, m); else - return bus_socket_write_message(bus, message, idx); + r = bus_socket_write_message(bus, m, idx); + + if (r <= 0) + return r; + + if (bus->is_kernel || *idx >= BUS_MESSAGE_SIZE(m)) + log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s", + bus_message_type_to_string(m->header->type), + strna(sd_bus_message_get_sender(m)), + strna(sd_bus_message_get_destination(m)), + strna(sd_bus_message_get_path(m)), + strna(sd_bus_message_get_interface(m)), + strna(sd_bus_message_get_member(m)), + (unsigned long) BUS_MESSAGE_COOKIE(m), + (unsigned long) m->reply_cookie, + strna(m->error.message)); + + return r; } static int dispatch_wqueue(sd_bus *bus) { @@ -1456,7 +1473,7 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { } } -_public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *serial) { +_public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) { _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m); int r; @@ -1473,16 +1490,16 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *serial) { return -ENOTSUP; } - /* If the serial number isn't kept, then we know that no reply + /* If the cookie number isn't kept, then we know that no reply * is expected */ - if (!serial && !m->sealed) + if (!cookie && !m->sealed) m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED; r = bus_seal_message(bus, m, 0); if (r < 0) return r; - /* Remarshall if we have to. This will possible unref the + /* Remarshall if we have to. This will possibly unref the * message and place a replacement in m */ r = bus_remarshal_message(bus, &m); if (r < 0) @@ -1490,7 +1507,7 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *serial) { /* If this is a reply and no reply was requested, then let's * suppress this, if we can */ - if (m->dont_send && !serial) + if (m->dont_send && !cookie) return 1; if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) { @@ -1498,8 +1515,10 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *serial) { r = bus_write_message(bus, m, &idx); if (r < 0) { - if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN) + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); + return -ECONNRESET; + } return r; } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) { @@ -1524,13 +1543,13 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *serial) { bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m); } - if (serial) - *serial = BUS_MESSAGE_SERIAL(m); + if (cookie) + *cookie = BUS_MESSAGE_COOKIE(m); return 1; } -_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) { +_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) { int r; assert_return(bus, -EINVAL); @@ -1548,7 +1567,7 @@ _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destinat return r; } - return sd_bus_send(bus, m, serial); + return sd_bus_send(bus, m, cookie); } static usec_t calc_elapse(uint64_t usec) { @@ -1582,7 +1601,7 @@ _public_ int sd_bus_call_async( sd_bus_message_handler_t callback, void *userdata, uint64_t usec, - uint64_t *serial) { + uint64_t *cookie) { _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m); struct reply_callback *c; @@ -1618,10 +1637,10 @@ _public_ int sd_bus_call_async( c->callback = callback; c->userdata = userdata; - c->serial = BUS_MESSAGE_SERIAL(m); + c->cookie = BUS_MESSAGE_COOKIE(m); c->timeout = calc_elapse(m->timeout); - r = hashmap_put(bus->reply_callbacks, &c->serial, c); + r = hashmap_put(bus->reply_callbacks, &c->cookie, c); if (r < 0) { free(c); return r; @@ -1631,28 +1650,28 @@ _public_ int sd_bus_call_async( r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx); if (r < 0) { c->timeout = 0; - sd_bus_call_async_cancel(bus, c->serial); + sd_bus_call_async_cancel(bus, c->cookie); return r; } } - r = sd_bus_send(bus, m, serial); + r = sd_bus_send(bus, m, cookie); if (r < 0) { - sd_bus_call_async_cancel(bus, c->serial); + sd_bus_call_async_cancel(bus, c->cookie); return r; } return r; } -_public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) { +_public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t cookie) { struct reply_callback *c; assert_return(bus, -EINVAL); - assert_return(serial != 0, -EINVAL); + assert_return(cookie != 0, -EINVAL); assert_return(!bus_pid_changed(bus), -ECHILD); - c = hashmap_remove(bus->reply_callbacks, &serial); + c = hashmap_remove(bus->reply_callbacks, &cookie); if (!c) return 0; @@ -1697,7 +1716,7 @@ _public_ int sd_bus_call( _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m); usec_t timeout; - uint64_t serial; + uint64_t cookie; unsigned i; int r; @@ -1723,7 +1742,7 @@ _public_ int sd_bus_call( if (r < 0) return r; - r = sd_bus_send(bus, m, &serial); + r = sd_bus_send(bus, m, &cookie); if (r < 0) return r; @@ -1737,7 +1756,7 @@ _public_ int sd_bus_call( incoming = bus->rqueue[i]; - if (incoming->reply_serial == serial) { + if (incoming->reply_cookie == cookie) { /* Found a match! */ memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1)); @@ -1759,7 +1778,7 @@ _public_ int sd_bus_call( sd_bus_message_unref(incoming); return r; - } else if (incoming->header->serial == serial && + } else if (BUS_MESSAGE_COOKIE(incoming) == cookie && bus->unique_name && incoming->sender && streq(bus->unique_name, incoming->sender)) { @@ -1782,8 +1801,10 @@ _public_ int sd_bus_call( r = bus_read_message(bus); if (r < 0) { - if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN) + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); + return -ECONNRESET; + } return r; } @@ -1809,8 +1830,10 @@ _public_ int sd_bus_call( r = dispatch_wqueue(bus); if (r < 0) { - if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN) + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); + return -ECONNRESET; + } return r; } @@ -1909,7 +1932,7 @@ static int process_timeout(sd_bus *bus) { r = bus_message_new_synthetic_error( bus, - c->serial, + c->cookie, &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"), &m); if (r < 0) @@ -1922,7 +1945,7 @@ static int process_timeout(sd_bus *bus) { return r; assert_se(prioq_pop(bus->reply_callbacks_prioq) == c); - hashmap_remove(bus->reply_callbacks, &c->serial); + hashmap_remove(bus->reply_callbacks, &c->cookie); bus->current = m; bus->iteration_counter ++; @@ -1952,7 +1975,7 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) { m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) return -EIO; - if (m->reply_serial != bus->hello_serial) + if (m->reply_cookie != bus->hello_cookie) return -EIO; return 0; @@ -1970,7 +1993,7 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) { m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) return 0; - c = hashmap_remove(bus->reply_callbacks, &m->reply_serial); + c = hashmap_remove(bus->reply_callbacks, &m->reply_cookie); if (!c) return 0; @@ -2051,6 +2074,9 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) { assert(bus); assert(m); + if (bus->manual_peer_interface) + return 0; + if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) return 0; @@ -2101,11 +2127,16 @@ static int process_message(sd_bus *bus, sd_bus_message *m) { bus->current = m; bus->iteration_counter++; - log_debug("Got message sender=%s object=%s interface=%s member=%s", + log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s", + bus_message_type_to_string(m->header->type), strna(sd_bus_message_get_sender(m)), + strna(sd_bus_message_get_destination(m)), strna(sd_bus_message_get_path(m)), strna(sd_bus_message_get_interface(m)), - strna(sd_bus_message_get_member(m))); + strna(sd_bus_message_get_member(m)), + (unsigned long) BUS_MESSAGE_COOKIE(m), + (unsigned long) m->reply_cookie, + strna(m->error.message)); r = process_hello(bus, m); if (r != 0) @@ -2209,7 +2240,7 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) { /* First, fail all outstanding method calls */ r = bus_message_new_synthetic_error( bus, - c->serial, + c->cookie, &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"), &m); if (r < 0) @@ -2222,7 +2253,7 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) { if (c->timeout != 0) prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx); - hashmap_remove(bus->reply_callbacks, &c->serial); + hashmap_remove(bus->reply_callbacks, &c->cookie); bus->current = m; bus->iteration_counter++; @@ -2300,7 +2331,7 @@ _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { case BUS_OPENING: r = bus_socket_process_opening(bus); - if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); r = 1; } else if (r < 0) @@ -2311,7 +2342,7 @@ _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { case BUS_AUTHENTICATING: r = bus_socket_process_authenticating(bus); - if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); r = 1; } else if (r < 0) @@ -2325,7 +2356,7 @@ _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { case BUS_RUNNING: case BUS_HELLO: r = process_running(bus, ret); - if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); r = 1; @@ -2437,8 +2468,10 @@ _public_ int sd_bus_flush(sd_bus *bus) { for (;;) { r = dispatch_wqueue(bus); if (r < 0) { - if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN) + if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); + return -ECONNRESET; + } return r; }