Skip to content

Commit

Permalink
test: add kdbus-api.h
Browse files Browse the repository at this point in the history
Add a simple ioctl() wrapper to provide type-safety for callers of the
API, and convert over all direct ioctl() users in test/.

Signed-off-by: Daniel Mack <daniel@zonque.org>
  • Loading branch information
zonque committed Feb 6, 2015
1 parent d47913a commit 0d8a559
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 179 deletions.
103 changes: 103 additions & 0 deletions test/kdbus-api.h
@@ -0,0 +1,103 @@
#ifndef KDBUS_API_H
#define KDBUS_API_H

#include <sys/ioctl.h>
#include "../kdbus.h"

static inline int kdbus_cmd_bus_make(int control_fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(control_fd, KDBUS_CMD_BUS_MAKE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_endpoint_make(int bus_fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(bus_fd, KDBUS_CMD_ENDPOINT_MAKE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_endpoint_update(int ep_fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(ep_fd, KDBUS_CMD_ENDPOINT_UPDATE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_hello(int bus_fd, struct kdbus_cmd_hello *cmd)
{
int ret = ioctl(bus_fd, KDBUS_CMD_HELLO, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_update(int fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(fd, KDBUS_CMD_UPDATE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_byebye(int conn_fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_BYEBYE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_free(int conn_fd, struct kdbus_cmd_free *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_FREE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_conn_info(int conn_fd, struct kdbus_cmd_info *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_CONN_INFO, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_bus_creator_info(int conn_fd, struct kdbus_cmd_info *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_BUS_CREATOR_INFO, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_list(int fd, struct kdbus_cmd_list *cmd)
{
int ret = ioctl(fd, KDBUS_CMD_LIST, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_send(int conn_fd, struct kdbus_cmd_send *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_SEND, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_recv(int conn_fd, struct kdbus_cmd_recv *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_RECV, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_name_acquire(int conn_fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_NAME_ACQUIRE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_name_release(int conn_fd, struct kdbus_cmd *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_NAME_RELEASE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_match_add(int conn_fd, struct kdbus_cmd_match *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_MATCH_ADD, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

static inline int kdbus_cmd_match_remove(int conn_fd, struct kdbus_cmd_match *cmd)
{
int ret = ioctl(conn_fd, KDBUS_CMD_MATCH_REMOVE, cmd);
return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0;
}

#endif /* KDBUS_API_H */
1 change: 0 additions & 1 deletion test/kdbus-enum.c
Expand Up @@ -15,7 +15,6 @@
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <sys/ioctl.h>

#include "kdbus-util.h"
#include "kdbus-enum.h"
Expand Down
72 changes: 27 additions & 45 deletions test/kdbus-util.c
Expand Up @@ -25,7 +25,6 @@
#include <poll.h>
#include <grp.h>
#include <sys/capability.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
Expand All @@ -44,6 +43,7 @@
#endif
#endif

#include "kdbus-api.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"

Expand Down Expand Up @@ -119,7 +119,7 @@ int kdbus_create_bus(int control_fd, const char *name,
char **path)
{
struct {
struct kdbus_cmd head;
struct kdbus_cmd cmd;

/* bloom size item */
struct {
Expand Down Expand Up @@ -165,8 +165,8 @@ int kdbus_create_bus(int control_fd, const char *name,
bus_make.name.size = KDBUS_ITEM_HEADER_SIZE +
strlen(bus_make.name.str) + 1;

bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
bus_make.head.size = sizeof(bus_make.head) +
bus_make.cmd.flags = KDBUS_MAKE_ACCESS_WORLD;
bus_make.cmd.size = sizeof(bus_make.cmd) +
bus_make.bp.size +
bus_make.attach[0].size +
bus_make.attach[1].size +
Expand All @@ -175,9 +175,8 @@ int kdbus_create_bus(int control_fd, const char *name,
kdbus_printf("Creating bus with name >%s< on control fd %d ...\n",
name, control_fd);

ret = ioctl(control_fd, KDBUS_CMD_BUS_MAKE, &bus_make);
ret = kdbus_cmd_bus_make(control_fd, &bus_make.cmd);
if (ret < 0) {
ret = -errno;
kdbus_printf("--- error when making bus: %d (%m)\n", ret);
return ret;
}
Expand Down Expand Up @@ -229,9 +228,8 @@ kdbus_hello(const char *path, uint64_t flags,
h.hello.size = sizeof(h);
h.hello.pool_size = POOL_SIZE;

ret = ioctl(fd, KDBUS_CMD_HELLO, &h.hello);
ret = kdbus_cmd_hello(fd, (struct kdbus_cmd_hello *) &h.hello);
if (ret < 0) {
ret = -errno;
kdbus_printf("--- error when saying hello: %d (%m)\n", ret);
return NULL;
}
Expand All @@ -246,7 +244,7 @@ kdbus_hello(const char *path, uint64_t flags,

cmd_free.size = sizeof(cmd_free);
cmd_free.offset = h.hello.offset;
ioctl(fd, KDBUS_CMD_FREE, &cmd_free);
kdbus_cmd_free(fd, &cmd_free);

conn = malloc(sizeof(*conn));
if (!conn) {
Expand Down Expand Up @@ -333,9 +331,8 @@ int kdbus_bus_creator_info(struct kdbus_conn *conn,
cmd->size = size;
cmd->flags = flags;

ret = ioctl(conn->fd, KDBUS_CMD_BUS_CREATOR_INFO, cmd);
ret = kdbus_cmd_bus_creator_info(conn->fd, cmd);
if (ret < 0) {
ret = -errno;
kdbus_printf("--- error when requesting info: %d (%m)\n", ret);
return ret;
}
Expand Down Expand Up @@ -373,9 +370,8 @@ int kdbus_conn_info(struct kdbus_conn *conn, uint64_t id,
cmd->id = id;
}

ret = ioctl(conn->fd, KDBUS_CMD_CONN_INFO, cmd);
ret = kdbus_cmd_conn_info(conn->fd, cmd);
if (ret < 0) {
ret = -errno;
kdbus_printf("--- error when requesting info: %d (%m)\n", ret);
return ret;
}
Expand Down Expand Up @@ -588,12 +584,11 @@ static int __kdbus_msg_send(const struct kdbus_conn *conn,
item = KDBUS_ITEM_NEXT(item);
}

ret = ioctl(conn->fd, KDBUS_CMD_SEND, cmd);
ret = kdbus_cmd_send(conn->fd, cmd);
if (memfd >= 0)
close(memfd);

if (ret < 0) {
ret = -errno;
kdbus_printf("error sending message: %d (%m)\n", ret);
return ret;
}
Expand Down Expand Up @@ -673,11 +668,9 @@ int kdbus_msg_send_reply(const struct kdbus_conn *conn,
cmd.size = sizeof(cmd);
cmd.msg_address = (uintptr_t)msg;

ret = ioctl(conn->fd, KDBUS_CMD_SEND, &cmd);
if (ret < 0) {
ret = -errno;
ret = kdbus_cmd_send(conn->fd, &cmd);
if (ret < 0)
kdbus_printf("error sending message: %d (%m)\n", ret);
}

free(msg);

Expand Down Expand Up @@ -975,9 +968,8 @@ int kdbus_msg_recv(struct kdbus_conn *conn,
struct kdbus_msg *msg;
int ret;

ret = ioctl(conn->fd, KDBUS_CMD_RECV, &recv);
ret = kdbus_cmd_recv(conn->fd, &recv);
if (ret < 0) {
ret = -errno;
/* store how many lost packets */
if (ret == -EOVERFLOW && offset)
*offset = recv.dropped_msgs;
Expand Down Expand Up @@ -1066,10 +1058,10 @@ int kdbus_free(const struct kdbus_conn *conn, uint64_t offset)
cmd_free.offset = offset;
cmd_free.flags = 0;

ret = ioctl(conn->fd, KDBUS_CMD_FREE, &cmd_free);
ret = kdbus_cmd_free(conn->fd, &cmd_free);
if (ret < 0) {
kdbus_printf("KDBUS_CMD_FREE failed: %d (%m)\n", ret);
return -errno;
return ret;
}

return 0;
Expand Down Expand Up @@ -1097,9 +1089,8 @@ int kdbus_name_acquire(struct kdbus_conn *conn,
if (flags)
cmd_name->flags = *flags;

ret = ioctl(conn->fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name);
ret = kdbus_cmd_name_acquire(conn->fd, cmd_name);
if (ret < 0) {
ret = -errno;
kdbus_printf("error aquiring name: %s\n", strerror(-ret));
return ret;
}
Expand Down Expand Up @@ -1135,9 +1126,8 @@ int kdbus_name_release(struct kdbus_conn *conn, const char *name)
kdbus_printf("conn %lld giving up name '%s'\n",
(unsigned long long) conn->id, name);

ret = ioctl(conn->fd, KDBUS_CMD_NAME_RELEASE, cmd_name);
ret = kdbus_cmd_name_release(conn->fd, cmd_name);
if (ret < 0) {
ret = -errno;
kdbus_printf("error releasing name: %s\n", strerror(-ret));
return ret;
}
Expand All @@ -1154,10 +1144,10 @@ int kdbus_list(struct kdbus_conn *conn, uint64_t flags)
cmd_list.size = sizeof(cmd_list);
cmd_list.flags = flags;

ret = ioctl(conn->fd, KDBUS_CMD_LIST, &cmd_list);
ret = kdbus_cmd_list(conn->fd, &cmd_list);
if (ret < 0) {
kdbus_printf("error listing names: %d (%m)\n", ret);
return EXIT_FAILURE;
return ret;
}

kdbus_printf("REGISTRY:\n");
Expand Down Expand Up @@ -1222,11 +1212,9 @@ int kdbus_conn_update_attach_flags(struct kdbus_conn *conn,
item->data64[0] = attach_flags_recv;
item = KDBUS_ITEM_NEXT(item);

ret = ioctl(conn->fd, KDBUS_CMD_UPDATE, update);
if (ret < 0) {
ret = -errno;
ret = kdbus_cmd_update(conn->fd, update);
if (ret < 0)
kdbus_printf("error conn update: %d (%m)\n", ret);
}

free(update);

Expand Down Expand Up @@ -1275,11 +1263,9 @@ int kdbus_conn_update_policy(struct kdbus_conn *conn, const char *name,
item = KDBUS_ITEM_NEXT(item);
}

ret = ioctl(conn->fd, KDBUS_CMD_UPDATE, update);
if (ret < 0) {
ret = -errno;
ret = kdbus_cmd_update(conn->fd, update);
if (ret < 0)
kdbus_printf("error conn update: %d (%m)\n", ret);
}

free(update);

Expand Down Expand Up @@ -1307,11 +1293,9 @@ int kdbus_add_match_id(struct kdbus_conn *conn, uint64_t cookie,
buf.item.type = type;
buf.item.chg.id = id;

ret = ioctl(conn->fd, KDBUS_CMD_MATCH_ADD, &buf);
if (ret < 0) {
ret = -errno;
ret = kdbus_cmd_match_add(conn->fd, &buf.cmd);
if (ret < 0)
kdbus_printf("--- error adding conn match: %d (%m)\n", ret);
}

return ret;
}
Expand All @@ -1332,11 +1316,9 @@ int kdbus_add_match_empty(struct kdbus_conn *conn)

buf.cmd.size = sizeof(buf.cmd) + buf.item.size;

ret = ioctl(conn->fd, KDBUS_CMD_MATCH_ADD, &buf);
if (ret < 0) {
ret = kdbus_cmd_match_add(conn->fd, &buf.cmd);
if (ret < 0)
kdbus_printf("--- error adding conn match: %d (%m)\n", ret);
ret = -errno;
}

return ret;
}
Expand Down
1 change: 0 additions & 1 deletion test/test-activator.c
Expand Up @@ -12,7 +12,6 @@
#include <poll.h>
#include <sys/capability.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>

#include "kdbus-test.h"
Expand Down
9 changes: 4 additions & 5 deletions test/test-attach-flags.c
Expand Up @@ -9,12 +9,12 @@
#include <errno.h>
#include <assert.h>
#include <sys/capability.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/unistd.h>

#include "kdbus-api.h"
#include "kdbus-test.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"
Expand Down Expand Up @@ -63,9 +63,8 @@ static struct kdbus_conn *__kdbus_hello(const char *path, uint64_t flags,
h.hello.size = sizeof(h);
h.hello.pool_size = POOL_SIZE;

ret = ioctl(fd, KDBUS_CMD_HELLO, &h.hello);
ret = kdbus_cmd_hello(fd, (struct kdbus_cmd_hello *) &h.hello);
if (ret < 0) {
ret = -errno;
kdbus_printf("--- error when saying hello: %d (%m)\n", ret);
return NULL;
}
Expand All @@ -75,7 +74,7 @@ static struct kdbus_conn *__kdbus_hello(const char *path, uint64_t flags,

cmd_free.size = sizeof(cmd_free);
cmd_free.offset = h.hello.offset;
ret = ioctl(fd, KDBUS_CMD_FREE, &cmd_free);
ret = kdbus_cmd_free(fd, &cmd_free);
if (ret < 0)
return NULL;

Expand Down Expand Up @@ -726,7 +725,7 @@ int kdbus_test_attach_flags(struct kdbus_test_env *env)
ASSERT_RETURN(ret == 0);

/*
* Test the CONN_INFO ioctl attach flags
* Test the CONN_INFO attach flags
*/
ret = kdbus_test_peers_info(env);
/* Restore previous kdbus mask */
Expand Down

0 comments on commit 0d8a559

Please sign in to comment.