X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibelogind%2Fsd-bus%2Fbus-message.c;h=cbe9c6259cc21236cdf0dc0cbe5b2c1b13bb2c8d;hb=ec82d020534270d1979da2d0b27f84e1846b2851;hp=18685be8ff53d13db7776ca5114760c21bdaf3ab;hpb=c075af2785a99062537699a87b5f83dd666144b7;p=elogind.git diff --git a/src/libelogind/sd-bus/bus-message.c b/src/libelogind/sd-bus/bus-message.c index 18685be8f..cbe9c6259 100644 --- a/src/libelogind/sd-bus/bus-message.c +++ b/src/libelogind/sd-bus/bus-message.c @@ -1,5 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - /*** This file is part of systemd. @@ -23,19 +21,23 @@ #include #include -#include "util.h" -#include "utf8.h" -#include "strv.h" -#include "time-util.h" -#include "memfd-util.h" - #include "sd-bus.h" -#include "bus-message.h" + +#include "alloc-util.h" +#include "bus-gvariant.h" #include "bus-internal.h" -#include "bus-type.h" +#include "bus-message.h" #include "bus-signature.h" -#include "bus-gvariant.h" +#include "bus-type.h" #include "bus-util.h" +#include "fd-util.h" +#include "io-util.h" +#include "memfd-util.h" +#include "string-util.h" +#include "strv.h" +#include "time-util.h" +#include "utf8.h" +#include "util.h" static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored); @@ -113,8 +115,7 @@ static void message_reset_containers(sd_bus_message *m) { free(m->containers[i].offsets); } - free(m->containers); - m->containers = NULL; + m->containers = mfree(m->containers); m->n_containers = m->containers_allocated = 0; m->root_container.index = 0; @@ -144,11 +145,7 @@ static void message_free(sd_bus_message *m) { if (m->iovec != m->iovec_fixed) free(m->iovec); - if (m->destination_ptr) { - free(m->destination_ptr); - m->destination_ptr = NULL; - } - + m->destination_ptr = mfree(m->destination_ptr); message_reset_containers(m); free(m->root_container.signature); free(m->root_container.offsets); @@ -184,7 +181,7 @@ static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz, b if (!np) goto poison; } else { - /* Initially, the header is allocated as part of of + /* Initially, the header is allocated as part of * the sd_bus_message itself, let's replace it by * dynamic data */ @@ -608,8 +605,8 @@ static sd_bus_message *message_new(sd_bus *bus, uint8_t type) { m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message))); m->header->endian = BUS_NATIVE_ENDIAN; m->header->type = type; - m->header->version = bus ? bus->message_version : 1; - m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING); + m->header->version = bus->message_version; + m->allow_fds = bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING); m->root_container.need_offsets = BUS_MESSAGE_IS_GVARIANT(m); m->bus = sd_bus_ref(bus); @@ -803,7 +800,7 @@ _public_ int sd_bus_message_new_method_errorf( const char *format, ...) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; va_list ap; assert_return(name, -EINVAL); @@ -822,7 +819,7 @@ _public_ int sd_bus_message_new_method_errno( int error, const sd_bus_error *p) { - _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL; if (sd_bus_error_is_set(p)) return sd_bus_message_new_method_error(call, m, p); @@ -832,6 +829,7 @@ _public_ int sd_bus_message_new_method_errno( return sd_bus_message_new_method_error(call, m, &berror); } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_new_method_errnof( sd_bus_message *call, sd_bus_message **m, @@ -839,7 +837,7 @@ _public_ int sd_bus_message_new_method_errnof( const char *format, ...) { - _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL; va_list ap; va_start(ap, format); @@ -848,6 +846,7 @@ _public_ int sd_bus_message_new_method_errnof( return sd_bus_message_new_method_error(call, m, &berror); } +#endif // 0 void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m) { assert(bus); @@ -920,7 +919,9 @@ fail: } _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) { - assert_return(m, NULL); + + if (!m) + return NULL; assert(m->n_ref > 0); m->n_ref++; @@ -943,6 +944,7 @@ _public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) { return NULL; } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) { assert_return(m, -EINVAL); assert_return(type, -EINVAL); @@ -988,6 +990,7 @@ _public_ int sd_bus_message_get_auto_start(sd_bus_message *m) { return !(m->header->flags & BUS_MESSAGE_NO_AUTO_START); } +#endif // 0 _public_ int sd_bus_message_get_allow_interactive_authorization(sd_bus_message *m) { assert_return(m, -EINVAL); @@ -1028,11 +1031,14 @@ _public_ const char *sd_bus_message_get_sender(sd_bus_message *m) { _public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) { assert_return(m, NULL); - assert_return(sd_bus_error_is_set(&m->error), NULL); + + if (!sd_bus_error_is_set(&m->error)) + return NULL; return &m->error; } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_get_monotonic_usec(sd_bus_message *m, uint64_t *usec) { assert_return(m, -EINVAL); assert_return(usec, -EINVAL); @@ -1065,6 +1071,7 @@ _public_ int sd_bus_message_get_seqnum(sd_bus_message *m, uint64_t *seqnum) { *seqnum = m->seqnum; return 0; } +#endif // 0 _public_ sd_bus_creds *sd_bus_message_get_creds(sd_bus_message *m) { assert_return(m, NULL); @@ -1125,15 +1132,13 @@ _public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) return 1; } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_set_expect_reply(sd_bus_message *m, int b) { assert_return(m, -EINVAL); assert_return(!m->sealed, -EPERM); assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM); - if (b) - m->header->flags &= ~BUS_MESSAGE_NO_REPLY_EXPECTED; - else - m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED; + SET_FLAG(m->header->flags, BUS_MESSAGE_NO_REPLY_EXPECTED, !b); return 0; } @@ -1142,10 +1147,7 @@ _public_ int sd_bus_message_set_auto_start(sd_bus_message *m, int b) { assert_return(m, -EINVAL); assert_return(!m->sealed, -EPERM); - if (b) - m->header->flags &= ~BUS_MESSAGE_NO_AUTO_START; - else - m->header->flags |= BUS_MESSAGE_NO_AUTO_START; + SET_FLAG(m->header->flags, BUS_MESSAGE_NO_AUTO_START, !b); return 0; } @@ -1154,13 +1156,11 @@ _public_ int sd_bus_message_set_allow_interactive_authorization(sd_bus_message * assert_return(m, -EINVAL); assert_return(!m->sealed, -EPERM); - if (b) - m->header->flags |= BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION; - else - m->header->flags &= ~BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION; + SET_FLAG(m->header->flags, BUS_MESSAGE_ALLOW_INTERACTIVE_AUTHORIZATION, b); return 0; } +#endif // 0 static struct bus_container *message_get_container(sd_bus_message *m) { assert(m); @@ -1197,7 +1197,7 @@ struct bus_body_part *message_append_part(sd_bus_message *m) { part->memfd = -1; m->body_end = part; - m->n_body_parts ++; + m->n_body_parts++; return part; } @@ -1642,7 +1642,7 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void } if (type == SD_BUS_TYPE_UNIX_FD) - m->n_fds ++; + m->n_fds++; if (c->enclosing != SD_BUS_TYPE_ARRAY) c->index++; @@ -1655,6 +1655,7 @@ _public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const voi return message_append_basic(m, type, p, NULL); } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_append_string_space( sd_bus_message *m, size_t size, @@ -1745,6 +1746,7 @@ _public_ int sd_bus_message_append_string_iovec( return 0; } +#endif // 0 static int bus_message_open_array( sd_bus_message *m, @@ -2209,7 +2211,14 @@ static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c, assert(!c->need_offsets || i == c->n_offsets); assert(c->need_offsets || n_variable == 0); - if (n_variable <= 0) { + if (isempty(c->signature)) { + /* The unary type is encoded as fixed 1 byte padding */ + a = message_extend_body(m, 1, 1, add_offset, false); + if (!a) + return -ENOMEM; + + *a = 0; + } else if (n_variable <= 0) { int alignment = 1; /* Structures with fixed-size members only have to be @@ -2342,7 +2351,7 @@ static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const cha return 1; } -int bus_message_append_ap( +_public_ int sd_bus_message_appendv( sd_bus_message *m, const char *types, va_list ap) { @@ -2352,10 +2361,10 @@ int bus_message_append_ap( unsigned stack_ptr = 0; int r; - assert(m); - - if (!types) - return 0; + assert_return(m, -EINVAL); + assert_return(types, -EINVAL); + assert_return(!m->sealed, -EPERM); + assert_return(!m->poisoned, -ESTALE); n_array = (unsigned) -1; n_struct = strlen(types); @@ -2379,9 +2388,9 @@ int bus_message_append_ap( t = types; if (n_array != (unsigned) -1) - n_array --; + n_array--; else { - types ++; + types++; n_struct--; } @@ -2556,7 +2565,7 @@ _public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) { assert_return(!m->poisoned, -ESTALE); va_start(ap, types); - r = bus_message_append_ap(m, types, ap); + r = sd_bus_message_appendv(m, types, ap); va_end(ap); return r; @@ -2605,6 +2614,7 @@ _public_ int sd_bus_message_append_array_space( return 0; } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_append_array( sd_bus_message *m, char type, @@ -2623,8 +2633,7 @@ _public_ int sd_bus_message_append_array( if (r < 0) return r; - if (size > 0) - memcpy(p, ptr, size); + memcpy_safe(p, ptr, size); return 0; } @@ -2680,7 +2689,7 @@ _public_ int sd_bus_message_append_array_memfd( int r; assert_return(m, -EINVAL); - assert_return(memfd >= 0, -EINVAL); + assert_return(memfd >= 0, -EBADF); assert_return(bus_type_is_trivial(type), -EINVAL); assert_return(size > 0, -EINVAL); assert_return(!m->sealed, -EPERM); @@ -2756,7 +2765,7 @@ _public_ int sd_bus_message_append_string_memfd( int r; assert_return(m, -EINVAL); - assert_return(memfd >= 0, -EINVAL); + assert_return(memfd >= 0, -EBADF); assert_return(size > 0, -EINVAL); assert_return(!m->sealed, -EPERM); assert_return(!m->poisoned, -ESTALE); @@ -2839,6 +2848,7 @@ _public_ int sd_bus_message_append_string_memfd( return 0; } +#endif // 0 _public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) { char **i; @@ -2867,7 +2877,7 @@ static int bus_message_close_header(sd_bus_message *m) { /* The actual user data is finished now, we just complete the variant and struct now (at least on gvariant). Remember - this position, so that during parsing we know where to to + this position, so that during parsing we know where to put the outer container end. */ m->user_body_size = m->body_size; @@ -2899,18 +2909,20 @@ static int bus_message_close_header(sd_bus_message *m) { signature = strempty(m->root_container.signature); l = strlen(signature); - sz = bus_gvariant_determine_word_size(sizeof(struct bus_header) + ALIGN8(m->fields_size) + m->body_size + 1 + l, 1); - d = message_extend_body(m, 1, 1 + l + sz, false, true); + sz = bus_gvariant_determine_word_size(sizeof(struct bus_header) + ALIGN8(m->fields_size) + m->body_size + 1 + l + 2, 1); + d = message_extend_body(m, 1, 1 + l + 2 + sz, false, true); if (!d) return -ENOMEM; *(uint8_t*) d = 0; - memcpy((uint8_t*) d + 1, signature, l); + *((uint8_t*) d + 1) = SD_BUS_TYPE_STRUCT_BEGIN; + memcpy((uint8_t*) d + 2, signature, l); + *((uint8_t*) d + 1 + l + 1) = SD_BUS_TYPE_STRUCT_END; - bus_gvariant_write_word_le((uint8_t*) d + 1 + l, sz, sizeof(struct bus_header) + m->fields_size); + bus_gvariant_write_word_le((uint8_t*) d + 1 + l + 2, sz, sizeof(struct bus_header) + m->fields_size); m->footer = d; - m->footer_accessible = 1 + l + sz; + m->footer_accessible = 1 + l + 2 + sz; } else { m->header->dbus1.fields_size = m->fields_size; m->header->dbus1.body_size = m->body_size; @@ -3137,6 +3149,7 @@ static bool message_end_of_array(sd_bus_message *m, size_t index) { } } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) { assert_return(m, -EINVAL); assert_return(m->sealed, -EPERM); @@ -3152,6 +3165,7 @@ _public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) { return false; } +#endif // 0 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) { struct bus_body_part *part; @@ -3814,6 +3828,14 @@ static int build_struct_offsets( assert(n_offsets); if (isempty(signature)) { + /* Unary type is encoded as *fixed* 1 byte padding */ + r = message_peek_body(m, &m->rindex, 1, 1, &q); + if (r < 0) + return r; + + if (*(uint8_t *) q != 0) + return -EBADMSG; + *item_size = 0; *offsets = NULL; *n_offsets = 0; @@ -3849,7 +3871,7 @@ static int build_struct_offsets( if (r < 0) return r; if (r == 0 && p[n] != 0) /* except the last item */ - n_variable ++; + n_variable++; n_total++; p += n; @@ -3954,12 +3976,6 @@ static int enter_struct_or_dict_entry( if (r < 0) return r; - } else if (c->item_size <= 0) { - - /* gvariant empty struct */ - *item_size = 0; - *offsets = NULL; - *n_offsets = 0; } else /* gvariant with contents */ return build_struct_offsets(m, contents, c->item_size, item_size, offsets, n_offsets); @@ -4146,7 +4162,14 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m, w->before = before; w->begin = m->rindex; - w->end = m->rindex + c->item_size; + + /* Unary type has fixed size of 1, but virtual size of 0 */ + if (BUS_MESSAGE_IS_GVARIANT(m) && + type == SD_BUS_TYPE_STRUCT && + isempty(signature)) + w->end = m->rindex + 0; + else + w->end = m->rindex + c->item_size; w->array_size = array_size; w->item_size = item_size; @@ -4448,9 +4471,9 @@ static int message_read_ap( t = types; if (n_array != (unsigned) -1) - n_array --; + n_array--; else { - types ++; + types++; n_struct--; } @@ -4756,7 +4779,6 @@ _public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) { r = sd_bus_message_skip(m, s); if (r < 0) return r; - assert(r != 0); r = sd_bus_message_exit_container(m); if (r < 0) @@ -5164,11 +5186,21 @@ int bus_message_parse_fields(sd_bus_message *m) { return -EBADMSG; if (*p == 0) { + size_t l; char *c; - /* We found the beginning of the signature string, yay! */ + /* We found the beginning of the signature + * string, yay! We require the body to be a + * structure, so verify it and then strip the + * opening/closing brackets. */ - c = strndup(p + 1, ((char*) m->footer + m->footer_accessible) - p - (1 + sz)); + l = ((char*) m->footer + m->footer_accessible) - p - (1 + sz); + if (l < 2 || + p[1] != SD_BUS_TYPE_STRUCT_BEGIN || + p[1 + l - 1] != SD_BUS_TYPE_STRUCT_END) + return -EBADMSG; + + c = strndup(p + 1 + 1, l - 2); if (!c) return -ENOMEM; @@ -5517,6 +5549,7 @@ _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *desti return message_append_field_string(m, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination); } +#if 0 /// UNNEEDED by elogind int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) { size_t total; void *p, *e; @@ -5544,6 +5577,7 @@ int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) { return 0; } +#endif // 0 int bus_message_read_strv_extend(sd_bus_message *m, char ***l) { const char *s; @@ -5589,21 +5623,23 @@ _public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***l) { return 1; } -int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str, char ***strv) { - const char *contents; +static int bus_message_get_arg_skip( + sd_bus_message *m, + unsigned i, + char *_type, + const char **_contents) { + unsigned j; - char type; int r; - assert(m); - assert(str); - assert(strv); - r = sd_bus_message_rewind(m, true); if (r < 0) return r; for (j = 0;; j++) { + const char *contents; + char type; + r = sd_bus_message_peek_type(m, &type, &contents); if (r < 0) return r; @@ -5615,31 +5651,56 @@ int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str, char ** !(type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g"))) return -ENXIO; - if (j >= i) - break; + if (j >= i) { + if (_contents) + *_contents = contents; + if (_type) + *_type = type; + return 0; + } r = sd_bus_message_skip(m, NULL); if (r < 0) return r; } - if (type == SD_BUS_TYPE_ARRAY) { +} - r = sd_bus_message_read_strv(m, strv); - if (r < 0) - return r; +int bus_message_get_arg(sd_bus_message *m, unsigned i, const char **str) { + char type; + int r; - *str = NULL; + assert(m); + assert(str); - } else { - r = sd_bus_message_read_basic(m, type, str); - if (r < 0) - return r; + r = bus_message_get_arg_skip(m, i, &type, NULL); + if (r < 0) + return r; - *strv = NULL; - } + if (!IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) + return -ENXIO; - return 0; + return sd_bus_message_read_basic(m, type, str); +} + +int bus_message_get_arg_strv(sd_bus_message *m, unsigned i, char ***strv) { + const char *contents; + char type; + int r; + + assert(m); + assert(strv); + + r = bus_message_get_arg_skip(m, i, &type, &contents); + if (r < 0) + return r; + + if (type != SD_BUS_TYPE_ARRAY) + return -ENXIO; + if (!STR_IN_SET(contents, "s", "o", "g")) + return -ENXIO; + + return sd_bus_message_read_strv(m, strv); } _public_ int sd_bus_message_get_errno(sd_bus_message *m) { @@ -5660,6 +5721,7 @@ _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complet return strempty(c->signature); } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_is_empty(sd_bus_message *m) { assert_return(m, -EINVAL); @@ -5671,6 +5733,7 @@ _public_ int sd_bus_message_has_signature(sd_bus_message *m, const char *signatu return streq(strempty(m->root_container.signature), strempty(signature)); } +#endif // 0 _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) { bool done_something = false; @@ -5751,6 +5814,7 @@ _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int return done_something; } +#if 0 /// UNNEEDED by elogind _public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) { const char *c; char t; @@ -5775,6 +5839,7 @@ _public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char return 1; } +#endif // 0 _public_ sd_bus *sd_bus_message_get_bus(sd_bus_message *m) { assert_return(m, NULL); @@ -5783,7 +5848,7 @@ _public_ sd_bus *sd_bus_message_get_bus(sd_bus_message *m) { } int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) { - _cleanup_bus_message_unref_ sd_bus_message *n = NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *n = NULL; usec_t timeout; int r; @@ -5867,6 +5932,7 @@ int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) { return 0; } +#if 0 /// UNNEEDED by elogind int bus_message_append_sender(sd_bus_message *m, const char *sender) { assert(m); assert(sender); @@ -5892,3 +5958,4 @@ _public_ int sd_bus_message_set_priority(sd_bus_message *m, int64_t priority) { m->priority = priority; return 0; } +#endif // 0