chiark / gitweb /
kdbus: parse even more kernel meta data fields
authorLennart Poettering <lennart@poettering.net>
Sat, 13 Apr 2013 19:53:11 +0000 (21:53 +0200)
committerLennart Poettering <lennart@poettering.net>
Sat, 13 Apr 2013 19:53:11 +0000 (21:53 +0200)
src/libsystemd-bus/bus-kernel.c
src/libsystemd-bus/bus-message.c
src/libsystemd-bus/bus-message.h
src/libsystemd-bus/bus-socket.c
src/libsystemd-bus/kdbus.h
src/libsystemd-bus/test-bus-chat.c
src/systemd/sd-bus.h

index f948b4e316a40d3522a35842b4b43cb04b12e7d1..ca47ea2f89b106e9f825f1fd05ec4658e7aebd1f 100644 (file)
@@ -118,15 +118,16 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
 
         sz = offsetof(struct kdbus_msg, data);
 
-        /* Add in fixed header, fields header, fields header padding and payload */
-        sz += 4 * ALIGN8(offsetof(struct kdbus_msg_data, vec) + sizeof(struct kdbus_vec));
+        /* Add in fixed header, fields header and payload */
+        sz += 3 * ALIGN8(offsetof(struct kdbus_msg_data, vec) + sizeof(struct kdbus_vec));
 
+        /* Add space for bloom filter */
         sz += ALIGN8(offsetof(struct kdbus_msg_data, data) + b->bloom_size);
 
         /* Add in well-known destination header */
         if (well_known) {
                 dl = strlen(m->destination);
-                sz += ALIGN8(offsetof(struct kdbus_msg, data) + dl + 1);
+                sz += ALIGN8(offsetof(struct kdbus_msg_data, str) + dl + 1);
         }
 
         m->kdbus = aligned_alloc(8, sz);
@@ -153,15 +154,8 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
 
         append_payload_vec(&d, m->header, sizeof(*m->header));
 
-        if (m->fields) {
-                append_payload_vec(&d, m->fields, m->header->fields_size);
-
-                if (m->header->fields_size % 8 != 0) {
-                        static const uint8_t padding[7] = {};
-
-                        append_payload_vec(&d, padding, 8 - (m->header->fields_size % 8));
-                }
-        }
+        if (m->fields)
+                append_payload_vec(&d, m->fields, ALIGN8(m->header->fields_size));
 
         if (m->body)
                 append_payload_vec(&d, m->body, m->header->body_size);
@@ -184,7 +178,18 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
 }
 
 int bus_kernel_take_fd(sd_bus *b) {
-        struct kdbus_cmd_hello hello = {};
+        struct kdbus_cmd_hello hello = {
+                .conn_flags =
+                        KDBUS_CMD_HELLO_ACCEPT_FD|
+                        KDBUS_CMD_HELLO_ACCEPT_MMAP|
+                        KDBUS_CMD_HELLO_ATTACH_COMM|
+                        KDBUS_CMD_HELLO_ATTACH_EXE|
+                        KDBUS_CMD_HELLO_ATTACH_CMDLINE|
+                        KDBUS_CMD_HELLO_ATTACH_CGROUP|
+                        KDBUS_CMD_HELLO_ATTACH_CAPS|
+                        KDBUS_CMD_HELLO_ATTACH_SECLABEL|
+                        KDBUS_CMD_HELLO_ATTACH_AUDIT
+        };
         int r;
 
         assert(b);
@@ -271,9 +276,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
         _cleanup_free_ int *fds = NULL;
         struct bus_header *h = NULL;
         size_t total, n_bytes = 0, idx = 0;
-        struct kdbus_creds *creds = NULL;
-        uint64_t nsec = 0;
-        const char *destination = NULL;
+        const char *destination = NULL, *seclabel = NULL;
         int r;
 
         assert(bus);
@@ -313,12 +316,10 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
                         memcpy(fds + n_fds, d->fds, j);
                         n_fds += j;
 
-                } else if (d->type == KDBUS_MSG_SRC_CREDS)
-                        creds = &d->creds;
-                else if (d->type == KDBUS_MSG_TIMESTAMP)
-                        nsec = d->ts_ns;
-                else if (d->type == KDBUS_MSG_DST_NAME)
+                } else if (d->type == KDBUS_MSG_DST_NAME)
                         destination = d->str;
+                else if (d->type == KDBUS_MSG_SRC_SECLABEL)
+                        seclabel = d->str;
         }
 
         if (!h)
@@ -331,42 +332,46 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k, sd_bus_mess
         if (n_bytes != total)
                 return -EBADMSG;
 
-        r = bus_message_from_header(h, sizeof(struct bus_header), fds, n_fds, NULL, NULL, 0, &m);
+        r = bus_message_from_header(h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m);
         if (r < 0)
                 return r;
 
         KDBUS_MSG_FOREACH_DATA(d, k) {
                 size_t l;
 
-                if (d->type != KDBUS_MSG_PAYLOAD)
-                        continue;
-
                 l = d->size - offsetof(struct kdbus_msg_data, data);
-                if (idx == sizeof(struct bus_header) &&
-                    l == BUS_MESSAGE_FIELDS_SIZE(m))
-                        m->fields = d->data;
-                else if (idx == sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) &&
-                         l == BUS_MESSAGE_BODY_SIZE(m))
-                        m->body = d->data;
-                else if (!(idx == 0 && l == sizeof(struct bus_header)) &&
-                         !(idx == sizeof(struct bus_header) + BUS_MESSAGE_FIELDS_SIZE(m))) {
-                        sd_bus_message_unref(m);
-                        return -EBADMSG;
-                }
 
-                idx += l;
-        }
+                if (d->type == KDBUS_MSG_PAYLOAD) {
 
-        if (creds) {
-                m->pid_starttime = creds->starttime / NSEC_PER_USEC;
-                m->uid = creds->uid;
-                m->gid = creds->gid;
-                m->pid = creds->pid;
-                m->tid = creds->tid;
-                m->uid_valid = m->gid_valid = true;
-        }
+                        if (idx == sizeof(struct bus_header) &&
+                            l == ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)))
+                                m->fields = d->data;
+                        else if (idx == sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m)) &&
+                                 l == BUS_MESSAGE_BODY_SIZE(m))
+                                m->body = d->data;
+                        else if (!(idx == 0 && l == sizeof(struct bus_header))) {
+                                sd_bus_message_unref(m);
+                                return -EBADMSG;
+                        }
 
-        m->timestamp = nsec / NSEC_PER_USEC;
+                        idx += l;
+                } else if (d->type == KDBUS_MSG_SRC_CREDS) {
+                        m->pid_starttime = d->creds.starttime / NSEC_PER_USEC;
+                        m->uid = d->creds.uid;
+                        m->gid = d->creds.gid;
+                        m->pid = d->creds.pid;
+                        m->tid = d->creds.tid;
+                        m->uid_valid = m->gid_valid = true;
+                } else if (d->type == KDBUS_MSG_TIMESTAMP) {
+                        m->realtime = d->timestamp.realtime_ns / NSEC_PER_USEC;
+                        m->monotonic = d->timestamp.monotonic_ns / NSEC_PER_USEC;
+                } else if (d->type == KDBUS_MSG_SRC_PID_COMM)
+                        m->comm = d->str;
+                else if (d->type == KDBUS_MSG_SRC_TID_COMM)
+                        m->tid_comm = d->str;
+                else if (d->type == KDBUS_MSG_SRC_EXE)
+                        m->exe = d->str;
+        }
 
         r = bus_message_parse_fields(m);
         if (r < 0) {
index 467b519039a7d32d7abec7ed17dfc4483ea82e72..bae0812facabbf109cc72b5a3113744ecf2fb348 100644 (file)
@@ -702,24 +702,56 @@ int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
         return 0;
 }
 
-const char *sd_bus_message_get_label(sd_bus_message *m) {
+const char *sd_bus_message_get_selinux_context(sd_bus_message *m) {
         if (!m)
                 return NULL;
 
         return m->label;
 }
 
-int sd_bus_message_get_timestamp(sd_bus_message *m, uint64_t *usec) {
+int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
         if (!m)
                 return -EINVAL;
 
-        if (m->timestamp <= 0)
+        if (m->monotonic <= 0)
                 return -ENOENT;
 
-        *usec = m->timestamp;
+        *usec = m->monotonic;
         return 0;
 }
 
+int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
+        if (!m)
+                return -EINVAL;
+
+        if (m->realtime <= 0)
+                return -ENOENT;
+
+        *usec = m->realtime;
+        return 0;
+}
+
+const char *sd_bus_message_get_comm(sd_bus_message *m) {
+        if (!m)
+                return NULL;
+
+        return m->comm;
+}
+
+const char *sd_bus_message_get_tid_comm(sd_bus_message *m) {
+        if (!m)
+                return NULL;
+
+        return m->tid_comm;
+}
+
+const char *sd_bus_message_get_exe(sd_bus_message *m) {
+        if (!m)
+                return NULL;
+
+        return m->exe;
+}
+
 int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
         if (!m)
                 return -EINVAL;
@@ -2837,6 +2869,7 @@ int bus_message_parse_fields(sd_bus_message *m) {
 
 int bus_message_seal(sd_bus_message *m, uint64_t serial) {
         int r;
+        size_t l, a;
 
         assert(m);
 
@@ -2859,6 +2892,22 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
                         return r;
         }
 
+        l = BUS_MESSAGE_FIELDS_SIZE(m);
+        a = ALIGN8(l) - l;
+
+        if (a > 0) {
+                /* Add padding at the end, since we know the body
+                 * needs to start at an 8 byte alignment. */
+                void *p;
+
+                p = message_extend_fields(m, 1, a);
+                if (!p)
+                        return -ENOMEM;
+
+                memset(p, 0, a);
+                m->header->fields_size -= a;
+        }
+
         m->header->serial = serial;
         m->sealed = true;
 
@@ -2933,8 +2982,18 @@ int bus_message_dump(sd_bus_message *m) {
                 printf("\tgid=%lu\n", (unsigned long) m->gid);
         if (m->pid_starttime != 0)
                 printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
-        if (m->timestamp)
-                printf("\ttimestamp=%llu\n", (unsigned long long) m->timestamp);
+        if (m->monotonic != 0)
+                printf("\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
+        if (m->realtime != 0)
+                printf("\trealtime=%llu\n", (unsigned long long) m->realtime);
+        if (m->exe)
+                printf("\texe=[%s]\n", m->exe);
+        if (m->comm)
+                printf("\tcomm=[%s]\n", m->comm);
+        if (m->tid_comm)
+                printf("\ttid_comm=[%s]\n", m->tid_comm);
+        if (m->label)
+                printf("\tlabel=[%s]\n", m->label);
 
         r = sd_bus_message_rewind(m, true);
         if (r < 0) {
index 8eea46baf17edb35d0b12553682d45db763dc91a..1c403777faf963b3c14c41da0520431289b6d8cd 100644 (file)
@@ -68,7 +68,8 @@ struct sd_bus_message {
         pid_t pid;
         pid_t tid;
         usec_t pid_starttime;
-        usec_t timestamp;
+        usec_t monotonic;
+        usec_t realtime;
 
         bool sealed:1;
         bool dont_send:1;
@@ -96,7 +97,7 @@ struct sd_bus_message {
         struct bus_container root_container, *containers;
         unsigned n_containers;
 
-        struct iovec iovec[4];
+        struct iovec iovec[3];
         unsigned n_iovec;
 
         char *peeked_signature;
@@ -105,6 +106,10 @@ struct sd_bus_message {
 
         char sender_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
         char destination_buffer[3 + DECIMAL_STR_MAX(uint64_t) + 1];
+
+        const char *exe;
+        const char *comm;
+        const char *tid_comm;
 };
 
 #define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_NATIVE_ENDIAN)
index bce81aeffce1b8c9584bf178b20f651ac1ba1a27..8a86b02c681080e8f95c8a78c59dc3cad20bf368 100644 (file)
@@ -77,15 +77,8 @@ static void bus_message_setup_iovec(sd_bus_message *m) {
 
         append_iovec(m, m->header, sizeof(*m->header));
 
-        if (m->fields) {
-                append_iovec(m, m->fields, m->header->fields_size);
-
-                if (m->header->fields_size % 8 != 0) {
-                        static const uint8_t padding[7] = {};
-
-                        append_iovec(m, padding, 8 - (m->header->fields_size % 8));
-                }
-        }
+        if (m->fields)
+                append_iovec(m, m->fields, ALIGN8(m->header->fields_size));
 
         if (m->body)
                 append_iovec(m, m->body, m->header->body_size);
index 8c643feceb947b35b7a5b350236565f45039ec87..441bfab9e7b35d69be153093d9f0c031c53bdec6 100644 (file)
@@ -53,6 +53,11 @@ struct kdbus_audit {
        __u64 loginuid;
 };
 
+struct kdbus_timestamp {
+       __u64 monotonic_ns;
+       __u64 realtime_ns;
+};
+
 #define KDBUS_SRC_ID_KERNEL            (0)
 #define KDBUS_DST_ID_WELL_KNOWN_NAME   (0)
 #define KDBUS_MATCH_SRC_ID_ANY         (~0ULL)
@@ -70,7 +75,7 @@ enum {
 
        /* Filled in by kernelspace */
        KDBUS_MSG_SRC_NAMES     = 0x200,/* NUL separated string list with well-known names of source */
-       KDBUS_MSG_TIMESTAMP,            /* .ts_ns of CLOCK_MONOTONIC */
+       KDBUS_MSG_TIMESTAMP,            /* .timestamp */
        KDBUS_MSG_SRC_CREDS,            /* .creds */
        KDBUS_MSG_SRC_PID_COMM,         /* optional, in .str */
        KDBUS_MSG_SRC_TID_COMM,         /* optional, in .str */
@@ -110,17 +115,15 @@ struct kdbus_msg_data {
                /* inline data */
                __u8 data[0];
                char str[0];
-               __u32 data_u32[0];
-               __u64 data_u64[0];
 
                /* data vector */
                struct kdbus_vec vec;
 
                /* specific fields */
                int fds[0];                             /* int array of file descriptors */
-               __u64 ts_ns;                            /* timestamp in nanoseconds */
                struct kdbus_creds creds;
                struct kdbus_audit audit;
+               struct kdbus_timestamp timestamp;
                struct kdbus_manager_msg_name_change name_change;
                struct kdbus_manager_msg_id_change id_change;
        };
index 371c7a7513fa8eac10098cc7763c224c1b6fa7d2..9f4a5597f9ff80e67c405e76b6894b84b7ea4b39 100644 (file)
@@ -159,7 +159,7 @@ static int server(sd_bus *bus) {
                         continue;
 
                 sd_bus_message_get_pid(m, &pid);
-                log_info("Got message! member=%s pid=%lu label=%s", strna(sd_bus_message_get_member(m)), (unsigned long) pid, strna(sd_bus_message_get_label(m)));
+                log_info("Got message! member=%s pid=%lu label=%s", strna(sd_bus_message_get_member(m)), (unsigned long) pid, strna(sd_bus_message_get_selinux_context(m)));
                 /* bus_message_dump(m); */
                 /* sd_bus_message_rewind(m, true); */
 
index 91445bb62d3788ae31d8e9c17968572520605f7c..70d41ebc489ff3a84dfb5fd66667f2f5518e00cf 100644 (file)
@@ -33,11 +33,13 @@ extern "C" {
 #endif
 
 /* TODO:
- * - instead of adding in padding iovec when sending simply extend header buffer
  * - add page donation logic
  * - api for appending/reading fixed arrays
  * - merge busctl into systemctl or so?
  * - default policy (allow uid == 0 and our own uid)
+ *
+ * - enforce alignment of pointers passed in
+ * - negotiation for attach attributes
  */
 
 typedef struct sd_bus sd_bus;
@@ -121,13 +123,17 @@ const char *sd_bus_message_get_destination(sd_bus_message *m);
 const char *sd_bus_message_get_sender(sd_bus_message *m);
 const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m);
 
+int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec);
+int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec);
 int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid);
 int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid);
 int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid);
 int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid);
 int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec);
-const char *sd_bus_message_get_label(sd_bus_message *m);
-int sd_bus_message_get_timestamp(sd_bus_message *m, uint64_t *usec);
+const char *sd_bus_message_get_selinux_context(sd_bus_message *m);
+const char *sd_bus_message_get_comm(sd_bus_message *m);
+const char *sd_bus_message_get_tid_comm(sd_bus_message *m);
+const char *sd_bus_message_get_exe(sd_bus_message *m);
 
 int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member);
 int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member);