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 reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
63 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
68 set_sync_flag(&a_started, true);
70 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
73 b_receive_cb(mesh, channel, data, len);
79 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
82 meshlink_set_channel_poll_cb(mesh, channel, NULL);
83 set_sync_flag(&b_responded, true);
86 static int main1(void) {
89 meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.1", "a", "echo-fork", DEV_CLASS_BACKBONE);
92 meshlink_set_channel_accept_cb(mesh, reject_cb);
94 assert(meshlink_start(mesh));
98 meshlink_node_t *b = meshlink_get_node(mesh, "b");
101 meshlink_channel_t *channel = meshlink_channel_open(mesh, b, 7, a_receive_cb, NULL, 0);
104 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
106 // read and buffer stdin
107 int BUF_SIZE = 1024 * 1024;
108 char buffer[BUF_SIZE];
110 assert(wait_sync_flag(&b_responded, 20));
113 ssize_t len = read(0, buffer, BUF_SIZE);
122 ssize_t sent = meshlink_channel_send(mesh, channel, p, len);
125 fprintf(stderr, "Sending message failed\n");
138 meshlink_channel_close(mesh, channel);
142 meshlink_close(mesh);
148 static int main2(void) {
150 prctl(PR_SET_PDEATHSIG, SIGTERM);
155 // Start mesh and wait for incoming channels.
157 meshlink_handle_t *mesh = meshlink_open("echo-fork_conf.2", "b", "echo-fork", DEV_CLASS_BACKBONE);
160 meshlink_set_channel_accept_cb(mesh, accept_cb);
162 assert(meshlink_start(mesh));
164 // Let it run until a disappears.
166 assert(wait_sync_flag(&a_started, 20));
167 assert(wait_sync_flag(&a_stopped, 1000000));
171 meshlink_close(mesh);
178 init_sync_flag(&a_started);
179 init_sync_flag(&a_stopped);
180 init_sync_flag(&b_responded);
182 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
184 // Initialize and exchange configuration.
186 meshlink_handle_t *mesh_a, *mesh_b;
188 open_meshlink_pair(&mesh_a, &mesh_b, "echo-fork");
189 close_meshlink_pair(mesh_a, mesh_b);
195 assert(main1() == 0);
198 assert(wait(&wstatus) != -1);
199 assert(WIFEXITED(wstatus));
200 assert(WEXITSTATUS(wstatus) == 0);