File: ipc-api-tests-exported.h

package info (click to toggle)
mmlib 1.4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: ansic: 18,071; makefile: 431; sh: 135; python: 63
file content (107 lines) | stat: -rw-r--r-- 2,298 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef IPC_API_TESTS_EXPORTED_H
#define IPC_API_TESTS_EXPORTED_H

#include <stdio.h>

#include <mmsysio.h>
#include <mmthread.h>

API_EXPORTED intptr_t test_client_process(void * arg);

enum {
	SHARED_FILE,
	SHARED_MEM,
	SHARED_PIPE,
	SHARED_IPC,
};

struct ipc_test_ctx {
	int nclients;
	int run_mode;
	int index;
	int shared_object;
	int fd;
};

/* small helper for debug purposes */
static inline void
dump_ipc_test_ctx(const struct ipc_test_ctx * c)
{
	if (c == NULL)
		fprintf(stdout, "{NULL}\n");
	else
		fprintf(stdout, "{nclients=%d, run_mode=%d, index=%d, "
		        "shared_object=%d, fd=%d}\n",
		        c->nclients, c->run_mode, c->index,
		        c->shared_object, c->fd);
}

#define IPC_ADDR "mmlib-test-ipc-addr"
#define IPC_TMPFILE "ipc-test-tmp-file"

/* small helper to send a mm_ipc msg with a file descriptor in the metadatas */
static inline
ssize_t mm_ipc_build_send_msg(int fd, const void * data, size_t len, int sentfd)
{
	struct iovec vec = {.iov_len = len, .iov_base = (void*) data};
	struct mm_ipc_msg msg = {
		.iov = &vec,
		.num_iov = 1,
	};
	if (sentfd > 0) {
		msg.fds = &sentfd;
		msg.num_fds = 1;
	}

	return mm_ipc_sendmsg(fd, &msg);
}

/* small helper to receive a mm_ipc msg with a file descriptor in the metadatas */
static inline
ssize_t recv_msg_and_fd(int fd, void* data, size_t len, int* recvfd)
{
	struct iovec vec = {.iov_len = len, .iov_base = data};
	struct mm_ipc_msg msg = {
		.iov = &vec,
		.num_iov = 1,
		.fds = recvfd,
		.num_fds_max = 1,
	};

	return mm_ipc_recvmsg(fd, &msg);
}

/* Can open 1 OR 2 file descriptors.
 * Will return the one intended to be sent to the client */
static inline
int open_shared_object_of_type(const struct ipc_test_ctx * ctx,
                               int * rvfd)
{
	char filename[64];

	switch (ctx->shared_object) {
	case SHARED_FILE:
		sprintf(filename, "%s-%d", IPC_TMPFILE, ctx->index);
		*rvfd = mm_open(filename, O_CREAT|O_TRUNC|O_RDWR,
		                S_IWUSR|S_IRUSR);
		return *rvfd;

	case SHARED_MEM:
		*rvfd = mm_anon_shm();
		return *rvfd;

	case SHARED_PIPE:
		mm_pipe(rvfd);
		return rvfd[1];

	case SHARED_IPC:
		mm_ipc_connected_pair(rvfd);
		return rvfd[1];

	default:
		fprintf(stderr, "Test bug: no shared object type given");
		return -1;
	}
}

#endif /* IPC_API_TESTS_EXPORTED_H */