[systemd-commits] 9 commits - Makefile.am src/libsystemd-bus src/stdio-bridge src/systemd
Lennart Poettering
lennart at kemper.freedesktop.org
Sun Mar 31 18:30:00 PDT 2013
Makefile.am | 7
src/libsystemd-bus/bus-internal.h | 4
src/libsystemd-bus/bus-message.c | 1
src/libsystemd-bus/bus-socket.c | 317 ++++++++++++++++----------
src/libsystemd-bus/sd-bus.c | 115 +++++----
src/libsystemd-bus/test-bus-server.c | 4
src/stdio-bridge/stdio-bridge.c | 422 +++++++++++------------------------
src/systemd/sd-bus.h | 2
8 files changed, 420 insertions(+), 452 deletions(-)
New commits:
commit d51539b182c0a8869e2063e82d04f8a5849bfbab
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Apr 1 03:28:42 2013 +0200
stdio-bridge: rework stdio bridge to use libsystemd-bus
diff --git a/Makefile.am b/Makefile.am
index 2a2a9fe..bcd26a4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1612,7 +1612,9 @@ systemd_stdio_bridge_SOURCES = \
src/stdio-bridge/stdio-bridge.c
systemd_stdio_bridge_LDADD = \
- libsystemd-shared.la
+ libsystemd-shared.la \
+ libsystemd-bus.la \
+ libsystemd-daemon.la
# ------------------------------------------------------------------------------
systemd_tty_ask_password_agent_SOURCES = \
diff --git a/src/stdio-bridge/stdio-bridge.c b/src/stdio-bridge/stdio-bridge.c
index ea82c8f..8667bbf 100644
--- a/src/stdio-bridge/stdio-bridge.c
+++ b/src/stdio-bridge/stdio-bridge.c
@@ -26,121 +26,22 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
-#include <sys/epoll.h>
+#include <sys/poll.h>
#include <stddef.h>
#include "log.h"
#include "util.h"
#include "socket-util.h"
-
-#define BUFFER_SIZE (64*1024)
-#define EXTRA_SIZE 16
-
-static bool initial_nul = false;
-static bool auth_over = false;
-
-static void format_uid(char *buf, size_t l) {
- char text[20 + 1]; /* enough space for a 64bit integer plus NUL */
- unsigned j;
-
- assert(l > 0);
-
- snprintf(text, sizeof(text)-1, "%llu", (unsigned long long) geteuid());
- char_array_0(text);
-
- memset(buf, 0, l);
-
- for (j = 0; text[j] && j*2+2 < l; j++) {
- buf[j*2] = hexchar(text[j] >> 4);
- buf[j*2+1] = hexchar(text[j] & 0xF);
- }
-
- buf[j*2] = 0;
-}
-
-static size_t patch_in_line(char *line, size_t l, size_t left) {
- size_t r;
-
- if (line[0] == 0 && !initial_nul) {
- initial_nul = true;
- line += 1;
- l -= 1;
- r = 1;
- } else
- r = 0;
-
- if (l == 5 && strneq(line, "BEGIN", 5)) {
- r += l;
- auth_over = true;
-
- } else if (l == 17 && strneq(line, "NEGOTIATE_UNIX_FD", 17)) {
- memmove(line + 13, line + 17, left);
- memcpy(line, "NEGOTIATE_NOP", 13);
- r += 13;
-
- } else if (l >= 14 && strneq(line, "AUTH EXTERNAL ", 14)) {
- char uid[20*2 + 1];
- size_t len;
-
- format_uid(uid, sizeof(uid));
- len = strlen(uid);
- assert(len <= EXTRA_SIZE);
-
- memmove(line + 14 + len, line + l, left);
- memcpy(line + 14, uid, len);
-
- r += 14 + len;
- } else
- r += l;
-
- return r;
-}
-
-static size_t patch_in_buffer(char* in_buffer, size_t *in_buffer_full) {
- size_t i, good = 0;
-
- if (*in_buffer_full <= 0)
- return *in_buffer_full;
-
- /* If authentication is done, we don't touch anything anymore */
- if (auth_over)
- return *in_buffer_full;
-
- if (*in_buffer_full < 2)
- return 0;
-
- for (i = 0; i <= *in_buffer_full - 2; i ++) {
-
- /* Fully lines can be send on */
- if (in_buffer[i] == '\r' && in_buffer[i+1] == '\n') {
- if (i > good) {
- size_t old_length, new_length;
-
- old_length = i - good;
- new_length = patch_in_line(in_buffer+good, old_length, *in_buffer_full - i);
- *in_buffer_full = *in_buffer_full + new_length - old_length;
-
- good += new_length + 2;
-
- } else
- good = i+2;
- }
-
- if (auth_over)
- break;
- }
-
- return good;
-}
+#include "sd-daemon.h"
+#include "sd-bus.h"
+#include "bus-internal.h"
+#include "bus-message.h"
int main(int argc, char *argv[]) {
- int r = EXIT_FAILURE, fd = -1, ep = -1;
- union sockaddr_union sa;
- char in_buffer[BUFFER_SIZE+EXTRA_SIZE], out_buffer[BUFFER_SIZE+EXTRA_SIZE];
- size_t in_buffer_full = 0, out_buffer_full = 0;
- struct epoll_event stdin_ev, stdout_ev, fd_ev;
- bool stdin_readable = false, stdout_writable = false, fd_readable = false, fd_writable = false;
- bool stdin_rhup = false, stdout_whup = false, fd_rhup = false, fd_whup = false;
+ _cleanup_bus_unref_ sd_bus *a = NULL, *b = NULL;
+ sd_id128_t server_id;
+ bool is_unix;
+ int r;
if (argc > 1) {
log_error("This program takes no argument.");
@@ -151,217 +52,182 @@ int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
- if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
- log_error("Failed to create socket: %s", strerror(errno));
+ is_unix =
+ sd_is_socket(STDIN_FILENO, AF_UNIX, 0, 0) > 0 &&
+ sd_is_socket(STDOUT_FILENO, AF_UNIX, 0, 0) > 0;
+
+ r = sd_bus_new(&a);
+ if (r < 0) {
+ log_error("Failed to allocate bus: %s", strerror(-r));
goto finish;
}
- zero(sa);
- sa.un.sun_family = AF_UNIX;
- strncpy(sa.un.sun_path, "/run/dbus/system_bus_socket", sizeof(sa.un.sun_path));
+ r = sd_bus_set_address(a, "unix:path=/run/dbus/system_bus_socket");
+ if (r < 0) {
+ log_error("Failed to set address to connect to: %s", strerror(-r));
+ goto finish;
+ }
- if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
- log_error("Failed to connect: %m");
+ r = sd_bus_set_negotiate_fds(a, is_unix);
+ if (r < 0) {
+ log_error("Failed to set FD negotiation: %s", strerror(-r));
goto finish;
}
- fd_nonblock(STDIN_FILENO, 1);
- fd_nonblock(STDOUT_FILENO, 1);
+ r = sd_bus_start(a);
+ if (r < 0) {
+ log_error("Failed to start bus client: %s", strerror(-r));
+ goto finish;
+ }
- if ((ep = epoll_create1(EPOLL_CLOEXEC)) < 0) {
- log_error("Failed to create epoll: %m");
+ r = sd_bus_get_server_id(a, &server_id);
+ if (r < 0) {
+ log_error("Failed to get server ID: %s", strerror(-r));
goto finish;
}
- zero(stdin_ev);
- stdin_ev.events = EPOLLIN|EPOLLET;
- stdin_ev.data.fd = STDIN_FILENO;
+ r = sd_bus_new(&b);
+ if (r < 0) {
+ log_error("Failed to allocate bus: %s", strerror(-r));
+ goto finish;
+ }
- zero(stdout_ev);
- stdout_ev.events = EPOLLOUT|EPOLLET;
- stdout_ev.data.fd = STDOUT_FILENO;
+ r = sd_bus_set_fd(b, STDIN_FILENO, STDOUT_FILENO);
+ if (r < 0) {
+ log_error("Failed to set fds: %s", strerror(-r));
+ goto finish;
+ }
- zero(fd_ev);
- fd_ev.events = EPOLLIN|EPOLLOUT|EPOLLET;
- fd_ev.data.fd = fd;
+ r = sd_bus_set_server(b, 1, server_id);
+ if (r < 0) {
+ log_error("Failed to set server mode: %s", strerror(-r));
+ goto finish;
+ }
- if (epoll_ctl(ep, EPOLL_CTL_ADD, STDIN_FILENO, &stdin_ev) < 0 ||
- epoll_ctl(ep, EPOLL_CTL_ADD, STDOUT_FILENO, &stdout_ev) < 0 ||
- epoll_ctl(ep, EPOLL_CTL_ADD, fd, &fd_ev) < 0) {
- log_error("Failed to regiser fds in epoll: %m");
+ r = sd_bus_set_negotiate_fds(b, is_unix);
+ if (r < 0) {
+ log_error("Failed to set FD negotiation: %s", strerror(-r));
goto finish;
}
- do {
- struct epoll_event ev[16];
- ssize_t k;
- int i, nfds;
+ r = sd_bus_set_anonymous(b, true);
+ if (r < 0) {
+ log_error("Failed to set anonymous authentication: %s", strerror(-r));
+ goto finish;
+ }
- if ((nfds = epoll_wait(ep, ev, ELEMENTSOF(ev), -1)) < 0) {
+ r = sd_bus_start(b);
+ if (r < 0) {
+ log_error("Failed to start bus client: %s", strerror(-r));
+ goto finish;
+ }
- if (errno == EINTR || errno == EAGAIN)
- continue;
+ for (;;) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ struct pollfd p[3];
+ int events_a, events_b, fd;
+ uint64_t timeout_a, timeout_b, t;
+ struct timespec _ts, *ts;
- log_error("epoll_wait(): %m");
+ r = sd_bus_process(a, &m);
+ if (r < 0) {
+ log_error("Failed to process bus: %s", strerror(-r));
goto finish;
}
- assert(nfds >= 1);
-
- for (i = 0; i < nfds; i++) {
- if (ev[i].data.fd == STDIN_FILENO) {
-
- if (!stdin_rhup && (ev[i].events & (EPOLLHUP|EPOLLIN)))
- stdin_readable = true;
-
- } else if (ev[i].data.fd == STDOUT_FILENO) {
-
- if (ev[i].events & EPOLLHUP) {
- stdout_writable = false;
- stdout_whup = true;
- }
-
- if (!stdout_whup && (ev[i].events & EPOLLOUT))
- stdout_writable = true;
-
- } else if (ev[i].data.fd == fd) {
+ if (m) {
+ r = sd_bus_send(b, m, NULL);
+ if (r < 0) {
+ log_error("Failed to send message: %s", strerror(-r));
+ goto finish;
+ }
+ }
- if (ev[i].events & EPOLLHUP) {
- fd_writable = false;
- fd_whup = true;
- }
+ if (r > 0)
+ continue;
- if (!fd_rhup && (ev[i].events & (EPOLLHUP|EPOLLIN)))
- fd_readable = true;
+ r = sd_bus_process(b, &m);
+ if (r < 0) {
+ log_error("Failed to process bus: %s", strerror(-r));
+ goto finish;
+ }
- if (!fd_whup && (ev[i].events & EPOLLOUT))
- fd_writable = true;
+ if (m) {
+ r = sd_bus_send(a, m, NULL);
+ if (r < 0) {
+ log_error("Failed to send message: %s", strerror(-r));
+ goto finish;
}
}
- while ((stdin_readable && in_buffer_full <= 0) ||
- (fd_writable && patch_in_buffer(in_buffer, &in_buffer_full) > 0) ||
- (fd_readable && out_buffer_full <= 0) ||
- (stdout_writable && out_buffer_full > 0)) {
-
- size_t in_buffer_good = 0;
-
- if (stdin_readable && in_buffer_full < BUFFER_SIZE) {
-
- if ((k = read(STDIN_FILENO, in_buffer + in_buffer_full, BUFFER_SIZE - in_buffer_full)) < 0) {
-
- if (errno == EAGAIN)
- stdin_readable = false;
- else if (errno == EPIPE || errno == ECONNRESET)
- k = 0;
- else {
- log_error("read(): %m");
- goto finish;
- }
- } else
- in_buffer_full += (size_t) k;
-
- if (k == 0) {
- stdin_rhup = true;
- stdin_readable = false;
- shutdown(STDIN_FILENO, SHUT_RD);
- close_nointr_nofail(STDIN_FILENO);
- }
- }
+ if (r > 0)
+ continue;
- in_buffer_good = patch_in_buffer(in_buffer, &in_buffer_full);
+ fd = sd_bus_get_fd(a);
+ if (fd < 0) {
+ log_error("Failed to get fd: %s", strerror(-r));
+ goto finish;
+ }
- if (fd_writable && in_buffer_good > 0) {
+ events_a = sd_bus_get_events(a);
+ if (events_a < 0) {
+ log_error("Failed to get events mask: %s", strerror(-r));
+ goto finish;
+ }
- if ((k = write(fd, in_buffer, in_buffer_good)) < 0) {
+ r = sd_bus_get_timeout(a, &timeout_a);
+ if (r < 0) {
+ log_error("Failed to get timeout: %s", strerror(-r));
+ goto finish;
+ }
- if (errno == EAGAIN)
- fd_writable = false;
- else if (errno == EPIPE || errno == ECONNRESET) {
- fd_whup = true;
- fd_writable = false;
- shutdown(fd, SHUT_WR);
- } else {
- log_error("write(): %m");
- goto finish;
- }
+ events_b = sd_bus_get_events(b);
+ if (events_b < 0) {
+ log_error("Failed to get events mask: %s", strerror(-r));
+ goto finish;
+ }
- } else {
- assert(in_buffer_full >= (size_t) k);
- memmove(in_buffer, in_buffer + k, in_buffer_full - k);
- in_buffer_full -= k;
- }
- }
+ r = sd_bus_get_timeout(b, &timeout_b);
+ if (r < 0) {
+ log_error("Failed to get timeout: %s", strerror(-r));
+ goto finish;
+ }
- if (fd_readable && out_buffer_full < BUFFER_SIZE) {
-
- if ((k = read(fd, out_buffer + out_buffer_full, BUFFER_SIZE - out_buffer_full)) < 0) {
-
- if (errno == EAGAIN)
- fd_readable = false;
- else if (errno == EPIPE || errno == ECONNRESET)
- k = 0;
- else {
- log_error("read(): %m");
- goto finish;
- }
- } else
- out_buffer_full += (size_t) k;
-
- if (k == 0) {
- fd_rhup = true;
- fd_readable = false;
- shutdown(fd, SHUT_RD);
- }
- }
+ t = timeout_a;
+ if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a))
+ t = timeout_b;
- if (stdout_writable && out_buffer_full > 0) {
-
- if ((k = write(STDOUT_FILENO, out_buffer, out_buffer_full)) < 0) {
-
- if (errno == EAGAIN)
- stdout_writable = false;
- else if (errno == EPIPE || errno == ECONNRESET) {
- stdout_whup = true;
- stdout_writable = false;
- shutdown(STDOUT_FILENO, SHUT_WR);
- close_nointr(STDOUT_FILENO);
- } else {
- log_error("write(): %m");
- goto finish;
- }
-
- } else {
- assert(out_buffer_full >= (size_t) k);
- memmove(out_buffer, out_buffer + k, out_buffer_full - k);
- out_buffer_full -= k;
- }
- }
- }
+ if (t == (uint64_t) -1)
+ ts = NULL;
+ else {
+ usec_t nw;
- if (stdin_rhup && in_buffer_full <= 0 && !fd_whup) {
- fd_whup = true;
- fd_writable = false;
- shutdown(fd, SHUT_WR);
- }
+ nw = now(CLOCK_MONOTONIC);
+ if (t > nw)
+ t -= nw;
+ else
+ t = 0;
- if (fd_rhup && out_buffer_full <= 0 && !stdout_whup) {
- stdout_whup = true;
- stdout_writable = false;
- shutdown(STDOUT_FILENO, SHUT_WR);
- close_nointr(STDOUT_FILENO);
+ ts = timespec_store(&_ts, t);
}
- } while (!stdout_whup || !fd_whup);
+ zero(p);
+ p[0].fd = fd;
+ p[0].events = events_a;
+ p[1].fd = STDIN_FILENO;
+ p[1].events = events_b & POLLIN;
+ p[2].fd = STDOUT_FILENO;
+ p[2].events = events_b & POLLOUT;
+
+ r = ppoll(p, ELEMENTSOF(p), ts, NULL);
+ if (r < 0) {
+ log_error("ppoll() failed: %m");
+ goto finish;
+ }
+ }
- r = EXIT_SUCCESS;
+ r = 0;
finish:
- if (fd >= 0)
- close_nointr_nofail(fd);
-
- if (ep >= 0)
- close_nointr_nofail(ep);
-
- return r;
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
commit adee69fa7a26df2f1b63cb813c1074f51a2a2612
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Apr 1 03:26:38 2013 +0200
bus: always return something in sd_bus_get_timeout()
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 4f004ad..0964649 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -1425,12 +1425,16 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
return 1;
}
- if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO)
+ if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
+ *timeout_usec = (uint64_t) -1;
return 0;
+ }
c = prioq_peek(bus->reply_callbacks_prioq);
- if (!c)
+ if (!c) {
+ *timeout_usec = (uint64_t) -1;
return 0;
+ }
*timeout_usec = c->timeout;
return 1;
commit ea8f194f4b402774aafb9bc3c3ea1c0f288f12a7
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Apr 1 03:25:31 2013 +0200
bus: be more careful when determining credential data
As it turns out SCM_CREDENTIALS is not always supported on socketpair(),
so let's also try SO_PEERCRED then.
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index 82e683a..5b5a731 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -513,16 +513,23 @@ static int bus_socket_read_auth(sd_bus *b) {
cmsg->cmsg_type == SCM_CREDENTIALS &&
cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
- memcpy(&b->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
- b->ucred_valid = true;
+ /* Ignore bogus data, which we might
+ * get on socketpair() sockets */
+ if (((struct ucred*) CMSG_DATA(cmsg))->pid != 0) {
+ memcpy(&b->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
+ b->ucred_valid = true;
+ }
} else if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_SECURITY) {
size_t l;
+
l = cmsg->cmsg_len - CMSG_LEN(0);
- memcpy(&b->label, CMSG_DATA(cmsg), l);
- b->label[l] = 0;
+ if (l > 0) {
+ memcpy(&b->label, CMSG_DATA(cmsg), l);
+ b->label[l] = 0;
+ }
}
}
}
@@ -536,6 +543,7 @@ static int bus_socket_read_auth(sd_bus *b) {
static int bus_socket_setup(sd_bus *b) {
int enable;
+ socklen_t l;
assert(b);
@@ -549,6 +557,11 @@ static int bus_socket_setup(sd_bus *b) {
fd_inc_rcvbuf(b->input_fd, 1024*1024);
fd_inc_sndbuf(b->output_fd, 1024*1024);
+ /* Get the peer for socketpair() sockets */
+ l = sizeof(b->ucred);
+ if (getsockopt(b->input_fd, SOL_SOCKET, SO_PEERCRED, &b->ucred, &l) >= 0 && l >= sizeof(b->ucred))
+ b->ucred_valid = b->ucred.pid > 0;
+
return 0;
}
@@ -940,16 +953,22 @@ int bus_socket_read_message(sd_bus *bus, sd_bus_message **m) {
cmsg->cmsg_type == SCM_CREDENTIALS &&
cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
- memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
- bus->ucred_valid = true;
+ /* Ignore bogus data, which we might
+ * get on socketpair() sockets */
+ if (((struct ucred*) CMSG_DATA(cmsg))->pid != 0) {
+ memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
+ bus->ucred_valid = true;
+ }
} else if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_SECURITY) {
size_t l;
l = cmsg->cmsg_len - CMSG_LEN(0);
- memcpy(&bus->label, CMSG_DATA(cmsg), l);
- bus->label[l] = 0;
+ if (l > 0) {
+ memcpy(&bus->label, CMSG_DATA(cmsg), l);
+ bus->label[l] = 0;
+ }
}
}
}
commit 2b4ac8896bdc6cc5159088d7d208559d53caacf3
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Apr 1 03:24:43 2013 +0200
bus: when the first char a server receives isn't the NUL byte immediately fail
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index 30e5942..82e683a 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -313,13 +313,16 @@ static int bus_socket_auth_verify_server(sd_bus *b) {
assert(b);
- if (b->rbuffer_size < 3)
+ if (b->rbuffer_size < 1)
return 0;
/* First char must be a NUL byte */
if (*(char*) b->rbuffer != 0)
return -EIO;
+ if (b->rbuffer_size < 3)
+ return 0;
+
/* Begin with the first line */
if (b->auth_rbegin <= 0)
b->auth_rbegin = 1;
commit 8411d2a2b15ec8020cd563162cf1b372d8c0319a
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Apr 1 03:24:03 2013 +0200
bus: ignore the passed UID on EXTERNAL auth if ANONYMOUS is allowed
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index b9ef7c0..30e5942 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -235,7 +235,7 @@ static int verify_external_token(sd_bus *b, const char *p, size_t l) {
* the owner of this bus wanted authentication he should have
* checked SO_PEERCRED before even creating the bus object. */
- if (!b->ucred_valid)
+ if (!b->anonymous_auth && !b->ucred_valid)
return 0;
if (l <= 0)
@@ -258,7 +258,9 @@ static int verify_external_token(sd_bus *b, const char *p, size_t l) {
if (r < 0)
return 0;
- if (u != b->ucred.uid)
+ /* We ignore the passed value if anonymous authentication is
+ * on anyway. */
+ if (!b->anonymous_auth && u != b->ucred.uid)
return 0;
return 1;
commit 638866cbea2d95b3f65f3abc4ada240a94e64c3a
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon Apr 1 03:23:27 2013 +0200
bus: when reading a message from the socket, make sure to note down the size
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index c7cc32e..773a670 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -302,6 +302,7 @@ int bus_message_from_malloc(
m->n_iovec = 1;
m->iovec[0].iov_base = buffer;
m->iovec[0].iov_len = length;
+ m->size = length;
r = message_parse_fields(m);
if (r < 0)
commit 9ab32f9daa9601250f183a694586712654be2ffe
Author: Lennart Poettering <lennart at poettering.net>
Date: Sun Mar 31 20:19:44 2013 +0200
bus: reuse sd_is_socket() call
diff --git a/Makefile.am b/Makefile.am
index ef13ad2..2a2a9fe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1689,7 +1689,8 @@ libsystemd_bus_la_SOURCES = \
libsystemd_bus_la_LIBADD = \
libsystemd-id128-internal.la \
- libsystemd-shared.la
+ libsystemd-shared.la \
+ libsystemd-daemon.la
noinst_LTLIBRARIES += \
libsystemd-bus.la
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index ce6af49..b9ef7c0 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -31,6 +31,7 @@
#include "missing.h"
#include "strv.h"
#include "utf8.h"
+#include "sd-daemon.h"
#include "sd-bus.h"
#include "bus-socket.h"
@@ -589,25 +590,17 @@ static int bus_socket_start_auth_client(sd_bus *b) {
}
static int bus_socket_start_auth(sd_bus *b) {
- int domain = 0, r;
- socklen_t sl;
-
assert(b);
b->state = BUS_AUTHENTICATING;
b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT;
- sl = sizeof(domain);
- r = getsockopt(b->input_fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl);
- if (r < 0 || domain != AF_UNIX)
+ if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0)
b->negotiate_fds = false;
- if (b->output_fd != b->input_fd) {
- r = getsockopt(b->output_fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl);
- if (r < 0 || domain != AF_UNIX)
+ if (b->output_fd != b->input_fd)
+ if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0)
b->negotiate_fds = false;
- }
-
if (b->is_server)
return bus_socket_read_auth(b);
commit 15d5af8145a36194385b4558eeeef91512f2151e
Author: Lennart Poettering <lennart at poettering.net>
Date: Sun Mar 31 20:19:18 2013 +0200
bus: fall back to readv/writev if recvmsg/sendmsg don't work
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 4997936..feafed0 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -83,6 +83,8 @@ struct sd_bus {
bool ucred_valid:1;
bool is_server:1;
bool anonymous_auth:1;
+ bool prefer_readv:1;
+ bool prefer_writev:1;
void *rbuffer;
size_t rbuffer_size;
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index 9d08674..ce6af49 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -75,7 +75,6 @@ bool bus_socket_auth_needs_write(sd_bus *b) {
}
static int bus_socket_write_auth(sd_bus *b) {
- struct msghdr mh;
ssize_t k;
assert(b);
@@ -84,16 +83,26 @@ static int bus_socket_write_auth(sd_bus *b) {
if (!bus_socket_auth_needs_write(b))
return 0;
- zero(mh);
- mh.msg_iov = b->auth_iovec + b->auth_index;
- mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index;
+ if (b->prefer_writev)
+ k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index);
+ else {
+ struct msghdr mh;
+ zero(mh);
+
+ mh.msg_iov = b->auth_iovec + b->auth_index;
+ mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index;
+
+ k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ if (k < 0 && errno == ENOTSOCK) {
+ b->prefer_writev = true;
+ k = writev(b->output_fd, b->auth_iovec + b->auth_index, ELEMENTSOF(b->auth_iovec) - b->auth_index);
+ }
+ }
- k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k);
-
return 1;
}
@@ -431,6 +440,7 @@ static int bus_socket_read_auth(sd_bus *b) {
CMSG_SPACE(NAME_MAX)]; /*selinux label */
} control;
struct cmsghdr *cmsg;
+ bool handle_cmsg = false;
assert(b);
assert(b->state == BUS_AUTHENTICATING);
@@ -457,13 +467,22 @@ static int bus_socket_read_auth(sd_bus *b) {
iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size;
iov.iov_len = n - b->rbuffer_size;
- zero(mh);
- mh.msg_iov = &iov;
- mh.msg_iovlen = 1;
- mh.msg_control = &control;
- mh.msg_controllen = sizeof(control);
-
- k = recvmsg(b->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ if (b->prefer_readv)
+ k = readv(b->input_fd, &iov, 1);
+ else {
+ zero(mh);
+ mh.msg_iov = &iov;
+ mh.msg_iovlen = 1;
+ mh.msg_control = &control;
+ mh.msg_controllen = sizeof(control);
+
+ k = recvmsg(b->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ if (k < 0 && errno == ENOTSOCK) {
+ b->prefer_readv = true;
+ k = readv(b->input_fd, &iov, 1);
+ } else
+ handle_cmsg = true;
+ }
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
if (k == 0)
@@ -471,32 +490,34 @@ static int bus_socket_read_auth(sd_bus *b) {
b->rbuffer_size += k;
- for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
- if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_RIGHTS) {
- int j;
-
- /* Whut? We received fds during the auth
- * protocol? Somebody is playing games with
- * us. Close them all, and fail */
- j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
- close_many((int*) CMSG_DATA(cmsg), j);
- return -EIO;
+ if (handle_cmsg) {
+ for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_RIGHTS) {
+ int j;
+
+ /* Whut? We received fds during the auth
+ * protocol? Somebody is playing games with
+ * us. Close them all, and fail */
+ j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+ close_many((int*) CMSG_DATA(cmsg), j);
+ return -EIO;
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_CREDENTIALS &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
+ } else if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_CREDENTIALS &&
+ cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
- memcpy(&b->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
- b->ucred_valid = true;
+ memcpy(&b->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
+ b->ucred_valid = true;
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_SECURITY) {
+ } else if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_SECURITY) {
- size_t l;
- l = cmsg->cmsg_len - CMSG_LEN(0);
- memcpy(&b->label, CMSG_DATA(cmsg), l);
- b->label[l] = 0;
+ size_t l;
+ l = cmsg->cmsg_len - CMSG_LEN(0);
+ memcpy(&b->label, CMSG_DATA(cmsg), l);
+ b->label[l] = 0;
+ }
}
}
@@ -687,7 +708,6 @@ int bus_socket_take_fd(sd_bus *b) {
}
int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
- struct msghdr mh;
struct iovec *iov;
ssize_t k;
size_t n;
@@ -700,18 +720,6 @@ int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
if (*idx >= m->size)
return 0;
- zero(mh);
-
- if (m->n_fds > 0) {
- struct cmsghdr *control;
- control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds));
-
- mh.msg_control = control;
- control->cmsg_level = SOL_SOCKET;
- control->cmsg_type = SCM_RIGHTS;
- mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
- memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
- }
n = m->n_iovec * sizeof(struct iovec);
iov = alloca(n);
@@ -720,10 +728,33 @@ int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
j = 0;
iovec_advance(iov, &j, *idx);
- mh.msg_iov = iov;
- mh.msg_iovlen = m->n_iovec;
+ if (bus->prefer_writev)
+ k = writev(bus->output_fd, iov, m->n_iovec);
+ else {
+ struct msghdr mh;
+ zero(mh);
+
+ if (m->n_fds > 0) {
+ struct cmsghdr *control;
+ control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds));
+
+ mh.msg_control = control;
+ control->cmsg_level = SOL_SOCKET;
+ control->cmsg_type = SCM_RIGHTS;
+ mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
+ memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
+ }
+
+ mh.msg_iov = iov;
+ mh.msg_iovlen = m->n_iovec;
+
+ k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ if (k < 0 && errno == ENOTSOCK) {
+ bus->prefer_writev = true;
+ k = writev(bus->output_fd, iov, m->n_iovec);
+ }
+ }
- k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
@@ -835,6 +866,7 @@ int bus_socket_read_message(sd_bus *bus, sd_bus_message **m) {
CMSG_SPACE(NAME_MAX)]; /*selinux label */
} control;
struct cmsghdr *cmsg;
+ bool handle_cmsg;
assert(bus);
assert(m);
@@ -857,13 +889,22 @@ int bus_socket_read_message(sd_bus *bus, sd_bus_message **m) {
iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size;
iov.iov_len = need - bus->rbuffer_size;
- zero(mh);
- mh.msg_iov = &iov;
- mh.msg_iovlen = 1;
- mh.msg_control = &control;
- mh.msg_controllen = sizeof(control);
-
- k = recvmsg(bus->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ if (bus->prefer_readv)
+ k = readv(bus->input_fd, &iov, 1);
+ else {
+ zero(mh);
+ mh.msg_iov = &iov;
+ mh.msg_iovlen = 1;
+ mh.msg_control = &control;
+ mh.msg_controllen = sizeof(control);
+
+ k = recvmsg(bus->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ if (k < 0 && errno == ENOTSOCK) {
+ bus->prefer_readv = true;
+ k = readv(bus->input_fd, &iov, 1);
+ } else
+ handle_cmsg = true;
+ }
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
if (k == 0)
@@ -871,45 +912,47 @@ int bus_socket_read_message(sd_bus *bus, sd_bus_message **m) {
bus->rbuffer_size += k;
- for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
- if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_RIGHTS) {
- int n, *f;
-
- n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
-
- if (!bus->can_fds) {
- /* Whut? We received fds but this
- * isn't actually enabled? Close them,
- * and fail */
-
- close_many((int*) CMSG_DATA(cmsg), n);
- return -EIO;
- }
-
- f = realloc(bus->fds, sizeof(int) + (bus->n_fds + n));
- if (!f) {
- close_many((int*) CMSG_DATA(cmsg), n);
- return -ENOMEM;
+ if (handle_cmsg) {
+ for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_RIGHTS) {
+ int n, *f;
+
+ n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+
+ if (!bus->can_fds) {
+ /* Whut? We received fds but this
+ * isn't actually enabled? Close them,
+ * and fail */
+
+ close_many((int*) CMSG_DATA(cmsg), n);
+ return -EIO;
+ }
+
+ f = realloc(bus->fds, sizeof(int) + (bus->n_fds + n));
+ if (!f) {
+ close_many((int*) CMSG_DATA(cmsg), n);
+ return -ENOMEM;
+ }
+
+ memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int));
+ bus->fds = f;
+ bus->n_fds += n;
+ } else if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_CREDENTIALS &&
+ cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
+
+ memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
+ bus->ucred_valid = true;
+
+ } else if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_SECURITY) {
+
+ size_t l;
+ l = cmsg->cmsg_len - CMSG_LEN(0);
+ memcpy(&bus->label, CMSG_DATA(cmsg), l);
+ bus->label[l] = 0;
}
-
- memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int));
- bus->fds = f;
- bus->n_fds += n;
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_CREDENTIALS &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
-
- memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
- bus->ucred_valid = true;
-
- } else if (cmsg->cmsg_level == SOL_SOCKET &&
- cmsg->cmsg_type == SCM_SECURITY) {
-
- size_t l;
- l = cmsg->cmsg_len - CMSG_LEN(0);
- memcpy(&bus->label, CMSG_DATA(cmsg), l);
- bus->label[l] = 0;
}
}
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index f792bfa..057931d 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -57,7 +57,7 @@ int sd_bus_open_user(sd_bus **ret);
int sd_bus_new(sd_bus **ret);
int sd_bus_set_address(sd_bus *bus, const char *address);
-int sd_bus_set_fd(sd_bus *bus, int fd);
+int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd);
int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]);
int sd_bus_set_bus_client(sd_bus *bus, int b);
int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id);
commit e82c950997fd1c8f7eb1cea881cb35387321af3d
Author: Lennart Poettering <lennart at poettering.net>
Date: Sun Mar 31 19:10:49 2013 +0200
bus: allow two different fds for input/output in sd_bus_set_fd()
This is useful so that we can speak D-Bus over a FIFO pair such as
stdin+stdout.
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 0149604..4997936 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -74,7 +74,7 @@ enum bus_auth {
struct sd_bus {
unsigned n_ref;
enum bus_state state;
- int fd;
+ int input_fd, output_fd;
int message_version;
bool negotiate_fds:1;
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index fed1971..9d08674 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -88,7 +88,7 @@ static int bus_socket_write_auth(sd_bus *b) {
mh.msg_iov = b->auth_iovec + b->auth_index;
mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index;
- k = sendmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ k = sendmsg(b->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
@@ -463,7 +463,7 @@ static int bus_socket_read_auth(sd_bus *b) {
mh.msg_control = &control;
mh.msg_controllen = sizeof(control);
- k = recvmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ k = recvmsg(b->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
if (k == 0)
@@ -515,12 +515,12 @@ static int bus_socket_setup(sd_bus *b) {
/* Enable SO_PASSCRED + SO_PASSEC. We try this on any
* socket, just in case. */
enable = !b->bus_client;
- setsockopt(b->fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable));
- setsockopt(b->fd, SOL_SOCKET, SO_PASSSEC, &enable, sizeof(enable));
+ setsockopt(b->input_fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable));
+ setsockopt(b->input_fd, SOL_SOCKET, SO_PASSSEC, &enable, sizeof(enable));
/* Increase the buffers to a MB */
- fd_inc_rcvbuf(b->fd, 1024*1024);
- fd_inc_sndbuf(b->fd, 1024*1024);
+ fd_inc_rcvbuf(b->input_fd, 1024*1024);
+ fd_inc_sndbuf(b->output_fd, 1024*1024);
return 0;
}
@@ -577,13 +577,17 @@ static int bus_socket_start_auth(sd_bus *b) {
b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT;
sl = sizeof(domain);
- r = getsockopt(b->fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl);
- if (r < 0)
- return -errno;
-
- if (domain != AF_UNIX)
+ r = getsockopt(b->input_fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl);
+ if (r < 0 || domain != AF_UNIX)
b->negotiate_fds = false;
+ if (b->output_fd != b->input_fd) {
+ r = getsockopt(b->output_fd, SOL_SOCKET, SO_DOMAIN, &domain, &sl);
+ if (r < 0 || domain != AF_UNIX)
+ b->negotiate_fds = false;
+ }
+
+
if (b->is_server)
return bus_socket_read_auth(b);
else
@@ -594,18 +598,21 @@ int bus_socket_connect(sd_bus *b) {
int r;
assert(b);
- assert(b->fd < 0);
+ assert(b->input_fd < 0);
+ assert(b->output_fd < 0);
assert(b->sockaddr.sa.sa_family != AF_UNSPEC);
- b->fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
- if (b->fd < 0)
+ b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
+ if (b->input_fd < 0)
return -errno;
+ b->output_fd = b->input_fd;
+
r = bus_socket_setup(b);
if (r < 0)
return r;
- r = connect(b->fd, &b->sockaddr.sa, b->sockaddr_size);
+ r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size);
if (r < 0) {
if (errno == EINPROGRESS)
return 1;
@@ -617,15 +624,16 @@ int bus_socket_connect(sd_bus *b) {
}
int bus_socket_exec(sd_bus *b) {
- int s[2];
+ int s[2], r;
pid_t pid;
assert(b);
- assert(b->fd < 0);
+ assert(b->input_fd < 0);
+ assert(b->output_fd < 0);
assert(b->exec_path);
- b->fd = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
- if (b->fd < 0)
+ r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
+ if (r < 0)
return -errno;
pid = fork();
@@ -636,8 +644,9 @@ int bus_socket_exec(sd_bus *b) {
if (pid == 0) {
/* Child */
- close_all_fds(s, 2);
- close_nointr_nofail(s[0]);
+ reset_all_signal_handlers();
+
+ close_all_fds(s+1, 1);
assert_se(dup3(s[1], STDIN_FILENO, 0) == STDIN_FILENO);
assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO);
@@ -661,7 +670,7 @@ int bus_socket_exec(sd_bus *b) {
}
close_nointr_nofail(s[1]);
- b->fd = s[0];
+ b->output_fd = b->input_fd = s[0];
return bus_socket_start_auth(b);
}
@@ -714,7 +723,7 @@ int bus_socket_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
mh.msg_iov = iov;
mh.msg_iovlen = m->n_iovec;
- k = sendmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ k = sendmsg(bus->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
@@ -854,7 +863,7 @@ int bus_socket_read_message(sd_bus *bus, sd_bus_message **m) {
mh.msg_control = &control;
mh.msg_controllen = sizeof(control);
- k = recvmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
+ k = recvmsg(bus->input_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
if (k < 0)
return errno == EAGAIN ? 0 : -errno;
if (k == 0)
@@ -924,7 +933,7 @@ int bus_socket_process_opening(sd_bus *b) {
assert(b->state == BUS_OPENING);
zero(p);
- p.fd = b->fd;
+ p.fd = b->output_fd;
p.events = POLLOUT;
r = poll(&p, 1, 0);
@@ -934,7 +943,7 @@ int bus_socket_process_opening(sd_bus *b) {
if (!(p.revents & (POLLOUT|POLLERR|POLLHUP)))
return 0;
- r = getsockopt(b->fd, SOL_SOCKET, SO_ERROR, &error, &slen);
+ r = getsockopt(b->output_fd, SOL_SOCKET, SO_ERROR, &error, &slen);
if (r < 0)
b->last_connect_error = errno;
else if (error != 0)
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index a71d7b5..4f004ad 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -48,8 +48,7 @@ static void bus_free(sd_bus *b) {
assert(b);
- if (b->fd >= 0)
- close_nointr_nofail(b->fd);
+ sd_bus_close(b);
free(b->rbuffer);
free(b->unique_name);
@@ -101,7 +100,7 @@ int sd_bus_new(sd_bus **ret) {
return -ENOMEM;
r->n_ref = 1;
- r->fd = -1;
+ r->input_fd = r->output_fd = -1;
r->message_version = 1;
r->negotiate_fds = true;
@@ -137,15 +136,18 @@ int sd_bus_set_address(sd_bus *bus, const char *address) {
return 0;
}
-int sd_bus_set_fd(sd_bus *bus, int fd) {
+int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
if (!bus)
return -EINVAL;
if (bus->state != BUS_UNSET)
return -EPERM;
- if (fd < 0)
+ if (input_fd < 0)
+ return -EINVAL;
+ if (output_fd < 0)
return -EINVAL;
- bus->fd = fd;
+ bus->input_fd = input_fd;
+ bus->output_fd = output_fd;
return 0;
}
@@ -679,10 +681,7 @@ static int bus_start_address(sd_bus *b) {
assert(b);
for (;;) {
- if (b->fd >= 0) {
- close_nointr_nofail(b->fd);
- b->fd = -1;
- }
+ sd_bus_close(b);
if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
@@ -720,15 +719,27 @@ static int bus_start_fd(sd_bus *b) {
int r;
assert(b);
+ assert(b->input_fd >= 0);
+ assert(b->output_fd >= 0);
- r = fd_nonblock(b->fd, true);
+ r = fd_nonblock(b->input_fd, true);
if (r < 0)
return r;
- r = fd_cloexec(b->fd, true);
+ r = fd_cloexec(b->input_fd, true);
if (r < 0)
return r;
+ if (b->input_fd != b->output_fd) {
+ r = fd_nonblock(b->output_fd, true);
+ if (r < 0)
+ return r;
+
+ r = fd_cloexec(b->output_fd, true);
+ if (r < 0)
+ return r;
+ }
+
return bus_socket_take_fd(b);
}
@@ -745,7 +756,7 @@ int sd_bus_start(sd_bus *bus) {
if (bus->is_server && bus->bus_client)
return -EINVAL;
- if (bus->fd >= 0)
+ if (bus->input_fd >= 0)
r = bus_start_fd(bus);
else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
r = bus_start_address(bus);
@@ -848,11 +859,13 @@ fail:
void sd_bus_close(sd_bus *bus) {
if (!bus)
return;
- if (bus->fd < 0)
- return;
- close_nointr_nofail(bus->fd);
- bus->fd = -1;
+ if (bus->input_fd >= 0)
+ close_nointr_nofail(bus->input_fd);
+ if (bus->output_fd >= 0 && bus->output_fd != bus->input_fd)
+ close_nointr_nofail(bus->output_fd);
+
+ bus->input_fd = bus->output_fd = -1;
}
sd_bus *sd_bus_ref(sd_bus *bus) {
@@ -882,7 +895,7 @@ int sd_bus_is_open(sd_bus *bus) {
if (!bus)
return -EINVAL;
- return bus->state != BUS_UNSET && bus->fd >= 0;
+ return bus->state != BUS_UNSET && bus->input_fd >= 0;
}
int sd_bus_can_send(sd_bus *bus, char type) {
@@ -890,7 +903,7 @@ int sd_bus_can_send(sd_bus *bus, char type) {
if (!bus)
return -EINVAL;
- if (bus->fd < 0)
+ if (bus->output_fd < 0)
return -ENOTCONN;
if (type == SD_BUS_TYPE_UNIX_FD) {
@@ -941,7 +954,7 @@ static int dispatch_wqueue(sd_bus *bus) {
assert(bus);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
- if (bus->fd < 0)
+ if (bus->output_fd < 0)
return -ENOTCONN;
while (bus->wqueue_size > 0) {
@@ -984,7 +997,7 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
assert(m);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
if (bus->rqueue_size > 0) {
@@ -1020,7 +1033,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
return -EINVAL;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
- if (bus->fd < 0)
+ if (bus->output_fd < 0)
return -ENOTCONN;
if (!m)
return -EINVAL;
@@ -1129,7 +1142,7 @@ int sd_bus_send_with_reply(
return -EINVAL;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
- if (bus->fd < 0)
+ if (bus->output_fd < 0)
return -ENOTCONN;
if (!m)
return -EINVAL;
@@ -1211,7 +1224,7 @@ int bus_ensure_running(sd_bus *bus) {
assert(bus);
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
@@ -1248,7 +1261,7 @@ int sd_bus_send_with_reply_and_block(
if (!bus)
return -EINVAL;
- if (bus->fd < 0)
+ if (bus->output_fd < 0)
return -ENOTCONN;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
@@ -1358,11 +1371,12 @@ int sd_bus_send_with_reply_and_block(
int sd_bus_get_fd(sd_bus *bus) {
if (!bus)
return -EINVAL;
-
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
+ if (bus->input_fd != bus->output_fd)
+ return -EPERM;
- return bus->fd;
+ return bus->input_fd;
}
int sd_bus_get_events(sd_bus *bus) {
@@ -1372,7 +1386,7 @@ int sd_bus_get_events(sd_bus *bus) {
return -EINVAL;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
if (bus->state == BUS_OPENING)
@@ -1403,7 +1417,7 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
return -EINVAL;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
if (bus->state == BUS_AUTHENTICATING) {
@@ -1824,7 +1838,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
if (!bus)
return -EINVAL;
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
switch (bus->state) {
@@ -1859,14 +1873,14 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
}
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
- struct pollfd p;
- int r, e;
+ struct pollfd p[2];
+ int r, e, n;
struct timespec ts;
usec_t until, m;
assert(bus);
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
e = sd_bus_get_events(bus);
@@ -1882,19 +1896,28 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
if (r == 0)
m = (uint64_t) -1;
else {
- usec_t n;
- n = now(CLOCK_MONOTONIC);
- m = until > n ? until - n : 0;
+ usec_t nw;
+ nw = now(CLOCK_MONOTONIC);
+ m = until > nw ? until - nw : 0;
}
if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
m = timeout_usec;
zero(p);
- p.fd = bus->fd;
- p.events = e;
+ p[0].fd = bus->input_fd;
+
+ if (bus->output_fd == bus->input_fd) {
+ p[0].events = e;
+ n = 1;
+ } else {
+ p[0].events = e & POLLIN;
+ p[1].fd = bus->output_fd;
+ p[1].events = e & POLLOUT;
+ n = 2;
+ }
- r = ppoll(&p, 1, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
+ r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
if (r < 0)
return -errno;
@@ -1907,7 +1930,7 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
return -EINVAL;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
- if (bus->fd < 0)
+ if (bus->input_fd < 0)
return -ENOTCONN;
if (bus->rqueue_size > 0)
return 0;
@@ -1922,7 +1945,7 @@ int sd_bus_flush(sd_bus *bus) {
return -EINVAL;
if (bus->state == BUS_UNSET)
return -ENOTCONN;
- if (bus->fd < 0)
+ if (bus->output_fd < 0)
return -ENOTCONN;
r = bus_ensure_running(bus);
diff --git a/src/libsystemd-bus/test-bus-server.c b/src/libsystemd-bus/test-bus-server.c
index 2cb4157..a594ce3 100644
--- a/src/libsystemd-bus/test-bus-server.c
+++ b/src/libsystemd-bus/test-bus-server.c
@@ -53,7 +53,7 @@ static void *server(void *p) {
assert_se(sd_id128_randomize(&id) >= 0);
assert_se(sd_bus_new(&bus) >= 0);
- assert_se(sd_bus_set_fd(bus, c->fds[0]) >= 0);
+ assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
assert_se(sd_bus_set_server(bus, 1, id) >= 0);
assert_se(sd_bus_set_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
@@ -132,7 +132,7 @@ static int client(struct context *c) {
int r;
assert_se(sd_bus_new(&bus) >= 0);
- assert_se(sd_bus_set_fd(bus, c->fds[1]) >= 0);
+ assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
assert_se(sd_bus_set_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
assert_se(sd_bus_start(bus) >= 0);
More information about the systemd-commits
mailing list