summaryrefslogtreecommitdiffstats
path: root/source3/lib/unix_msg/test_drain.c
blob: 675ac6f94e51b4cbc0887b0eb1f3c3e4b5eac668 (plain)
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
#include "replace.h"
#include "unix_msg.h"
#include "poll_funcs/poll_funcs_tevent.h"
#include "tevent.h"
#include "system/select.h"

struct cb_state {
	unsigned num_received;
	uint8_t *buf;
	size_t buflen;
};

static void recv_cb(struct unix_msg_ctx *ctx,
		    uint8_t *msg, size_t msg_len,
		    int *fds, size_t num_fds,
		    void *private_data);

int main(int argc, const char *argv[])
{
	struct poll_funcs *funcs;
	void *handle;
	struct sockaddr_un addr;
	struct unix_msg_ctx *ctx;
	struct tevent_context *ev;
	int ret;

	struct cb_state state;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <sockname>\n", argv[0]);
		return 1;
	}

	addr = (struct sockaddr_un) { .sun_family = AF_UNIX };
	strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
	unlink(addr.sun_path);

	ev = tevent_context_init(NULL);
	if (ev == NULL) {
		perror("tevent_context_init failed");
		return 1;
	}
	funcs = poll_funcs_init_tevent(ev);
	if (funcs == NULL) {
		fprintf(stderr, "poll_funcs_init_tevent failed\n");
		return 1;
	}

	handle = poll_funcs_tevent_register(ev, funcs, ev);
	if (handle == NULL) {
		fprintf(stderr, "poll_funcs_tevent_register failed\n");
		exit(1);
	}

	ret = unix_msg_init(&addr, funcs, 256, recv_cb, &state, &ctx);
	if (ret != 0) {
		fprintf(stderr, "unix_msg_init failed: %s\n",
			strerror(ret));
		return 1;
	}

	while (1) {
		ret = tevent_loop_once(ev);
		if (ret == -1) {
			fprintf(stderr, "tevent_loop_once failed: %s\n",
				strerror(errno));
			exit(1);
		}
	}
	return 0;
}

static void recv_cb(struct unix_msg_ctx *ctx,
		    uint8_t *msg, size_t msg_len,
		    int *fds, size_t num_fds,
		    void *private_data)
{
	unsigned num;
	if (msg_len == sizeof(num)) {
		memcpy(&num, msg, msg_len);
		printf("%u\n", num);
	}
}