diff options
author | Michael Biebl <biebl@debian.org> | 2018-12-21 22:06:22 +0100 |
---|---|---|
committer | Michael Biebl <biebl@debian.org> | 2018-12-21 22:06:22 +0100 |
commit | 6e866b331d7cd4a5e0759dd160dea6edabd3678e (patch) | |
tree | 4d24c1ffe4ae946f04d8910956090e8d13aecd9a /src/libsystemd/sd-bus | |
parent | b012e92123bdc9fa10c2f079ec5bd9313b23e21a (diff) | |
download | systemd-6e866b331d7cd4a5e0759dd160dea6edabd3678e.tar.gz |
New upstream version 240
Diffstat (limited to 'src/libsystemd/sd-bus')
54 files changed, 679 insertions, 811 deletions
diff --git a/src/libsystemd/sd-bus/bus-common-errors.c b/src/libsystemd/sd-bus/bus-common-errors.c index ff0790bf5a..6e5fe00e06 100644 --- a/src/libsystemd/sd-bus/bus-common-errors.c +++ b/src/libsystemd/sd-bus/bus-common-errors.c @@ -41,6 +41,9 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_common_errors[] = { SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_USER_MAPPING, ENXIO), SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_GROUP_MAPPING, ENXIO), + SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_PORTABLE_IMAGE, ENOENT), + SD_BUS_ERROR_MAP(BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE, EMEDIUMTYPE), + SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_SESSION, ENXIO), SD_BUS_ERROR_MAP(BUS_ERROR_NO_SESSION_FOR_PID, ENXIO), SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_USER, ENXIO), @@ -71,6 +74,8 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_common_errors[] = { SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_LINK, ENXIO), SD_BUS_ERROR_MAP(BUS_ERROR_LINK_BUSY, EBUSY), SD_BUS_ERROR_MAP(BUS_ERROR_NETWORK_DOWN, ENETDOWN), + SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_DNSSD_SERVICE, ENOENT), + SD_BUS_ERROR_MAP(BUS_ERROR_DNSSD_SERVICE_EXISTS, EEXIST), SD_BUS_ERROR_MAP(_BUS_ERROR_DNS "FORMERR", EBADMSG), SD_BUS_ERROR_MAP(_BUS_ERROR_DNS "SERVFAIL", EHOSTDOWN), @@ -94,5 +99,7 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_common_errors[] = { SD_BUS_ERROR_MAP(BUS_ERROR_NO_SUCH_TRANSFER, ENXIO), SD_BUS_ERROR_MAP(BUS_ERROR_TRANSFER_IN_PROGRESS, EBUSY), + SD_BUS_ERROR_MAP(BUS_ERROR_NO_PRODUCT_UUID, EOPNOTSUPP), + SD_BUS_ERROR_MAP_END }; diff --git a/src/libsystemd/sd-bus/bus-common-errors.h b/src/libsystemd/sd-bus/bus-common-errors.h index 3945c7f6ac..8339feb768 100644 --- a/src/libsystemd/sd-bus/bus-common-errors.h +++ b/src/libsystemd/sd-bus/bus-common-errors.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "bus-error.h" #define BUS_ERROR_NO_SUCH_UNIT "org.freedesktop.systemd1.NoSuchUnit" @@ -40,6 +37,7 @@ #define BUS_ERROR_NO_SUCH_GROUP_MAPPING "org.freedesktop.machine1.NoSuchGroupMapping" #define BUS_ERROR_NO_SUCH_PORTABLE_IMAGE "org.freedesktop.portable1.NoSuchImage" +#define BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE "org.freedesktop.portable1.BadImageType" #define BUS_ERROR_NO_SUCH_SESSION "org.freedesktop.login1.NoSuchSession" #define BUS_ERROR_NO_SESSION_FOR_PID "org.freedesktop.login1.NoSessionForPID" @@ -78,4 +76,6 @@ #define BUS_ERROR_NO_SUCH_TRANSFER "org.freedesktop.import1.NoSuchTransfer" #define BUS_ERROR_TRANSFER_IN_PROGRESS "org.freedesktop.import1.TransferInProgress" +#define BUS_ERROR_NO_PRODUCT_UUID "org.freedesktop.hostname1.NoProductUUID" + BUS_ERROR_MAP_ELF_USE(bus_common_errors); diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c index f50274a6a4..2cfeefc2c3 100644 --- a/src/libsystemd/sd-bus/bus-container.c +++ b/src/libsystemd/sd-bus/bus-container.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <fcntl.h> #include <unistd.h> @@ -47,51 +45,27 @@ int bus_container_connect_socket(sd_bus *b) { if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) return -errno; - r = safe_fork("(sd-buscntr)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child); + r = namespace_fork("(sd-buscntrns)", "(sd-buscntr)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, + pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child); if (r < 0) return r; if (r == 0) { - pid_t grandchild; - pair[0] = safe_close(pair[0]); - r = namespace_enter(pidnsfd, mntnsfd, -1, usernsfd, rootfd); - if (r < 0) - _exit(EXIT_FAILURE); - - /* We just changed PID namespace, however it will only - * take effect on the children we now fork. Hence, - * let's fork another time, and connect from this - * grandchild, so that SO_PEERCRED of our connection - * comes from a process from within the container, and - * not outside of it */ - - r = safe_fork("(sd-buscntr2)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &grandchild); - if (r < 0) + r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size); + if (r < 0) { + /* Try to send error up */ + error_buf = errno; + (void) write(pair[1], &error_buf, sizeof(error_buf)); _exit(EXIT_FAILURE); - if (r == 0) { - - r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size); - if (r < 0) { - /* Try to send error up */ - error_buf = errno; - (void) write(pair[1], &error_buf, sizeof(error_buf)); - _exit(EXIT_FAILURE); - } - - _exit(EXIT_SUCCESS); } - r = wait_for_terminate_and_check("(sd-buscntr2)", grandchild, 0); - if (r < 0) - _exit(EXIT_FAILURE); - - _exit(r); + _exit(EXIT_SUCCESS); } pair[1] = safe_close(pair[1]); - r = wait_for_terminate_and_check("(sd-buscntr)", child, 0); + r = wait_for_terminate_and_check("(sd-buscntrns)", child, 0); if (r < 0) return r; if (r != EXIT_SUCCESS) diff --git a/src/libsystemd/sd-bus/bus-container.h b/src/libsystemd/sd-bus/bus-container.h index dd115b4e24..f6ef688032 100644 --- a/src/libsystemd/sd-bus/bus-container.h +++ b/src/libsystemd/sd-bus/bus-container.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "sd-bus.h" int bus_container_connect_socket(sd_bus *b); diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c index 18a2cc2c9b..7775d2b376 100644 --- a/src/libsystemd/sd-bus/bus-control.c +++ b/src/libsystemd/sd-bus/bus-control.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #if HAVE_VALGRIND_MEMCHECK_H #include <valgrind/memcheck.h> @@ -432,7 +430,7 @@ _public_ int sd_bus_get_name_creds( _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_unique = NULL, *reply = NULL; _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL; - const char *unique = NULL; + const char *unique; pid_t pid = 0; int r; @@ -461,9 +459,12 @@ _public_ int sd_bus_get_name_creds( if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; - /* Only query the owner if the caller wants to know it or if - * the caller just wants to check whether a name exists */ - if ((mask & SD_BUS_CREDS_UNIQUE_NAME) || mask == 0) { + /* If the name is unique anyway, we can use it directly */ + unique = name[0] == ':' ? name : NULL; + + /* Only query the owner if the caller wants to know it and the name is not unique anyway, or if the caller just + * wants to check whether a name exists */ + if ((FLAGS_SET(mask, SD_BUS_CREDS_UNIQUE_NAME) && !unique) || mask == 0) { r = sd_bus_call_method( bus, "org.freedesktop.DBus", @@ -485,6 +486,7 @@ _public_ int sd_bus_get_name_creds( if (mask != 0) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; bool need_pid, need_uid, need_selinux, need_separate_calls; + c = bus_creds_new(); if (!c) return -ENOMEM; @@ -663,7 +665,7 @@ _public_ int sd_bus_get_name_creds( NULL, &reply, "s", - unique ? unique : name); + unique ?: name); if (r < 0) return r; @@ -690,7 +692,7 @@ _public_ int sd_bus_get_name_creds( &error, &reply, "s", - unique ? unique : name); + unique ?: name); if (r < 0) { if (!sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown")) return r; @@ -701,7 +703,7 @@ _public_ int sd_bus_get_name_creds( if (r < 0) return r; - c->label = strndup(p, sz); + c->label = memdup_suffix0(p, sz); if (!c->label) return -ENOMEM; diff --git a/src/libsystemd/sd-bus/bus-control.h b/src/libsystemd/sd-bus/bus-control.h index 9017297496..3fb52b67c6 100644 --- a/src/libsystemd/sd-bus/bus-control.h +++ b/src/libsystemd/sd-bus/bus-control.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "sd-bus.h" int bus_add_match_internal(sd_bus *bus, const char *match); diff --git a/src/libsystemd/sd-bus/bus-convenience.c b/src/libsystemd/sd-bus/bus-convenience.c index 41910515db..c4d4016fc0 100644 --- a/src/libsystemd/sd-bus/bus-convenience.c +++ b/src/libsystemd/sd-bus/bus-convenience.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "bus-internal.h" #include "bus-message.h" diff --git a/src/libsystemd/sd-bus/bus-creds.c b/src/libsystemd/sd-bus/bus-creds.c index aae9fcd58b..81d97ff968 100644 --- a/src/libsystemd/sd-bus/bus-creds.c +++ b/src/libsystemd/sd-bus/bus-creds.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <linux/capability.h> #include <stdlib.h> @@ -651,19 +649,22 @@ _public_ int sd_bus_creds_get_description(sd_bus_creds *c, const char **ret) { return 0; } -static int has_cap(sd_bus_creds *c, unsigned offset, int capability) { +static int has_cap(sd_bus_creds *c, size_t offset, int capability) { + unsigned long lc; size_t sz; assert(c); assert(capability >= 0); assert(c->capability); - if ((unsigned) capability > cap_last_cap()) + lc = cap_last_cap(); + + if ((unsigned long) capability > lc) return 0; - sz = DIV_ROUND_UP(cap_last_cap(), 32U); + sz = DIV_ROUND_UP(lc, 32LU); - return !!(c->capability[offset * sz + CAP_TO_INDEX(capability)] & CAP_TO_MASK(capability)); + return !!(c->capability[offset * sz + CAP_TO_INDEX((uint32_t) capability)] & CAP_TO_MASK_CORRECTED((uint32_t) capability)); } _public_ int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability) { @@ -802,10 +803,15 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) { else if (!IN_SET(errno, EPERM, EACCES)) return -errno; } else { - char line[LINE_MAX]; - FOREACH_LINE(line, f, return -errno) { - truncate_nl(line); + for (;;) { + _cleanup_free_ char *line = NULL; + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; if (missing & SD_BUS_CREDS_PPID) { p = startswith(line, "PPid:"); diff --git a/src/libsystemd/sd-bus/bus-creds.h b/src/libsystemd/sd-bus/bus-creds.h index 7b77a1d735..508ef9d352 100644 --- a/src/libsystemd/sd-bus/bus-creds.h +++ b/src/libsystemd/sd-bus/bus-creds.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <stdbool.h> #include "sd-bus.h" diff --git a/src/libsystemd/sd-bus/bus-dump.c b/src/libsystemd/sd-bus/bus-dump.c index 3a28c7c6e3..9a6a81d7aa 100644 --- a/src/libsystemd/sd-bus/bus-dump.c +++ b/src/libsystemd/sd-bus/bus-dump.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ + +#include <sys/time.h> #include "alloc-util.h" #include "bus-dump.h" @@ -59,8 +59,14 @@ int bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags) { "%s%s%s Type=%s%s%s Endian=%c Flags=%u Version=%u Priority=%"PRIi64, m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() : m->header->type == SD_BUS_MESSAGE_METHOD_RETURN ? ansi_highlight_green() : - m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", special_glyph(TRIANGULAR_BULLET), ansi_normal(), - ansi_highlight(), bus_message_type_to_string(m->header->type), ansi_normal(), + m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", + special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), + ansi_normal(), + + ansi_highlight(), + bus_message_type_to_string(m->header->type) ?: "(unknown)", + ansi_normal(), + m->header->endian, m->header->flags, m->header->version, diff --git a/src/libsystemd/sd-bus/bus-dump.h b/src/libsystemd/sd-bus/bus-dump.h index 8e47411a45..373a86dd4f 100644 --- a/src/libsystemd/sd-bus/bus-dump.h +++ b/src/libsystemd/sd-bus/bus-dump.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <stdbool.h> #include <stdio.h> diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c index ec359ac13c..dc952375b6 100644 --- a/src/libsystemd/sd-bus/bus-error.c +++ b/src/libsystemd/sd-bus/bus-error.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <errno.h> #include <stdarg.h> @@ -56,8 +54,8 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = { }; /* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */ -extern const sd_bus_error_map __start_BUS_ERROR_MAP[]; -extern const sd_bus_error_map __stop_BUS_ERROR_MAP[]; +extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[]; +extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[]; /* Additional maps registered with sd_bus_error_add_map() are in this * NULL terminated array */ @@ -91,9 +89,8 @@ static int bus_error_name_to_errno(const char *name) { return m->code; } - m = __start_BUS_ERROR_MAP; -#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - while (m < __stop_BUS_ERROR_MAP) { + m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*)); + while (m < __stop_SYSTEMD_BUS_ERROR_MAP) { /* For magic ELF error maps, the end marker might * appear in the middle of things, since multiple maps * might appear in the same section. Hence, let's skip @@ -101,7 +98,7 @@ static int bus_error_name_to_errno(const char *name) { * boundary, which is the selected alignment for the * arrays. */ if (m->code == BUS_ERROR_MAP_END_MARKER) { - m = ALIGN8_PTR(m+1); + m = ALIGN_TO_PTR(m + 1, sizeof(void*)); continue; } @@ -110,7 +107,6 @@ static int bus_error_name_to_errno(const char *name) { m++; } -#endif return EIO; } @@ -207,8 +203,7 @@ _public_ void sd_bus_error_free(sd_bus_error *e) { free((void*) e->message); } - e->name = e->message = NULL; - e->_need_free = 0; + *e = SD_BUS_ERROR_NULL; } _public_ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) { @@ -310,6 +305,28 @@ finish: return -bus_error_name_to_errno(e->name); } +_public_ int sd_bus_error_move(sd_bus_error *dest, sd_bus_error *e) { + int r; + + if (!sd_bus_error_is_set(e)) { + + if (dest) + *dest = SD_BUS_ERROR_NULL; + + return 0; + } + + r = -bus_error_name_to_errno(e->name); + + if (dest) { + *dest = *e; + *e = SD_BUS_ERROR_NULL; + } else + sd_bus_error_free(e); + + return r; +} + _public_ int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) { if (!name) return 0; diff --git a/src/libsystemd/sd-bus/bus-error.h b/src/libsystemd/sd-bus/bus-error.h index 93cb9acd91..a6523e57a2 100644 --- a/src/libsystemd/sd-bus/bus-error.h +++ b/src/libsystemd/sd-bus/bus-error.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <stdbool.h> #include "sd-bus.h" @@ -34,13 +31,15 @@ int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_lis */ #define BUS_ERROR_MAP_ELF_REGISTER \ - __attribute__ ((__section__("BUS_ERROR_MAP"))) \ - __attribute__ ((__used__)) \ - __attribute__ ((aligned(8))) + _section_("SYSTEMD_BUS_ERROR_MAP") \ + _used_ \ + _alignptr_ \ + _variable_no_sanitize_address_ #define BUS_ERROR_MAP_ELF_USE(errors) \ extern const sd_bus_error_map errors[]; \ - __attribute__ ((used)) static const sd_bus_error_map * const CONCATENATE(errors ## _copy_, __COUNTER__) = errors; + _used_ \ + static const sd_bus_error_map * const CONCATENATE(errors ## _copy_, __COUNTER__) = errors; /* We use something exotic as end marker, to ensure people build the * maps using the macsd-ros. */ diff --git a/src/libsystemd/sd-bus/bus-gvariant.c b/src/libsystemd/sd-bus/bus-gvariant.c index 05b17589dd..ba503b3213 100644 --- a/src/libsystemd/sd-bus/bus-gvariant.c +++ b/src/libsystemd/sd-bus/bus-gvariant.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <errno.h> #include <string.h> diff --git a/src/libsystemd/sd-bus/bus-gvariant.h b/src/libsystemd/sd-bus/bus-gvariant.h index 40e3053ec6..644b5f4b20 100644 --- a/src/libsystemd/sd-bus/bus-gvariant.h +++ b/src/libsystemd/sd-bus/bus-gvariant.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "macro.h" int bus_gvariant_get_size(const char *signature) _pure_; diff --git a/src/libsystemd/sd-bus/bus-internal.c b/src/libsystemd/sd-bus/bus-internal.c index 7bb653338d..40acae2133 100644 --- a/src/libsystemd/sd-bus/bus-internal.c +++ b/src/libsystemd/sd-bus/bus-internal.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "alloc-util.h" #include "bus-internal.h" @@ -153,26 +151,6 @@ bool service_name_is_valid(const char *p) { return true; } -char* service_name_startswith(const char *a, const char *b) { - const char *p; - - if (!service_name_is_valid(a) || - !service_name_is_valid(b)) - return NULL; - - p = startswith(a, b); - if (!p) - return NULL; - - if (*p == 0) - return (char*) p; - - if (*p == '.') - return (char*) p + 1; - - return NULL; -} - bool member_name_is_valid(const char *p) { const char *q; diff --git a/src/libsystemd/sd-bus/bus-internal.h b/src/libsystemd/sd-bus/bus-internal.h index 2087ef8eeb..f208b294d8 100644 --- a/src/libsystemd/sd-bus/bus-internal.h +++ b/src/libsystemd/sd-bus/bus-internal.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <pthread.h> #include <sys/socket.h> @@ -214,6 +211,7 @@ struct sd_bus { bool accept_fd:1; bool attach_timestamp:1; bool connected_signal:1; + bool close_on_exit:1; int use_memfd; @@ -307,8 +305,6 @@ struct sd_bus { sd_bus **default_bus_ptr; pid_t tid; - char *cgroup_root; - char *description; char *patch_sender; @@ -319,9 +315,12 @@ struct sd_bus { int *inotify_watches; size_t n_inotify_watches; + + /* zero means use value specified by $SYSTEMD_BUS_TIMEOUT= environment variable or built-in default */ + usec_t method_call_timeout; }; -/* For method calls we time-out at 25s, like in the D-Bus reference implementation */ +/* For method calls we timeout at 25s, like in the D-Bus reference implementation */ #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) /* For the authentication phase we grant 90s, to provide extra room during boot, when RNGs and such are not filled up @@ -336,8 +335,7 @@ struct sd_bus { #define BUS_CONTAINER_DEPTH 128 -/* Defined by the specification as maximum size of an array in - * bytes */ +/* Defined by the specification as maximum size of an array in bytes */ #define BUS_ARRAY_MAX_SIZE 67108864 #define BUS_FDS_MAX 1024 @@ -346,7 +344,6 @@ struct sd_bus { bool interface_name_is_valid(const char *p) _pure_; bool service_name_is_valid(const char *p) _pure_; -char* service_name_startswith(const char *a, const char *b); bool member_name_is_valid(const char *p) _pure_; bool object_path_is_valid(const char *p) _pure_; char *object_path_startswith(const char *a, const char *b) _pure_; @@ -384,12 +381,11 @@ void bus_close_io_fds(sd_bus *b); #define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \ for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \ - _slash && !(_slash[(_slash) == (prefix)] = 0); \ + _slash && ((_slash[(_slash) == (prefix)] = 0), true); \ _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/')) /* If we are invoking callbacks of a bus object, ensure unreffing the - * bus from the callback doesn't destroy the object we are working - * on */ + * bus from the callback doesn't destroy the object we are working on */ #define BUS_DONT_DESTROY(bus) \ _cleanup_(sd_bus_unrefp) _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus) @@ -398,8 +394,6 @@ int bus_set_address_user(sd_bus *bus); int bus_set_address_system_remote(sd_bus *b, const char *host); int bus_set_address_system_machine(sd_bus *b, const char *machine); -int bus_get_root_path(sd_bus *bus); - int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error); #define bus_assert_return(expr, r, error) \ diff --git a/src/libsystemd/sd-bus/bus-introspect.c b/src/libsystemd/sd-bus/bus-introspect.c index cfcbd8b072..f623dd9ce0 100644 --- a/src/libsystemd/sd-bus/bus-introspect.c +++ b/src/libsystemd/sd-bus/bus-introspect.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <stdio_ext.h> @@ -63,7 +61,7 @@ int introspect_write_child_nodes(struct introspect *i, Set *s, const char *prefi return 0; } -static void introspect_write_flags(struct introspect *i, int type, int flags) { +static void introspect_write_flags(struct introspect *i, int type, uint64_t flags) { if (flags & SD_BUS_VTABLE_DEPRECATED) fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f); diff --git a/src/libsystemd/sd-bus/bus-introspect.h b/src/libsystemd/sd-bus/bus-introspect.h index 5dcaeace9d..bb2dd7ef7b 100644 --- a/src/libsystemd/sd-bus/bus-introspect.h +++ b/src/libsystemd/sd-bus/bus-introspect.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <stdio.h> #include "sd-bus.h" diff --git a/src/libsystemd/sd-bus/bus-kernel.c b/src/libsystemd/sd-bus/bus-kernel.c index 1f61bd3f95..34c8a9f8c8 100644 --- a/src/libsystemd/sd-bus/bus-kernel.c +++ b/src/libsystemd/sd-bus/bus-kernel.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #if HAVE_VALGRIND_MEMCHECK_H #include <valgrind/memcheck.h> diff --git a/src/libsystemd/sd-bus/bus-kernel.h b/src/libsystemd/sd-bus/bus-kernel.h index 44c9a76311..fbbc43f6fa 100644 --- a/src/libsystemd/sd-bus/bus-kernel.h +++ b/src/libsystemd/sd-bus/bus-kernel.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "sd-bus.h" #define MEMFD_CACHE_MAX 32 diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index 7d04cc4bd1..ad135406f6 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <stdio_ext.h> @@ -544,43 +542,6 @@ fail: return r; } -static int bus_match_find_compare_value( - struct bus_match_node *where, - enum bus_match_node_type t, - uint8_t value_u8, - const char *value_str, - struct bus_match_node **ret) { - - struct bus_match_node *c, *n; - - assert(where); - assert(IN_SET(where->type, BUS_MATCH_ROOT, BUS_MATCH_VALUE)); - assert(BUS_MATCH_IS_COMPARE(t)); - assert(ret); - - for (c = where->child; c && c->type != t; c = c->next) - ; - - if (!c) - return 0; - - if (t == BUS_MATCH_MESSAGE_TYPE) - n = hashmap_get(c->compare.children, UINT_TO_PTR(value_u8)); - else if (BUS_MATCH_CAN_HASH(t)) - n = hashmap_get(c->compare.children, value_str); - else { - for (n = c->child; n && !value_node_same(n, t, value_u8, value_str); n = n->next) - ; - } - - if (n) { - *ret = n; - return 1; - } - - return 0; -} - static int bus_match_add_leaf( struct bus_match_node *where, struct match_callback *callback) { @@ -609,34 +570,6 @@ static int bus_match_add_leaf( return 1; } -static int bus_match_find_leaf( - struct bus_match_node *where, - sd_bus_message_handler_t callback, - void *userdata, - struct bus_match_node **ret) { - - struct bus_match_node *c; - - assert(where); - assert(IN_SET(where->type, BUS_MATCH_ROOT, BUS_MATCH_VALUE)); - assert(ret); - - for (c = where->child; c; c = c->next) { - sd_bus_slot *s; - - s = container_of(c->leaf.callback, sd_bus_slot, match_callback); - - if (c->type == BUS_MATCH_LEAF && - c->leaf.callback->callback == callback && - s->userdata == userdata) { - *ret = c; - return 1; - } - } - - return 0; -} - enum bus_match_node_type bus_match_node_type_from_string(const char *k, size_t n) { assert(k); @@ -762,15 +695,8 @@ enum bus_match_node_type bus_match_node_type_from_string(const char *k, size_t n return -EINVAL; } -static int match_component_compare(const void *a, const void *b) { - const struct bus_match_component *x = a, *y = b; - - if (x->type < y->type) - return -1; - if (x->type > y->type) - return 1; - - return 0; +static int match_component_compare(const struct bus_match_component *a, const struct bus_match_component *b) { + return CMP(a->type, b->type); } void bus_match_parse_free(struct bus_match_component *components, unsigned n_components) { @@ -903,7 +829,7 @@ int bus_match_parse( } /* Order the whole thing, so that we always generate the same tree */ - qsort_safe(components, n_components, sizeof(struct bus_match_component), match_component_compare); + typesafe_qsort(components, n_components, match_component_compare); /* Check for duplicates */ for (i = 0; i+1 < n_components; i++) @@ -938,7 +864,7 @@ char *bus_match_to_string(struct bus_match_component *components, unsigned n_com if (!f) return NULL; - __fsetlocking(f, FSETLOCKING_BYCALLER); + (void) __fsetlocking(f, FSETLOCKING_BYCALLER); for (i = 0; i < n_components; i++) { char buf[32]; @@ -1023,43 +949,6 @@ int bus_match_remove( return 1; } -int bus_match_find( - struct bus_match_node *root, - struct bus_match_component *components, - unsigned n_components, - sd_bus_message_handler_t callback, - void *userdata, - struct match_callback **ret) { - - struct bus_match_node *n, **gc; - unsigned i; - int r; - - assert(root); - assert(ret); - - gc = newa(struct bus_match_node*, n_components); - - n = root; - for (i = 0; i < n_components; i++) { - r = bus_match_find_compare_value( - n, components[i].type, - components[i].value_u8, components[i].value_str, - &n); - if (r <= 0) - return r; - - gc[i] = n; - } - - r = bus_match_find_leaf(n, callback, userdata, &n); - if (r <= 0) - return r; - - *ret = n->leaf.callback; - return 1; -} - void bus_match_free(struct bus_match_node *node) { struct bus_match_node *c; diff --git a/src/libsystemd/sd-bus/bus-match.h b/src/libsystemd/sd-bus/bus-match.h index 050f4ba033..a6f67ce089 100644 --- a/src/libsystemd/sd-bus/bus-match.h +++ b/src/libsystemd/sd-bus/bus-match.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "sd-bus.h" #include "hashmap.h" @@ -69,8 +66,6 @@ int bus_match_run(sd_bus *bus, struct bus_match_node *root, sd_bus_message *m); int bus_match_add(struct bus_match_node *root, struct bus_match_component *components, unsigned n_components, struct match_callback *callback); int bus_match_remove(struct bus_match_node *root, struct match_callback *callback); -int bus_match_find(struct bus_match_node *root, struct bus_match_component *components, unsigned n_components, sd_bus_message_handler_t callback, void *userdata, struct match_callback **ret); - void bus_match_free(struct bus_match_node *node); void bus_match_dump(struct bus_match_node *node, unsigned level); diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c index 8d92bc2002..bb7e09c945 100644 --- a/src/libsystemd/sd-bus/bus-message.c +++ b/src/libsystemd/sd-bus/bus-message.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <errno.h> #include <fcntl.h> @@ -77,19 +75,38 @@ static void message_reset_parts(sd_bus_message *m) { m->cached_rindex_part_begin = 0; } -static void message_reset_containers(sd_bus_message *m) { - unsigned i; +static struct bus_container *message_get_last_container(sd_bus_message *m) { + assert(m); + + if (m->n_containers == 0) + return &m->root_container; + + assert(m->containers); + return m->containers + m->n_containers - 1; +} + +static void message_free_last_container(sd_bus_message *m) { + struct bus_container *c; + c = message_get_last_container(m); + + free(c->signature); + free(c->peeked_signature); + free(c->offsets); + + /* Move to previous container, but not if we are on root container */ + if (m->n_containers > 0) + m->n_containers--; +} + +static void message_reset_containers(sd_bus_message *m) { assert(m); - for (i = 0; i < m->n_containers; i++) { - free(m->containers[i].signature); - free(m->containers[i].offsets); - } + while (m->n_containers > 0) + message_free_last_container(m); m->containers = mfree(m->containers); - - m->n_containers = m->containers_allocated = 0; + m->containers_allocated = 0; m->root_container.index = 0; } @@ -112,10 +129,8 @@ static sd_bus_message* message_free(sd_bus_message *m) { free(m->iovec); message_reset_containers(m); - free(m->root_container.signature); - free(m->root_container.offsets); - - free(m->root_container.peeked_signature); + assert(m->n_containers == 0); + message_free_last_container(m); bus_creds_done(&m->creds); return mfree(m); @@ -210,7 +225,7 @@ static int message_append_field_string( /* dbus1 doesn't allow strings over 32bit, let's enforce this * globally, to not risk convertability */ l = strlen(s); - if (l > (size_t) (uint32_t) -1) + if (l > UINT32_MAX) return -EINVAL; /* Signature "(yv)" where the variant contains "s" */ @@ -539,8 +554,7 @@ int bus_message_from_malloc( m->n_iovec = 1; m->iovec = m->iovec_fixed; - m->iovec[0].iov_base = buffer; - m->iovec[0].iov_len = length; + m->iovec[0] = IOVEC_MAKE(buffer, length); r = bus_message_parse_fields(m); if (r < 0) @@ -876,30 +890,7 @@ int bus_message_new_synthetic_error( return 0; } -_public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) { - - if (!m) - return NULL; - - assert(m->n_ref > 0); - m->n_ref++; - - return m; -} - -_public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) { - - if (!m) - return NULL; - - assert(m->n_ref > 0); - m->n_ref--; - - if (m->n_ref > 0) - return NULL; - - return message_free(m); -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_message, sd_bus_message, message_free); _public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) { assert_return(m, -EINVAL); @@ -1113,16 +1104,6 @@ _public_ int sd_bus_message_set_allow_interactive_authorization(sd_bus_message * return 0; } -static struct bus_container *message_get_container(sd_bus_message *m) { - assert(m); - - if (m->n_containers == 0) - return &m->root_container; - - assert(m->containers); - return m->containers + m->n_containers - 1; -} - struct bus_body_part *message_append_part(sd_bus_message *m) { struct bus_body_part *part; @@ -1213,7 +1194,7 @@ static int message_add_offset(sd_bus_message *m, size_t offset) { /* Add offset to current container, unless this is the first * item in it, which will have the 0 offset, which we can * ignore. */ - c = message_get_container(m); + c = message_get_last_container(m); if (!c->need_offsets) return 0; @@ -1385,7 +1366,7 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void assert_return(bus_type_is_basic(type), -EINVAL); assert_return(!m->poisoned, -ESTALE); - c = message_get_container(m); + c = message_get_last_container(m); if (c->signature && c->signature[c->index]) { /* Container signature is already set */ @@ -1578,7 +1559,7 @@ _public_ int sd_bus_message_append_string_space( assert_return(!m->sealed, -EPERM); assert_return(!m->poisoned, -ESTALE); - c = message_get_container(m); + c = message_get_last_container(m); if (c->signature && c->signature[c->index]) { /* Container signature is already set */ @@ -1949,7 +1930,7 @@ _public_ int sd_bus_message_open_container( char type, const char *contents) { - struct bus_container *c, *w; + struct bus_container *c; uint32_t *array_size = NULL; _cleanup_free_ char *signature = NULL; size_t before, begin = 0; @@ -1967,7 +1948,7 @@ _public_ int sd_bus_message_open_container( return -ENOMEM; } - c = message_get_container(m); + c = message_get_last_container(m); signature = strdup(contents); if (!signature) { @@ -1994,16 +1975,14 @@ _public_ int sd_bus_message_open_container( return r; /* OK, let's fill it in */ - w = m->containers + m->n_containers++; - w->enclosing = type; - w->signature = TAKE_PTR(signature); - w->index = 0; - w->array_size = array_size; - w->before = before; - w->begin = begin; - w->n_offsets = w->offsets_allocated = 0; - w->offsets = NULL; - w->need_offsets = need_offsets; + m->containers[m->n_containers++] = (struct bus_container) { + .enclosing = type, + .signature = TAKE_PTR(signature), + .array_size = array_size, + .before = before, + .begin = begin, + .need_offsets = need_offsets, + }; return 0; } @@ -2194,7 +2173,7 @@ _public_ int sd_bus_message_close_container(sd_bus_message *m) { assert_return(m->n_containers > 0, -EINVAL); assert_return(!m->poisoned, -ESTALE); - c = message_get_container(m); + c = message_get_last_container(m); if (c->enclosing != SD_BUS_TYPE_ARRAY) if (c->signature && c->signature[c->index] != 0) @@ -2464,11 +2443,6 @@ _public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) { va_list ap; int r; - assert_return(m, -EINVAL); - assert_return(types, -EINVAL); - assert_return(!m->sealed, -EPERM); - assert_return(!m->poisoned, -ESTALE); - va_start(ap, types); r = sd_bus_message_appendv(m, types, ap); va_end(ap); @@ -2698,7 +2672,7 @@ _public_ int sd_bus_message_append_string_memfd( if (size > (uint64_t) (uint32_t) -1) return -EINVAL; - c = message_get_container(m); + c = message_get_last_container(m); if (c->signature && c->signature[c->index]) { /* Container signature is already set */ @@ -3031,7 +3005,7 @@ static bool message_end_of_signature(sd_bus_message *m) { assert(m); - c = message_get_container(m); + c = message_get_last_container(m); return !c->signature || c->signature[c->index] == 0; } @@ -3040,7 +3014,7 @@ static bool message_end_of_array(sd_bus_message *m, size_t index) { assert(m); - c = message_get_container(m); + c = message_get_last_container(m); if (c->enclosing != SD_BUS_TYPE_ARRAY) return false; @@ -3135,6 +3109,7 @@ static int container_next_item(sd_bus_message *m, struct bus_container *c, size_ assert(alignment > 0); *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment); + assert(c->offsets[c->offset_index+1] >= *rindex); c->item_size = c->offsets[c->offset_index+1] - *rindex; } else { @@ -3174,6 +3149,7 @@ static int container_next_item(sd_bus_message *m, struct bus_container *c, size_ assert(alignment > 0); *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment); + assert(c->offsets[c->offset_index+1] >= *rindex); c->item_size = c->offsets[c->offset_index+1] - *rindex; c->offset_index++; @@ -3301,7 +3277,7 @@ _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) { if (message_end_of_array(m, m->rindex)) return 0; - c = message_get_container(m); + c = message_get_last_container(m); if (c->signature[c->index] != type) return -ENXIO; @@ -3312,6 +3288,12 @@ _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) { if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) { bool ok; + /* D-Bus spec: The marshalling formats for the string-like types all end + * with a single zero (NUL) byte, but that byte is not considered to be part + * of the text. */ + if (c->item_size == 0) + return -EBADMSG; + r = message_peek_body(m, &rindex, 1, c->item_size, &q); if (r < 0) return r; @@ -3406,6 +3388,10 @@ _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) { return r; l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q); + if (l == UINT32_MAX) + /* avoid overflow right below */ + return -EBADMSG; + r = message_peek_body(m, &rindex, 1, l+1, &q); if (r < 0) return r; @@ -3428,6 +3414,10 @@ _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) { return r; l = *(uint8_t*) q; + if (l == UINT8_MAX) + /* avoid overflow right below */ + return -EBADMSG; + r = message_peek_body(m, &rindex, 1, l+1, &q); if (r < 0) return r; @@ -3519,7 +3509,7 @@ static int bus_message_enter_array( size_t rindex; void *q; - int r, alignment; + int r; assert(m); assert(c); @@ -3545,6 +3535,7 @@ static int bus_message_enter_array( if (!BUS_MESSAGE_IS_GVARIANT(m)) { /* dbus1 */ + int alignment; r = message_peek_body(m, &rindex, 4, 4, &q); if (r < 0) @@ -3578,7 +3569,8 @@ static int bus_message_enter_array( *n_offsets = 0; } else { - size_t where, p = 0, framing, sz; + size_t where, previous = 0, framing, sz; + int alignment; unsigned i; /* gvariant: variable length array */ @@ -3606,17 +3598,22 @@ static int bus_message_enter_array( if (!*offsets) return -ENOMEM; + alignment = bus_gvariant_get_alignment(c->signature); + assert(alignment > 0); + for (i = 0; i < *n_offsets; i++) { - size_t x; + size_t x, start; + + start = ALIGN_TO(previous, alignment); x = bus_gvariant_read_word_le((uint8_t*) q + i * sz, sz); if (x > c->item_size - sz) return -EBADMSG; - if (x < p) + if (x < start) return -EBADMSG; (*offsets)[i] = rindex + x; - p = x; + previous = x; } *item_size = (*offsets)[0] - rindex; @@ -3686,6 +3683,10 @@ static int bus_message_enter_variant( return r; l = *(uint8_t*) q; + if (l == UINT8_MAX) + /* avoid overflow right below */ + return -EBADMSG; + r = message_peek_body(m, &rindex, 1, l+1, &q); if (r < 0) return r; @@ -3714,7 +3715,7 @@ static int build_struct_offsets( size_t *n_offsets) { unsigned n_variable = 0, n_total = 0, v; - size_t previous = 0, where; + size_t previous, where; const char *p; size_t sz; void *q; @@ -3793,6 +3794,7 @@ static int build_struct_offsets( /* Second, loop again and build an offset table */ p = signature; + previous = m->rindex; while (*p != 0) { size_t n, offset; int k; @@ -3806,37 +3808,39 @@ static int build_struct_offsets( memcpy(t, p, n); t[n] = 0; + size_t align = bus_gvariant_get_alignment(t); + assert(align > 0); + + /* The possible start of this member after including alignment */ + size_t start = ALIGN_TO(previous, align); + k = bus_gvariant_get_size(t); if (k < 0) { size_t x; - /* variable size */ + /* Variable size */ if (v > 0) { v--; x = bus_gvariant_read_word_le((uint8_t*) q + v*sz, sz); if (x >= size) return -EBADMSG; - if (m->rindex + x < previous) - return -EBADMSG; } else - /* The last item's end - * is determined from - * the start of the - * offset array */ + /* The last item's end is determined + * from the start of the offset array */ x = size - (n_variable * sz); offset = m->rindex + x; - - } else { - size_t align; - - /* fixed size */ - align = bus_gvariant_get_alignment(t); - assert(align > 0); - - offset = (*n_offsets == 0 ? m->rindex : ALIGN_TO((*offsets)[*n_offsets-1], align)) + k; - } + if (offset < start) + return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), + "For type %s with alignment %zu, message specifies offset %zu which is smaller than previous end %zu + alignment = %zu", + t, align, + offset, + previous, + start); + } else + /* Fixed size */ + offset = start + k; } previous = (*offsets)[(*n_offsets)++] = offset; @@ -3966,10 +3970,10 @@ static int bus_message_enter_dict_entry( _public_ int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents) { - struct bus_container *c, *w; + struct bus_container *c; uint32_t *array_size = NULL; _cleanup_free_ char *signature = NULL; - size_t before; + size_t before, end; _cleanup_free_ size_t *offsets = NULL; size_t n_offsets = 0, item_size = 0; int r; @@ -4025,7 +4029,7 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m, if (message_end_of_array(m, m->rindex)) return 0; - c = message_get_container(m); + c = message_get_last_container(m); signature = strdup(contents); if (!signature) @@ -4048,28 +4052,26 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m, return r; /* OK, let's fill it in */ - w = m->containers + m->n_containers++; - w->enclosing = type; - w->signature = TAKE_PTR(signature); - w->peeked_signature = NULL; - w->index = 0; - - w->before = before; - w->begin = m->rindex; - - /* 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; + end = m->rindex + 0; else - w->end = m->rindex + c->item_size; - - w->array_size = array_size; - w->item_size = item_size; - w->offsets = TAKE_PTR(offsets); - w->n_offsets = n_offsets; - w->offset_index = 0; + end = m->rindex + c->item_size; + + m->containers[m->n_containers++] = (struct bus_container) { + .enclosing = type, + .signature = TAKE_PTR(signature), + + .before = before, + .begin = m->rindex, + /* Unary type has fixed size of 1, but virtual size of 0 */ + .end = end, + .array_size = array_size, + .item_size = item_size, + .offsets = TAKE_PTR(offsets), + .n_offsets = n_offsets, + }; return 1; } @@ -4083,7 +4085,7 @@ _public_ int sd_bus_message_exit_container(sd_bus_message *m) { assert_return(m->sealed, -EPERM); assert_return(m->n_containers > 0, -ENXIO); - c = message_get_container(m); + c = message_get_last_container(m); if (c->enclosing != SD_BUS_TYPE_ARRAY) { if (c->signature && c->signature[c->index] != 0) @@ -4102,13 +4104,9 @@ _public_ int sd_bus_message_exit_container(sd_bus_message *m) { return -EBUSY; } - free(c->signature); - free(c->peeked_signature); - free(c->offsets); - m->n_containers--; - - c = message_get_container(m); + message_free_last_container(m); + c = message_get_last_container(m); saved = c->index; c->index = c->saved_index; r = container_next_item(m, c, &m->rindex); @@ -4126,19 +4124,16 @@ static void message_quit_container(sd_bus_message *m) { assert(m->sealed); assert(m->n_containers > 0); - c = message_get_container(m); - /* Undo seeks */ + c = message_get_last_container(m); assert(m->rindex >= c->before); m->rindex = c->before; /* Free container */ - free(c->signature); - free(c->offsets); - m->n_containers--; + message_free_last_container(m); /* Correct index of new top-level container */ - c = message_get_container(m); + c = message_get_last_container(m); c->index = c->saved_index; } @@ -4155,7 +4150,7 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char if (message_end_of_array(m, m->rindex)) goto eof; - c = message_get_container(m); + c = message_get_last_container(m); if (bus_type_is_basic(c->signature[c->index])) { if (contents) @@ -4169,20 +4164,20 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char if (contents) { size_t l; - char *sig; r = signature_element_length(c->signature+c->index+1, &l); if (r < 0) return r; - assert(l >= 1); + /* signature_element_length does verification internally */ - sig = strndup(c->signature + c->index + 1, l); - if (!sig) + /* The array element must not be empty */ + assert(l >= 1); + if (free_and_strndup(&c->peeked_signature, + c->signature + c->index + 1, l) < 0) return -ENOMEM; - free(c->peeked_signature); - *contents = c->peeked_signature = sig; + *contents = c->peeked_signature; } if (type) @@ -4195,19 +4190,17 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char if (contents) { size_t l; - char *sig; r = signature_element_length(c->signature+c->index, &l); if (r < 0) return r; - assert(l >= 2); - sig = strndup(c->signature + c->index + 1, l - 2); - if (!sig) + assert(l >= 3); + if (free_and_strndup(&c->peeked_signature, + c->signature + c->index + 1, l - 2) < 0) return -ENOMEM; - free(c->peeked_signature); - *contents = c->peeked_signature = sig; + *contents = c->peeked_signature; } if (type) @@ -4247,9 +4240,8 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char if (k > c->item_size) return -EBADMSG; - free(c->peeked_signature); - c->peeked_signature = strndup((char*) q + 1, k - 1); - if (!c->peeked_signature) + if (free_and_strndup(&c->peeked_signature, + (char*) q + 1, k - 1) < 0) return -ENOMEM; if (!signature_is_valid(c->peeked_signature, true)) @@ -4265,6 +4257,10 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char return r; l = *(uint8_t*) q; + if (l == UINT8_MAX) + /* avoid overflow right below */ + return -EBADMSG; + r = message_peek_body(m, &rindex, 1, l+1, &q); if (r < 0) return r; @@ -4302,11 +4298,10 @@ _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) { message_reset_containers(m); m->rindex = 0; - c = message_get_container(m); + c = message_get_last_container(m); } else { - c = message_get_container(m); + c = message_get_last_container(m); - c->offset_index = 0; c->index = 0; m->rindex = c->begin; } @@ -4317,7 +4312,7 @@ _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) { return !isempty(c->signature); } -static int message_read_ap( +_public_ int sd_bus_message_readv( sd_bus_message *m, const char *types, va_list ap) { @@ -4328,7 +4323,9 @@ static int message_read_ap( unsigned n_loop = 0; int r; - assert(m); + assert_return(m, -EINVAL); + assert_return(m->sealed, -EPERM); + assert_return(types, -EINVAL); if (isempty(types)) return 0; @@ -4519,12 +4516,8 @@ _public_ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) { va_list ap; int r; - assert_return(m, -EINVAL); - assert_return(m->sealed, -EPERM); - assert_return(types, -EINVAL); - va_start(ap, types); - r = message_read_ap(m, types, ap); + r = sd_bus_message_readv(m, types, ap); va_end(ap); return r; @@ -4547,7 +4540,7 @@ _public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) { if (message_end_of_array(m, m->rindex)) return 0; - c = message_get_container(m); + c = message_get_last_container(m); r = signature_element_length(c->signature + c->index, &l); if (r < 0) @@ -4713,7 +4706,7 @@ _public_ int sd_bus_message_read_array( if (r <= 0) return r; - c = message_get_container(m); + c = message_get_last_container(m); if (BUS_MESSAGE_IS_GVARIANT(m)) { align = bus_gvariant_get_alignment(CHAR_TO_STR(type)); @@ -4850,6 +4843,10 @@ static int message_peek_field_string( if (r < 0) return r; + if (l == UINT32_MAX) + /* avoid overflow right below */ + return -EBADMSG; + r = message_peek_fields(m, ri, 1, l+1, &q); if (r < 0) return r; @@ -4901,6 +4898,10 @@ static int message_peek_field_signature( return r; l = *(uint8_t*) q; + if (l == UINT8_MAX) + /* avoid overflow right below */ + return -EBADMSG; + r = message_peek_fields(m, ri, 1, l+1, &q); if (r < 0) return r; @@ -4982,18 +4983,18 @@ static int message_skip_fields( } else if (t == SD_BUS_TYPE_ARRAY) { - r = signature_element_length(*signature+1, &l); + r = signature_element_length(*signature + 1, &l); if (r < 0) return r; assert(l >= 1); { - char sig[l-1], *s; + char sig[l + 1], *s = sig; uint32_t nas; int alignment; - strncpy(sig, *signature + 1, l-1); - s = sig; + strncpy(sig, *signature + 1, l); + sig[l] = '\0'; alignment = bus_type_get_alignment(sig[0]); if (alignment < 0) @@ -5037,9 +5038,9 @@ static int message_skip_fields( assert(l >= 2); { - char sig[l-1], *s; - strncpy(sig, *signature + 1, l-1); - s = sig; + char sig[l + 1], *s = sig; + strncpy(sig, *signature + 1, l); + sig[l] = '\0'; r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s); if (r < 0) @@ -5048,7 +5049,7 @@ static int message_skip_fields( *signature += l; } else - return -EINVAL; + return -EBADMSG; } } @@ -5078,26 +5079,25 @@ int bus_message_parse_fields(sd_bus_message *m) { return -EBADMSG; if (*p == 0) { + char *k; size_t l; - char *c; /* 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. */ - l = ((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) + k = memdup_suffix0(p + 1 + 1, l - 2); + if (!k) return -ENOMEM; - free(m->root_container.signature); - m->root_container.signature = c; + free_and_replace(m->root_container.signature, k); break; } @@ -5419,6 +5419,8 @@ int bus_message_parse_fields(sd_bus_message *m) { &m->root_container.item_size, &m->root_container.offsets, &m->root_container.n_offsets); + if (r == -EINVAL) + return -EBADMSG; if (r < 0) return r; } @@ -5433,6 +5435,7 @@ int bus_message_parse_fields(sd_bus_message *m) { _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) { assert_return(m, -EINVAL); assert_return(destination, -EINVAL); + assert_return(service_name_is_valid(destination), -EINVAL); assert_return(!m->sealed, -EPERM); assert_return(!m->destination, -EEXIST); @@ -5442,6 +5445,7 @@ _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *desti _public_ int sd_bus_message_set_sender(sd_bus_message *m, const char *sender) { assert_return(m, -EINVAL); assert_return(sender, -EINVAL); + assert_return(service_name_is_valid(sender), -EINVAL); assert_return(!m->sealed, -EPERM); assert_return(!m->sender, -EEXIST); @@ -5612,7 +5616,7 @@ _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complet assert_return(m, NULL); - c = complete ? &m->root_container : message_get_container(m); + c = complete ? &m->root_container : message_get_last_container(m); return strempty(c->signature); } @@ -5809,8 +5813,11 @@ int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) { return r; timeout = (*m)->timeout; - if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)) - timeout = BUS_DEFAULT_TIMEOUT; + if (timeout == 0 && !((*m)->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)) { + r = sd_bus_get_method_call_timeout(bus, &timeout); + if (r < 0) + return r; + } r = sd_bus_message_seal(n, BUS_MESSAGE_COOKIE(*m), timeout); if (r < 0) @@ -5822,16 +5829,6 @@ int bus_message_remarshal(sd_bus *bus, sd_bus_message **m) { return 0; } -int bus_message_append_sender(sd_bus_message *m, const char *sender) { - assert(m); - assert(sender); - - assert_return(!m->sealed, -EPERM); - assert_return(!m->sender, -EPERM); - - return message_append_field_string(m, BUS_MESSAGE_HEADER_SENDER, SD_BUS_TYPE_STRING, sender, &m->sender); -} - _public_ int sd_bus_message_get_priority(sd_bus_message *m, int64_t *priority) { assert_return(m, -EINVAL); assert_return(priority, -EINVAL); diff --git a/src/libsystemd/sd-bus/bus-message.h b/src/libsystemd/sd-bus/bus-message.h index 97f6060e30..0115437d26 100644 --- a/src/libsystemd/sd-bus/bus-message.h +++ b/src/libsystemd/sd-bus/bus-message.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <byteswap.h> #include <stdbool.h> #include <sys/socket.h> @@ -212,7 +209,5 @@ int bus_message_new_synthetic_error(sd_bus *bus, uint64_t serial, const sd_bus_e int bus_message_remarshal(sd_bus *bus, sd_bus_message **m); -int bus_message_append_sender(sd_bus_message *m, const char *sender); - void bus_message_set_sender_driver(sd_bus *bus, sd_bus_message *m); void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m); diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c index 9609834fa9..d0538104ae 100644 --- a/src/libsystemd/sd-bus/bus-objects.c +++ b/src/libsystemd/sd-bus/bus-objects.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "alloc-util.h" #include "bus-internal.h" @@ -11,6 +9,7 @@ #include "bus-slot.h" #include "bus-type.h" #include "bus-util.h" +#include "missing_capability.h" #include "set.h" #include "string-util.h" #include "strv.h" @@ -166,7 +165,7 @@ static int add_subtree_to_set( sd_bus *bus, const char *prefix, struct node *n, - unsigned int flags, + unsigned flags, Set *s, sd_bus_error *error) { @@ -215,7 +214,7 @@ static int get_child_nodes( sd_bus *bus, const char *prefix, struct node *n, - unsigned int flags, + unsigned flags, Set **_s, sd_bus_error *error) { @@ -1584,9 +1583,7 @@ _public_ int sd_bus_add_fallback( return bus_add_object(bus, slot, true, prefix, callback, userdata); } -static void vtable_member_hash_func(const void *a, struct siphash *state) { - const struct vtable_member *m = a; - +static void vtable_member_hash_func(const struct vtable_member *m, struct siphash *state) { assert(m); string_hash_func(m->path, state); @@ -1594,8 +1591,7 @@ static void vtable_member_hash_func(const void *a, struct siphash *state) { string_hash_func(m->member, state); } -static int vtable_member_compare_func(const void *a, const void *b) { - const struct vtable_member *x = a, *y = b; +static int vtable_member_compare_func(const struct vtable_member *x, const struct vtable_member *y) { int r; assert(x); @@ -1612,10 +1608,7 @@ static int vtable_member_compare_func(const void *a, const void *b) { return strcmp(x->member, y->member); } -static const struct hash_ops vtable_member_hash_ops = { - .hash = vtable_member_hash_func, - .compare = vtable_member_compare_func -}; +DEFINE_PRIVATE_HASH_OPS(vtable_member_hash_ops, struct vtable_member, vtable_member_hash_func, vtable_member_compare_func); static int add_object_vtable_internal( sd_bus *bus, @@ -2090,7 +2083,6 @@ _public_ int sd_bus_emit_properties_changed_strv( const char *interface, char **names) { - BUS_DONT_DESTROY(bus); bool found_interface = false; char *prefix; int r; @@ -2111,6 +2103,8 @@ _public_ int sd_bus_emit_properties_changed_strv( if (names && names[0] == NULL) return 0; + BUS_DONT_DESTROY(bus); + do { bus->nodes_modified = false; @@ -2310,8 +2304,6 @@ static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *p } _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) { - BUS_DONT_DESTROY(bus); - _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; struct node *object_manager; int r; @@ -2341,6 +2333,8 @@ _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) { if (r == 0) return -ESRCH; + BUS_DONT_DESTROY(bus); + do { bus->nodes_modified = false; m = sd_bus_message_unref(m); @@ -2481,8 +2475,6 @@ static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char } _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) { - BUS_DONT_DESTROY(bus); - _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; struct node *object_manager; int r; @@ -2512,6 +2504,8 @@ _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) { if (r == 0) return -ESRCH; + BUS_DONT_DESTROY(bus); + do { bus->nodes_modified = false; m = sd_bus_message_unref(m); @@ -2645,8 +2639,6 @@ static int interfaces_added_append_one( } _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) { - BUS_DONT_DESTROY(bus); - _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; struct node *object_manager; char **i; @@ -2669,6 +2661,8 @@ _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, ch if (r == 0) return -ESRCH; + BUS_DONT_DESTROY(bus); + do { bus->nodes_modified = false; m = sd_bus_message_unref(m); diff --git a/src/libsystemd/sd-bus/bus-objects.h b/src/libsystemd/sd-bus/bus-objects.h index e8e1a522cb..a119ff95c0 100644 --- a/src/libsystemd/sd-bus/bus-objects.h +++ b/src/libsystemd/sd-bus/bus-objects.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "bus-internal.h" int bus_process_object(sd_bus *bus, sd_bus_message *m); diff --git a/src/libsystemd/sd-bus/bus-protocol.h b/src/libsystemd/sd-bus/bus-protocol.h index 20d19d4022..a5f4724aa9 100644 --- a/src/libsystemd/sd-bus/bus-protocol.h +++ b/src/libsystemd/sd-bus/bus-protocol.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <endian.h> #include "macro.h" diff --git a/src/libsystemd/sd-bus/bus-signature.c b/src/libsystemd/sd-bus/bus-signature.c index 18c91e8707..1ecd6e8b7e 100644 --- a/src/libsystemd/sd-bus/bus-signature.c +++ b/src/libsystemd/sd-bus/bus-signature.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <util.h> @@ -58,6 +56,12 @@ static int signature_element_length_internal( p += t; } + if (p - s < 2) + /* D-Bus spec: Empty structures are not allowed; there + * must be at least one type code between the parentheses. + */ + return -EINVAL; + *l = p - s + 1; return 0; } diff --git a/src/libsystemd/sd-bus/bus-signature.h b/src/libsystemd/sd-bus/bus-signature.h index d4b43bac00..b87bec8329 100644 --- a/src/libsystemd/sd-bus/bus-signature.h +++ b/src/libsystemd/sd-bus/bus-signature.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <stdbool.h> bool signature_is_single(const char *s, bool allow_dict_entry); diff --git a/src/libsystemd/sd-bus/bus-slot.c b/src/libsystemd/sd-bus/bus-slot.c index fbf37320d3..c9aca07f90 100644 --- a/src/libsystemd/sd-bus/bus-slot.c +++ b/src/libsystemd/sd-bus/bus-slot.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "sd-bus.h" @@ -39,18 +37,7 @@ sd_bus_slot *bus_slot_allocate( return slot; } -_public_ sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot) { - - if (!slot) - return NULL; - - assert(slot->n_ref > 0); - - slot->n_ref++; - return slot; -} - -void bus_slot_disconnect(sd_bus_slot *slot) { +void bus_slot_disconnect(sd_bus_slot *slot, bool unref) { sd_bus *bus; assert(slot); @@ -81,7 +68,7 @@ void bus_slot_disconnect(sd_bus_slot *slot) { (void) bus_remove_match_internal(slot->bus, slot->match_callback.match_string); if (slot->match_callback.install_slot) { - bus_slot_disconnect(slot->match_callback.install_slot); + bus_slot_disconnect(slot->match_callback.install_slot, true); slot->match_callback.install_slot = sd_bus_slot_unref(slot->match_callback.install_slot); } @@ -185,21 +172,14 @@ void bus_slot_disconnect(sd_bus_slot *slot) { if (!slot->floating) sd_bus_unref(bus); + else if (unref) + sd_bus_slot_unref(slot); } -_public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) { - - if (!slot) - return NULL; - - assert(slot->n_ref > 0); - - if (slot->n_ref > 1) { - slot->n_ref--; - return NULL; - } +static sd_bus_slot* bus_slot_free(sd_bus_slot *slot) { + assert(slot); - bus_slot_disconnect(slot); + bus_slot_disconnect(slot, false); if (slot->destroy_callback) slot->destroy_callback(slot->userdata); @@ -208,6 +188,8 @@ _public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) { return mfree(slot); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_slot, sd_bus_slot, bus_slot_free); + _public_ sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot) { assert_return(slot, NULL); diff --git a/src/libsystemd/sd-bus/bus-slot.h b/src/libsystemd/sd-bus/bus-slot.h index f1e1e23ac6..48eb0453dc 100644 --- a/src/libsystemd/sd-bus/bus-slot.h +++ b/src/libsystemd/sd-bus/bus-slot.h @@ -1,13 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "sd-bus.h" #include "bus-internal.h" sd_bus_slot *bus_slot_allocate(sd_bus *bus, bool floating, BusSlotType type, size_t extra, void *userdata); -void bus_slot_disconnect(sd_bus_slot *slot); +void bus_slot_disconnect(sd_bus_slot *slot, bool unref); diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c index b147a3843a..ed185131b8 100644 --- a/src/libsystemd/sd-bus/bus-socket.c +++ b/src/libsystemd/sd-bus/bus-socket.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <endian.h> #include <poll.h> @@ -23,6 +21,7 @@ #include "missing.h" #include "path-util.h" #include "process-util.h" +#include "rlimit-util.h" #include "selinux-util.h" #include "signal-util.h" #include "stdio-util.h" @@ -46,8 +45,7 @@ static void iovec_advance(struct iovec iov[], unsigned *idx, size_t size) { size -= i->iov_len; - i->iov_base = NULL; - i->iov_len = 0; + *i = IOVEC_MAKE(NULL, 0); (*idx)++; } @@ -58,9 +56,7 @@ static int append_iovec(sd_bus_message *m, const void *p, size_t sz) { assert(p); assert(sz > 0); - m->iovec[m->n_iovec].iov_base = (void*) p; - m->iovec[m->n_iovec].iov_len = sz; - m->n_iovec++; + m->iovec[m->n_iovec++] = IOVEC_MAKE((void*) p, sz); return 0; } @@ -248,10 +244,7 @@ static bool line_begins(const char *s, size_t m, const char *word) { const char *p; p = memory_startswith(s, m, word); - if (!p) - return false; - - return IN_SET(*p, 0, ' '); + return p && (p == (s + m) || *p == ' '); } static int verify_anonymous_token(sd_bus *b, const char *p, size_t l) { @@ -521,8 +514,7 @@ static int bus_socket_read_auth(sd_bus *b) { b->rbuffer = p; - iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size; - iov.iov_len = n - b->rbuffer_size; + iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size); if (b->prefer_readv) k = readv(b->input_fd, &iov, 1); @@ -639,12 +631,9 @@ static int bus_socket_start_auth_client(sd_bus *b) { else auth_suffix = "\r\nBEGIN\r\n"; - b->auth_iovec[0].iov_base = (void*) auth_prefix; - b->auth_iovec[0].iov_len = 1 + strlen(auth_prefix + 1); - b->auth_iovec[1].iov_base = (void*) b->auth_buffer; - b->auth_iovec[1].iov_len = l * 2; - b->auth_iovec[2].iov_base = (void*) auth_suffix; - b->auth_iovec[2].iov_len = strlen(auth_suffix); + b->auth_iovec[0] = IOVEC_MAKE((void*) auth_prefix, 1 + strlen(auth_prefix + 1)); + b->auth_iovec[1] = IOVEC_MAKE(b->auth_buffer, l * 2); + b->auth_iovec[2] = IOVEC_MAKE_STRING(auth_suffix); return bus_socket_write_auth(b); } @@ -944,6 +933,8 @@ int bus_socket_exec(sd_bus *b) { if (rearrange_stdio(s[1], s[1], STDERR_FILENO) < 0) _exit(EXIT_FAILURE); + (void) rlimit_nofile_safe(); + if (b->exec_argv) execvp(b->exec_path, b->exec_argv); else { @@ -1151,8 +1142,7 @@ int bus_socket_read_message(sd_bus *bus) { bus->rbuffer = b; - iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size; - iov.iov_len = need - bus->rbuffer_size; + iov = IOVEC_MAKE((uint8_t *)bus->rbuffer + bus->rbuffer_size, need - bus->rbuffer_size); if (bus->prefer_readv) k = readv(bus->input_fd, &iov, 1); diff --git a/src/libsystemd/sd-bus/bus-socket.h b/src/libsystemd/sd-bus/bus-socket.h index d1118ca1d4..f8d24556c9 100644 --- a/src/libsystemd/sd-bus/bus-socket.h +++ b/src/libsystemd/sd-bus/bus-socket.h @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include "sd-bus.h" void bus_socket_setup(sd_bus *b); diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c index 16bf615f50..efbd3ed5f0 100644 --- a/src/libsystemd/sd-bus/bus-track.c +++ b/src/libsystemd/sd-bus/bus-track.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "sd-bus.h" @@ -52,6 +50,8 @@ static struct track_item* track_item_free(struct track_item *i) { } DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free); +DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(track_item_hash_ops, char, string_hash_func, string_compare_func, + struct track_item, track_item_free); static void bus_track_add_to_queue(sd_bus_track *track) { assert(track); @@ -145,33 +145,14 @@ _public_ int sd_bus_track_new( return 0; } -_public_ sd_bus_track* sd_bus_track_ref(sd_bus_track *track) { - - if (!track) - return NULL; - - assert(track->n_ref > 0); - - track->n_ref++; - - return track; -} - -_public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { - if (!track) - return NULL; - - assert(track->n_ref > 0); - track->n_ref--; - - if (track->n_ref > 0) - return NULL; +static sd_bus_track *track_free(sd_bus_track *track) { + assert(track); if (track->in_list) LIST_REMOVE(tracks, track->bus->tracks, track); bus_track_remove_from_queue(track); - track->names = hashmap_free_with_destructor(track->names, track_item_free); + track->names = hashmap_free(track->names); track->bus = sd_bus_unref(track->bus); if (track->destroy_callback) @@ -180,6 +161,8 @@ _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { return mfree(track); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_track, sd_bus_track, track_free); + static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { sd_bus_track *track = userdata; const char *name, *old, *new; @@ -220,7 +203,7 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) { return 0; } - r = hashmap_ensure_allocated(&track->names, &string_hash_ops); + r = hashmap_ensure_allocated(&track->names, &track_item_hash_ops); if (r < 0) return r; @@ -416,7 +399,7 @@ void bus_track_close(sd_bus_track *track) { return; /* Let's flush out all names */ - hashmap_clear_with_destructor(track->names, track_item_free); + hashmap_clear(track->names); /* Invoke handler */ if (track->handler) diff --git a/src/libsystemd/sd-bus/bus-track.h b/src/libsystemd/sd-bus/bus-track.h index f9590265d7..209b989d27 100644 --- a/src/libsystemd/sd-bus/bus-track.h +++ b/src/libsystemd/sd-bus/bus-track.h @@ -1,8 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - void bus_track_dispatch(sd_bus_track *track); void bus_track_close(sd_bus_track *track); diff --git a/src/libsystemd/sd-bus/bus-type.c b/src/libsystemd/sd-bus/bus-type.c index bc6726f9cf..18564a5383 100644 --- a/src/libsystemd/sd-bus/bus-type.c +++ b/src/libsystemd/sd-bus/bus-type.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <errno.h> @@ -32,32 +30,6 @@ bool bus_type_is_valid(char c) { return !!memchr(valid, c, sizeof(valid)); } -bool bus_type_is_valid_in_signature(char c) { - static const char valid[] = { - SD_BUS_TYPE_BYTE, - SD_BUS_TYPE_BOOLEAN, - SD_BUS_TYPE_INT16, - SD_BUS_TYPE_UINT16, - SD_BUS_TYPE_INT32, - SD_BUS_TYPE_UINT32, - SD_BUS_TYPE_INT64, - SD_BUS_TYPE_UINT64, - SD_BUS_TYPE_DOUBLE, - SD_BUS_TYPE_STRING, - SD_BUS_TYPE_OBJECT_PATH, - SD_BUS_TYPE_SIGNATURE, - SD_BUS_TYPE_ARRAY, - SD_BUS_TYPE_VARIANT, - SD_BUS_TYPE_STRUCT_BEGIN, - SD_BUS_TYPE_STRUCT_END, - SD_BUS_TYPE_DICT_ENTRY_BEGIN, - SD_BUS_TYPE_DICT_ENTRY_END, - SD_BUS_TYPE_UNIX_FD - }; - - return !!memchr(valid, c, sizeof(valid)); -} - bool bus_type_is_basic(char c) { static const char valid[] = { SD_BUS_TYPE_BYTE, diff --git a/src/libsystemd/sd-bus/bus-type.h b/src/libsystemd/sd-bus/bus-type.h index cdac55c62e..0ecd8513fd 100644 --- a/src/libsystemd/sd-bus/bus-type.h +++ b/src/libsystemd/sd-bus/bus-type.h @@ -1,15 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -/*** -***/ - #include <stdbool.h> #include "macro.h" bool bus_type_is_valid(char c) _const_; -bool bus_type_is_valid_in_signature(char c) _const_; bool bus_type_is_basic(char c) _const_; /* "trivial" is systemd's term for what the D-Bus Specification calls * a "fixed type": that is, a basic type of fixed length */ diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 089b51a6d9..3b00bc8157 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <endian.h> #include <netdb.h> @@ -180,8 +178,7 @@ static sd_bus* bus_free(sd_bus *b) { * apps, but are dead. */ assert(s->floating); - bus_slot_disconnect(s); - sd_bus_slot_unref(s); + bus_slot_disconnect(s, true); } if (b->default_bus_ptr) @@ -197,7 +194,6 @@ static sd_bus* bus_free(sd_bus *b) { free(b->auth_buffer); free(b->address); free(b->machine); - free(b->cgroup_root); free(b->description); free(b->patch_sender); @@ -235,18 +231,22 @@ _public_ int sd_bus_new(sd_bus **ret) { assert_return(ret, -EINVAL); - b = new0(sd_bus, 1); + b = new(sd_bus, 1); if (!b) return -ENOMEM; - b->n_ref = REFCNT_INIT; - b->input_fd = b->output_fd = -1; - b->inotify_fd = -1; - b->message_version = 1; - b->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME; - b->accept_fd = true; - b->original_pid = getpid_cached(); - b->n_groups = (size_t) -1; + *b = (sd_bus) { + .n_ref = REFCNT_INIT, + .input_fd = -1, + .output_fd = -1, + .inotify_fd = -1, + .message_version = 1, + .creds_mask = SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME, + .accept_fd = true, + .original_pid = getpid_cached(), + .n_groups = (size_t) -1, + .close_on_exit = true, + }; assert_se(pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0); @@ -733,20 +733,28 @@ static int parse_unix_address(sd_bus *b, const char **p, char **guid) { if (path) { l = strlen(path); - if (l > sizeof(b->sockaddr.un.sun_path)) + if (l >= sizeof(b->sockaddr.un.sun_path)) /* We insist on NUL termination */ return -E2BIG; - b->sockaddr.un.sun_family = AF_UNIX; - strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path)); - b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l; - } else if (abstract) { + b->sockaddr.un = (struct sockaddr_un) { + .sun_family = AF_UNIX, + }; + + memcpy(b->sockaddr.un.sun_path, path, l); + b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 1; + + } else { + assert(abstract); + l = strlen(abstract); - if (l > sizeof(b->sockaddr.un.sun_path) - 1) + if (l >= sizeof(b->sockaddr.un.sun_path) - 1) /* We insist on NUL termination */ return -E2BIG; - b->sockaddr.un.sun_family = AF_UNIX; - b->sockaddr.un.sun_path[0] = 0; - strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1); + b->sockaddr.un = (struct sockaddr_un) { + .sun_family = AF_UNIX, + }; + + memcpy(b->sockaddr.un.sun_path+1, abstract, l); b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l; } @@ -952,7 +960,7 @@ static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) return -EINVAL; if (machine) { - if (!machine_name_is_valid(machine)) + if (!streq(machine, ".host") && !machine_name_is_valid(machine)) return -EINVAL; free_and_replace(b->machine, machine); @@ -967,9 +975,11 @@ static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) } else b->nspid = 0; - b->sockaddr.un.sun_family = AF_UNIX; - /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */ - strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path)); + b->sockaddr.un = (struct sockaddr_un) { + .sun_family = AF_UNIX, + /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */ + .sun_path = "/var/run/dbus/system_bus_socket", + }; b->sockaddr_size = SOCKADDR_UN_LEN(b->sockaddr.un); b->is_local = false; @@ -1360,38 +1370,88 @@ _public_ int sd_bus_open_user(sd_bus **ret) { int bus_set_address_system_remote(sd_bus *b, const char *host) { _cleanup_free_ char *e = NULL; - char *m = NULL, *c = NULL, *a; + char *m = NULL, *c = NULL, *a, *rbracket = NULL, *p = NULL; assert(b); assert(host); - /* Let's see if we shall enter some container */ - m = strchr(host, ':'); - if (m) { - m++; + /* Skip ":"s in ipv6 addresses */ + if (*host == '[') { + char *t; - /* Let's make sure this is not a port of some kind, - * and is a valid machine name. */ - if (!in_charset(m, DIGITS) && machine_name_is_valid(m)) { - char *t; - - /* Cut out the host part */ - t = strndupa(host, m - host - 1); + rbracket = strchr(host, ']'); + if (!rbracket) + return -EINVAL; + t = strndupa(host + 1, rbracket - host - 1); + e = bus_address_escape(t); + if (!e) + return -ENOMEM; + } else if ((a = strchr(host, '@'))) { + if (*(a + 1) == '[') { + _cleanup_free_ char *t = NULL; + + rbracket = strchr(a + 1, ']'); + if (!rbracket) + return -EINVAL; + t = new0(char, strlen(host)); + if (!t) + return -ENOMEM; + strncat(t, host, a - host + 1); + strncat(t, a + 2, rbracket - a - 2); e = bus_address_escape(t); if (!e) return -ENOMEM; + } else if (*(a + 1) == '\0' || strchr(a + 1, '@')) + return -EINVAL; + } + + /* Let's see if a port was given */ + m = strchr(rbracket ? rbracket + 1 : host, ':'); + if (m) { + char *t; + bool got_forward_slash = false; + + p = m + 1; - c = strjoina(",argv5=--machine=", m); + t = strchr(p, '/'); + if (t) { + p = strndupa(p, t - p); + got_forward_slash = true; } + + if (!in_charset(p, "0123456789") || *p == '\0') { + if (!machine_name_is_valid(p) || got_forward_slash) + return -EINVAL; + + m = TAKE_PTR(p); + goto interpret_port_as_machine_old_syntax; + } + } + + /* Let's see if a machine was given */ + m = strchr(rbracket ? rbracket + 1 : host, '/'); + if (m) { + m++; +interpret_port_as_machine_old_syntax: + /* Let's make sure this is not a port of some kind, + * and is a valid machine name. */ + if (!in_charset(m, "0123456789") && machine_name_is_valid(m)) + c = strjoina(",argv", p ? "7" : "5", "=--machine=", m); } if (!e) { - e = bus_address_escape(host); + char *t; + + t = strndupa(host, strcspn(host, ":/")); + + e = bus_address_escape(t); if (!e) return -ENOMEM; } - a = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=systemd-stdio-bridge", c); + a = strjoin("unixexec:path=ssh,argv1=-xT", p ? ",argv2=-p,argv3=" : "", strempty(p), + ",argv", p ? "4" : "2", "=--,argv", p ? "5" : "3", "=", e, + ",argv", p ? "6" : "4", "=systemd-stdio-bridge", c); if (!a) return -ENOMEM; @@ -1450,7 +1510,7 @@ _public_ int sd_bus_open_system_machine(sd_bus **ret, const char *machine) { assert_return(machine, -EINVAL); assert_return(ret, -EINVAL); - assert_return(machine_name_is_valid(machine), -EINVAL); + assert_return(streq(machine, ".host") || machine_name_is_valid(machine), -EINVAL); r = sd_bus_new(&b); if (r < 0) @@ -1518,27 +1578,7 @@ void bus_enter_closing(sd_bus *bus) { bus_set_state(bus, BUS_CLOSING); } -_public_ sd_bus *sd_bus_ref(sd_bus *bus) { - if (!bus) - return NULL; - - assert_se(REFCNT_INC(bus->n_ref) >= 2); - - return bus; -} - -_public_ sd_bus *sd_bus_unref(sd_bus *bus) { - unsigned i; - - if (!bus) - return NULL; - - i = REFCNT_DEC(bus->n_ref); - if (i > 0) - return NULL; - - return bus_free(bus); -} +DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(sd_bus, sd_bus, bus_free); _public_ int sd_bus_is_open(sd_bus *bus) { assert_return(bus, -EINVAL); @@ -1611,8 +1651,11 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) { return 0; } - if (timeout == 0) - timeout = BUS_DEFAULT_TIMEOUT; + if (timeout == 0) { + r = sd_bus_get_method_call_timeout(b, &timeout); + if (r < 0) + return r; + } if (!m->sender && b->patch_sender) { r = sd_bus_message_set_sender(m, b->patch_sender); @@ -1913,13 +1956,7 @@ static int timeout_compare(const void *a, const void *b) { if (x->timeout_usec == 0 && y->timeout_usec != 0) return 1; - if (x->timeout_usec < y->timeout_usec) - return -1; - - if (x->timeout_usec > y->timeout_usec) - return 1; - - return 0; + return CMP(x->timeout_usec, y->timeout_usec); } _public_ int sd_bus_call_async( @@ -2359,10 +2396,8 @@ static int process_timeout(sd_bus *bus) { bus->current_slot = NULL; bus->current_message = NULL; - if (slot->floating) { - bus_slot_disconnect(slot); - sd_bus_slot_unref(slot); - } + if (slot->floating) + bus_slot_disconnect(slot, true); sd_bus_slot_unref(slot); @@ -2464,10 +2499,8 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) { bus->current_handler = NULL; bus->current_slot = NULL; - if (slot->floating) { - bus_slot_disconnect(slot); - sd_bus_slot_unref(slot); - } + if (slot->floating) + bus_slot_disconnect(slot, true); sd_bus_slot_unref(slot); @@ -2809,10 +2842,8 @@ static int process_closing_reply_callback(sd_bus *bus, struct reply_callback *c) bus->current_slot = NULL; bus->current_message = NULL; - if (slot->floating) { - bus_slot_disconnect(slot); - sd_bus_slot_unref(slot); - } + if (slot->floating) + bus_slot_disconnect(slot, true); sd_bus_slot_unref(slot); @@ -2883,7 +2914,6 @@ finish: } static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) { - BUS_DONT_DESTROY(bus); int r; /* Returns 0 when we didn't do anything. This should cause the @@ -2897,7 +2927,9 @@ static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priorit /* We don't allow recursively invoking sd_bus_process(). */ assert_return(!bus->current_message, -EBUSY); - assert(!bus->current_slot); + assert(!bus->current_slot); /* This should be NULL whenever bus->current_message is */ + + BUS_DONT_DESTROY(bus); switch (bus->state) { @@ -3166,10 +3198,8 @@ static int add_match_callback( r = 1; } - if (failed && match_slot->floating) { - bus_slot_disconnect(match_slot); - sd_bus_slot_unref(match_slot); - } + if (failed && match_slot->floating) + bus_slot_disconnect(match_slot, true); sd_bus_slot_unref(match_slot); @@ -3382,8 +3412,10 @@ static int quit_callback(sd_event_source *event, void *userdata) { assert(event); - sd_bus_flush(bus); - sd_bus_close(bus); + if (bus->close_on_exit) { + sd_bus_flush(bus); + sd_bus_close(bus); + } return 1; } @@ -3896,24 +3928,6 @@ _public_ int sd_bus_get_description(sd_bus *bus, const char **description) { return 0; } -int bus_get_root_path(sd_bus *bus) { - int r; - - if (bus->cgroup_root) - return 0; - - r = cg_get_root_path(&bus->cgroup_root); - if (r == -ENOENT) { - bus->cgroup_root = strdup("/"); - if (!bus->cgroup_root) - return -ENOMEM; - - r = 0; - } - - return r; -} - _public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) { assert_return(bus, -EINVAL); assert_return(bus = bus_resolve(bus), -ENOPKG); @@ -4075,3 +4089,51 @@ _public_ int sd_bus_get_n_queued_write(sd_bus *bus, uint64_t *ret) { *ret = bus->wqueue_size; return 0; } + +_public_ int sd_bus_set_method_call_timeout(sd_bus *bus, uint64_t usec) { + assert_return(bus, -EINVAL); + assert_return(bus = bus_resolve(bus), -ENOPKG); + + bus->method_call_timeout = usec; + return 0; +} + +_public_ int sd_bus_get_method_call_timeout(sd_bus *bus, uint64_t *ret) { + const char *e; + usec_t usec; + + assert_return(bus, -EINVAL); + assert_return(bus = bus_resolve(bus), -ENOPKG); + assert_return(ret, -EINVAL); + + if (bus->method_call_timeout != 0) { + *ret = bus->method_call_timeout; + return 0; + } + + e = secure_getenv("SYSTEMD_BUS_TIMEOUT"); + if (e && parse_sec(e, &usec) >= 0 && usec != 0) { + /* Save the parsed value to avoid multiple parsing. To change the timeout value, + * use sd_bus_set_method_call_timeout() instead of setenv(). */ + *ret = bus->method_call_timeout = usec; + return 0; + } + + *ret = bus->method_call_timeout = BUS_DEFAULT_TIMEOUT; + return 0; +} + +_public_ int sd_bus_set_close_on_exit(sd_bus *bus, int b) { + assert_return(bus, -EINVAL); + assert_return(bus = bus_resolve(bus), -ENOPKG); + + bus->close_on_exit = b; + return 0; +} + +_public_ int sd_bus_get_close_on_exit(sd_bus *bus) { + assert_return(bus, -EINVAL); + assert_return(bus = bus_resolve(bus), -ENOPKG); + + return bus->close_on_exit; +} diff --git a/src/libsystemd/sd-bus/test-bus-address.c b/src/libsystemd/sd-bus/test-bus-address.c new file mode 100644 index 0000000000..db5ff72ef4 --- /dev/null +++ b/src/libsystemd/sd-bus/test-bus-address.c @@ -0,0 +1,69 @@ +#include "sd-bus.h" + +#include "bus-internal.h" +#include "log.h" +#include "string-util.h" +#include "strv.h" + +static void test_one_address(sd_bus *b, + const char *host, + int result, const char *expected) { + int r; + + r = bus_set_address_system_remote(b, host); + log_info("\"%s\" → %d, \"%s\"", host, r, strna(r >= 0 ? b->address : NULL)); + if (result < 0 || expected) { + assert(r == result); + if (r >= 0) + assert_se(streq(b->address, expected)); + } +} + +static void test_bus_set_address_system_remote(char **args) { + _cleanup_(sd_bus_unrefp) sd_bus *b = NULL; + + assert_se(sd_bus_new(&b) >= 0); + if (!strv_isempty(args)) { + char **a; + STRV_FOREACH(a, args) + test_one_address(b, *a, 0, NULL); + return; + }; + + test_one_address(b, "host", + 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=host,argv4=systemd-stdio-bridge"); + test_one_address(b, "host:123", + 0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=123,argv4=--,argv5=host,argv6=systemd-stdio-bridge"); + test_one_address(b, "host:123:123", + -EINVAL, NULL); + test_one_address(b, "host:", + -EINVAL, NULL); + test_one_address(b, "user@host", + 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40host,argv4=systemd-stdio-bridge"); + test_one_address(b, "user@host@host", + -EINVAL, NULL); + test_one_address(b, "[::1]", + 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=%3a%3a1,argv4=systemd-stdio-bridge"); + test_one_address(b, "user@[::1]", + 0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40%3a%3a1,argv4=systemd-stdio-bridge"); + test_one_address(b, "user@[::1]:99", + 0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=99,argv4=--,argv5=user%40%3a%3a1,argv6=systemd-stdio-bridge"); + test_one_address(b, "user@[::1]:", + -EINVAL, NULL); + test_one_address(b, "user@[::1:", + -EINVAL, NULL); + test_one_address(b, "user@", + -EINVAL, NULL); + test_one_address(b, "user@@", + -EINVAL, NULL); +} + +int main(int argc, char *argv[]) { + log_set_max_level(LOG_INFO); + log_parse_environment(); + log_open(); + + test_bus_set_address_system_remote(argv + 1); + + return 0; +} diff --git a/src/libsystemd/sd-bus/test-bus-benchmark.c b/src/libsystemd/sd-bus/test-bus-benchmark.c index 8134abba2f..2dd3d41a30 100644 --- a/src/libsystemd/sd-bus/test-bus-benchmark.c +++ b/src/libsystemd/sd-bus/test-bus-benchmark.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <sys/wait.h> @@ -12,6 +10,7 @@ #include "bus-util.h" #include "def.h" #include "fd-util.h" +#include "missing_resource.h" #include "time-util.h" #include "util.h" diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index 1e25e94586..6181fb163e 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <fcntl.h> #include <pthread.h> @@ -18,6 +16,7 @@ #include "format-util.h" #include "log.h" #include "macro.h" +#include "tests.h" #include "util.h" static int match_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { @@ -246,7 +245,7 @@ fail: return r; } -static void* client1(void*p) { +static void* client1(void *p) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -345,7 +344,7 @@ static int quit_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_er return 1; } -static void* client2(void*p) { +static void* client2(void *p) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -511,11 +510,11 @@ int main(int argc, char *argv[]) { void *p; int q, r; + test_setup_logging(LOG_INFO); + r = server_init(&bus); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); log_info("Initialized..."); diff --git a/src/libsystemd/sd-bus/test-bus-cleanup.c b/src/libsystemd/sd-bus/test-bus-cleanup.c index d1d962ebb2..bea722ba06 100644 --- a/src/libsystemd/sd-bus/test-bus-cleanup.c +++ b/src/libsystemd/sd-bus/test-bus-cleanup.c @@ -8,6 +8,9 @@ #include "bus-message.h" #include "bus-util.h" #include "refcnt.h" +#include "tests.h" + +static bool use_system_bus = false; static void test_bus_new(void) { _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; @@ -21,8 +24,12 @@ static int test_bus_open(void) { int r; r = sd_bus_open_user(&bus); - if (IN_SET(r, -ECONNREFUSED, -ENOENT)) - return r; + if (IN_SET(r, -ECONNREFUSED, -ENOENT)) { + r = sd_bus_open_system(&bus); + if (IN_SET(r, -ECONNREFUSED, -ENOENT)) + return r; + use_system_bus = true; + } assert_se(r >= 0); printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref)); @@ -34,7 +41,7 @@ static void test_bus_new_method_call(void) { sd_bus *bus = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; - assert_se(sd_bus_open_user(&bus) >= 0); + assert_se(use_system_bus ? sd_bus_open_system(&bus) >= 0 : sd_bus_open_user(&bus) >= 0); assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0); @@ -48,7 +55,7 @@ static void test_bus_new_signal(void) { sd_bus *bus = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; - assert_se(sd_bus_open_user(&bus) >= 0); + assert_se(use_system_bus ? sd_bus_open_system(&bus) >= 0 : sd_bus_open_user(&bus) >= 0); assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0); @@ -59,17 +66,12 @@ static void test_bus_new_signal(void) { } int main(int argc, char **argv) { - int r; - - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); test_bus_new(); - r = test_bus_open(); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + + if (test_bus_open() < 0) + return log_tests_skipped("Failed to connect to bus"); test_bus_new_method_call(); test_bus_new_signal(); diff --git a/src/libsystemd/sd-bus/test-bus-creds.c b/src/libsystemd/sd-bus/test-bus-creds.c index 6746c0973e..c02c459663 100644 --- a/src/libsystemd/sd-bus/test-bus-creds.c +++ b/src/libsystemd/sd-bus/test-bus-creds.c @@ -1,25 +1,20 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "sd-bus.h" #include "bus-dump.h" #include "bus-util.h" #include "cgroup-util.h" +#include "tests.h" int main(int argc, char *argv[]) { _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); - if (cg_unified_flush() == -ENOMEDIUM) { - log_info("Skipping test: /sys/fs/cgroup/ not available"); - return EXIT_TEST_SKIP; - } + if (cg_unified_flush() == -ENOMEDIUM) + return log_tests_skipped("/sys/fs/cgroup/ not available"); r = sd_bus_creds_new_from_pid(&creds, 0, _SD_BUS_CREDS_ALL); log_full_errno(r < 0 ? LOG_ERR : LOG_DEBUG, r, "sd_bus_creds_new_from_pid: %m"); diff --git a/src/libsystemd/sd-bus/test-bus-error.c b/src/libsystemd/sd-bus/test-bus-error.c index 250657b18e..f464b5b23d 100644 --- a/src/libsystemd/sd-bus/test-bus-error.c +++ b/src/libsystemd/sd-bus/test-bus-error.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "sd-bus.h" @@ -115,18 +113,18 @@ static void test_error(void) { assert_se(!sd_bus_error_is_set(&error)); } -extern const sd_bus_error_map __start_BUS_ERROR_MAP[]; -extern const sd_bus_error_map __stop_BUS_ERROR_MAP[]; +extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[]; +extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[]; static void dump_mapping_table(void) { const sd_bus_error_map *m; printf("----- errno mappings ------\n"); - m = __start_BUS_ERROR_MAP; - while (m < __stop_BUS_ERROR_MAP) { + m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*)); + while (m < __stop_SYSTEMD_BUS_ERROR_MAP) { if (m->code == BUS_ERROR_MAP_END_MARKER) { - m = ALIGN8_PTR(m+1); + m = ALIGN_TO_PTR(m + 1, sizeof(void*)); continue; } diff --git a/src/libsystemd/sd-bus/test-bus-gvariant.c b/src/libsystemd/sd-bus/test-bus-gvariant.c index 75804f3458..1a9a35d56b 100644 --- a/src/libsystemd/sd-bus/test-bus-gvariant.c +++ b/src/libsystemd/sd-bus/test-bus-gvariant.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #if HAVE_GLIB #include <glib.h> @@ -15,11 +13,14 @@ #include "bus-message.h" #include "bus-util.h" #include "macro.h" +#include "tests.h" #include "util.h" static void test_bus_gvariant_is_fixed_size(void) { + log_info("/* %s */", __func__); + assert_se(bus_gvariant_is_fixed_size("") > 0); - assert_se(bus_gvariant_is_fixed_size("()") > 0); + assert_se(bus_gvariant_is_fixed_size("()") == -EINVAL); assert_se(bus_gvariant_is_fixed_size("y") > 0); assert_se(bus_gvariant_is_fixed_size("u") > 0); assert_se(bus_gvariant_is_fixed_size("b") > 0); @@ -43,8 +44,10 @@ static void test_bus_gvariant_is_fixed_size(void) { } static void test_bus_gvariant_get_size(void) { + log_info("/* %s */", __func__); + assert_se(bus_gvariant_get_size("") == 0); - assert_se(bus_gvariant_get_size("()") == 1); + assert_se(bus_gvariant_get_size("()") == -EINVAL); assert_se(bus_gvariant_get_size("y") == 1); assert_se(bus_gvariant_get_size("u") == 4); assert_se(bus_gvariant_get_size("b") == 1); @@ -75,8 +78,10 @@ static void test_bus_gvariant_get_size(void) { } static void test_bus_gvariant_get_alignment(void) { + log_info("/* %s */", __func__); + assert_se(bus_gvariant_get_alignment("") == 1); - assert_se(bus_gvariant_get_alignment("()") == 1); + assert_se(bus_gvariant_get_alignment("()") == -EINVAL); assert_se(bus_gvariant_get_alignment("y") == 1); assert_se(bus_gvariant_get_alignment("b") == 1); assert_se(bus_gvariant_get_alignment("u") == 4); @@ -115,20 +120,25 @@ static void test_bus_gvariant_get_alignment(void) { assert_se(bus_gvariant_get_alignment("((t)(t))") == 8); } -static void test_marshal(void) { +static int test_marshal(void) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *n = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - _cleanup_free_ void *blob; + _cleanup_free_ void *blob = NULL; size_t sz; int r; r = sd_bus_open_user(&bus); if (r < 0) - exit(EXIT_TEST_SKIP); + r = sd_bus_open_system(&bus); + if (r < 0) + return log_tests_skipped_errno(r, "Failed to connect to bus"); bus->message_version = 2; /* dirty hack to enable gvariant */ - assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path/which/is/really/really/long/so/that/we/hit/the/eight/bit/boundary/by/quite/some/margin/to/test/this/stuff/that/it/really/works", "an.interface.name", "AMethodName") >= 0); + r = sd_bus_message_new_method_call(bus, &m, "a.service.name", + "/an/object/path/which/is/really/really/long/so/that/we/hit/the/eight/bit/boundary/by/quite/some/margin/to/test/this/stuff/that/it/really/works", + "an.interface.name", "AMethodName"); + assert_se(r >= 0); assert_cc(sizeof(struct bus_header) == 16); @@ -196,14 +206,16 @@ static void test_marshal(void) { assert_se(sd_bus_message_seal(m, 4712, 0) >= 0); assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0); + + return EXIT_SUCCESS; } int main(int argc, char *argv[]) { + test_setup_logging(LOG_DEBUG); test_bus_gvariant_is_fixed_size(); test_bus_gvariant_get_size(); test_bus_gvariant_get_alignment(); - test_marshal(); - return 0; + return test_marshal(); } diff --git a/src/libsystemd/sd-bus/test-bus-introspect.c b/src/libsystemd/sd-bus/test-bus-introspect.c index 00167cb643..940dbfe0e7 100644 --- a/src/libsystemd/sd-bus/test-bus-introspect.c +++ b/src/libsystemd/sd-bus/test-bus-introspect.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "bus-introspect.h" #include "log.h" diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index c647f0ff12..1e9810ce4f 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <math.h> #include <stdlib.h> @@ -20,9 +18,10 @@ #include "bus-label.h" #include "bus-message.h" #include "bus-util.h" +#include "escape.h" #include "fd-util.h" -#include "hexdecoct.h" #include "log.h" +#include "tests.h" #include "util.h" static void test_bus_path_encode_unique(void) { @@ -112,7 +111,7 @@ int main(int argc, char *argv[]) { uint8_t u, v; void *buffer = NULL; size_t sz; - char *h; + _cleanup_free_ char *h = NULL; const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array; char *s; _cleanup_free_ char *first = NULL, *second = NULL, *third = NULL; @@ -122,9 +121,13 @@ int main(int argc, char *argv[]) { double dbl; uint64_t u64; + test_setup_logging(LOG_INFO); + r = sd_bus_default_user(&bus); if (r < 0) - return EXIT_TEST_SKIP; + r = sd_bus_default_system(&bus); + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep"); assert_se(r >= 0); @@ -151,7 +154,7 @@ int main(int argc, char *argv[]) { assert_se(r >= 0); r = sd_bus_message_append(m, "()"); - assert_se(r >= 0); + assert_se(r == -EINVAL); r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3"); assert_se(r >= 0); @@ -194,14 +197,12 @@ int main(int argc, char *argv[]) { r = bus_message_get_blob(m, &buffer, &sz); assert_se(r >= 0); - h = hexmem(buffer, sz); + h = cescape_length(buffer, sz); assert_se(h); - log_info("message size = %zu, contents =\n%s", sz, h); - free(h); #if HAVE_GLIB -#ifndef __SANITIZE_ADDRESS__ +#if !HAS_FEATURE_ADDRESS_SANITIZER { GDBusMessage *g; char *p; @@ -295,7 +296,7 @@ int main(int argc, char *argv[]) { assert_se(v == 10); r = sd_bus_message_read(m, "()"); - assert_se(r > 0); + assert_se(r < 0); r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d); assert_se(r > 0); @@ -376,7 +377,7 @@ int main(int argc, char *argv[]) { assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0); - r = sd_bus_message_skip(m, "a{yv}y(ty)y(yt)y()"); + r = sd_bus_message_skip(m, "a{yv}y(ty)y(yt)y"); assert_se(r >= 0); assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0); diff --git a/src/libsystemd/sd-bus/test-bus-match.c b/src/libsystemd/sd-bus/test-bus-match.c index 2822a8f82a..0949d8dee6 100644 --- a/src/libsystemd/sd-bus/test-bus-match.c +++ b/src/libsystemd/sd-bus/test-bus-match.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "bus-match.h" #include "bus-message.h" @@ -8,6 +6,7 @@ #include "bus-util.h" #include "log.h" #include "macro.h" +#include "tests.h" static bool mask[32]; @@ -79,9 +78,13 @@ int main(int argc, char *argv[]) { sd_bus_slot slots[19]; int r; + test_setup_logging(LOG_INFO); + r = sd_bus_open_user(&bus); if (r < 0) - return EXIT_TEST_SKIP; + r = sd_bus_open_system(&bus); + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); assert_se(match_add(slots, &root, "arg2='wal\\'do',sender='foo',type='signal',interface='bar.x',", 1) >= 0); assert_se(match_add(slots, &root, "arg2='wal\\'do2',sender='foo',type='signal',interface='bar.x',", 2) >= 0); diff --git a/src/libsystemd/sd-bus/test-bus-objects.c b/src/libsystemd/sd-bus/test-bus-objects.c index 094dd6c8a0..3c5bb88f4e 100644 --- a/src/libsystemd/sd-bus/test-bus-objects.c +++ b/src/libsystemd/sd-bus/test-bus-objects.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <pthread.h> #include <stdlib.h> @@ -209,7 +207,7 @@ static const sd_bus_vtable vtable2[] = { static int enumerator_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { if (object_path_startswith("/value", path)) - assert_se(*nodes = strv_new("/value/a", "/value/b", "/value/c", NULL)); + assert_se(*nodes = strv_new("/value/a", "/value/b", "/value/c")); return 1; } @@ -217,7 +215,7 @@ static int enumerator_callback(sd_bus *bus, const char *path, void *userdata, ch static int enumerator2_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { if (object_path_startswith("/value/a", path)) - assert_se(*nodes = strv_new("/value/a/x", "/value/a/y", "/value/a/z", NULL)); + assert_se(*nodes = strv_new("/value/a/x", "/value/a/y", "/value/a/z")); return 1; } diff --git a/src/libsystemd/sd-bus/test-bus-server.c b/src/libsystemd/sd-bus/test-bus-server.c index 31b54e252c..0f1b9645bc 100644 --- a/src/libsystemd/sd-bus/test-bus-server.c +++ b/src/libsystemd/sd-bus/test-bus-server.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <pthread.h> #include <stdlib.h> @@ -130,10 +128,8 @@ static int client(struct context *c) { return log_error_errno(r, "Failed to allocate method call: %m"); r = sd_bus_call(bus, m, 0, &error, &reply); - if (r < 0) { - log_error("Failed to issue method call: %s", bus_error_message(&error, -r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, -r)); return 0; } diff --git a/src/libsystemd/sd-bus/test-bus-signature.c b/src/libsystemd/sd-bus/test-bus-signature.c index 1ba1909198..84648dbc2a 100644 --- a/src/libsystemd/sd-bus/test-bus-signature.c +++ b/src/libsystemd/sd-bus/test-bus-signature.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include "bus-internal.h" #include "bus-signature.h" @@ -16,9 +14,9 @@ int main(int argc, char *argv[]) { assert_se(signature_is_single("v", false)); assert_se(signature_is_single("as", false)); assert_se(signature_is_single("(ss)", false)); - assert_se(signature_is_single("()", false)); - assert_se(signature_is_single("(()()()()())", false)); - assert_se(signature_is_single("(((())))", false)); + assert_se(!signature_is_single("()", false)); + assert_se(!signature_is_single("(()()()()())", false)); + assert_se(!signature_is_single("(((())))", false)); assert_se(signature_is_single("((((s))))", false)); assert_se(signature_is_single("{ss}", true)); assert_se(signature_is_single("a{ss}", false)); @@ -63,7 +61,7 @@ int main(int argc, char *argv[]) { assert_se(signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas", false)); assert_se(!signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau", false)); - assert_se(signature_is_valid("(((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))", false)); + assert_se(signature_is_valid("((((((((((((((((((((((((((((((((s))))))))))))))))))))))))))))))))", false)); assert_se(!signature_is_valid("((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))", false)); assert_se(namespace_complex_pattern("", "")); diff --git a/src/libsystemd/sd-bus/test-bus-track.c b/src/libsystemd/sd-bus/test-bus-track.c index 48d708b258..68a0010368 100644 --- a/src/libsystemd/sd-bus/test-bus-track.c +++ b/src/libsystemd/sd-bus/test-bus-track.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <errno.h> #include <sys/socket.h> @@ -8,6 +6,7 @@ #include "sd-bus.h" #include "macro.h" +#include "tests.h" static bool track_cb_called_x = false; static bool track_cb_called_y = false; @@ -46,23 +45,31 @@ int main(int argc, char *argv[]) { _cleanup_(sd_event_unrefp) sd_event *event = NULL; _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL; _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL; + bool use_system_bus = false; const char *unique; int r; + test_setup_logging(LOG_INFO); + r = sd_event_default(&event); assert_se(r >= 0); r = sd_bus_open_user(&a); if (IN_SET(r, -ECONNREFUSED, -ENOENT)) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; + r = sd_bus_open_system(&a); + if (IN_SET(r, -ECONNREFUSED, -ENOENT)) + return log_tests_skipped("Failed to connect to bus"); + use_system_bus = true; } assert_se(r >= 0); r = sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL); assert_se(r >= 0); - r = sd_bus_open_user(&b); + if (use_system_bus) + r = sd_bus_open_system(&b); + else + r = sd_bus_open_user(&b); assert_se(r >= 0); r = sd_bus_attach_event(b, event, SD_EVENT_PRIORITY_NORMAL); diff --git a/src/libsystemd/sd-bus/test-bus-watch-bind.c b/src/libsystemd/sd-bus/test-bus-watch-bind.c index 42a9ce5301..4b3da30079 100644 --- a/src/libsystemd/sd-bus/test-bus-watch-bind.c +++ b/src/libsystemd/sd-bus/test-bus-watch-bind.c @@ -1,6 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** -***/ #include <pthread.h> @@ -10,7 +8,6 @@ #include "alloc-util.h" #include "fd-util.h" -#include "fileio.h" #include "fs-util.h" #include "mkdir.h" #include "path-util.h" @@ -18,6 +15,7 @@ #include "rm-rf.h" #include "socket-util.h" #include "string-util.h" +#include "tmpfile-util.h" static int method_foobar(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { log_info("Got Foobar() call."); @@ -42,10 +40,9 @@ static const sd_bus_vtable vtable[] = { static void* thread_server(void *p) { _cleanup_free_ char *suffixed = NULL, *suffixed2 = NULL, *d = NULL; _cleanup_close_ int fd = -1; - union sockaddr_union u = { - .un.sun_family = AF_UNIX, - }; + union sockaddr_union u = {}; const char *path = p; + int salen; log_debug("Initializing server"); @@ -68,12 +65,13 @@ static void* thread_server(void *p) { assert_se(symlink(basename(suffixed), suffixed2) >= 0); (void) usleep(100 * USEC_PER_MSEC); - strncpy(u.un.sun_path, path, sizeof(u.un.sun_path)); + salen = sockaddr_un_set_path(&u.un, path); + assert_se(salen >= 0); fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); assert_se(fd >= 0); - assert_se(bind(fd, &u.sa, SOCKADDR_UN_LEN(u.un)) >= 0); + assert_se(bind(fd, &u.sa, salen) >= 0); usleep(100 * USEC_PER_MSEC); assert_se(listen(fd, SOMAXCONN) >= 0); |