lib: Add unix_msg
[Samba.git] / source3 / lib / unix_msg / test_drain.c
blob6fe8c188367912de5a6396091c3d41fe6b80c304
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 poll_funcs_init_tevent(&funcs, ev);
42 ret = unix_msg_init(sock, &funcs, 256, 1,
43 recv_cb, &state, &ctx);
44 if (ret != 0) {
45 fprintf(stderr, "unix_msg_init failed: %s\n",
46 strerror(ret));
47 return 1;
50 while (1) {
51 ret = tevent_loop_once(ev);
52 if (ret == -1) {
53 fprintf(stderr, "tevent_loop_once failed: %s\n",
54 strerror(errno));
55 exit(1);
58 return 0;
61 static void recv_cb(struct unix_msg_ctx *ctx,
62 uint8_t *msg, size_t msg_len,
63 void *private_data)
65 unsigned num;
66 if (msg_len == sizeof(num)) {
67 memcpy(&num, msg, msg_len);
68 printf("%u\n", num);