unix_msg: Lift sockaddr_un handling from unix_msg_send
[Samba.git] / source3 / lib / unix_msg / test_source.c
blob7f6a7a5e8c07cb56c395803201d0887922b26d71
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;
16 struct sockaddr_un dst;
18 if (argc < 2) {
19 fprintf(stderr, "Usage: %s <sockname> [num_contexts]\n", argv[0]);
20 return 1;
22 if (argc > 2) {
23 num_ctxs = atoi(argv[2]);
26 ev = tevent_context_init(NULL);
27 if (ev == NULL) {
28 perror("tevent_context_init failed");
29 return 1;
31 funcs = poll_funcs_init_tevent(NULL);
32 if (funcs == NULL) {
33 fprintf(stderr, "poll_funcs_init_tevent failed\n");
34 return 1;
36 tevent_handle = poll_funcs_tevent_register(NULL, funcs, ev);
37 if (tevent_handle == NULL) {
38 fprintf(stderr, "poll_funcs_tevent_register failed\n");
39 return 1;
42 ctxs = talloc_array(ev, struct unix_msg_ctx *, num_ctxs);
43 if (ctxs == NULL) {
44 fprintf(stderr, "talloc failed\n");
45 return 1;
48 for (i=0; i<num_ctxs; i++) {
49 ret = unix_msg_init(NULL, funcs, 256, 1, NULL, NULL,
50 &ctxs[i]);
51 if (ret != 0) {
52 fprintf(stderr, "unix_msg_init failed: %s\n",
53 strerror(ret));
54 return 1;
58 iov.iov_base = &i;
59 iov.iov_len = sizeof(i);
61 dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
62 strlcpy(dst.sun_path, argv[1], sizeof(dst.sun_path));
64 for (i=0; i<num_ctxs; i++) {
65 unsigned j;
67 for (j=0; j<100000; j++) {
68 ret = unix_msg_send(ctxs[i], &dst, &iov, 1);
69 if (ret != 0) {
70 fprintf(stderr, "unix_msg_send failed: %s\n",
71 strerror(ret));
72 return 1;
77 while (true) {
78 ret = tevent_loop_once(ev);
79 if (ret == -1) {
80 fprintf(stderr, "tevent_loop_once failed: %s\n",
81 strerror(errno));
82 exit(1);
86 for (i=0; i<num_ctxs; i++) {
87 unix_msg_free(ctxs[i]);
90 talloc_free(ev);
92 return 0;