12 #include <sys/prctl.h>
19 * To run this test case, direct a large file to strd
22 static struct sync_flag a_started;
23 static struct sync_flag a_stopped;
24 static struct sync_flag b_responded;
26 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
35 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
40 set_sync_flag(&a_stopped, true);
41 meshlink_channel_close(mesh, channel);
45 assert(write(1, data, len) == (ssize_t)len);
48 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
58 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
63 set_sync_flag(&a_started, true);
65 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
68 b_receive_cb(mesh, channel, data, len);
74 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
77 meshlink_set_channel_poll_cb(mesh, channel, NULL);
78 set_sync_flag(&b_responded, true);
81 static int main1(void) {
84 meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.1", "a", "echo-fork", DEV_CLASS_BACKBONE);
87 meshlink_set_channel_accept_cb(mesh, reject_cb);
89 assert(meshlink_start(mesh));
93 meshlink_node_t *b = meshlink_get_node(mesh, "b");
96 meshlink_channel_t *channel = meshlink_channel_open(mesh, b, 7, a_receive_cb, NULL, 0);
99 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
101 // read and buffer stdin
102 int BUF_SIZE = 1024 * 1024;
103 char buffer[BUF_SIZE];
105 assert(wait_sync_flag(&b_responded, 20));
108 ssize_t len = read(0, buffer, BUF_SIZE);
117 ssize_t sent = meshlink_channel_send(mesh, channel, p, len);
120 fprintf(stderr, "Sending message failed\n");
133 meshlink_channel_close(mesh, channel);
137 meshlink_close(mesh);
143 static int main2(void) {
145 prctl(PR_SET_PDEATHSIG, SIGTERM);
150 // Start mesh and wait for incoming channels.
152 meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.2", "b", "echo-fork", DEV_CLASS_BACKBONE);
155 meshlink_set_channel_accept_cb(mesh, accept_cb);
157 assert(meshlink_start(mesh));
159 // Let it run until a disappears.
161 assert(wait_sync_flag(&a_started, 20));
162 assert(wait_sync_flag(&a_stopped, 1000000));
166 meshlink_close(mesh);
173 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
175 // Initialize and exchange configuration.
177 meshlink_handle_t *mesh_a, *mesh_b;
179 open_meshlink_pair(&mesh_a, &mesh_b, "echo-fork");
180 close_meshlink_pair(mesh_a, mesh_b);
186 assert(main1() == 0);
189 assert(wait(&wstatus) != -1);
190 assert(WIFEXITED(wstatus));
191 assert(WEXITSTATUS(wstatus) == 0);