13 #include <sys/types.h>
17 #include <sys/prctl.h>
24 * To run this test case, direct a large file to strd
27 static struct sync_flag a_started;
28 static struct sync_flag a_stopped;
29 static struct sync_flag b_responded;
31 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
40 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
45 set_sync_flag(&a_stopped, true);
46 meshlink_channel_close(mesh, channel);
50 assert(write(1, data, len) == (ssize_t)len);
53 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
58 set_sync_flag(&a_started, true);
60 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
63 b_receive_cb(mesh, channel, data, len);
69 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
72 meshlink_set_channel_poll_cb(mesh, channel, NULL);
73 set_sync_flag(&b_responded, true);
76 static int main1(void) {
79 meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.1", "a", "echo-fork", DEV_CLASS_BACKBONE);
82 assert(meshlink_start(mesh));
86 meshlink_node_t *b = meshlink_get_node(mesh, "b");
89 meshlink_channel_t *channel = meshlink_channel_open(mesh, b, 7, a_receive_cb, NULL, 0);
92 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
94 // read and buffer stdin
95 int BUF_SIZE = 1024 * 1024;
96 char buffer[BUF_SIZE];
98 assert(wait_sync_flag(&b_responded, 20));
101 ssize_t len = read(0, buffer, BUF_SIZE);
110 ssize_t sent = meshlink_channel_send(mesh, channel, p, len);
113 fprintf(stderr, "Sending message failed\n");
126 meshlink_channel_close(mesh, channel);
130 meshlink_close(mesh);
136 static int main2(void) {
138 prctl(PR_SET_PDEATHSIG, SIGTERM);
143 // Start mesh and wait for incoming channels.
145 meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.2", "b", "echo-fork", DEV_CLASS_BACKBONE);
148 meshlink_set_channel_accept_cb(mesh, accept_cb);
150 assert(meshlink_start(mesh));
152 // Let it run until a disappears.
154 assert(wait_sync_flag(&a_started, 20));
155 assert(wait_sync_flag(&a_stopped, 1000000));
159 meshlink_close(mesh);
166 init_sync_flag(&a_started);
167 init_sync_flag(&a_stopped);
168 init_sync_flag(&b_responded);
170 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
172 // Initialize and exchange configuration.
174 meshlink_handle_t *mesh_a, *mesh_b;
176 open_meshlink_pair(&mesh_a, &mesh_b, "echo-fork");
177 close_meshlink_pair(mesh_a, mesh_b);
183 assert(main1() == 0);
186 assert(wait(&wstatus) != -1);
187 assert(WIFEXITED(wstatus));
188 assert(WEXITSTATUS(wstatus) == 0);