]> git.meshlink.io Git - meshlink/blob - test/echo-fork.c
33966da16645043e712c99ff9a22d98966e55048
[meshlink] / test / echo-fork.c
1 #define _GNU_SOURCE
2
3 #ifdef NDEBUG
4 #undef NDEBUG
5 #endif
6
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <assert.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15
16 #ifdef __linux__
17 #include <sys/prctl.h>
18 #endif
19
20 #include "meshlink.h"
21 #include "utils.h"
22
23 /*
24  * To run this test case, direct a large file to strd
25  */
26
27 static struct sync_flag a_started;
28 static struct sync_flag a_stopped;
29 static struct sync_flag b_responded;
30
31 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
32         (void)mesh;
33         (void)channel;
34         (void)data;
35         (void)len;
36
37         // One way only.
38 }
39
40 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
41         (void)mesh;
42         (void)channel;
43
44         if(!len) {
45                 set_sync_flag(&a_stopped, true);
46                 meshlink_channel_close(mesh, channel);
47                 return;
48         }
49
50         assert(write(1, data, len) == (ssize_t)len);
51 }
52
53 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
54         if(port != 7) {
55                 return false;
56         }
57
58         set_sync_flag(&a_started, true);
59
60         meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
61
62         if(data) {
63                 b_receive_cb(mesh, channel, data, len);
64         }
65
66         return true;
67 }
68
69 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
70         (void)len;
71
72         meshlink_set_channel_poll_cb(mesh, channel, NULL);
73         set_sync_flag(&b_responded, true);
74 }
75
76 static int main1(void) {
77         close(1);
78
79         meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.1", "a", "echo-fork", DEV_CLASS_BACKBONE);
80         assert(mesh);
81
82         assert(meshlink_start(mesh));
83
84         // Open a channel.
85
86         meshlink_node_t *b = meshlink_get_node(mesh, "b");
87         assert(b);
88
89         meshlink_channel_t *channel = meshlink_channel_open(mesh, b, 7, a_receive_cb, NULL, 0);
90         assert(channel);
91
92         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
93
94         // read and buffer stdin
95         int BUF_SIZE = 1024 * 1024;
96         char buffer[BUF_SIZE];
97
98         assert(wait_sync_flag(&b_responded, 20));
99
100         do {
101                 ssize_t len = read(0, buffer, BUF_SIZE);
102
103                 if(len <= 0) {
104                         break;
105                 }
106
107                 char *p = buffer;
108
109                 while(len > 0) {
110                         ssize_t sent = meshlink_channel_send(mesh, channel, p, len);
111
112                         if(sent < 0) {
113                                 fprintf(stderr, "Sending message failed\n");
114                                 return 1;
115                         }
116
117                         if(!sent) {
118                                 usleep(100000);
119                         } else {
120                                 len -= sent;
121                                 p += sent;
122                         }
123                 }
124         } while(true);
125
126         meshlink_channel_close(mesh, channel);
127
128         // Clean up.
129
130         meshlink_close(mesh);
131
132         return 0;
133 }
134
135
136 static int main2(void) {
137 #ifdef __linux__
138         prctl(PR_SET_PDEATHSIG, SIGTERM);
139 #endif
140
141         close(0);
142
143         // Start mesh and wait for incoming channels.
144
145         meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.2", "b", "echo-fork", DEV_CLASS_BACKBONE);
146         assert(mesh);
147
148         meshlink_set_channel_accept_cb(mesh, accept_cb);
149
150         assert(meshlink_start(mesh));
151
152         // Let it run until a disappears.
153
154         assert(wait_sync_flag(&a_started, 20));
155         assert(wait_sync_flag(&a_stopped, 1000000));
156
157         // Clean up.
158
159         meshlink_close(mesh);
160
161         return 0;
162 }
163
164
165 int main(void) {
166         init_sync_flag(&a_started);
167         init_sync_flag(&a_stopped);
168         init_sync_flag(&b_responded);
169
170         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
171
172         // Initialize and exchange configuration.
173
174         meshlink_handle_t *mesh_a, *mesh_b;
175
176         open_meshlink_pair(&mesh_a, &mesh_b, "echo-fork");
177         close_meshlink_pair(mesh_a, mesh_b);
178
179         if(!fork()) {
180                 return main2();
181         }
182
183         assert(main1() == 0);
184
185         int wstatus = 0;
186         assert(wait(&wstatus) != -1);
187         assert(WIFEXITED(wstatus));
188         assert(WEXITSTATUS(wstatus) == 0);
189 }