chiark / gitweb /
bus: hook up client with socket communication
authorLennart Poettering <lennart@poettering.net>
Wed, 20 Mar 2013 21:56:25 +0000 (22:56 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 Mar 2013 22:00:10 +0000 (23:00 +0100)
.gitignore
Makefile.am
src/libsystemd-bus/bus-control.c
src/libsystemd-bus/bus-error.c
src/libsystemd-bus/bus-internal.h
src/libsystemd-bus/bus-message.c
src/libsystemd-bus/bus-message.h
src/libsystemd-bus/busctl.c
src/libsystemd-bus/sd-bus.c
src/libsystemd-bus/sd-bus.h
src/libsystemd-bus/test-bus-chat.c [new file with mode: 0644]

index 917b0663bf31143461c0fd5bc3cb123bf66ea09c..2d95f149c0c37dd405346a63d128ce1e0da46fa0 100644 (file)
@@ -4,6 +4,7 @@
 /ata_id
 /bootctl
 /build-aux
+/busctl
 /cdrom_id
 /collect
 /gtk-doc.make
@@ -82,6 +83,7 @@
 /tags
 /test-bus-marshal
 /test-bus-signature
+/test-bus-chat
 /test-calendarspec
 /test-catalog
 /test-cgroup
index c94c192ce314bca48f7b76193ba4ad2a4d9f233e..d8e9b16be071d57321e977e850647fb4f8259ad3 100644 (file)
@@ -1654,12 +1654,19 @@ libsystemd_bus_la_SOURCES = \
        src/libsystemd-bus/bus-type.c \
        src/libsystemd-bus/bus-type.h
 
+libsystemd_bus_la_LIBADD =  \
+       libsystemd-id128-internal.la
+
 noinst_LTLIBRARIES += \
         libsystemd-bus.la
 
 noinst_tests += \
        test-bus-marshal \
-       test-bus-signature
+       test-bus-signature \
+       test-bus-chat
+
+noinst_PROGRAMS += \
+       busctl
 
 test_bus_marshal_SOURCES = \
        src/libsystemd-bus/test-bus-marshal.c
@@ -1682,6 +1689,25 @@ test_bus_signature_LDADD = \
        libsystemd-shared.la \
        libsystemd-bus.la
 
+test_bus_chat_SOURCES = \
+       src/libsystemd-bus/test-bus-chat.c
+
+test_bus_chat_CFLAGS = \
+       $(AM_CFLAGS) \
+       -pthread
+
+test_bus_chat_LDADD = \
+       libsystemd-shared.la \
+       libsystemd-bus.la \
+       libsystemd-id128-internal.la
+
+busctl_SOURCES = \
+       src/libsystemd-bus/busctl.c
+
+busctl_LDADD = \
+       libsystemd-shared.la \
+       libsystemd-bus.la
+
 # ------------------------------------------------------------------------------
 if ENABLE_GTK_DOC
 SUBDIRS += \
index e4cc251a4b2937046fc159d6a6f7c089e4be3c54..50e1a2c00a64aa7940f7f0e264c30674a478b579 100644 (file)
@@ -107,7 +107,6 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
 
 int sd_bus_list_names(sd_bus *bus, char ***l) {
         _cleanup_bus_message_unref_ sd_bus_message *m1 = NULL, *reply1 = NULL, *m2 = NULL, *reply2 = NULL;
-        _cleanup_strv_free_ char **a = NULL, **b = NULL;
         char **x = NULL;
         int r;
 
@@ -144,17 +143,17 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_read(reply1, "as", &a);
-        if (r < 0)
+        r = bus_message_read_strv_extend(reply1, &x);
+        if (r < 0) {
+                strv_free(x);
                 return r;
+        }
 
-        r = sd_bus_message_read(reply2, "as", &b);
-        if (r < 0)
+        r = bus_message_read_strv_extend(reply2, &x);
+        if (r < 0) {
+                strv_free(x);
                 return r;
-
-        x = strv_merge(a, b);
-        if (!x)
-                return -ENOMEM;
+        }
 
         *l = strv_uniq(x);
         return 0;
@@ -162,6 +161,7 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
 
 int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        const char *found;
         int r;
 
         if (!bus)
@@ -187,7 +187,21 @@ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
         if (r < 0)
                 return r;
 
-        return sd_bus_message_read(reply, "s", owner);
+        r = sd_bus_message_read(reply, "s", &found);
+        if (r < 0)
+                return r;
+
+        if (owner) {
+                char *t;
+
+                t = strdup(found);
+                if (!t)
+                        return -ENOMEM;
+
+                *owner = t;
+        }
+
+        return 0;
 }
 
 int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
@@ -245,7 +259,7 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
                         "org.freedesktop.DBus",
                         "/",
                         "org.freedesktop.DBus",
-                        "GetConnectionUnixUser",
+                        "GetConnectionUnixProcessID",
                         &m);
         if (r < 0)
                 return r;
index 015fae70adebb8212582463ad3a0bdb5d03ce540..f86da624fddeb89a7ef0509309ac9f35761d80a2 100644 (file)
@@ -86,6 +86,8 @@ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...)
 }
 
 int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
+        char *x, *y = NULL;
+
         if (!dest)
                 return 0;
         if (bus_error_is_dirty(dest))
@@ -93,27 +95,21 @@ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
         if (!sd_bus_error_is_set(e))
                 return 0;
 
-        if (e->need_free) {
-                char *x, *y = NULL;
+        x = strdup(e->name);
+        if (!x)
+                return -ENOMEM;
 
-                x = strdup(e->name);
-                if (!x)
+        if (e->message) {
+                y = strdup(e->message);
+                if (!y) {
+                        free(x);
                         return -ENOMEM;
-
-                if (e->message) {
-                        y = strdup(e->message);
-                        if (!y) {
-                                free(x);
-                                return -ENOMEM;
-                        }
                 }
+        }
 
-                dest->name = x;
-                dest->message = y;
-                dest->need_free = true;
-        } else
-                *dest = *e;
-
+        dest->name = x;
+        dest->message = y;
+        dest->need_free = true;
         return 0;
 }
 
index d8cdc48ce0441972ae4979c102cf6e2fefb890e0..9a12d661e17ec277afdc2f83e7e828985f86c0ac 100644 (file)
@@ -50,8 +50,7 @@ enum bus_state {
         BUS_OPENING,
         BUS_AUTHENTICATING,
         BUS_HELLO,
-        BUS_RUNNING,
-        BUS_CLOSED
+        BUS_RUNNING
 };
 
 struct sd_bus {
@@ -60,7 +59,7 @@ struct sd_bus {
         int fd;
         int message_version;
         bool can_fds:1;
-        bool send_hello:1;
+        bool sent_hello:1;
 
         void *rbuffer;
         size_t rbuffer_size;
@@ -99,3 +98,9 @@ struct sd_bus {
         size_t auth_size;
         char *auth_uid;
 };
+
+static inline void bus_unrefp(sd_bus **b) {
+        sd_bus_unref(*b);
+}
+
+#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
index 5f3dbdb75de8a55d2a41076b045b7de9af0b9507..c385ef5ed05b1e39387e6e447677a827c6b2a25c 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "util.h"
 #include "utf8.h"
+#include "strv.h"
 
 #include "sd-bus.h"
 #include "bus-message.h"
@@ -322,6 +323,8 @@ int sd_bus_message_new_signal(
         if (!t)
                 return -ENOMEM;
 
+        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
+
         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
         if (r < 0)
                 goto fail;
@@ -409,6 +412,7 @@ static int message_new_reply(
         if (!t)
                 return -ENOMEM;
 
+        t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
         t->reply_serial = BUS_MESSAGE_SERIAL(call);
 
         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
@@ -424,6 +428,7 @@ static int message_new_reply(
         t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
 
         *m = t;
+        return 0;
 
 fail:
         message_free(t);
@@ -1891,7 +1896,7 @@ eof:
         return 0;
 }
 
-int sd_bus_message_rewind(sd_bus_message *m, bool complete) {
+int sd_bus_message_rewind(sd_bus_message *m, int complete) {
         struct bus_container *c;
 
         if (!m)
@@ -2424,6 +2429,10 @@ static int message_parse_fields(sd_bus_message *m) {
                 break;
         }
 
+        /* Try to read the error message, but if we can't it's a non-issue */
+        if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+                sd_bus_message_read(m, "s", &m->error.message);
+
         return 0;
 }
 
@@ -2724,3 +2733,34 @@ int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
 
         return 0;
 }
+
+int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
+        int r;
+
+        assert(m);
+        assert(l);
+
+        r = sd_bus_message_enter_container(m, 'a', "s");
+        if (r < 0)
+                return r;
+
+        for (;;) {
+                const char *s;
+
+                r = sd_bus_message_read_basic(m, 's', &s);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                r = strv_extend(l, s);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_bus_message_exit_container(m);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
index 3035efc8c20be4a031a05457aa4d03ea4e3ddd5e..0184d18dd9a0afca1195073c0e94adeafdb57411 100644 (file)
@@ -123,8 +123,8 @@ static inline void bus_message_unrefp(sd_bus_message **m) {
 
 #define _cleanup_bus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
 
-int bus_message_parse(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, sd_bus_message **ret);
+int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
index 88446fcc7c0b10631873c3513efcae26fb7524d3..7348894c5b6f05735a7ecc6da7f52dda478ffb29 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "strv.h"
+#include "util.h"
+#include "log.h"
+
 #include "sd-bus.h"
+#include "bus-message.h"
+#include "bus-internal.h"
 
 int main(int argc, char *argv[]) {
         _cleanup_bus_unref_ sd_bus *bus = NULL;
         _cleanup_strv_free_ char **l = NULL;
         char **i;
         int r;
+        size_t max_i = 0;
 
-        r = bus_open_system(&bus);
+        r = sd_bus_open_user(&bus);
         if (r < 0) {
                 log_error("Failed to connect to bus: %s", strerror(-r));
                 goto fail;
@@ -39,26 +46,57 @@ int main(int argc, char *argv[]) {
                 goto fail;
         }
 
+        strv_sort(l);
+
+        STRV_FOREACH(i, l)
+                max_i = MAX(max_i, strlen(*i));
+
+        printf("%-*s %*s %-*s %-*s CONNECTION\n",
+               (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
+
         STRV_FOREACH(i, l) {
                 _cleanup_free_ char *owner = NULL;
-                pid_t pid = 0;
+                pid_t pid;
                 uid_t uid;
-                bool uid_valid;
 
-                r = sd_bus_get_owner(bus, *i, &owner);
-                if (r == -ENXIO)
-                        continue;
+                /* if ((*i)[0] == ':') */
+                /*         continue; */
+
+                printf("%-*s", (int) max_i, *i);
 
-                r = sd_get_owner_pid(bus, *i, &pid);
-                if (r == -ENXIO)
-                        continue;
+                r = sd_bus_get_owner_pid(bus, *i, &pid);
+                if (r >= 0) {
+                        _cleanup_free_ char *comm = NULL;
 
-                r = sd_get_owner_uid(bus, *i, &pid);
-                if (r == -ENXIO)
-                        continue;
-                uid_valid = r >= 0;
+                        printf(" %10lu", (unsigned long) pid);
 
-                printf("%s (%s) %llu %llu\n", *i, owner, (unsigned long long) pid, (unsigned long long) uid);
+                        get_process_comm(pid, &comm);
+                        printf(" %-15s", strna(comm));
+                } else
+                        printf("          - -              ");
+
+                r = sd_bus_get_owner_uid(bus, *i, &uid);
+                if (r >= 0) {
+                        _cleanup_free_ char *u = NULL;
+
+                        u = uid_to_name(uid);
+                        if (!u) {
+                                log_oom();
+                                goto fail;
+                        }
+
+                        if (strlen(u) > 16)
+                                u[16] = 0;
+
+                        printf(" %-16s", u);
+                } else
+                        printf(" -               ");
+
+                r = sd_bus_get_owner(bus, *i, &owner);
+                if (r >= 0)
+                        printf(" %s\n", owner);
+                else
+                        printf(" -\n");
         }
 
         r = 0;
index c5cbb5682d8cc370dac7f55d08b56c7ba1fc68fd..f27d47cdea06bd808b7faa57a40204e5f951abf4 100644 (file)
@@ -39,6 +39,7 @@
 
 static void bus_free(sd_bus *b) {
         struct filter_callback *f;
+        unsigned i;
 
         assert(b);
 
@@ -46,9 +47,17 @@ static void bus_free(sd_bus *b) {
                 close_nointr_nofail(b->fd);
 
         free(b->rbuffer);
+        free(b->unique_name);
+        free(b->auth_uid);
+        free(b->address);
+
+        for (i = 0; i < b->rqueue_size; i++)
+                sd_bus_message_unref(b->rqueue[i]);
         free(b->rqueue);
+
+        for (i = 0; i < b->wqueue_size; i++)
+                sd_bus_message_unref(b->wqueue[i]);
         free(b->wqueue);
-        free(b->unique_name);
 
         hashmap_free_free(b->reply_callbacks);
 
@@ -122,24 +131,19 @@ static int bus_send_hello(sd_bus *bus) {
         if (r < 0)
                 return r;
 
-        return 0;
+        bus->sent_hello = true;
+        return r;
 }
 
 static int bus_start_running(sd_bus *bus) {
-        int r;
-
         assert(bus);
 
-        if (bus->send_hello) {
+        if (bus->sent_hello) {
                 bus->state = BUS_HELLO;
-
-                r = bus_send_hello(bus);
-                if (r < 0)
-                        return r;
+                return 0;
         }
 
         bus->state = BUS_RUNNING;
-
         return 0;
 }
 
@@ -154,7 +158,7 @@ static int parse_address_key(const char **p, const char *key, char **value) {
         assert(value);
 
         l = strlen(key);
-        if (!strncmp(*p, key, l) != 0)
+        if (strncmp(*p, key, l) != 0)
                 return 0;
 
         if ((*p)[l] != '=')
@@ -164,7 +168,7 @@ static int parse_address_key(const char **p, const char *key, char **value) {
                 return -EINVAL;
 
         a = *p + l + 1;
-        while (*a != ';' && *a != 0) {
+        while (*a != ',' && *a != 0) {
                 char c, *t;
 
                 if (*a == '%') {
@@ -182,12 +186,14 @@ static int parse_address_key(const char **p, const char *key, char **value) {
                                 return y;
                         }
 
-                        a += 3;
                         c = (char) ((x << 4) | y);
-                } else
+                        a += 3;
+                } else {
                         c = *a;
+                        a++;
+                }
 
-                t = realloc(r, n + 1);
+                t = realloc(r, n + 2);
                 if (!t) {
                         free(r);
                         return -ENOMEM;
@@ -197,6 +203,16 @@ static int parse_address_key(const char **p, const char *key, char **value) {
                 r[n++] = c;
         }
 
+        if (!r) {
+                r = strdup("");
+                if (!r)
+                        return -ENOMEM;
+        } else
+                r[n] = 0;
+
+        if (*a == ',')
+                a++;
+
         *p = a;
         *value = r;
         return 1;
@@ -206,7 +222,10 @@ static void skip_address_key(const char **p) {
         assert(p);
         assert(*p);
 
-        *p += strcspn(*p, ";");
+        *p += strcspn(*p, ",");
+
+        if (**p == ',')
+                (*p) ++;
 }
 
 static int bus_parse_next_address(sd_bus *b) {
@@ -231,7 +250,7 @@ static int bus_parse_next_address(sd_bus *b) {
                 _cleanup_free_ char *path = NULL, *abstract = NULL;
 
                 p = a + 5;
-                while (*p != 0 && *p != ';') {
+                while (*p != 0) {
                         r = parse_address_key(&p, "guid", &guid);
                         if (r < 0)
                                 return r;
@@ -272,13 +291,13 @@ static int bus_parse_next_address(sd_bus *b) {
                 } else if (abstract) {
                         size_t l;
 
-                        l = strlen(path);
+                        l = strlen(abstract);
                         if (l > sizeof(b->sockaddr.un.sun_path) - 1)
                                 return -E2BIG;
 
                         b->sockaddr.un.sun_family = AF_UNIX;
                         b->sockaddr.un.sun_path[0] = 0;
-                        strncpy(b->sockaddr.un.sun_path+1, path, sizeof(b->sockaddr.un.sun_path)-1);
+                        strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
                         b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
                 }
 
@@ -287,7 +306,7 @@ static int bus_parse_next_address(sd_bus *b) {
                 struct addrinfo hints, *result;
 
                 p = a + 4;
-                while (*p != 0 && *p != ';') {
+                while (*p != 0) {
                         r = parse_address_key(&p, "guid", &guid);
                         if (r < 0)
                                 return r;
@@ -409,7 +428,7 @@ static int bus_auth_verify(sd_bus *b) {
         if (!e)
                 return 0;
 
-        f = memmem(e, b->rbuffer_size - (e - (char*) b->rbuffer), "\r\n", 2);
+        f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
         if (!f)
                 return 0;
 
@@ -423,7 +442,7 @@ static int bus_auth_verify(sd_bus *b) {
                 int x, y;
 
                 x = unhexchar(((char*) b->rbuffer)[3 + i]);
-                y = unhexchar(((char*) b->rbuffer)[3 + i + 2]);
+                y = unhexchar(((char*) b->rbuffer)[3 + i + 1]);
 
                 if (x < 0 || y < 0)
                         return -EINVAL;
@@ -446,6 +465,8 @@ static int bus_auth_verify(sd_bus *b) {
                 memmove(b->rbuffer, f + 2, b->rbuffer_size);
         }
 
+        b->rbuffer_size = 0;
+
         r = bus_start_running(b);
         if (r < 0)
                 return r;
@@ -459,6 +480,7 @@ static int bus_read_auth(sd_bus *b) {
         size_t n;
         ssize_t k;
         int r;
+        void *p;
 
         assert(b);
 
@@ -467,6 +489,11 @@ static int bus_read_auth(sd_bus *b) {
                 return r;
 
         n = MAX(3 + 32 + 2 + sizeof("AGREE_UNIX_FD") - 1 + 2, b->rbuffer_size * 2);
+        p = realloc(b->rbuffer, n);
+        if (!p)
+                return -ENOMEM;
+
+        b->rbuffer = p;
 
         zero(iov);
         iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size;
@@ -490,7 +517,7 @@ static int bus_read_auth(sd_bus *b) {
 }
 
 static int bus_start_auth(sd_bus *b) {
-        static const char auth_prefix[] = "\0AUTH_EXTERNAL ";
+        static const char auth_prefix[] = "\0AUTH EXTERNAL ";
         static const char auth_suffix[] = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
 
         char text[20 + 1]; /* enough space for a 64bit integer plus NUL */
@@ -570,25 +597,25 @@ int sd_bus_open_system(sd_bus **ret) {
                 r = sd_bus_open_address(e, &b);
                 if (r < 0)
                         return r;
+        } else {
+                b = bus_new();
+                if (!b)
+                        return -ENOMEM;
 
-                b->send_hello = true;
-                *ret = b;
-                return r;
-        }
-
-        b = bus_new();
-        if (!b)
-                return -ENOMEM;
-
-        b->send_hello = true;
+                b->sockaddr.un.sun_family = AF_UNIX;
+                strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
+                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
 
-        b->sockaddr.un.sun_family = AF_UNIX;
-        strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
-        b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
+                r = bus_start_connect(b);
+                if (r < 0) {
+                        bus_free(b);
+                        return r;
+                }
+        }
 
-        r = bus_start_connect(b);
+        r = bus_send_hello(b);
         if (r < 0) {
-                bus_free(b);
+                sd_bus_unref(b);
                 return r;
         }
 
@@ -610,33 +637,33 @@ int sd_bus_open_user(sd_bus **ret) {
                 r = sd_bus_open_address(e, &b);
                 if (r < 0)
                         return r;
+        } else {
+                e = getenv("XDG_RUNTIME_DIR");
+                if (!e)
+                        return -ENOENT;
 
-                b->send_hello = true;
-                *ret = b;
-                return r;
-        }
-
-        e = getenv("XDG_RUNTIME_DIR");
-        if (!e)
-                return -ENOENT;
-
-        l = strlen(e);
-        if (l + 4 > sizeof(b->sockaddr.un.sun_path))
-                return -E2BIG;
+                l = strlen(e);
+                if (l + 4 > sizeof(b->sockaddr.un.sun_path))
+                        return -E2BIG;
 
-        b = bus_new();
-        if (!b)
-                return -ENOMEM;
+                b = bus_new();
+                if (!b)
+                        return -ENOMEM;
 
-        b->send_hello = true;
+                b->sockaddr.un.sun_family = AF_UNIX;
+                memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
+                b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
 
-        b->sockaddr.un.sun_family = AF_UNIX;
-        memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
-        b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
+                r = bus_start_connect(b);
+                if (r < 0) {
+                        bus_free(b);
+                        return r;
+                }
+        }
 
-        r = bus_start_connect(b);
+        r = bus_send_hello(b);
         if (r < 0) {
-                bus_free(b);
+                sd_bus_unref(b);
                 return r;
         }
 
@@ -747,6 +774,8 @@ int sd_bus_can_send(sd_bus *bus, char type) {
 
         if (!bus)
                 return -EINVAL;
+        if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO)
+                return -EAGAIN;
 
         if (type == SD_BUS_TYPE_UNIX_FD)
                 return bus->can_fds;
@@ -757,6 +786,9 @@ int sd_bus_can_send(sd_bus *bus, char type) {
 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
         assert(m);
 
+        if (m->header->version > b->message_version)
+                return -EPERM;
+
         if (m->sealed)
                 return 0;
 
@@ -773,6 +805,7 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
         assert(bus);
         assert(m);
         assert(idx);
+        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
         n = m->n_iovec * sizeof(struct iovec);
         iov = alloca(n);
@@ -792,7 +825,7 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
         *idx += (size_t) k;
         iovec_advance(iov, &j, *idx);
 
-        return j > m->n_iovec;
+        return j >= m->n_iovec;
 }
 
 static int message_read_need(sd_bus *bus, size_t *need) {
@@ -801,8 +834,9 @@ static int message_read_need(sd_bus *bus, size_t *need) {
 
         assert(bus);
         assert(need);
+        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
-        if (bus->rbuffer_size <= sizeof(struct bus_header)) {
+        if (bus->rbuffer_size < sizeof(struct bus_header)) {
                 *need = sizeof(struct bus_header);
                 return 0;
         }
@@ -818,9 +852,9 @@ static int message_read_need(sd_bus *bus, size_t *need) {
                 a = be32toh(a);
                 b = be32toh(b);
         } else
-                return -EIO;
+                return -EBADMSG;
 
-        *need = sizeof(struct bus_header) + ALIGN_TO(a, 8) + b;
+        *need = sizeof(struct bus_header) + ALIGN_TO(b, 8) + a;
         return 0;
 }
 
@@ -832,6 +866,7 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
         assert(bus);
         assert(m);
         assert(bus->rbuffer_size >= size);
+        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
         if (bus->rbuffer_size > size) {
                 b = memdup((const uint8_t*) bus->rbuffer + size, bus->rbuffer_size - size);
@@ -850,12 +885,6 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
         bus->rbuffer = b;
         bus->rbuffer_size -= size;
 
-        r = bus_message_parse(t);
-        if (r < 0) {
-                sd_bus_message_unref(t);
-                return r;
-        }
-
         *m = t;
         return 1;
 }
@@ -870,6 +899,7 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
 
         assert(bus);
         assert(m);
+        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
         r = message_read_need(bus, &need);
         if (r < 0)
@@ -882,6 +912,8 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
         if (!b)
                 return -ENOMEM;
 
+        bus->rbuffer = b;
+
         zero(iov);
         iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size;
         iov.iov_len = need - bus->rbuffer_size;
@@ -910,6 +942,7 @@ static int dispatch_wqueue(sd_bus *bus) {
         int r, c = 0;
 
         assert(bus);
+        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
         if (bus->fd < 0)
                 return -ENOTCONN;
@@ -951,6 +984,7 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
 
         assert(bus);
         assert(m);
+        assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
         if (bus->fd < 0)
                 return -ENOTCONN;
@@ -983,8 +1017,6 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
-        if (m->header->version > bus->message_version)
-                return -EPERM;
 
         r = bus_seal_message(bus, m);
         if (r < 0)
@@ -995,7 +1027,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         if (m->dont_send && !serial)
                 return 0;
 
-        if (bus->wqueue_size <= 0) {
+        if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
                 size_t idx = 0;
 
                 r = message_write(bus, m, &idx);
@@ -1057,14 +1089,20 @@ int sd_bus_send_with_reply(
 
         if (!bus)
                 return -EINVAL;
-        if (!bus->fd < 0)
+        if (bus->fd < 0)
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
-        if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                 return -EINVAL;
+        if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return -EINVAL;
+
+        r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
+        if (r < 0)
+                return r;
 
         r = bus_seal_message(bus, m);
         if (r < 0)
@@ -1111,6 +1149,30 @@ int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
         return 1;
 }
 
+static int ensure_running(sd_bus *bus) {
+        int r;
+
+        assert(bus);
+
+        r = sd_bus_is_running(bus);
+        if (r != 0)
+                return r;
+
+        for (;;) {
+                r = sd_bus_process(bus, NULL);
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_is_running(bus);
+                if (r != 0)
+                        return r;
+
+                r = sd_bus_wait(bus, (uint64_t) -1);
+                if (r < 0)
+                        return r;
+        }
+}
+
 int sd_bus_send_with_reply_and_block(
                 sd_bus *bus,
                 sd_bus_message *m,
@@ -1125,14 +1187,20 @@ int sd_bus_send_with_reply_and_block(
 
         if (!bus)
                 return -EINVAL;
-        if (!bus->fd < 0)
+        if (bus->fd < 0)
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
-        if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                 return -EINVAL;
-        if (sd_bus_error_is_dirty(error))
+        if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return -EINVAL;
+        if (bus_error_is_dirty(error))
+                return -EINVAL;
+
+        r = ensure_running(bus);
+        if (r < 0)
+                return r;
 
         r = sd_bus_send(bus, m, &serial);
         if (r < 0)
@@ -1162,6 +1230,9 @@ int sd_bus_send_with_reply_and_block(
                 if (r < 0)
                         return r;
                 if (r > 0) {
+                        /* bus_message_dump(incoming); */
+                        /* sd_bus_message_rewind(incoming, true); */
+
                         if (incoming->reply_serial == serial) {
                                 /* Found a match! */
 
@@ -1223,7 +1294,7 @@ int sd_bus_get_fd(sd_bus *bus) {
                 return -EINVAL;
 
         if (bus->fd < 0)
-                return -EINVAL;
+                return -ENOTCONN;
 
         return bus->fd;
 }
@@ -1233,13 +1304,19 @@ int sd_bus_get_events(sd_bus *bus) {
 
         if (!bus)
                 return -EINVAL;
-
         if (bus->fd < 0)
-                return -EINVAL;
+                return -ENOTCONN;
 
         if (bus->state == BUS_OPENING)
                 flags |= POLLOUT;
-        else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
+        else if (bus->state == BUS_AUTHENTICATING) {
+
+                if (bus->auth_index < ELEMENTSOF(bus->auth_iovec))
+                        flags |= POLLOUT;
+
+                flags |= POLLIN;
+
+        } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
                 if (bus->rqueue_size <= 0)
                         flags |= POLLIN;
                 if (bus->wqueue_size > 0)
@@ -1250,7 +1327,6 @@ int sd_bus_get_events(sd_bus *bus) {
 }
 
 int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
-        sd_bus_message *m;
         int r;
 
         if (!bus)
@@ -1270,7 +1346,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
                         return -errno;
 
                 if (p.revents & (POLLOUT|POLLERR|POLLHUP)) {
-                        int error;
+                        int error = 0;
                         socklen_t slen = sizeof(error);
 
                         r = getsockopt(bus->fd, SOL_SOCKET, SO_ERROR, &error, &slen);
@@ -1297,13 +1373,14 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
                         return r;
 
                 r = bus_read_auth(bus);
-                if (r <= 0)
+                if (r < 0)
                         return r;
 
-                return bus_start_running(bus);
+                return 0;
 
         } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
                 struct filter_callback *l;
+                _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
 
                 r = dispatch_wqueue(bus);
                 if (r < 0)
@@ -1321,31 +1398,40 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
                                 r = c->callback(bus, m, c->userdata);
                                 free(c);
 
-                                if (r != 0) {
-                                        sd_bus_message_unref(m);
+                                if (r != 0)
                                         return r < 0 ? r : 0;
-                                }
                         }
                 }
 
                 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
                         r = l->callback(bus, m, l->userdata);
-                        if (r != 0) {
-                                sd_bus_message_unref(m);
+                        if (r != 0)
                                 return r < 0 ? r : 0;
-                        }
                 }
 
                 if (ret) {
                         *ret = m;
+                        m = NULL;
                         return 1;
                 }
 
-                sd_bus_message_unref(m);
+                if (sd_bus_message_is_method_call(m, NULL, NULL)) {
+                        const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownObject", "Unknown object.");
+                        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+
+                        r = sd_bus_message_new_method_error(bus, m, &e, &reply);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_bus_send(bus, reply, NULL);
+                        if (r < 0)
+                                return r;
+                }
+
                 return 0;
         }
 
-        return -ENOTSUP;
+        assert_not_reached("Unknown state");
 }
 
 int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
@@ -1356,7 +1442,10 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
         if (!bus)
                 return -EINVAL;
         if (bus->fd < 0)
-                return -ECONNREFUSED;
+                return -ENOTCONN;
+
+        if (bus->rqueue_size > 0)
+                return 0;
 
         e = sd_bus_get_events(bus);
         if (e < 0)
@@ -1368,7 +1457,7 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
 
         r = ppoll(&p, 1, timeout_usec == (uint64_t) -1 ? NULL : timespec_store(&ts, timeout_usec), NULL);
         if (r < 0)
-                return -EINVAL;
+                return -errno;
 
         return r;
 }
@@ -1381,7 +1470,11 @@ int sd_bus_flush(sd_bus *bus) {
         if (bus->fd < 0)
                 return -ENOTCONN;
 
-        if (bus->state == BUS_RUNNING && bus->wqueue_size <= 0)
+        r = ensure_running(bus);
+        if (r < 0)
+                return r;
+
+        if (bus->wqueue_size <= 0)
                 return 0;
 
         for (;;) {
@@ -1389,7 +1482,7 @@ int sd_bus_flush(sd_bus *bus) {
                 if (r < 0)
                         return r;
 
-                if (bus->state == BUS_RUNNING && bus->wqueue_size <= 0)
+                if (bus->wqueue_size <= 0)
                         return 0;
 
                 r = sd_bus_wait(bus, (uint64_t) -1);
index d892562b5f6362cd1db04fe809b8afbc207e7b21..03ea4b88483ee2b67b1249a7108cb9c0a7f57909 100644 (file)
  * - make unix fd passing work
  * - add page donation logic
  * - api for appending/reading fixed arrays
+ * - always verify container depth
+ * - implement method timeout logic
+ * - implicitly set no_reply when a message-call is sent an the serial number ignored
+ * - reduce number of ppoll()s if we can avoid it
+ * - handle NULL strings nicer when appending
+ * - merge busctl into systemctl or so?
  */
 
 typedef struct sd_bus sd_bus;
@@ -117,7 +123,7 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p);
 int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents);
 int sd_bus_message_exit_container(sd_bus_message *m);
 int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents);
-int sd_bus_message_rewind(sd_bus_message *m, bool complete);
+int sd_bus_message_rewind(sd_bus_message *m, int complete);
 
 /* Bus management */
 
@@ -133,7 +139,8 @@ int sd_bus_remove_match(sd_bus *bus, const char *match);
 
 /* Error structures */
 
-#define SD_BUS_ERROR_INIT (NULL, NULL, 0)
+#define SD_BUS_ERROR_INIT {NULL, NULL, 0}
+#define SD_BUS_ERROR_INIT_CONST(name, message) {(name), (message), 0}
 
 void sd_bus_error_free(sd_bus_error *e);
 int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...);
diff --git a/src/libsystemd-bus/test-bus-chat.c b/src/libsystemd-bus/test-bus-chat.c
new file mode 100644 (file)
index 0000000..4260ca3
--- /dev/null
@@ -0,0 +1,242 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <assert.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "log.h"
+#include "util.h"
+
+#include "sd-bus.h"
+#include "bus-message.h"
+
+static int server_init(sd_bus **_bus) {
+        sd_bus *bus = NULL;
+        int r;
+
+        assert(_bus);
+
+        r = sd_bus_open_user(&bus);
+        if (r < 0) {
+                log_error("Failed to connect to user bus: %s", strerror(-r));
+                goto fail;
+        }
+
+        r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0);
+        if (r < 0) {
+                log_error("Failed to acquire name: %s", strerror(-r));
+                goto fail;
+        }
+
+        *_bus = bus;
+        return 0;
+
+fail:
+        if (bus)
+                sd_bus_unref(bus);
+
+        return r;
+}
+
+static void* server(void *p) {
+        sd_bus *bus = p;
+        int r;
+
+        for (;;) {
+                _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+
+                r = sd_bus_process(bus, &m);
+                if (r < 0) {
+                        log_error("Failed to process requests: %s", strerror(-r));
+                        goto fail;
+                }
+                if (r == 0) {
+                        r = sd_bus_wait(bus, (uint64_t) -1);
+                        if (r < 0) {
+                                log_error("Failed to wait: %s", strerror(-r));
+                                goto fail;
+                        }
+
+                        continue;
+                }
+
+                log_info("Got message! %s", strna(sd_bus_message_get_member(m)));
+                /* bus_message_dump(m); */
+                /* sd_bus_message_rewind(m, true); */
+
+                if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) {
+                        const char *hello;
+                        _cleanup_free_ char *lowercase = NULL;
+
+                        r = sd_bus_message_read(m, "s", &hello);
+                        if (r < 0) {
+                                log_error("Failed to get parameter: %s", strerror(-r));
+                                goto fail;
+                        }
+
+                        r = sd_bus_message_new_method_return(bus, m, &reply);
+                        if (r < 0) {
+                                log_error("Failed to allocate return: %s", strerror(-r));
+                                goto fail;
+                        }
+
+                        lowercase = strdup(hello);
+                        if (!lowercase) {
+                                r = log_oom();
+                                goto fail;
+                        }
+
+                        ascii_strlower(lowercase);
+
+                        r = sd_bus_message_append(reply, "s", lowercase);
+                        if (r < 0) {
+                                log_error("Failed to append message: %s", strerror(-r));
+                                goto fail;
+                        }
+                } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit"))
+                        break;
+                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.");
+
+                        r = sd_bus_message_new_method_error(bus, m, &e, &reply);
+                        if (r < 0) {
+                                log_error("Failed to allocate return: %s", strerror(-r));
+                                goto fail;
+                        }
+                }
+
+                if (reply) {
+                        r = sd_bus_send(bus, reply, NULL);
+                        if (r < 0) {
+                                log_error("Failed to send reply: %s", strerror(-r));
+                                goto fail;
+                        }
+                }
+        }
+
+        r = 0;
+
+fail:
+        if (bus)
+                sd_bus_unref(bus);
+
+        return INT_TO_PTR(r);
+}
+
+static int client(void) {
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        sd_bus *bus = NULL;
+        sd_bus_error error = SD_BUS_ERROR_INIT;
+        const char *hello;
+        int r;
+
+        r = sd_bus_open_user(&bus);
+        if (r < 0) {
+                log_error("Failed to connect to user bus: %s", strerror(-r));
+                goto finish;
+        }
+
+        r = sd_bus_message_new_method_call(
+                        bus,
+                        "org.freedesktop.systemd.test",
+                        "/",
+                        "org.freedesktop.systemd.test",
+                        "LowerCase",
+                        &m);
+        if (r < 0) {
+                log_error("Failed to allocate method call: %s", strerror(-r));
+                goto finish;
+        }
+
+        r = sd_bus_message_append(m, "s", "HELLO");
+        if (r < 0) {
+                log_error("Failed to append string: %s", strerror(-r));
+                goto finish;
+        }
+
+        r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, &error, &reply);
+        if (r < 0) {
+                log_error("Failed to issue method call: %s", error.message);
+                goto finish;
+        }
+
+        r = sd_bus_message_read(reply, "s", &hello);
+        if (r < 0) {
+                log_error("Failed to get string: %s", strerror(-r));
+                goto finish;
+        }
+
+        assert(streq(hello, "hello"));
+
+        r = 0;
+
+finish:
+        if (bus) {
+                _cleanup_bus_message_unref_ sd_bus_message *q;
+
+                r = sd_bus_message_new_method_call(
+                                bus,
+                                "org.freedesktop.systemd.test",
+                                "/",
+                                "org.freedesktop.systemd.test",
+                                "Exit",
+                                &q);
+                if (r < 0) {
+                        log_error("Failed to allocate method call: %s", strerror(-r));
+                        goto finish;
+                }
+
+                sd_bus_send(bus, q, NULL);
+                sd_bus_flush(bus);
+                sd_bus_unref(bus);
+        }
+
+        sd_bus_error_free(&error);
+        return r;
+}
+
+int main(int argc, char *argv[]) {
+        pthread_t t;
+        sd_bus *bus;
+        void *p;
+        int q, r;
+
+        r = server_init(&bus);
+        if (r < 0)
+                return EXIT_FAILURE;
+
+        r = pthread_create(&t, NULL, server, bus);
+        if (r != 0) {
+                sd_bus_unref(bus);
+                return EXIT_FAILURE;
+        }
+
+        r = client();
+
+        q = pthread_join(t, &p);
+        if (q != 0)
+                return EXIT_FAILURE;
+        if (r < 0)
+                return EXIT_FAILURE;
+
+        return EXIT_SUCCESS;
+}