summaryrefslogtreecommitdiffstats
path: root/source3/lib/unix_msg/test_source.c
blob: bfafee1fd33b193a9b204aa5f2b3da27af164299 (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
#include "replace.h"
#include "unix_msg.h"
#include "poll_funcs/poll_funcs_tevent.h"
#include "tevent.h"

int main(int argc, const char *argv[])
{
	struct poll_funcs funcs;
	struct unix_msg_ctx **ctxs;
	struct tevent_context *ev;
	struct iovec iov;
	int ret;
	unsigned i;
	unsigned num_ctxs = 1;

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

	ev = tevent_context_init(NULL);
	if (ev == NULL) {
		perror("tevent_context_init failed");
		return 1;
	}
	poll_funcs_init_tevent(&funcs, ev);

	ctxs = talloc_array(ev, struct unix_msg_ctx *, num_ctxs);
	if (ctxs == NULL) {
		fprintf(stderr, "talloc failed\n");
		return 1;
	}

	for (i=0; i<num_ctxs; i++) {
		ret = unix_msg_init(NULL, &funcs, 256, 1, NULL, NULL,
				    &ctxs[i]);
		if (ret != 0) {
			fprintf(stderr, "unix_msg_init failed: %s\n",
				strerror(ret));
			return 1;
		}
	}

	iov.iov_base = &i;
	iov.iov_len = sizeof(i);

	for (i=0; i<num_ctxs; i++) {
		unsigned j;

		for (j=0; j<100000; j++) {
			ret = unix_msg_send(ctxs[i], argv[1], &iov, 1);
			if (ret != 0) {
				fprintf(stderr, "unix_msg_send failed: %s\n",
					strerror(ret));
				return 1;
			}
		}
	}

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

	for (i=0; i<num_ctxs; i++) {
		unix_msg_free(ctxs[i]);
	}

	talloc_free(ev);

	return 0;
}