3 #include "poll_funcs/poll_funcs_tevent.h"
12 static void recv_cb(struct unix_msg_ctx *ctx,
13 uint8_t *msg, size_t msg_len,
16 static void expect_messages(struct tevent_context *ev, struct cb_state *state,
19 state->num_received = 0;
21 while (state->num_received < num_msgs) {
24 ret = tevent_loop_once(ev);
26 fprintf(stderr, "tevent_loop_once failed: %s\n",
35 struct poll_funcs funcs;
36 const char *sock1 = "sock1";
37 const char *sock2 = "sock2";
38 struct unix_msg_ctx *ctx1, *ctx2;
39 struct tevent_context *ev;
43 static uint8_t buf[1755];
45 struct cb_state state;
50 ev = tevent_context_init(NULL);
52 perror("tevent_context_init failed");
55 poll_funcs_init_tevent(&funcs, ev);
57 ret = unix_msg_init(sock1, &funcs, 256, 1,
58 recv_cb, &state, &ctx1);
60 fprintf(stderr, "unix_msg_init failed: %s\n",
65 ret = unix_msg_init(sock1, &funcs, 256, 1,
66 recv_cb, &state, &ctx1);
68 fprintf(stderr, "unix_msg_init succeeded unexpectedly\n");
71 if (ret != EADDRINUSE) {
72 fprintf(stderr, "unix_msg_init returned %s, expected "
73 "EADDRINUSE\n", strerror(ret));
77 ret = unix_msg_init(sock2, &funcs, 256, 1,
78 recv_cb, &state, &ctx2);
80 fprintf(stderr, "unix_msg_init failed: %s\n",
85 printf("sending a 0-length message\n");
90 ret = unix_msg_send(ctx1, sock2, NULL, 0);
92 fprintf(stderr, "unix_msg_send failed: %s\n",
97 expect_messages(ev, &state, 1);
99 printf("sending a small message\n");
103 iov.iov_len = sizeof(msg);
105 state.buflen = sizeof(msg);
107 ret = unix_msg_send(ctx1, sock2, &iov, 1);
109 fprintf(stderr, "unix_msg_send failed: %s\n",
114 expect_messages(ev, &state, 1);
116 printf("sending six large, interleaved messages\n");
118 for (i=0; i<sizeof(buf); i++) {
123 iov.iov_len = sizeof(buf);
125 state.buflen = sizeof(buf);
127 for (i=0; i<3; i++) {
128 ret = unix_msg_send(ctx1, sock2, &iov, 1);
130 fprintf(stderr, "unix_msg_send failed: %s\n",
134 ret = unix_msg_send(ctx2, sock2, &iov, 1);
136 fprintf(stderr, "unix_msg_send failed: %s\n",
142 expect_messages(ev, &state, 6);
144 printf("sending a few messages in small pieces\n");
146 for (i = 0; i<5; i++) {
147 struct iovec iovs[20];
148 const size_t num_iovs = ARRAY_SIZE(iovs);
152 for (j=0; j<num_iovs-1; j++) {
153 size_t chunk = (random() % ((sizeof(buf) * 2) / num_iovs));
154 size_t space = (sizeof(buf) - (p - buf));
160 chunk = MIN(chunk, space);
162 iovs[j].iov_base = p;
163 iovs[j].iov_len = chunk;
167 if (p < (buf + sizeof(buf))) {
168 iovs[j].iov_base = p;
169 iovs[j].iov_len = (sizeof(buf) - (p - buf));
173 ret = unix_msg_send(ctx1, sock1, iovs, j);
175 fprintf(stderr, "unix_msg_send failed: %s\n",
181 expect_messages(ev, &state, 5);
183 printf("Filling send queues before freeing\n");
185 for (i=0; i<5; i++) {
186 ret = unix_msg_send(ctx1, sock2, &iov, 1);
188 fprintf(stderr, "unix_msg_send failed: %s\n",
192 ret = unix_msg_send(ctx1, sock1, &iov, 1);
194 fprintf(stderr, "unix_msg_send failed: %s\n",
200 expect_messages(ev, &state, 1); /* Read just one msg */
209 static void recv_cb(struct unix_msg_ctx *ctx,
210 uint8_t *msg, size_t msg_len,
213 struct cb_state *state = (struct cb_state *)private_data;
215 if (msg_len != state->buflen) {
216 fprintf(stderr, "expected %u bytes, got %u\n",
217 (unsigned)state->buflen, (unsigned)msg_len);
220 if ((msg_len != 0) && (memcmp(msg, state->buf, msg_len) != 0)) {
221 fprintf(stderr, "message content differs\n");
224 state->num_received += 1;