lib: Enhance poll_funcs_tevent for multiple tevent_contexts
[Samba.git] / source3 / lib / unix_msg / test_drain.c
blobc2568b6646b2632e829dce3205c35e12c2a625d6
1 #include "replace.h"
2 #include "unix_msg.h"
3 #include "poll_funcs/poll_funcs_tevent.h"
4 #include "tevent.h"
5 #include "system/select.h"
7 struct cb_state {
8 unsigned num_received;
9 uint8_t *buf;
10 size_t buflen;
13 static void recv_cb(struct unix_msg_ctx *ctx,
14 uint8_t *msg, size_t msg_len,
15 void *private_data);
17 int main(int argc, const char *argv[])
19 struct poll_funcs *funcs;
20 const char *sock;
21 struct unix_msg_ctx *ctx;
22 struct tevent_context *ev;
23 int ret;
25 struct cb_state state;
27 if (argc != 2) {
28 fprintf(stderr, "Usage: %s <sockname>\n", argv[0]);
29 return 1;
32 sock = argv[1];
33 unlink(sock);
35 ev = tevent_context_init(NULL);
36 if (ev == NULL) {
37 perror("tevent_context_init failed");
38 return 1;
40 funcs = poll_funcs_init_tevent(ev);
41 if (funcs == NULL) {
42 fprintf(stderr, "poll_funcs_init_tevent failed\n");
43 return 1;
46 ret = unix_msg_init(sock, funcs, 256, 1, recv_cb, &state, &ctx);
47 if (ret != 0) {
48 fprintf(stderr, "unix_msg_init failed: %s\n",
49 strerror(ret));
50 return 1;
53 while (1) {
54 ret = tevent_loop_once(ev);
55 if (ret == -1) {
56 fprintf(stderr, "tevent_loop_once failed: %s\n",
57 strerror(errno));
58 exit(1);
61 return 0;
64 static void recv_cb(struct unix_msg_ctx *ctx,
65 uint8_t *msg, size_t msg_len,
66 void *private_data)
68 unsigned num;
69 if (msg_len == sizeof(num)) {
70 memcpy(&num, msg, msg_len);
71 printf("%u\n", num);