simple-xinit.c revision 1b5d61b8
1/*
2 * Copyright © 2016 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#ifdef HAVE_DIX_CONFIG_H
25#include <dix-config.h>
26#endif
27
28#include <errno.h>
29#include <signal.h>
30#include <stdbool.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/wait.h>
35#include <unistd.h>
36
37static void
38kill_server(int server_pid)
39{
40    int ret = kill(server_pid, SIGTERM);
41    int wstatus;
42
43    if (ret) {
44        fprintf(stderr, "Failed to send kill to the server: %s\n",
45                strerror(errno));
46        exit(1);
47    }
48
49    ret = waitpid(server_pid, &wstatus, 0);
50    if (ret < 0) {
51        fprintf(stderr, "Failed to wait for X to die: %s\n", strerror(errno));
52        exit(1);
53    }
54}
55
56static void
57usage(int argc, char **argv)
58{
59    fprintf(stderr, "%s <client command> -- <server command>\n", argv[0]);
60    exit(1);
61}
62
63/* Starts the X server, returning its pid. */
64static int
65start_server(char *const *server_args)
66{
67    int server_pid = fork();
68
69    if (server_pid == -1) {
70        fprintf(stderr, "Fork failed: %s\n", strerror(errno));
71        exit(1);
72    } else if (server_pid != 0) {
73        /* Continue along the main process that will exec the client. */
74        return server_pid;
75    }
76
77    /* Execute the server.  This only returns if an error occurred. */
78    execvp(server_args[0], server_args);
79    fprintf(stderr, "Error starting the server: %s\n", strerror(errno));
80    exit(1);
81}
82
83/* Reads the display number out of the started server's display socket. */
84static int
85get_display(int displayfd)
86{
87    char display_string[10];
88    ssize_t ret;
89
90    ret = read(displayfd, display_string, sizeof(display_string) - 1);
91    if (ret <= 0) {
92        fprintf(stderr, "Failed reading displayfd: %s\n", strerror(errno));
93        exit(1);
94    }
95
96    /* We've read in the display number as a string terminated by
97     * '\n', but not '\0'.  Cap it and parse the number.
98     */
99    display_string[ret] = '\0';
100    return atoi(display_string);
101}
102
103static int
104start_client(char *const *client_args, int display)
105{
106    char *display_string;
107    int ret;
108    int client_pid;
109
110    ret = asprintf(&display_string, ":%d", display);
111    if (ret < 0) {
112        fprintf(stderr, "asprintf fail\n");
113        exit(1);
114    }
115
116    ret = setenv("DISPLAY", display_string, true);
117    if (ret) {
118        fprintf(stderr, "Failed to set DISPLAY\n");
119        exit(1);
120    }
121
122    client_pid = fork();
123    if (client_pid == -1) {
124        fprintf(stderr, "Fork failed: %s\n", strerror(errno));
125        exit(1);
126    } else if (client_pid) {
127        int wstatus;
128
129        ret = waitpid(client_pid, &wstatus, 0);
130        if (ret < 0) {
131            fprintf(stderr, "Error waiting for client to start: %s\n",
132                    strerror(errno));
133            return 1;
134        }
135
136        if (!WIFEXITED(wstatus))
137            return 1;
138
139        return WEXITSTATUS(wstatus);
140    } else {
141        execvp(client_args[0], client_args);
142        /* exec only returns if an error occurred. */
143        fprintf(stderr, "Error starting the client: %s\n", strerror(errno));
144        exit(1);
145    }
146}
147
148/* Splits the incoming argc/argv into a pair of NULL-terminated arrays
149 * of args.
150 */
151static void
152parse_args(int argc, char **argv,
153           char * const **out_client_args,
154           char * const **out_server_args,
155           int displayfd)
156{
157    /* We're stripping the -- and the program name, inserting two
158     * NULLs, and also the -displayfd and fd number.
159     */
160    char **args_storage = calloc(argc + 2, sizeof(char *));
161    char *const *client_args;
162    char *const *server_args = NULL;
163    char **next_arg = args_storage;
164    bool parsing_client = true;
165    int i, ret;
166    char *displayfd_string;
167
168    if (!args_storage)
169        exit(1);
170
171    client_args = args_storage;
172    for (i = 1; i < argc; i++) {
173        if (strcmp(argv[i], "--") == 0) {
174            if (!parsing_client)
175                usage(argc, argv);
176
177            /* Cap the client list */
178            *next_arg = NULL;
179            next_arg++;
180
181            /* Move to adding into server_args. */
182            server_args = next_arg;
183            parsing_client = false;
184            continue;
185        }
186
187        *next_arg = argv[i];
188        next_arg++;
189    }
190
191    if (client_args[0] == NULL || !server_args || server_args[0] == NULL)
192        usage(argc, argv);
193
194    /* Give the server -displayfd X */
195    *next_arg = (char *)"-displayfd";
196    next_arg++;
197
198    ret = asprintf(&displayfd_string, "%d", displayfd);
199    if (ret < 0) {
200        fprintf(stderr, "asprintf fail\n");
201        exit(1);
202    }
203    *next_arg = displayfd_string;
204    next_arg++;
205
206    *out_client_args = client_args;
207    *out_server_args = server_args;
208}
209
210int
211main(int argc, char **argv)
212{
213    char * const *client_args;
214    char * const *server_args;
215    int displayfd_pipe[2];
216    int display, server_pid;
217    int ret;
218
219    ret = pipe(displayfd_pipe);
220    if (ret) {
221        fprintf(stderr, "Pipe creation failure: %s", strerror(errno));
222        exit(1);
223    }
224
225    parse_args(argc, argv, &client_args, &server_args, displayfd_pipe[1]);
226    server_pid = start_server(server_args);
227    display = get_display(displayfd_pipe[0]);
228    ret = start_client(client_args, display);
229    kill_server(server_pid);
230
231    exit(ret);
232}
233