blob: 83389580a82d1cd07e33ef406fc1acef7a33f295 [file] [log] [blame]
Kristian Høgsberge10b1242012-05-21 16:48:05 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * 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:
Kristian Høgsberge10b1242012-05-21 16:48:05 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * 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.
Kristian Høgsberge10b1242012-05-21 16:48:05 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040027
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <unistd.h>
36#include <signal.h>
37
38#include "xwayland.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040039
40
41static int
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070042handle_sigusr1(int signal_number, void *data)
43{
44 struct weston_xserver *wxs = data;
45
46 /* We'd be safer if we actually had the struct
47 * signalfd_siginfo from the signalfd data and could verify
48 * this came from Xwayland.*/
Dima Ryazanov434cc232014-06-19 01:03:31 -070049 wxs->wm = weston_wm_create(wxs, wxs->wm_fd);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070050 wl_event_source_remove(wxs->sigusr1_source);
51
52 return 1;
53}
54
55static int
Kristian Høgsberge10b1242012-05-21 16:48:05 -040056weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
57{
Tiago Vignattif2684462012-11-30 17:19:56 -020058 struct weston_xserver *wxs = data;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070059 char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8];
60 int sv[2], wm[2], fd;
Maksim Melnikau92de1442013-08-14 22:33:10 +030061 char *xserver = NULL;
62 struct weston_config_section *section;
Kristian Høgsberge10b1242012-05-21 16:48:05 -040063
64 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070065 weston_log("wl connection socketpair failed\n");
66 return 1;
67 }
68
69 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
70 weston_log("X wm connection socketpair failed\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040071 return 1;
72 }
73
Tiago Vignattif2684462012-11-30 17:19:56 -020074 wxs->process.pid = fork();
75 switch (wxs->process.pid) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040076 case 0:
77 /* SOCK_CLOEXEC closes both ends, so we need to unset
78 * the flag on the client fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070079 fd = dup(sv[1]);
80 if (fd < 0)
81 goto fail;
82 snprintf(s, sizeof s, "%d", fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040083 setenv("WAYLAND_SOCKET", s, 1);
84
Tiago Vignattif2684462012-11-30 17:19:56 -020085 snprintf(display, sizeof display, ":%d", wxs->display);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040086
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070087 fd = dup(wxs->abstract_fd);
88 if (fd < 0)
89 goto fail;
90 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
91 fd = dup(wxs->unix_fd);
92 if (fd < 0)
93 goto fail;
94 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
95 fd = dup(wm[1]);
96 if (fd < 0)
97 goto fail;
98 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
99
100 section = weston_config_get_section(wxs->compositor->config,
101 "xwayland", NULL, NULL);
102 weston_config_section_get_string(section, "path",
103 &xserver, XSERVER_PATH);
104
105 /* Ignore SIGUSR1 in the child, which will make the X
106 * server send SIGUSR1 to the parent (weston) when
107 * it's done with initialization. During
108 * initialization the X server will round trip and
109 * block on the wayland compositor, so avoid making
110 * blocking requests (like xcb_connect_to_fd) until
111 * it's done with that. */
112 signal(SIGUSR1, SIG_IGN);
Maksim Melnikau92de1442013-08-14 22:33:10 +0300113
114 if (execl(xserver,
115 xserver,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400116 display,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400117 "-rootless",
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700118 "-listen", abstract_fd,
119 "-listen", unix_fd,
120 "-wm", wm_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400121 "-terminate",
122 NULL) < 0)
Arnout Engelen7da71ee2014-06-20 21:36:54 +0200123 weston_log("exec of '%s %s -rootless "
124 "-listen %s -listen %s -wm %s "
125 "-terminate' failed: %m\n",
126 xserver, display,
127 abstract_fd, unix_fd, wm_fd);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700128 fail:
Kristian Høgsberga58290b2013-06-18 01:03:02 -0400129 _exit(EXIT_FAILURE);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400130
131 default:
Tiago Vignattif2684462012-11-30 17:19:56 -0200132 weston_log("forked X server, pid %d\n", wxs->process.pid);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400133
134 close(sv[1]);
Tiago Vignattif2684462012-11-30 17:19:56 -0200135 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400136
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700137 close(wm[1]);
138 wxs->wm_fd = wm[0];
139
Tiago Vignattif2684462012-11-30 17:19:56 -0200140 weston_watch_process(&wxs->process);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400141
Tiago Vignattif2684462012-11-30 17:19:56 -0200142 wl_event_source_remove(wxs->abstract_source);
143 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400144 break;
145
146 case -1:
Martin Minarik6d118362012-06-07 18:01:59 +0200147 weston_log( "failed to fork\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400148 break;
149 }
150
151 return 1;
152}
153
154static void
155weston_xserver_shutdown(struct weston_xserver *wxs)
156{
157 char path[256];
158
159 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
160 unlink(path);
161 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
162 unlink(path);
163 if (wxs->process.pid == 0) {
164 wl_event_source_remove(wxs->abstract_source);
165 wl_event_source_remove(wxs->unix_source);
166 }
167 close(wxs->abstract_fd);
168 close(wxs->unix_fd);
Dima Ryazanov434cc232014-06-19 01:03:31 -0700169 if (wxs->wm) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400170 weston_wm_destroy(wxs->wm);
Dima Ryazanov434cc232014-06-19 01:03:31 -0700171 wxs->wm = NULL;
172 }
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400173 wxs->loop = NULL;
174}
175
176static void
177weston_xserver_cleanup(struct weston_process *process, int status)
178{
Tiago Vignattif2684462012-11-30 17:19:56 -0200179 struct weston_xserver *wxs =
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400180 container_of(process, struct weston_xserver, process);
181
Tiago Vignattif2684462012-11-30 17:19:56 -0200182 wxs->process.pid = 0;
183 wxs->client = NULL;
184 wxs->resource = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400185
Tiago Vignattif2684462012-11-30 17:19:56 -0200186 wxs->abstract_source =
187 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400188 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200189 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400190
Tiago Vignattif2684462012-11-30 17:19:56 -0200191 wxs->unix_source =
192 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400193 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200194 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400195
Tiago Vignattif2684462012-11-30 17:19:56 -0200196 if (wxs->wm) {
Martin Minarik6d118362012-06-07 18:01:59 +0200197 weston_log("xserver exited, code %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200198 weston_wm_destroy(wxs->wm);
199 wxs->wm = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400200 } else {
201 /* If the X server crashes before it binds to the
202 * xserver interface, shut down and don't try
203 * again. */
Martin Minarik6d118362012-06-07 18:01:59 +0200204 weston_log("xserver crashing too fast: %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200205 weston_xserver_shutdown(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400206 }
207}
208
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400209static int
210bind_to_abstract_socket(int display)
211{
212 struct sockaddr_un addr;
213 socklen_t size, name_size;
214 int fd;
215
216 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
217 if (fd < 0)
218 return -1;
219
220 addr.sun_family = AF_LOCAL;
221 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
222 "%c/tmp/.X11-unix/X%d", 0, display);
223 size = offsetof(struct sockaddr_un, sun_path) + name_size;
224 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400225 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400226 close(fd);
227 return -1;
228 }
229
230 if (listen(fd, 1) < 0) {
231 close(fd);
232 return -1;
233 }
234
235 return fd;
236}
237
238static int
239bind_to_unix_socket(int display)
240{
241 struct sockaddr_un addr;
242 socklen_t size, name_size;
243 int fd;
244
245 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
246 if (fd < 0)
247 return -1;
248
249 addr.sun_family = AF_LOCAL;
250 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
251 "/tmp/.X11-unix/X%d", display) + 1;
252 size = offsetof(struct sockaddr_un, sun_path) + name_size;
253 unlink(addr.sun_path);
254 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400255 weston_log("failed to bind to %s: %m\n", addr.sun_path);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400256 close(fd);
257 return -1;
258 }
259
260 if (listen(fd, 1) < 0) {
261 unlink(addr.sun_path);
262 close(fd);
263 return -1;
264 }
265
266 return fd;
267}
268
269static int
270create_lockfile(int display, char *lockfile, size_t lsize)
271{
272 char pid[16], *end;
273 int fd, size;
274 pid_t other;
275
276 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
277 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
278 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800279 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400280 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200281 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400282 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000283 if (fd >= 0)
284 close (fd);
285
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400286 errno = EEXIST;
287 return -1;
288 }
289
290 other = strtol(pid, &end, 0);
291 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400293 lockfile);
294 close(fd);
295 errno = EEXIST;
296 return -1;
297 }
298
299 if (kill(other, 0) < 0 && errno == ESRCH) {
300 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200301 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400302 close(fd);
303 if (unlink(lockfile))
304 /* If we fail to unlink, return EEXIST
305 so we try the next display number.*/
306 errno = EEXIST;
307 else
308 errno = EAGAIN;
309 return -1;
310 }
311
Martin Olsson19721412012-07-08 03:03:45 +0200312 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400313 errno = EEXIST;
314 return -1;
315 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200316 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400317 lockfile, strerror(errno));
318 return -1;
319 }
320
321 /* Subtle detail: we use the pid of the wayland
322 * compositor, not the xserver in the lock file. */
323 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
324 if (write(fd, pid, size) != size) {
325 unlink(lockfile);
326 close(fd);
327 return -1;
328 }
329
330 close(fd);
331
332 return 0;
333}
334
335static void
336weston_xserver_destroy(struct wl_listener *l, void *data)
337{
338 struct weston_xserver *wxs =
339 container_of(l, struct weston_xserver, destroy_listener);
340
341 if (!wxs)
342 return;
343
344 if (wxs->loop)
345 weston_xserver_shutdown(wxs);
346
347 free(wxs);
348}
349
350WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500351module_init(struct weston_compositor *compositor,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700352 int *argc, char *argv[])
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500353
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400354{
355 struct wl_display *display = compositor->wl_display;
Tiago Vignattif2684462012-11-30 17:19:56 -0200356 struct weston_xserver *wxs;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400357 char lockfile[256], display_name[8];
358
Peter Huttererf3d62272013-08-08 11:57:05 +1000359 wxs = zalloc(sizeof *wxs);
Bryce W. Harringtona212cbb2014-04-21 23:51:03 +0000360 if (wxs == NULL)
361 return -1;
Tiago Vignattif2684462012-11-30 17:19:56 -0200362 wxs->process.cleanup = weston_xserver_cleanup;
363 wxs->wl_display = display;
364 wxs->compositor = compositor;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400365
Tiago Vignattif2684462012-11-30 17:19:56 -0200366 wxs->display = 0;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400367
368 retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200369 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400370 if (errno == EAGAIN) {
371 goto retry;
372 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200373 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400374 goto retry;
375 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200376 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400377 return -1;
378 }
379 }
380
Tiago Vignattif2684462012-11-30 17:19:56 -0200381 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
382 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
383 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400384 unlink(lockfile);
385 goto retry;
386 }
387
Tiago Vignattif2684462012-11-30 17:19:56 -0200388 wxs->unix_fd = bind_to_unix_socket(wxs->display);
389 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400390 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200391 close(wxs->abstract_fd);
392 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400393 return -1;
394 }
395
Tiago Vignattif2684462012-11-30 17:19:56 -0200396 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200397 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400398 setenv("DISPLAY", display_name, 1);
399
Tiago Vignattif2684462012-11-30 17:19:56 -0200400 wxs->loop = wl_display_get_event_loop(display);
401 wxs->abstract_source =
402 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400403 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200404 weston_xserver_handle_event, wxs);
405 wxs->unix_source =
406 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400407 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200408 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400409
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700410 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
411 handle_sigusr1, wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200412 wxs->destroy_listener.notify = weston_xserver_destroy;
413 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400414
415 return 0;
416}