-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <endian.h>
#include <poll.h>
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-socket.h"
+#include "escape.h"
#include "fd-util.h"
#include "format-util.h"
#include "fs-util.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"
#include "string-util.h"
if (!d)
return 0;
- e = memmem(d + 2, b->rbuffer_size - (d - (char*) b->rbuffer) - 2, "\r\n", 2);
+ e = memmem_safe(d + 2, b->rbuffer_size - (d - (char*) b->rbuffer) - 2, "\r\n", 2);
if (!e)
return 0;
if (b->accept_fd) {
- f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
+ f = memmem_safe(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
if (!f)
return 0;
for (;;) {
/* Check if line is complete */
line = (char*) b->rbuffer + b->auth_rbegin;
- e = memmem(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2);
+ e = memmem_safe(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2);
if (!e)
return processed;
iov = IOVEC_MAKE((uint8_t *)b->rbuffer + b->rbuffer_size, n - b->rbuffer_size);
- if (b->prefer_readv)
+ if (b->prefer_readv) {
k = readv(b->input_fd, &iov, 1);
- else {
+ if (k < 0)
+ k = -errno;
+ } else {
mh = (struct msghdr) {
.msg_iov = &iov,
.msg_iovlen = 1,
assert(b);
assert(!b->ucred_valid);
assert(!b->label);
- assert(b->n_groups == (size_t) -1);
+ assert(b->n_groups == SIZE_MAX);
/* Get the peer for socketpair() sockets */
b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
static int bus_socket_inotify_setup(sd_bus *b) {
_cleanup_free_ int *new_watches = NULL;
_cleanup_free_ char *absolute = NULL;
- size_t n_allocated = 0, n = 0, done = 0, i;
+ size_t n = 0, done = 0, i;
unsigned max_follow = 32;
const char *p;
int wd, r;
* that exists we want to know when files are created or moved into it. For all parents of it we just care if
* they are removed or renamed. */
- if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
+ if (!GREEDY_REALLOC(new_watches, n + 1)) {
r = -ENOMEM;
goto fail;
}
goto fail;
}
- if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
+ if (!GREEDY_REALLOC(new_watches, n + 1)) {
r = -ENOMEM;
goto fail;
}
assert(b->output_fd < 0);
assert(b->sockaddr.sa.sa_family != AF_UNSPEC);
+ if (DEBUG_LOGGING) {
+ _cleanup_free_ char *pretty = NULL;
+ (void) sockaddr_pretty(&b->sockaddr.sa, b->sockaddr_size, false, true, &pretty);
+ log_debug("sd-bus: starting bus%s%s by connecting to %s...",
+ b->description ? " " : "", strempty(b->description), strnull(pretty));
+ }
+
b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (b->input_fd < 0)
return -errno;
assert(b->exec_path);
assert(b->busexec_pid == 0);
+ if (DEBUG_LOGGING) {
+ _cleanup_free_ char *line = NULL;
+
+ if (b->exec_argv)
+ line = quote_command_line(b->exec_argv);
+
+ log_debug("sd-bus: starting bus%s%s with %s%s",
+ b->description ? " " : "", strempty(b->description),
+ line ?: b->exec_path,
+ b->exec_argv && !line ? "…" : "");
+ }
+
r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
if (r < 0)
return -errno;
if (b->exec_argv)
execvp(b->exec_path, b->exec_argv);
- else {
- const char *argv[] = { b->exec_path, NULL };
- execvp(b->exec_path, (char**) argv);
- }
+ else
+ execvp(b->exec_path, STRV_MAKE(b->exec_path));
_exit(EXIT_FAILURE);
}
iov = IOVEC_MAKE((uint8_t *)bus->rbuffer + bus->rbuffer_size, need - bus->rbuffer_size);
- if (bus->prefer_readv)
+ if (bus->prefer_readv) {
k = readv(bus->input_fd, &iov, 1);
- else {
+ if (k < 0)
+ k = -errno;
+ } else {
mh = (struct msghdr) {
.msg_iov = &iov,
.msg_iovlen = 1,
}
int bus_socket_process_opening(sd_bus *b) {
- int error = 0;
+ int error = 0, events, r;
socklen_t slen = sizeof(error);
- struct pollfd p = {
- .fd = b->output_fd,
- .events = POLLOUT,
- };
- int r;
assert(b->state == BUS_OPENING);
- r = poll(&p, 1, 0);
- if (r < 0)
- return -errno;
- if (p.revents & POLLNVAL)
- return -EBADF;
-
- if (!(p.revents & (POLLOUT|POLLERR|POLLHUP)))
+ events = fd_wait_for_event(b->output_fd, POLLOUT, 0);
+ if (events < 0)
+ return events;
+ if (!(events & (POLLOUT|POLLERR|POLLHUP)))
return 0;
r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen);
b->last_connect_error = errno;
else if (error != 0)
b->last_connect_error = error;
- else if (p.revents & (POLLERR|POLLHUP))
+ else if (events & (POLLERR|POLLHUP))
b->last_connect_error = ECONNREFUSED;
else
return bus_socket_start_auth(b);