test: add conn_update() to test KDBUS_CMD_CONN_UPDATE
authorDjalal Harouni <tixxdz@opendz.org>
Fri, 20 Jun 2014 16:49:58 +0000 (17:49 +0100)
committerDaniel Mack <zonque@gmail.com>
Fri, 20 Jun 2014 17:35:42 +0000 (19:35 +0200)
Add conn_update() to perform KDBUS_CMD_CONN_UPDATE ioctl() calls.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test/kdbus-util.c
test/kdbus-util.h

index 965c95d31139577925e8ce600a07dbdc1968391e..6bb3bbf045ad83ecbbf0361597b57d3c2fed414a 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2013 Daniel Mack
  * Copyright (C) 2013 Kay Sievers
+ * Copyright (C) 2014 Djalal Harouni
  *
  * kdbus 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
@@ -606,6 +607,79 @@ int name_list(struct conn *conn, uint64_t flags)
        return 0;
 }
 
+int conn_update(struct conn *conn, const char *name,
+               const struct kdbus_policy_access *access,
+               size_t num_access, uint64_t flags)
+{
+       struct kdbus_cmd_update *update;
+       struct kdbus_item *item;
+       size_t i, size;
+       int ret;
+
+       size = sizeof(struct kdbus_cmd_update);
+       size += KDBUS_ITEM_SIZE(sizeof(uint64_t));
+       size += KDBUS_ITEM_SIZE(strlen(name) + 1);
+       size += num_access * KDBUS_ITEM_SIZE(sizeof(struct kdbus_policy_access));
+
+       update = malloc(size);
+       if (!update) {
+               ret = -errno;
+               fprintf(stderr, "error malloc: %d (%m)\n", ret);
+               return ret;
+       }
+
+       memset(update, 0, size);
+       update->size = size;
+
+       item = update->items;
+
+       /*
+        * normally having flags == 0 is valid, but just keep
+        * HELLO flags of __kdbus_hello(), don't check them.
+        */
+       item->type = KDBUS_ITEM_ATTACH_FLAGS;
+       item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(uint64_t);
+       item->data64[0] = flags ? flags : KDBUS_ATTACH_TIMESTAMP |
+                                         KDBUS_ATTACH_CREDS |
+                                         KDBUS_ATTACH_NAMES |
+                                         KDBUS_ATTACH_COMM |
+                                         KDBUS_ATTACH_EXE |
+                                         KDBUS_ATTACH_CMDLINE |
+                                         KDBUS_ATTACH_CAPS |
+                                         KDBUS_ATTACH_CGROUP |
+                                         KDBUS_ATTACH_SECLABEL |
+                                         KDBUS_ATTACH_AUDIT |
+                                         KDBUS_ATTACH_CONN_NAME;
+       item = KDBUS_ITEM_NEXT(item);
+
+       item->type = KDBUS_ITEM_NAME;
+       item->size = KDBUS_ITEM_HEADER_SIZE + strlen(name) + 1;
+       strcpy(item->str, name);
+       item = KDBUS_ITEM_NEXT(item);
+
+       for (i = 0; i < num_access; i++) {
+               item->size = KDBUS_ITEM_HEADER_SIZE +
+                            sizeof(struct kdbus_policy_access);
+               item->type = KDBUS_ITEM_POLICY_ACCESS;
+
+               item->policy_access.type = access[i].type;
+               item->policy_access.access = access[i].access;
+               item->policy_access.id = access[i].id;
+
+               item = KDBUS_ITEM_NEXT(item);
+       }
+
+       ret = ioctl(conn->fd, KDBUS_CMD_CONN_UPDATE, update);
+       if (ret < 0) {
+               ret = -errno;
+               fprintf(stderr, "error conn update: %d (%m)\n", ret);
+       }
+
+       free(update);
+
+       return ret;
+}
+
 void add_match_empty(int fd)
 {
        struct {
index dd7d7b6058ffebe468161fc1b4b858afd0c8a083..0fcfb72a3de0c47b796b52e7cce77200dd9c4640 100644 (file)
@@ -53,6 +53,10 @@ struct conn *kdbus_hello_activator(const char *path, const char *name,
                                   size_t num_access);
 struct kdbus_item *make_policy_name(const char *name);
 struct kdbus_item *make_policy_access(__u64 type, __u64 bits, __u64 id);
+int conn_update(struct conn *conn, const char *name,
+               const struct kdbus_policy_access *access,
+               size_t num_access, uint64_t flags);
+
 void add_match_empty(int fd);
 
 int drop_privileges(uid_t uid, gid_t gid);