Commit 2c93b4ef authored by Lennart Poettering's avatar Lennart Poettering

bus: implement support for FD passing

parent 3f95f83c
......@@ -114,6 +114,9 @@ struct sd_bus {
struct ucred ucred;
char label[NAME_MAX];
int *fds;
unsigned n_fds;
};
static inline void bus_unrefp(sd_bus **b) {
......@@ -137,6 +140,8 @@ static inline void bus_unrefp(sd_bus **b) {
* bytes */
#define BUS_ARRAY_MAX_SIZE 67108864
#define BUS_FDS_MAX 1024
bool object_path_is_valid(const char *p);
bool interface_name_is_valid(const char *p);
bool service_name_is_valid(const char *p);
......
This diff is collapsed.
......@@ -67,12 +67,14 @@ struct sd_bus_message {
pid_t tid;
bool sealed:1;
bool dont_send:1;
bool allow_fds:1;
bool uid_valid:1;
bool gid_valid:1;
bool free_header:1;
bool free_fields:1;
bool free_body:1;
bool dont_send:1;
bool free_fds:1;
struct bus_header *header;
void *fields;
......@@ -130,5 +132,13 @@ static inline void bus_message_unrefp(sd_bus_message **m) {
int bus_message_seal(sd_bus_message *m, uint64_t serial);
int bus_message_dump(sd_bus_message *m);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
int bus_message_from_malloc(void *buffer, size_t length, struct ucred *ucred, const char *label, sd_bus_message **ret);
int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
int bus_message_from_malloc(
void *buffer,
size_t length,
int *fds,
unsigned n_fds,
const struct ucred *ucred,
const char *label,
sd_bus_message **ret);
......@@ -53,6 +53,9 @@ static void bus_free(sd_bus *b) {
free(b->auth_uid);
free(b->address);
close_many(b->fds, b->n_fds);
free(b->fds);
for (i = 0; i < b->rqueue_size; i++)
sd_bus_message_unref(b->rqueue[i]);
free(b->rqueue);
......@@ -889,6 +892,18 @@ static int message_write(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);
......@@ -897,7 +912,6 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
j = 0;
iovec_advance(iov, &j, *idx);
zero(mh);
mh.msg_iov = iov;
mh.msg_iovlen = m->n_iovec;
......@@ -963,7 +977,7 @@ static int message_read_need(sd_bus *bus, size_t *need) {
static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
sd_bus_message *t;
void *b = NULL;
void *b;
int r;
assert(bus);
......@@ -976,11 +990,14 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
bus->rbuffer_size - size);
if (!b)
return -ENOMEM;
}
} else
b = NULL;
r = bus_message_from_malloc(bus->rbuffer, size,
bus->fds, bus->n_fds,
bus->ucred_valid ? &bus->ucred : NULL,
bus->label[0] ? bus->label : NULL, &t);
bus->label[0] ? bus->label : NULL,
&t);
if (r < 0) {
free(b);
return r;
......@@ -989,6 +1006,9 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
bus->rbuffer = b;
bus->rbuffer_size -= size;
bus->fds = NULL;
bus->n_fds = 0;
*m = t;
return 1;
}
......@@ -1002,7 +1022,8 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
void *b;
union {
struct cmsghdr cmsghdr;
uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) +
CMSG_SPACE(sizeof(struct ucred)) +
CMSG_SPACE(NAME_MAX)]; /*selinux label */
} control;
struct cmsghdr *cmsg;
......@@ -1039,11 +1060,24 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
return errno == EAGAIN ? 0 : -errno;
bus->rbuffer_size += k;
bus->ucred_valid = false;
bus->label[0] = 0;
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);
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))) {
......@@ -1157,6 +1191,8 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
return -ENOTCONN;
if (!m)
return -EINVAL;
if (m->n_fds > 0 && !bus->can_fds)
return -ENOTSUP;
/* If the serial number isn't kept, then we know that no reply
* is expected */
......
......@@ -28,7 +28,6 @@
#include "sd-bus-protocol.h"
/* TODO:
* - make unix fd passing work
* - implicitly add stub introspection calls
* - implement unix exec protocol
* - server side
......
......@@ -23,6 +23,7 @@
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "util.h"
......@@ -202,6 +203,31 @@ static int server(sd_bus *bus) {
}
sleep(1);
} else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "FileDescriptor")) {
int fd;
static const char x = 'X';
r = sd_bus_message_read(m, "h", &fd);
if (r < 0) {
log_error("Failed to get parameter: %s", strerror(-r));
goto fail;
}
if (write(fd, &x, 1) < 0) {
log_error("Failed to write to fd: %m");
close_nointr_nofail(fd);
goto fail;
}
close_nointr_nofail(fd);
r = sd_bus_message_new_method_return(bus, m, &reply);
if (r < 0) {
log_error("Failed to allocate return: %s", strerror(-r));
goto fail;
}
} else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownMethod", "Unknown method.");
......@@ -242,6 +268,8 @@ static void* client1(void*p) {
sd_bus_error error = SD_BUS_ERROR_INIT;
const char *hello;
int r;
int pp[2] = { -1, -1 };
char x;
r = sd_bus_open_user(&bus);
if (r < 0) {
......@@ -281,6 +309,46 @@ static void* client1(void*p) {
assert(streq(hello, "hello"));
if (pipe2(pp, O_CLOEXEC|O_NONBLOCK) < 0) {
log_error("Failed to allocate pipe: %m");
r = -errno;
goto finish;
}
sd_bus_message_unref(m);
m = NULL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.systemd.test",
"/",
"org.freedesktop.systemd.test",
"FileDescriptor",
&m);
if (r < 0) {
log_error("Failed to allocate method call: %s", strerror(-r));
goto finish;
}
r = sd_bus_message_append(m, "h", pp[1]);
if (r < 0) {
log_error("Failed to append string: %s", strerror(-r));
goto finish;
}
sd_bus_message_unref(reply);
reply = NULL;
r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, &reply);
if (r < 0) {
log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
goto finish;
}
errno = 0;
if (read(pp[0], &x, 1) <= 0) {
log_error("Failed to read from pipe: %s", errno ? strerror(errno) : "early read");
goto finish;
}
r = 0;
finish:
......@@ -305,6 +373,9 @@ finish:
}
sd_bus_error_free(&error);
close_pipe(pp);
return INT_TO_PTR(r);
}
......
......@@ -121,7 +121,7 @@ int main(int argc, char *argv[]) {
m = sd_bus_message_unref(m);
r = bus_message_from_malloc(buffer, sz, NULL, NULL, &m);
r = bus_message_from_malloc(buffer, sz, NULL, 0, NULL, NULL, &m);
assert_se(r >= 0);
bus_message_dump(m);
......
......@@ -219,6 +219,8 @@ void close_nointr_nofail(int fd) {
void close_many(const int fds[], unsigned n_fd) {
unsigned i;
assert(fds || n_fd <= 0);
for (i = 0; i < n_fd; i++)
close_nointr_nofail(fds[i]);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment