Debian Codegraph @

test/xvfb-wrapper.c

raw
1 /*
2 * Copyright © 2014 Ran Benita <[email protected]>
3 * Copyright © 2023 Pierre Le Marre <[email protected]>
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <signal.h>
11 #include <spawn.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16
17 #include "test.h"
18 #include "xvfb-wrapper.h"
19
20 static volatile bool xvfb_is_ready;
21
22 static void
23 sigusr1_handler(int signal)
24 {
25 xvfb_is_ready = true;
26 }
27
28 int
29 xvfb_wrapper(x11_test_func_t test_func, void *private)
30 {
31 int ret = EXIT_SUCCESS;
32 FILE * display_fd;
33 char display_fd_string[32];
34 sigset_t mask;
35 struct sigaction sa;
36 char *xvfb_argv[] = {
37 (char *) "Xvfb", (char *) "-displayfd", display_fd_string, NULL
38 };
39 char *envp[] = { NULL };
40 pid_t xvfb_pid = 0;
41 size_t counter = 0;
42 char display[32] = ":";
43 size_t length;
44
45 /* File descriptor to retrieve the display number */
46 display_fd = tmpfile();
47 if (display_fd == NULL){
48 fprintf(stderr, "Unable to create temporary file.\n");
49 goto err_display_fd;
50 }
51 snprintf(display_fd_string, sizeof(display_fd_string), "%d", fileno(display_fd));
52
53 /* Set SIGUSR1 to SIG_IGN so Xvfb will send us that signal when it's ready
54 * to accept connections. In order to avoid race condition, we block the
55 * until we are ready to process it. */
56 sigemptyset(&mask);
57 sigaddset(&mask, SIGUSR1);
58 sigprocmask(SIG_BLOCK, &mask, NULL);
59 sa.sa_handler = sigusr1_handler;
60 sa.sa_flags = SA_RESTART;
61 sigemptyset(&sa.sa_mask);
62 struct sigaction sa_old;
63 sigaction(SIGUSR1, &sa, &sa_old);
64
65 xvfb_is_ready = false;
66
67 /*
68 * Xvfb command: let the server find an available display.
69 *
70 * Note that it may generate multiple times the following output in stderr:
71 * _XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
72 * It is expected: this is the server trying the ports until it finds one
73 * that works.
74 */
75 ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
76 if (ret != 0) {
77 fprintf(stderr,
78 "[ERROR] Cannot run Xvfb. posix_spawnp error %d: %s\n",
79 ret, strerror(ret));
80 if (ret == ENOENT) {
81 fprintf(stderr,
82 "[ERROR] Xvfb may be missing. "
83 "Please install the corresponding package, "
84 "e.g. \"xvfb\" or \"xorg-x11-server-Xvfb\".\n");
85 }
86 ret = TEST_SETUP_FAILURE;
87 goto err_xvfd;
88 }
89
90 sigprocmask(SIG_UNBLOCK, &mask, NULL);
91
92 /* Now wait for the SIGUSR1 signal that Xvfb is ready */
93 while (!xvfb_is_ready) {
94 usleep(1000);
95 if (++counter >= 3000) /* 3 seconds max wait */
96 break;
97 }
98
99 sigaction(SIGUSR1, &sa_old, NULL);
100
101 /* Check if Xvfb is still alive */
102 pid_t pid = waitpid(xvfb_pid, NULL, WNOHANG);
103 if (pid != 0) {
104 fprintf(stderr, "ERROR: Xvfb not alive\n");
105 ret = TEST_SETUP_FAILURE;
106 goto err_xvfd;
107 }
108
109 /* Retrieve the display number: Xvfd writes the display number as a newline-
110 * terminated string; copy this number to form a proper display string. */
111 fseek(display_fd, 0, SEEK_SET);
112 length = fread(&display[1], 1, sizeof(display) - 2, display_fd);
113 if (length <= 0) {
114 fprintf(stderr, "fread error: length=%zu\n", length);
115 ret = TEST_SETUP_FAILURE;
116 goto err_xvfd;
117 } else {
118 /* Drop the newline character */
119 display[length] = '\0';
120 }
121
122 /* Run the function requiring a running X server.
123 * Because it may call abort() via assert(), we fork to be able to
124 * exit gracefully and not hang waiting for Xvfb. */
125 pid_t test_pid = fork();
126 switch (test_pid) {
127 case -1:
128 perror("fork");
129 ret = TEST_SETUP_FAILURE;
130 break;
131 case 0:
132 fprintf(stderr, "Running test using Xvfb wrapper...\n");
133 ret = test_func(display, private);
134 fprintf(stderr,
135 "Test using Xvfb wrapper finished with code %d.\n", ret);
136 _exit(ret);
137 default:
138 {
139 int test_status = 0;
140 pid_t test_pid2 = waitpid(test_pid, &test_status, 0);
141 ret = (test_pid2 > 0 && WIFEXITED(test_status))
142 ? WEXITSTATUS(test_status)
143 : EXIT_FAILURE;
144 fprintf(stderr,
145 "Test finished with code %d. "
146 "Shutting down Xvfb (pid: %d)...\n",
147 ret, xvfb_pid);
148 }
149 }
150
151 err_xvfd:
152 if (xvfb_pid > 0) {
153 fprintf(stderr, "Sending SIGTERM to Xvfb...\n");
154 kill(xvfb_pid, SIGTERM);
155 fprintf(stderr, "Waiting for Xvfb to exit (pid: %d)...\n", xvfb_pid);
156 int xvfb_status = 0;
157 if (waitpid(xvfb_pid, &xvfb_status, 0) <= 0) {
158 perror("Xvfb waitpid failed.");
159 } else if (WIFEXITED(xvfb_status)) {
160 fprintf(stderr, "Xvfb shut down (pid: %d) with exit code %d.\n",
161 xvfb_pid, WEXITSTATUS(xvfb_status));
162 } else {
163 fprintf(stderr, "Xvfb shut down (pid: %d) abnormally.\n", xvfb_pid);
164 }
165 }
166 fclose(display_fd);
167 err_display_fd:
168 return ret;
169 }
170
171 /* All X11_TEST functions are in the test_func_sec ELF section.
172 * __start and __stop point to the start and end of that section. See the
173 * __attribute__(section) documentation.
174 */
175 DECLARE_TEST_ELF_SECTION_POINTERS(TEST_ELF_SECTION);
176
177 int
178 x11_tests_run(void)
179 {
180 int rc = 0;
181 for (const struct test_function *t = &__start_test_func_sec;
182 t < &__stop_test_func_sec;
183 t++) {
184 fprintf(stderr, "------ Running test: %s from %s ------\n",
185 t->name, t->file);
186 rc = xvfb_wrapper(t->func, NULL);
187 if (rc != EXIT_SUCCESS) {
188 break;
189 }
190 }
191
192 return rc;
193 }