1 /*
2 * Copyright (c) 2014 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37
38 #define WL_HIDE_DEPRECATED
39
40 #include "test-runner.h"
41 #include "test-compositor.h"
42
43 /* --- Protocol --- */
44 struct test_compositor;
45
46 static const struct wl_message tc_requests[] = {
47 /* this request serves as a barrier for synchronizing*/
48 { "stop_display", "u", NULL }
49 };
50
51 static const struct wl_message tc_events[] = {
52 { "display_resumed", "", NULL }
53 };
54
55 const struct wl_interface test_compositor_interface = {
56 "test", 1,
57 1, tc_requests,
58 1, tc_events
59 };
60
61 struct test_compositor_interface {
62 void (*stop_display)(struct wl_client *client,
63 struct wl_resource *resource,
64 uint32_t num);
65 };
66
67 struct test_compositor_listener {
68 void (*display_resumed)(void *data, struct test_compositor *tc);
69
70 };
71
72 enum {
73 STOP_DISPLAY = 0
74 };
75
76 enum {
77 DISPLAY_RESUMED = 0
78 };
79
80 /* Since tests can run parallelly, we need unique socket names
81 * for each test, otherwise the test can fail on wl_display_add_socket. */
82 static const char *
get_socket_name(void)83 get_socket_name(void)
84 {
85 struct timeval tv;
86 static char retval[64];
87
88 gettimeofday(&tv, NULL);
89 snprintf(retval, sizeof retval, "wayland-test-%d-%ld%ld",
90 getpid(), tv.tv_sec, tv.tv_usec);
91
92 return retval;
93 }
94
95 static void
handle_client_destroy(void * data)96 handle_client_destroy(void *data)
97 {
98 struct client_info *ci = data;
99 struct display *d;
100 siginfo_t status;
101
102 d = ci->display;
103
104 assert(waitid(P_PID, ci->pid, &status, WEXITED) != -1);
105
106 switch (status.si_code) {
107 case CLD_KILLED:
108 case CLD_DUMPED:
109 fprintf(stderr, "Client '%s' was killed by signal %d\n",
110 ci->name, status.si_status);
111 ci->exit_code = status.si_status;
112 break;
113 case CLD_EXITED:
114 if (status.si_status != EXIT_SUCCESS)
115 fprintf(stderr, "Client '%s' exited with code %d\n",
116 ci->name, status.si_status);
117
118 ci->exit_code = status.si_status;
119 break;
120 }
121
122 ++d->clients_terminated_no;
123 if (d->clients_no == d->clients_terminated_no) {
124 wl_display_terminate(d->wl_display);
125 }
126
127 /* the clients are not removed from the list, because
128 * at the end of the test we check the exit codes of all
129 * clients. In the case that the test would go through
130 * the clients list manually, zero out the wl_client as a sign
131 * that the client is not running anymore */
132 }
133
134 /**
135 * Check client's state and terminate display when all clients exited
136 */
137 static void
client_destroyed(struct wl_listener * listener,void * data)138 client_destroyed(struct wl_listener *listener, void *data)
139 {
140 struct client_info *ci;
141 struct display *d;
142 struct wl_event_loop *loop;
143
144 /* Wait for client in an idle handler to avoid blocking the actual
145 * client destruction (fd close etc. */
146 ci = wl_container_of(listener, ci, destroy_listener);
147 d = ci->display;
148 loop = wl_display_get_event_loop(d->wl_display);
149 wl_event_loop_add_idle(loop, handle_client_destroy, ci);
150
151 ci->wl_client = NULL;
152 }
153
154 static void
run_client(void (* client_main)(void * data),void * data,int wayland_sock,int client_pipe)155 run_client(void (*client_main)(void *data), void *data,
156 int wayland_sock, int client_pipe)
157 {
158 char s[8];
159 int cur_fds;
160 int can_continue = 0;
161
162 /* Wait until display signals that client can continue */
163 assert(read(client_pipe, &can_continue, sizeof(int)) == sizeof(int));
164
165 if (can_continue == 0)
166 abort(); /* error in parent */
167
168 /* for wl_display_connect() */
169 snprintf(s, sizeof s, "%d", wayland_sock);
170 setenv("WAYLAND_SOCKET", s, 0);
171
172 cur_fds = count_open_fds();
173
174 client_main(data);
175
176 /* Clients using wl_display_connect() will end up closing the socket
177 * passed in through the WAYLAND_SOCKET environment variable. When
178 * doing this, it clears the environment variable, so if it's been
179 * unset, then we assume the client consumed the file descriptor and
180 * do not count it towards leak checking. */
181 if (!getenv("WAYLAND_SOCKET"))
182 cur_fds--;
183
184 check_fd_leaks(cur_fds);
185 }
186
187 static struct client_info *
display_create_client(struct display * d,void (* client_main)(void * data),void * data,const char * name)188 display_create_client(struct display *d,
189 void (*client_main)(void *data),
190 void *data,
191 const char *name)
192 {
193 int pipe_cli[2];
194 int sock_wayl[2];
195 pid_t pid;
196 int can_continue = 0;
197 struct client_info *cl;
198
199 assert(pipe(pipe_cli) == 0 && "Failed creating pipe");
200 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sock_wayl) == 0
201 && "Failed creating socket pair");
202
203 pid = fork();
204 assert(pid != -1 && "Fork failed");
205
206 if (pid == 0) {
207 close(sock_wayl[1]);
208 close(pipe_cli[1]);
209
210 run_client(client_main, data, sock_wayl[0], pipe_cli[0]);
211
212 close(sock_wayl[0]);
213 close(pipe_cli[0]);
214
215 exit(0);
216 }
217
218 close(sock_wayl[0]);
219 close(pipe_cli[0]);
220
221 cl = calloc(1, sizeof(struct client_info));
222 assert(cl && "Out of memory");
223
224 wl_list_insert(&d->clients, &cl->link);
225
226 cl->display = d;
227 cl->name = name;
228 cl->pid = pid;
229 cl->pipe = pipe_cli[1];
230 cl->destroy_listener.notify = &client_destroyed;
231
232 cl->wl_client = wl_client_create(d->wl_display, sock_wayl[1]);
233 if (!cl->wl_client) {
234 int ret;
235
236 /* abort the client */
237 ret = write(cl->pipe, &can_continue, sizeof(int));
238 assert(ret == sizeof(int) && "aborting the client failed");
239 assert(0 && "Couldn't create wayland client");
240 }
241
242 wl_client_add_destroy_listener(cl->wl_client,
243 &cl->destroy_listener);
244
245 ++d->clients_no;
246
247 return cl;
248 }
249
250 struct client_info *
client_create_with_name(struct display * d,void (* client_main)(void * data),void * data,const char * name)251 client_create_with_name(struct display *d,
252 void (*client_main)(void *data), void *data,
253 const char *name)
254 {
255 int can_continue = 1;
256 struct client_info *cl = display_create_client(d,
257 client_main, data,
258 name);
259
260 /* let the show begin! */
261 assert(write(cl->pipe, &can_continue, sizeof(int)) == sizeof(int));
262
263 return cl;
264 }
265
266 /* wfr = waiting for resume */
267 struct wfr {
268 struct wl_resource *resource;
269 struct wl_list link;
270 };
271
272 static void
handle_stop_display(struct wl_client * client,struct wl_resource * resource,uint32_t num)273 handle_stop_display(struct wl_client *client,
274 struct wl_resource *resource, uint32_t num)
275 {
276 struct display *d = wl_resource_get_user_data(resource);
277 struct wfr *wfr;
278
279 assert(d->wfr_num < num
280 && "test error: Too many clients sent stop_display request");
281
282 ++d->wfr_num;
283
284 wfr = malloc(sizeof *wfr);
285 if (!wfr) {
286 wl_client_post_no_memory(client);
287 assert(0 && "Out of memory");
288 }
289
290 wfr->resource = resource;
291 wl_list_insert(&d->waiting_for_resume, &wfr->link);
292
293 if (d->wfr_num == num)
294 wl_display_terminate(d->wl_display);
295 }
296
297 static const struct test_compositor_interface tc_implementation = {
298 handle_stop_display
299 };
300
301 static void
tc_bind(struct wl_client * client,void * data,uint32_t ver,uint32_t id)302 tc_bind(struct wl_client *client, void *data,
303 uint32_t ver, uint32_t id)
304 {
305 struct wl_resource *res;
306
307 res = wl_resource_create(client, &test_compositor_interface, ver, id);
308 if (!res) {
309 wl_client_post_no_memory(client);
310 assert(0 && "Out of memory");
311 }
312
313 wl_resource_set_implementation(res, &tc_implementation, data, NULL);
314 }
315
316 struct display *
display_create(void)317 display_create(void)
318 {
319 struct display *d = NULL;
320 struct wl_global *g;
321 const char *socket_name;
322 int stat = 0;
323
324 d = calloc(1, sizeof *d);
325 assert(d && "Out of memory");
326
327 d->wl_display = wl_display_create();
328 assert(d->wl_display && "Creating display failed");
329
330 /* hope the path won't be longer than 108 ... */
331 socket_name = get_socket_name();
332 stat = wl_display_add_socket(d->wl_display, socket_name);
333 assert(stat == 0 && "Failed adding socket");
334
335 wl_list_init(&d->clients);
336 d->clients_no = d->clients_terminated_no = 0;
337
338 wl_list_init(&d->waiting_for_resume);
339 d->wfr_num = 0;
340
341 g = wl_global_create(d->wl_display, &test_compositor_interface,
342 1, d, tc_bind);
343 assert(g && "Creating test global failed");
344
345 return d;
346 }
347
348 void
display_run(struct display * d)349 display_run(struct display *d)
350 {
351 assert(d->wfr_num == 0
352 && "test error: Have waiting clients. Use display_resume.");
353 wl_display_run(d->wl_display);
354 }
355
356 void
display_resume(struct display * d)357 display_resume(struct display *d)
358 {
359 struct wfr *wfr, *next;
360
361 assert(d->wfr_num > 0 && "test error: No clients waiting.");
362
363 wl_list_for_each_safe(wfr, next, &d->waiting_for_resume, link) {
364 wl_resource_post_event(wfr->resource, DISPLAY_RESUMED);
365 wl_list_remove(&wfr->link);
366 free(wfr);
367 }
368
369 assert(wl_list_empty(&d->waiting_for_resume));
370 d->wfr_num = 0;
371
372 wl_display_run(d->wl_display);
373 }
374
375 void
display_destroy(struct display * d)376 display_destroy(struct display *d)
377 {
378 struct client_info *cl, *next;
379 int failed = 0;
380
381 assert(d->wfr_num == 0
382 && "test error: Didn't you forget to call display_resume?");
383
384 wl_list_for_each_safe(cl, next, &d->clients, link) {
385 assert(cl->wl_client == NULL);
386
387 if (cl->exit_code != 0) {
388 ++failed;
389 fprintf(stderr, "Client '%s' failed\n", cl->name);
390 }
391
392 close(cl->pipe);
393 free(cl);
394 }
395
396 wl_display_destroy(d->wl_display);
397 free(d);
398
399 if (failed) {
400 fprintf(stderr, "%d child(ren) failed\n", failed);
401 abort();
402 }
403 }
404
405 /*
406 * --- Client helper functions ---
407 */
408 static void
handle_display_resumed(void * data,struct test_compositor * tc)409 handle_display_resumed(void *data, struct test_compositor *tc)
410 {
411 struct client *c = data;
412
413 c->display_stopped = 0;
414 }
415
416 static const struct test_compositor_listener tc_listener = {
417 handle_display_resumed
418 };
419
420 static void
registry_handle_globals(void * data,struct wl_registry * registry,uint32_t id,const char * intf,uint32_t ver)421 registry_handle_globals(void *data, struct wl_registry *registry,
422 uint32_t id, const char *intf, uint32_t ver)
423 {
424 struct client *c = data;
425
426 if (strcmp(intf, "test") != 0)
427 return;
428
429 c->tc = wl_registry_bind(registry, id, &test_compositor_interface, ver);
430 assert(c->tc && "Failed binding to registry");
431
432 wl_proxy_add_listener((struct wl_proxy *) c->tc,
433 (void *) &tc_listener, c);
434 }
435
436 static const struct wl_registry_listener registry_listener =
437 {
438 registry_handle_globals,
439 NULL
440 };
441
client_connect()442 struct client *client_connect()
443 {
444 struct wl_registry *reg;
445 struct client *c = calloc(1, sizeof *c);
446 assert(c && "Out of memory");
447
448 c->wl_display = wl_display_connect(NULL);
449 assert(c->wl_display && "Failed connecting to display");
450
451 /* create test_compositor proxy. Do it with temporary
452 * registry so that client can define it's own listener later */
453 reg = wl_display_get_registry(c->wl_display);
454 assert(reg);
455 wl_registry_add_listener(reg, ®istry_listener, c);
456 wl_display_roundtrip(c->wl_display);
457 assert(c->tc);
458
459 wl_registry_destroy(reg);
460
461 return c;
462 }
463
464 static void
check_error(struct wl_display * display)465 check_error(struct wl_display *display)
466 {
467 uint32_t ec, id;
468 const struct wl_interface *intf;
469 int err;
470
471 err = wl_display_get_error(display);
472 /* write out message about protocol error */
473 if (err == EPROTO) {
474 ec = wl_display_get_protocol_error(display, &intf, &id);
475 fprintf(stderr, "Client: Got protocol error %u on interface %s"
476 " (object %u)\n", ec, intf->name, id);
477 }
478
479 if (err) {
480 fprintf(stderr, "Client error: %s\n", strerror(err));
481 abort();
482 }
483 }
484
485 void
client_disconnect(struct client * c)486 client_disconnect(struct client *c)
487 {
488 /* check for errors */
489 check_error(c->wl_display);
490
491 wl_proxy_destroy((struct wl_proxy *) c->tc);
492 wl_display_disconnect(c->wl_display);
493 free(c);
494 }
495
496 /* num is number of clients that requests to stop display.
497 * Display is stopped after it receives num STOP_DISPLAY requests */
498 int
stop_display(struct client * c,int num)499 stop_display(struct client *c, int num)
500 {
501 int n = 0;
502
503 c->display_stopped = 1;
504 wl_proxy_marshal((struct wl_proxy *) c->tc, STOP_DISPLAY, num);
505
506 while (c->display_stopped && n >= 0) {
507 n = wl_display_dispatch(c->wl_display);
508 }
509
510 return n;
511 }
512