File: xvfb-wrapper.c

package info (click to toggle)
libxkbcommon 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,344 kB
  • sloc: ansic: 57,807; xml: 8,905; python: 7,451; yacc: 913; sh: 253; makefile: 23
file content (193 lines) | stat: -rw-r--r-- 5,959 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright © 2014 Ran Benita <ran234@gmail.com>
 * Copyright © 2023 Pierre Le Marre <dev@wismill.eu>
 * SPDX-License-Identifier: MIT
 */

#include "config.h"

#include <assert.h>
#include <signal.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "test.h"
#include "xvfb-wrapper.h"

static volatile bool xvfb_is_ready;

static void
sigusr1_handler(int signal)
{
    xvfb_is_ready = true;
}

int
xvfb_wrapper(x11_test_func_t test_func, void *private)
{
    int ret = EXIT_SUCCESS;
    FILE * display_fd;
    char display_fd_string[32];
    sigset_t mask;
    struct sigaction sa;
    char *xvfb_argv[] = {
        (char *) "Xvfb", (char *) "-displayfd", display_fd_string, NULL
    };
    char *envp[] = { NULL };
    pid_t xvfb_pid = 0;
    size_t counter = 0;
    char display[32] = ":";
    size_t length;

    /* File descriptor to retrieve the display number */
    display_fd = tmpfile();
    if (display_fd == NULL){
        fprintf(stderr, "Unable to create temporary file.\n");
        goto err_display_fd;
    }
    snprintf(display_fd_string, sizeof(display_fd_string), "%d", fileno(display_fd));

    /* Set SIGUSR1 to SIG_IGN so Xvfb will send us that signal when it's ready
     * to accept connections. In order to avoid race condition, we block the
     * until we are ready to process it. */
    sigemptyset(&mask);
    sigaddset(&mask, SIGUSR1);
    sigprocmask(SIG_BLOCK, &mask, NULL);
    sa.sa_handler = sigusr1_handler;
    sa.sa_flags = SA_RESTART;
    sigemptyset(&sa.sa_mask);
    struct sigaction sa_old;
    sigaction(SIGUSR1, &sa, &sa_old);

    xvfb_is_ready = false;

    /*
     * Xvfb command: let the server find an available display.
     *
     * Note that it may generate multiple times the following output in stderr:
     *    _XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
     * It is expected: this is the server trying the ports until it finds one
     * that works.
     */
    ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
    if (ret != 0) {
        fprintf(stderr,
                "[ERROR] Cannot run Xvfb. posix_spawnp error %d: %s\n",
                ret, strerror(ret));
        if (ret == ENOENT) {
            fprintf(stderr,
                    "[ERROR] Xvfb may be missing. "
                    "Please install the corresponding package, "
                    "e.g. \"xvfb\" or \"xorg-x11-server-Xvfb\".\n");
        }
        ret = TEST_SETUP_FAILURE;
        goto err_xvfd;
    }

    sigprocmask(SIG_UNBLOCK, &mask, NULL);

    /* Now wait for the SIGUSR1 signal that Xvfb is ready */
    while (!xvfb_is_ready) {
        usleep(1000);
        if (++counter >= 3000) /* 3 seconds max wait */
            break;
    }

    sigaction(SIGUSR1, &sa_old, NULL);

    /* Check if Xvfb is still alive */
    pid_t pid = waitpid(xvfb_pid, NULL, WNOHANG);
    if (pid != 0) {
        fprintf(stderr, "ERROR: Xvfb not alive\n");
        ret = TEST_SETUP_FAILURE;
        goto err_xvfd;
    }

    /* Retrieve the display number: Xvfd writes the display number as a newline-
     * terminated string; copy this number to form a proper display string. */
    fseek(display_fd, 0, SEEK_SET);
    length = fread(&display[1], 1, sizeof(display) - 2, display_fd);
    if (length <= 0) {
        fprintf(stderr, "fread error: length=%zu\n", length);
        ret = TEST_SETUP_FAILURE;
        goto err_xvfd;
    } else {
        /* Drop the newline character */
        display[length] = '\0';
    }

    /* Run the function requiring a running X server.
     * Because it may call abort() via assert(), we fork to be able to
     * exit gracefully and not hang waiting for Xvfb. */
    pid_t test_pid = fork();
    switch (test_pid) {
        case -1:
            perror("fork");
            ret = TEST_SETUP_FAILURE;
            break;
        case 0:
            fprintf(stderr, "Running test using Xvfb wrapper...\n");
            ret = test_func(display, private);
            fprintf(stderr,
                    "Test using Xvfb wrapper finished with code %d.\n", ret);
            _exit(ret);
        default:
            {
                int test_status = 0;
                pid_t test_pid2 = waitpid(test_pid, &test_status, 0);
                ret = (test_pid2 > 0 && WIFEXITED(test_status))
                    ? WEXITSTATUS(test_status)
                    : EXIT_FAILURE;
                fprintf(stderr,
                        "Test finished with code %d. "
                        "Shutting down Xvfb (pid: %d)...\n",
                        ret, xvfb_pid);
            }
    }

err_xvfd:
    if (xvfb_pid > 0) {
        fprintf(stderr, "Sending SIGTERM to Xvfb...\n");
        kill(xvfb_pid, SIGTERM);
        fprintf(stderr, "Waiting for Xvfb to exit (pid: %d)...\n", xvfb_pid);
        int xvfb_status = 0;
        if (waitpid(xvfb_pid, &xvfb_status, 0) <= 0) {
            perror("Xvfb waitpid failed.");
        } else if (WIFEXITED(xvfb_status)) {
            fprintf(stderr, "Xvfb shut down (pid: %d) with exit code %d.\n",
                    xvfb_pid, WEXITSTATUS(xvfb_status));
        } else {
            fprintf(stderr, "Xvfb shut down (pid: %d) abnormally.\n", xvfb_pid);
        }
    }
    fclose(display_fd);
err_display_fd:
    return ret;
}

/* All X11_TEST functions are in the test_func_sec ELF section.
 * __start and __stop point to the start and end of that section. See the
 * __attribute__(section) documentation.
 */
DECLARE_TEST_ELF_SECTION_POINTERS(TEST_ELF_SECTION);

int
x11_tests_run(void)
{
    int rc = 0;
    for (const struct test_function *t = &__start_test_func_sec;
         t < &__stop_test_func_sec;
         t++) {
        fprintf(stderr, "------ Running test: %s from %s ------\n",
                t->name, t->file);
        rc = xvfb_wrapper(t->func, NULL);
        if (rc != EXIT_SUCCESS) {
            break;
        }
    }

    return rc;
}