blob: f0a87f5bf9a747881be58c1ee7a52ad8e880d442 [file] [log] [blame]
Kristian Høgsberge10b1242012-05-21 16:48:05 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040024
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
34
35#include "xwayland.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040036
37
38static int
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070039handle_sigusr1(int signal_number, void *data)
40{
41 struct weston_xserver *wxs = data;
42
43 /* We'd be safer if we actually had the struct
44 * signalfd_siginfo from the signalfd data and could verify
45 * this came from Xwayland.*/
46 weston_wm_create(wxs, wxs->wm_fd);
47 wl_event_source_remove(wxs->sigusr1_source);
48
49 return 1;
50}
51
52static int
Kristian Høgsberge10b1242012-05-21 16:48:05 -040053weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
54{
Tiago Vignattif2684462012-11-30 17:19:56 -020055 struct weston_xserver *wxs = data;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070056 char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8];
57 int sv[2], wm[2], fd;
Maksim Melnikau92de1442013-08-14 22:33:10 +030058 char *xserver = NULL;
59 struct weston_config_section *section;
Kristian Høgsberge10b1242012-05-21 16:48:05 -040060
61 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070062 weston_log("wl connection socketpair failed\n");
63 return 1;
64 }
65
66 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
67 weston_log("X wm connection socketpair failed\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040068 return 1;
69 }
70
Tiago Vignattif2684462012-11-30 17:19:56 -020071 wxs->process.pid = fork();
72 switch (wxs->process.pid) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040073 case 0:
74 /* SOCK_CLOEXEC closes both ends, so we need to unset
75 * the flag on the client fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070076 fd = dup(sv[1]);
77 if (fd < 0)
78 goto fail;
79 snprintf(s, sizeof s, "%d", fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040080 setenv("WAYLAND_SOCKET", s, 1);
81
Tiago Vignattif2684462012-11-30 17:19:56 -020082 snprintf(display, sizeof display, ":%d", wxs->display);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040083
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070084 fd = dup(wxs->abstract_fd);
85 if (fd < 0)
86 goto fail;
87 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
88 fd = dup(wxs->unix_fd);
89 if (fd < 0)
90 goto fail;
91 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
92 fd = dup(wm[1]);
93 if (fd < 0)
94 goto fail;
95 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
96
97 section = weston_config_get_section(wxs->compositor->config,
98 "xwayland", NULL, NULL);
99 weston_config_section_get_string(section, "path",
100 &xserver, XSERVER_PATH);
101
102 /* Ignore SIGUSR1 in the child, which will make the X
103 * server send SIGUSR1 to the parent (weston) when
104 * it's done with initialization. During
105 * initialization the X server will round trip and
106 * block on the wayland compositor, so avoid making
107 * blocking requests (like xcb_connect_to_fd) until
108 * it's done with that. */
109 signal(SIGUSR1, SIG_IGN);
Maksim Melnikau92de1442013-08-14 22:33:10 +0300110
111 if (execl(xserver,
112 xserver,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400113 display,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400114 "-rootless",
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700115 "-listen", abstract_fd,
116 "-listen", unix_fd,
117 "-wm", wm_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400118 "-terminate",
119 NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200120 weston_log("exec failed: %m\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700121 fail:
Kristian Høgsberga58290b2013-06-18 01:03:02 -0400122 _exit(EXIT_FAILURE);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400123
124 default:
Tiago Vignattif2684462012-11-30 17:19:56 -0200125 weston_log("forked X server, pid %d\n", wxs->process.pid);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400126
127 close(sv[1]);
Tiago Vignattif2684462012-11-30 17:19:56 -0200128 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400129
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700130 close(wm[1]);
131 wxs->wm_fd = wm[0];
132
Tiago Vignattif2684462012-11-30 17:19:56 -0200133 weston_watch_process(&wxs->process);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400134
Tiago Vignattif2684462012-11-30 17:19:56 -0200135 wl_event_source_remove(wxs->abstract_source);
136 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400137 break;
138
139 case -1:
Martin Minarik6d118362012-06-07 18:01:59 +0200140 weston_log( "failed to fork\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400141 break;
142 }
143
144 return 1;
145}
146
147static void
148weston_xserver_shutdown(struct weston_xserver *wxs)
149{
150 char path[256];
151
152 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
153 unlink(path);
154 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
155 unlink(path);
156 if (wxs->process.pid == 0) {
157 wl_event_source_remove(wxs->abstract_source);
158 wl_event_source_remove(wxs->unix_source);
159 }
160 close(wxs->abstract_fd);
161 close(wxs->unix_fd);
162 if (wxs->wm)
163 weston_wm_destroy(wxs->wm);
164 wxs->loop = NULL;
165}
166
167static void
168weston_xserver_cleanup(struct weston_process *process, int status)
169{
Tiago Vignattif2684462012-11-30 17:19:56 -0200170 struct weston_xserver *wxs =
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400171 container_of(process, struct weston_xserver, process);
172
Tiago Vignattif2684462012-11-30 17:19:56 -0200173 wxs->process.pid = 0;
174 wxs->client = NULL;
175 wxs->resource = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400176
Tiago Vignattif2684462012-11-30 17:19:56 -0200177 wxs->abstract_source =
178 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400179 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200180 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400181
Tiago Vignattif2684462012-11-30 17:19:56 -0200182 wxs->unix_source =
183 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400184 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200185 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400186
Tiago Vignattif2684462012-11-30 17:19:56 -0200187 if (wxs->wm) {
Martin Minarik6d118362012-06-07 18:01:59 +0200188 weston_log("xserver exited, code %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200189 weston_wm_destroy(wxs->wm);
190 wxs->wm = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400191 } else {
192 /* If the X server crashes before it binds to the
193 * xserver interface, shut down and don't try
194 * again. */
Martin Minarik6d118362012-06-07 18:01:59 +0200195 weston_log("xserver crashing too fast: %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200196 weston_xserver_shutdown(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400197 }
198}
199
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400200static int
201bind_to_abstract_socket(int display)
202{
203 struct sockaddr_un addr;
204 socklen_t size, name_size;
205 int fd;
206
207 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
208 if (fd < 0)
209 return -1;
210
211 addr.sun_family = AF_LOCAL;
212 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
213 "%c/tmp/.X11-unix/X%d", 0, display);
214 size = offsetof(struct sockaddr_un, sun_path) + name_size;
215 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200216 weston_log("failed to bind to @%s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400217 addr.sun_path + 1, strerror(errno));
218 close(fd);
219 return -1;
220 }
221
222 if (listen(fd, 1) < 0) {
223 close(fd);
224 return -1;
225 }
226
227 return fd;
228}
229
230static int
231bind_to_unix_socket(int display)
232{
233 struct sockaddr_un addr;
234 socklen_t size, name_size;
235 int fd;
236
237 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
238 if (fd < 0)
239 return -1;
240
241 addr.sun_family = AF_LOCAL;
242 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
243 "/tmp/.X11-unix/X%d", display) + 1;
244 size = offsetof(struct sockaddr_un, sun_path) + name_size;
245 unlink(addr.sun_path);
246 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200247 weston_log("failed to bind to %s (%s)\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400248 addr.sun_path, strerror(errno));
249 close(fd);
250 return -1;
251 }
252
253 if (listen(fd, 1) < 0) {
254 unlink(addr.sun_path);
255 close(fd);
256 return -1;
257 }
258
259 return fd;
260}
261
262static int
263create_lockfile(int display, char *lockfile, size_t lsize)
264{
265 char pid[16], *end;
266 int fd, size;
267 pid_t other;
268
269 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
270 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
271 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800272 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400273 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200274 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400275 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000276 if (fd >= 0)
277 close (fd);
278
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400279 errno = EEXIST;
280 return -1;
281 }
282
283 other = strtol(pid, &end, 0);
284 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400286 lockfile);
287 close(fd);
288 errno = EEXIST;
289 return -1;
290 }
291
292 if (kill(other, 0) < 0 && errno == ESRCH) {
293 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400295 close(fd);
296 if (unlink(lockfile))
297 /* If we fail to unlink, return EEXIST
298 so we try the next display number.*/
299 errno = EEXIST;
300 else
301 errno = EAGAIN;
302 return -1;
303 }
304
Martin Olsson19721412012-07-08 03:03:45 +0200305 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400306 errno = EEXIST;
307 return -1;
308 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400310 lockfile, strerror(errno));
311 return -1;
312 }
313
314 /* Subtle detail: we use the pid of the wayland
315 * compositor, not the xserver in the lock file. */
316 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
317 if (write(fd, pid, size) != size) {
318 unlink(lockfile);
319 close(fd);
320 return -1;
321 }
322
323 close(fd);
324
325 return 0;
326}
327
328static void
329weston_xserver_destroy(struct wl_listener *l, void *data)
330{
331 struct weston_xserver *wxs =
332 container_of(l, struct weston_xserver, destroy_listener);
333
334 if (!wxs)
335 return;
336
337 if (wxs->loop)
338 weston_xserver_shutdown(wxs);
339
340 free(wxs);
341}
342
343WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500344module_init(struct weston_compositor *compositor,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700345 int *argc, char *argv[])
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500346
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400347{
348 struct wl_display *display = compositor->wl_display;
Tiago Vignattif2684462012-11-30 17:19:56 -0200349 struct weston_xserver *wxs;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400350 char lockfile[256], display_name[8];
351
Peter Huttererf3d62272013-08-08 11:57:05 +1000352 wxs = zalloc(sizeof *wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200353 wxs->process.cleanup = weston_xserver_cleanup;
354 wxs->wl_display = display;
355 wxs->compositor = compositor;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400356
Tiago Vignattif2684462012-11-30 17:19:56 -0200357 wxs->display = 0;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400358
359 retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200360 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400361 if (errno == EAGAIN) {
362 goto retry;
363 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200364 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400365 goto retry;
366 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200367 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400368 return -1;
369 }
370 }
371
Tiago Vignattif2684462012-11-30 17:19:56 -0200372 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
373 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
374 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400375 unlink(lockfile);
376 goto retry;
377 }
378
Tiago Vignattif2684462012-11-30 17:19:56 -0200379 wxs->unix_fd = bind_to_unix_socket(wxs->display);
380 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400381 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200382 close(wxs->abstract_fd);
383 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400384 return -1;
385 }
386
Tiago Vignattif2684462012-11-30 17:19:56 -0200387 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200388 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400389 setenv("DISPLAY", display_name, 1);
390
Tiago Vignattif2684462012-11-30 17:19:56 -0200391 wxs->loop = wl_display_get_event_loop(display);
392 wxs->abstract_source =
393 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400394 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200395 weston_xserver_handle_event, wxs);
396 wxs->unix_source =
397 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400398 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200399 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400400
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700401 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
402 handle_sigusr1, wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200403 wxs->destroy_listener.notify = weston_xserver_destroy;
404 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400405
406 return 0;
407}