lib: Enhance poll_funcs_tevent for multiple tevent_contexts
[Samba.git] / source3 / lib / unix_msg / test_source.c
blob94984d88523ba31bc9679fac85eb6d248ac76282
1 #include "replace.h"
2 #include "unix_msg.h"
3 #include "poll_funcs/poll_funcs_tevent.h"
4 #include "tevent.h"
6 int main(int argc, const char *argv[])
8 struct poll_funcs *funcs;
9 void *tevent_handle;
10 struct unix_msg_ctx **ctxs;
11 struct tevent_context *ev;
12 struct iovec iov;
13 int ret;
14 unsigned i;
15 unsigned num_ctxs = 1;
17 if (argc < 2) {
18 fprintf(stderr, "Usage: %s <sockname> [num_contexts]\n", argv[0]);
19 return 1;
21 if (argc > 2) {
22 num_ctxs = atoi(argv[2]);
25 ev = tevent_context_init(NULL);
26 if (ev == NULL) {
27 perror("tevent_context_init failed");
28 return 1;
30 funcs = poll_funcs_init_tevent(NULL);
31 if (funcs == NULL) {
32 fprintf(stderr, "poll_funcs_init_tevent failed\n");
33 return 1;
35 tevent_handle = poll_funcs_tevent_register(NULL, funcs, ev);
36 if (tevent_handle == NULL) {
37 fprintf(stderr, "poll_funcs_tevent_register failed\n");
38 return 1;
41 ctxs = talloc_array(ev, struct unix_msg_ctx *, num_ctxs);
42 if (ctxs == NULL) {
43 fprintf(stderr, "talloc failed\n");
44 return 1;
47 for (i=0; i<num_ctxs; i++) {
48 ret = unix_msg_init(NULL, funcs, 256, 1, NULL, NULL,
49 &ctxs[i]);
50 if (ret != 0) {
51 fprintf(stderr, "unix_msg_init failed: %s\n",
52 strerror(ret));
53 return 1;
57 iov.iov_base = &i;
58 iov.iov_len = sizeof(i);
60 for (i=0; i<num_ctxs; i++) {
61 unsigned j;
63 for (j=0; j<100000; j++) {
64 ret = unix_msg_send(ctxs[i], argv[1], &iov, 1);
65 if (ret != 0) {
66 fprintf(stderr, "unix_msg_send failed: %s\n",
67 strerror(ret));
68 return 1;
73 while (true) {
74 ret = tevent_loop_once(ev);
75 if (ret == -1) {
76 fprintf(stderr, "tevent_loop_once failed: %s\n",
77 strerror(errno));
78 exit(1);
82 for (i=0; i<num_ctxs; i++) {
83 unix_msg_free(ctxs[i]);
86 talloc_free(ev);
88 return 0;