X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fsd-bus.c;h=97a8c683bb04decf169c86648f700e13900df416;hp=bc88ac977c96f3c4903995485e0eb95f2b2a6a71;hb=5b12334d35eadf1f45cc3d631fd1a2e72ffaea0a;hpb=718db96199eb307751264e4163555662c9a389fa diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index bc88ac977..97a8c683b 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -99,10 +99,26 @@ static void bus_node_destroy(sd_bus *b, struct node *n) { free(n); } +static void bus_reset_queues(sd_bus *b) { + unsigned i; + + assert(b); + + for (i = 0; i < b->rqueue_size; i++) + sd_bus_message_unref(b->rqueue[i]); + free(b->rqueue); + + for (i = 0; i < b->wqueue_size; i++) + sd_bus_message_unref(b->wqueue[i]); + free(b->wqueue); + + b->rqueue = b->wqueue = NULL; + b->rqueue_size = b->wqueue_size = 0; +} + static void bus_free(sd_bus *b) { struct filter_callback *f; struct node *n; - unsigned i; assert(b); @@ -126,13 +142,7 @@ static void bus_free(sd_bus *b) { close_many(b->fds, b->n_fds); free(b->fds); - for (i = 0; i < b->rqueue_size; i++) - sd_bus_message_unref(b->rqueue[i]); - free(b->rqueue); - - for (i = 0; i < b->wqueue_size; i++) - sd_bus_message_unref(b->wqueue[i]); - free(b->wqueue); + bus_reset_queues(b); hashmap_free_free(b->reply_callbacks); prioq_free(b->reply_callbacks_prioq); @@ -171,7 +181,7 @@ _public_ int sd_bus_new(sd_bus **ret) { r->n_ref = REFCNT_INIT; r->input_fd = r->output_fd = -1; r->message_version = 1; - r->hello_flags |= KDBUS_HELLO_ACCEPT_FD; + r->hello_flags |= KDBUS_HELLO_ACCEPT_FD|KDBUS_HELLO_ATTACH_NAMES; r->original_pid = getpid(); assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0); @@ -273,75 +283,38 @@ _public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) { return 0; } -_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, int b) { +_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) { assert_return(bus, -EINVAL); + assert_return(mask <= _SD_BUS_CREDS_MAX, -EINVAL); assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(!bus_pid_changed(bus), -ECHILD); - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CREDS, b); - return 0; -} + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CREDS, + !!(mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID))); -_public_ int sd_bus_negotiate_attach_comm(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, + !!(mask & (SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM))); - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, b); - return 0; -} + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, + !!(mask & SD_BUS_CREDS_EXE)); -_public_ int sd_bus_negotiate_attach_exe(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, + !!(mask & SD_BUS_CREDS_CMDLINE)); - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, b); - return 0; -} + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, + !!(mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID))); -_public_ int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, + !!(mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS))); - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, b); - return 0; -} + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, + !!(mask & SD_BUS_CREDS_SELINUX_CONTEXT)); -_public_ int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, + !!(mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID))); - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, b); - return 0; -} - -_public_ int sd_bus_negotiate_attach_caps(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); + bus->creds_mask = mask; - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, b); - return 0; -} - -_public_ int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); - - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, b); - return 0; -} - -_public_ int sd_bus_negotiate_attach_audit(sd_bus *bus, int b) { - assert_return(bus, -EINVAL); - assert_return(bus->state == BUS_UNSET, -EPERM); - assert_return(!bus_pid_changed(bus), -ECHILD); - - SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, b); return 0; } @@ -365,12 +338,12 @@ _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) { return 0; } -static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) { +static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) { const char *s; int r; assert(bus); - assert(bus->state == BUS_HELLO); + assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING); assert(reply); r = sd_bus_message_get_errno(reply); @@ -390,7 +363,8 @@ static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) { if (!bus->unique_name) return -ENOMEM; - bus->state = BUS_RUNNING; + if (bus->state == BUS_HELLO) + bus->state = BUS_RUNNING; return 1; } @@ -1183,6 +1157,7 @@ _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) { } _public_ void sd_bus_close(sd_bus *bus) { + if (!bus) return; if (bus->state == BUS_CLOSED) @@ -1194,6 +1169,10 @@ _public_ void sd_bus_close(sd_bus *bus) { sd_bus_detach_event(bus); + /* Drop all queued messages so that they drop references to + * the bus object and the bus may be freed */ + bus_reset_queues(bus); + if (!bus->is_kernel) bus_close_fds(bus); @@ -1824,6 +1803,7 @@ _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) { } static int process_timeout(sd_bus *bus) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; _cleanup_bus_message_unref_ sd_bus_message* m = NULL; struct reply_callback *c; usec_t n; @@ -1842,7 +1822,7 @@ static int process_timeout(sd_bus *bus) { r = bus_message_new_synthetic_error( bus, c->serial, - &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Method call timed out"), + &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"), &m); if (r < 0) return r; @@ -1857,12 +1837,13 @@ static int process_timeout(sd_bus *bus) { bus->current = m; bus->iteration_counter ++; - r = c->callback(bus, m, c->userdata); + r = c->callback(bus, m, c->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); free(c); bus->current = NULL; - return r < 0 ? r : 1; + return r; } static int process_hello(sd_bus *bus, sd_bus_message *m) { @@ -1888,6 +1869,7 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) { } static int process_reply(sd_bus *bus, sd_bus_message *m) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; struct reply_callback *c; int r; @@ -1909,13 +1891,15 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) { if (r < 0) return r; - r = c->callback(bus, m, c->userdata); + r = c->callback(bus, m, c->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); free(c); - return r < 0 ? r : 1; + return r; } static int process_filter(sd_bus *bus, sd_bus_message *m) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; struct filter_callback *l; int r; @@ -1940,7 +1924,8 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) { if (r < 0) return r; - r = l->callback(bus, m, l->userdata); + r = l->callback(bus, m, l->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); if (r != 0) return r; @@ -1986,7 +1971,7 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) { return 1; if (streq_ptr(m->member, "Ping")) - r = sd_bus_message_new_method_return(bus, m, &reply); + r = sd_bus_message_new_method_return(m, &reply); else if (streq_ptr(m->member, "GetMachineId")) { sd_id128_t id; char sid[33]; @@ -1995,14 +1980,14 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) { if (r < 0) return r; - r = sd_bus_message_new_method_return(bus, m, &reply); + r = sd_bus_message_new_method_return(m, &reply); if (r < 0) return r; r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid)); } else { r = sd_bus_message_new_method_errorf( - bus, m, &reply, + m, &reply, SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method '%s' on interface '%s'.", m->member, m->interface); } @@ -2097,7 +2082,7 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) { if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) { r = sd_bus_reply_method_errorf( - bus, m, + m, SD_BUS_ERROR_UNKNOWN_OBJECT, "Unknown object '%s'.", m->path); if (r < 0) @@ -2123,11 +2108,13 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) { c = hashmap_first(bus->reply_callbacks); if (c) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; + /* First, fail all outstanding method calls */ r = bus_message_new_synthetic_error( bus, c->serial, - &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Connection terminated"), + &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"), &m); if (r < 0) return r; @@ -2144,12 +2131,10 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) { bus->current = m; bus->iteration_counter++; - r = c->callback(bus, m, c->userdata); + r = c->callback(bus, m, c->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); free(c); - if (r >= 0) - r = 1; - goto finish; } @@ -2626,17 +2611,25 @@ _public_ int sd_bus_detach_event(sd_bus *bus) { assert_return(bus, -EINVAL); assert_return(bus->event, -ENXIO); - if (bus->input_io_event_source) + if (bus->input_io_event_source) { + sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF); bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source); + } - if (bus->output_io_event_source) + if (bus->output_io_event_source) { + sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF); bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source); + } - if (bus->time_event_source) + if (bus->time_event_source) { + sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF); bus->time_event_source = sd_event_source_unref(bus->time_event_source); + } - if (bus->quit_event_source) + if (bus->quit_event_source) { + sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF); bus->quit_event_source = sd_event_source_unref(bus->quit_event_source); + } if (bus->event) bus->event = sd_event_unref(bus->event); @@ -2644,6 +2637,12 @@ _public_ int sd_bus_detach_event(sd_bus *bus) { return 0; } +_public_ sd_event* sd_bus_get_event(sd_bus *bus) { + assert_return(bus, NULL); + + return bus->event; +} + _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) { assert_return(bus, NULL); @@ -2704,3 +2703,120 @@ _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { return -ENXIO; } + +_public_ char *sd_bus_label_escape(const char *s) { + char *r, *t; + const char *f; + + assert_return(s, NULL); + + /* Escapes all chars that D-Bus' object path cannot deal + * with. Can be reversed with bus_path_unescape(). We special + * case the empty string. */ + + if (*s == 0) + return strdup("_"); + + r = new(char, strlen(s)*3 + 1); + if (!r) + return NULL; + + for (f = s, t = r; *f; f++) { + + /* Escape everything that is not a-zA-Z0-9. We also + * escape 0-9 if it's the first character */ + + if (!(*f >= 'A' && *f <= 'Z') && + !(*f >= 'a' && *f <= 'z') && + !(f > s && *f >= '0' && *f <= '9')) { + *(t++) = '_'; + *(t++) = hexchar(*f >> 4); + *(t++) = hexchar(*f); + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + +_public_ char *sd_bus_label_unescape(const char *f) { + char *r, *t; + + assert_return(f, NULL); + + /* Special case for the empty string */ + if (streq(f, "_")) + return strdup(""); + + r = new(char, strlen(f) + 1); + if (!r) + return NULL; + + for (t = r; *f; f++) { + + if (*f == '_') { + int a, b; + + if ((a = unhexchar(f[1])) < 0 || + (b = unhexchar(f[2])) < 0) { + /* Invalid escape code, let's take it literal then */ + *(t++) = '_'; + } else { + *(t++) = (char) ((a << 4) | b); + f += 2; + } + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + +_public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) { + sd_bus_creds *c; + pid_t pid = 0; + int r; + + assert_return(bus, -EINVAL); + assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP); + assert_return(ret, -EINVAL); + assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); + assert_return(!bus_pid_changed(bus), -ECHILD); + assert_return(!bus->is_kernel, -ENOTSUP); + + if (!bus->ucred_valid && !isempty(bus->label)) + return -ENODATA; + + c = bus_creds_new(); + if (!c) + return -ENOMEM; + + if (bus->ucred_valid) { + pid = c->pid = bus->ucred.pid; + c->uid = bus->ucred.uid; + c->gid = bus->ucred.gid; + + c->mask |= ((SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask) & bus->creds_mask; + } + + if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) { + c->label = strdup(bus->label); + if (!c->label) { + sd_bus_creds_unref(c); + return -ENOMEM; + } + + c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT | bus->creds_mask; + } + + r = bus_creds_add_more(c, mask, pid, 0); + if (r < 0) + return r; + + *ret = c; + return 0; +}