[systemd-commits] 5 commits - src/libsystemd-bus

Daniel Mack zonque at kemper.freedesktop.org
Fri Nov 29 13:02:06 PST 2013


 src/libsystemd-bus/bus-control.c |  213 +++++++++++++++++++++++++++++++++++++--
 src/libsystemd-bus/bus-kernel.c  |   35 ++++++
 src/libsystemd-bus/bus-kernel.h  |    3 
 src/libsystemd-bus/sd-bus.c      |   28 -----
 4 files changed, 241 insertions(+), 38 deletions(-)

New commits:
commit 3519d4c895b45adc6e0791e1a344574f78b2d1be
Author: Daniel Mack <zonque at gmail.com>
Date:   Fri Nov 29 21:58:39 2013 +0100

    libsystemd-bus: sd_bus_request_name: use kdbus_translate_request_name_flags()

diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index daa10eb..a0eeb30 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -68,7 +68,7 @@ _public_ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
                 l = strlen(name);
                 n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
                 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
-                n->flags = flags;
+                kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
                 memcpy(n->name, name, l+1);
 
 #ifdef HAVE_VALGRIND_MEMCHECK_H

commit 98f17eda0f2958946da2a206e9a614a489304624
Author: Daniel Mack <zonque at gmail.com>
Date:   Fri Nov 29 21:14:07 2013 +0100

    libsystemd-bus: rename sd_bus_kernel_translate_request_name_flags
    
    ... to keep the namespace clean.

diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 1b54ba5..cffdf09 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -1020,7 +1020,7 @@ void bus_kernel_flush_memfd(sd_bus *b) {
                 close_and_munmap(b->memfd_cache[i].fd, b->memfd_cache[i].address, b->memfd_cache[i].size);
 }
 
-int sd_bus_kernel_translate_request_name_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags) {
+int kdbus_translate_request_name_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags) {
 
         assert_return(kdbus_flags != NULL, -EINVAL);
 
diff --git a/src/libsystemd-bus/bus-kernel.h b/src/libsystemd-bus/bus-kernel.h
index 18ded49..4b63108 100644
--- a/src/libsystemd-bus/bus-kernel.h
+++ b/src/libsystemd-bus/bus-kernel.h
@@ -69,5 +69,5 @@ void bus_kernel_flush_memfd(sd_bus *bus);
 
 int bus_kernel_parse_unique_name(const char *s, uint64_t *id);
 
-int sd_bus_kernel_translate_request_name_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags);
+int kdbus_translate_request_name_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags);
 int kdbus_translate_attach_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags);

commit 6018903565b130a908481bb405026b7135ecb93b
Author: Daniel Mack <zonque at gmail.com>
Date:   Fri Nov 29 20:44:10 2013 +0100

    libsystemd-bus: add kdbus support for sd_bus_get_owner()

diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index 848a485..daa10eb 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -587,6 +587,188 @@ static int add_name_change_match(sd_bus *bus,
         return 0;
 }
 
+static int kdbus_name_query(
+                sd_bus *bus,
+                const char *name,
+                uint64_t mask,
+                char **owner,
+                sd_bus_creds **creds) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
+        _cleanup_free_ char *unique = NULL;
+        struct kdbus_cmd_name_info *name_info = NULL;
+        struct kdbus_item *item;
+        uint64_t attach_flags, m;
+        size_t slen, size;
+        int r;
+
+        r = kdbus_translate_attach_flags(mask, &attach_flags);
+        if (r < 0)
+                return r;
+
+        slen = strlen(name) + 1;
+
+        /*
+         * The structure is used for both directions. Start with 8k buffer size and
+         * expand to the size kdbus reports in case we fail.
+         */
+        size = slen + 8192;
+
+        for(;;) {
+                name_info = realloc(name_info, size);
+                if (!name_info)
+                        return -ENOMEM;
+
+                memset(name_info, 0, size);
+
+                name_info->size = size;
+                name_info->attach_flags = attach_flags;
+
+                item = name_info->items;
+                item->type = KDBUS_NAME_INFO_ITEM_NAME;
+                item->size = KDBUS_ITEM_SIZE(slen);
+                strcpy(item->str, name);
+
+                r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_QUERY, name_info);
+                if (r < 0) {
+                        if (errno == ENOBUFS && size != name_info->size) {
+                                size = name_info->size;
+                                continue;
+                        }
+
+                        return -errno;
+                }
+
+                break;
+        }
+
+        asprintf(&unique, ":1.%llu", (unsigned long long) name_info->id);
+
+        c = bus_creds_new();
+        if (!c)
+                return -ENOMEM;
+
+        KDBUS_PART_FOREACH(item, name_info, items) {
+                switch (item->type) {
+                case KDBUS_ITEM_CREDS:
+                        m = (SD_BUS_CREDS_UID | SD_BUS_CREDS_GID | SD_BUS_CREDS_PID |
+                             SD_BUS_CREDS_TID | SD_BUS_CREDS_PID_STARTTIME) & mask;
+
+                        if (m) {
+                                c->uid = item->creds.uid;
+                                c->pid = item->creds.pid;
+                                c->gid = item->creds.gid;
+                                c->tid = item->creds.tid;
+                                c->pid_starttime = item->creds.starttime;
+                                c->mask |= m;
+                        }
+                        break;
+
+                case KDBUS_ITEM_PID_COMM:
+                        if (mask & SD_BUS_CREDS_COMM) {
+                                c->comm = strdup(item->str);
+                                if (!c->comm)
+                                        return -ENOMEM;
+
+                                c->mask |= SD_BUS_CREDS_COMM;
+                        }
+                        break;
+
+                case KDBUS_ITEM_TID_COMM:
+                        if (mask & SD_BUS_CREDS_TID_COMM) {
+                                c->tid_comm = strdup(item->str);
+                                if (!c->tid_comm)
+                                        return -ENOMEM;
+
+                                c->mask |= SD_BUS_CREDS_TID_COMM;
+                        }
+                        break;
+
+                case KDBUS_ITEM_EXE:
+                        if (mask & SD_BUS_CREDS_EXE) {
+                                c->exe = strdup(item->str);
+                                if (!c->exe)
+                                        return -ENOMEM;
+
+                                c->mask |= SD_BUS_CREDS_EXE;
+                        }
+                        break;
+
+                case KDBUS_ITEM_CMDLINE:
+                        if (mask & SD_BUS_CREDS_CMDLINE) {
+                                c->cmdline_length = item->size - KDBUS_PART_HEADER_SIZE;
+                                c->cmdline = memdup(item->data, c->cmdline_length);
+                                if (!c->cmdline)
+                                        return -ENOMEM;
+
+                                c->mask |= SD_BUS_CREDS_CMDLINE;
+                        }
+                        break;
+
+                case KDBUS_ITEM_CGROUP:
+                        m = (SD_BUS_CREDS_CGROUP | SD_BUS_CREDS_UNIT |
+                             SD_BUS_CREDS_USER_UNIT | SD_BUS_CREDS_SLICE |
+                             SD_BUS_CREDS_SESSION | SD_BUS_CREDS_OWNER_UID) & mask;
+
+                        if (m) {
+                                c->cgroup = strdup(item->str);
+                                if (!c->cgroup)
+                                        return -ENOMEM;
+
+                                c->mask |= m;
+                        }
+                        break;
+
+                case KDBUS_ITEM_CAPS:
+                        m = (SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_PERMITTED_CAPS |
+                             SD_BUS_CREDS_INHERITABLE_CAPS | SD_BUS_CREDS_BOUNDING_CAPS) & mask;
+
+                        if (m) {
+                                c->capability_size = item->size - KDBUS_PART_HEADER_SIZE;
+                                c->capability = memdup(item->data, c->capability_size);
+                                if (!c->capability)
+                                        return -ENOMEM;
+
+                                c->mask |= m;
+                        }
+                        break;
+
+                case KDBUS_ITEM_SECLABEL:
+                        if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
+                                c->label = strdup(item->str);
+                                if (!c->label)
+                                        return -ENOMEM;
+
+                                c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
+                        }
+                        break;
+
+                case KDBUS_ITEM_AUDIT:
+                        m = (SD_BUS_CREDS_AUDIT_SESSION_ID | SD_BUS_CREDS_AUDIT_LOGIN_UID) & mask;
+
+                        if (m) {
+                                c->audit_session_id = item->audit.sessionid;
+                                c->audit_login_uid = item->audit.loginuid;
+                                c->mask |= m;
+                        }
+                        break;
+                }
+        }
+
+        if (creds) {
+                *creds = c;
+                c = NULL;
+        }
+
+        if (owner) {
+                *owner = unique;
+                unique = NULL;
+        }
+
+        return 0;
+}
+
 _public_ int sd_bus_get_owner(
                 sd_bus *bus,
                 const char *name,
@@ -601,6 +783,9 @@ _public_ int sd_bus_get_owner(
         assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
+        if (bus->is_kernel)
+                return kdbus_name_query(bus, name, mask, owner, creds);
+
         return sd_bus_get_owner_dbus(bus, name, mask, owner, creds);
 }
 

commit 87b522ae1936c4da9446ee2e05f7b4560ad549cd
Author: Daniel Mack <zonque at gmail.com>
Date:   Fri Nov 29 17:21:39 2013 +0100

    libsystemd-bus: add sd_bus_translate_attach_flags()
    
    Factor out code from sd_bus_negotiate_attach_creds() to a generic
    translate function, so it can be used from other places.

diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 1b586d8..1b54ba5 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -566,6 +566,39 @@ static int bus_kernel_translate_message(sd_bus *bus, struct kdbus_msg *k, sd_bus
         return translate[found->type](bus, k, d, ret);
 }
 
+int kdbus_translate_attach_flags(uint64_t mask, uint64_t *kdbus_mask) {
+
+        uint64_t m = 0;
+
+        SET_FLAG(m, KDBUS_ATTACH_CREDS,
+                 !!(mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID)));
+
+        SET_FLAG(m, KDBUS_ATTACH_COMM,
+                 !!(mask & (SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)));
+
+        SET_FLAG(m, KDBUS_ATTACH_EXE,
+                 !!(mask & SD_BUS_CREDS_EXE));
+
+        SET_FLAG(m, KDBUS_ATTACH_CMDLINE,
+                 !!(mask & SD_BUS_CREDS_CMDLINE));
+
+        SET_FLAG(m, KDBUS_ATTACH_CGROUP,
+                 !!(mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)));
+
+        SET_FLAG(m, KDBUS_ATTACH_CAPS,
+                 !!(mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)));
+
+        SET_FLAG(m, KDBUS_ATTACH_SECLABEL,
+                 !!(mask & SD_BUS_CREDS_SELINUX_CONTEXT));
+
+        SET_FLAG(m, KDBUS_ATTACH_AUDIT,
+                 !!(mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)));
+
+        *kdbus_mask = m;
+
+        return 0;
+}
+
 static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_message **ret) {
         sd_bus_message *m = NULL;
         struct kdbus_item *d;
diff --git a/src/libsystemd-bus/bus-kernel.h b/src/libsystemd-bus/bus-kernel.h
index 779a7de..18ded49 100644
--- a/src/libsystemd-bus/bus-kernel.h
+++ b/src/libsystemd-bus/bus-kernel.h
@@ -70,3 +70,4 @@ void bus_kernel_flush_memfd(sd_bus *bus);
 int bus_kernel_parse_unique_name(const char *s, uint64_t *id);
 
 int sd_bus_kernel_translate_request_name_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags);
+int kdbus_translate_attach_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags);
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index bfa6ba4..a894af0 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -290,33 +290,7 @@ _public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) {
         assert_return(bus->state == BUS_UNSET, -EPERM);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_CREDS,
-                 !!(mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID)));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_COMM,
-                 !!(mask & (SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_EXE,
-                 !!(mask & SD_BUS_CREDS_EXE));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_CMDLINE,
-                 !!(mask & SD_BUS_CREDS_CMDLINE));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_CGROUP,
-                 !!(mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_CAPS,
-                 !!(mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_SECLABEL,
-                 !!(mask & SD_BUS_CREDS_SELINUX_CONTEXT));
-
-        SET_FLAG(bus->attach_flags, KDBUS_ATTACH_AUDIT,
-                 !!(mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)));
-
-        bus->creds_mask = mask;
-
-        return 0;
+        return kdbus_translate_attach_flags(mask, &bus->creds_mask);
 }
 
 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {

commit c931748d20ba75e062b6198721970c6871bf23da
Author: Daniel Mack <zonque at gmail.com>
Date:   Fri Nov 29 16:52:06 2013 +0100

    libsystemd-bus: factor out DBus bits out of sd_bus_get_owner()
    
    Just a preparation for upcoming kdbus support.

diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index 5f218c1..848a485 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -260,7 +260,7 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***l) {
         return 0;
 }
 
-_public_ int sd_bus_get_owner(
+static int sd_bus_get_owner_dbus(
                 sd_bus *bus,
                 const char *name,
                 uint64_t mask,
@@ -273,13 +273,6 @@ _public_ int sd_bus_get_owner(
         pid_t pid = 0;
         int r;
 
-        assert_return(bus, -EINVAL);
-        assert_return(name, -EINVAL);
-        assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
-        assert_return(mask == 0 || creds, -EINVAL);
-        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
-        assert_return(!bus_pid_changed(bus), -ECHILD);
-
         /* Only query the owner if the caller wants to know it or if
          * the caller just wants to check whether a name exists */
         if (owner || mask == 0) {
@@ -594,6 +587,23 @@ static int add_name_change_match(sd_bus *bus,
         return 0;
 }
 
+_public_ int sd_bus_get_owner(
+                sd_bus *bus,
+                const char *name,
+                uint64_t mask,
+                char **owner,
+                sd_bus_creds **creds) {
+
+        assert_return(bus, -EINVAL);
+        assert_return(name, -EINVAL);
+        assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
+        assert_return(mask == 0 || creds, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        return sd_bus_get_owner_dbus(bus, name, mask, owner, creds);
+}
+
 int bus_add_match_internal(
                 sd_bus *bus,
                 const char *match,



More information about the systemd-commits mailing list