blob: db5e1d03b7c1c9205f8a8f0594eb7fe1e845091e [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"
Jon Cruz867d50e2015-06-15 15:37:10 -070039#include "shared/helpers.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040040
41
42static int
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070043handle_sigusr1(int signal_number, void *data)
44{
45 struct weston_xserver *wxs = data;
46
47 /* We'd be safer if we actually had the struct
48 * signalfd_siginfo from the signalfd data and could verify
49 * this came from Xwayland.*/
Dima Ryazanov434cc232014-06-19 01:03:31 -070050 wxs->wm = weston_wm_create(wxs, wxs->wm_fd);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070051 wl_event_source_remove(wxs->sigusr1_source);
52
53 return 1;
54}
55
56static int
Kristian Høgsberge10b1242012-05-21 16:48:05 -040057weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
58{
Tiago Vignattif2684462012-11-30 17:19:56 -020059 struct weston_xserver *wxs = data;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070060 char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8];
61 int sv[2], wm[2], fd;
Maksim Melnikau92de1442013-08-14 22:33:10 +030062 char *xserver = NULL;
63 struct weston_config_section *section;
Kristian Høgsberge10b1242012-05-21 16:48:05 -040064
65 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070066 weston_log("wl connection socketpair failed\n");
67 return 1;
68 }
69
70 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
71 weston_log("X wm connection socketpair failed\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040072 return 1;
73 }
74
Tiago Vignattif2684462012-11-30 17:19:56 -020075 wxs->process.pid = fork();
76 switch (wxs->process.pid) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040077 case 0:
78 /* SOCK_CLOEXEC closes both ends, so we need to unset
79 * the flag on the client fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070080 fd = dup(sv[1]);
81 if (fd < 0)
82 goto fail;
83 snprintf(s, sizeof s, "%d", fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040084 setenv("WAYLAND_SOCKET", s, 1);
85
Tiago Vignattif2684462012-11-30 17:19:56 -020086 snprintf(display, sizeof display, ":%d", wxs->display);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040087
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070088 fd = dup(wxs->abstract_fd);
89 if (fd < 0)
90 goto fail;
91 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
92 fd = dup(wxs->unix_fd);
93 if (fd < 0)
94 goto fail;
95 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
96 fd = dup(wm[1]);
97 if (fd < 0)
98 goto fail;
99 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
100
101 section = weston_config_get_section(wxs->compositor->config,
102 "xwayland", NULL, NULL);
103 weston_config_section_get_string(section, "path",
104 &xserver, XSERVER_PATH);
105
106 /* Ignore SIGUSR1 in the child, which will make the X
107 * server send SIGUSR1 to the parent (weston) when
108 * it's done with initialization. During
109 * initialization the X server will round trip and
110 * block on the wayland compositor, so avoid making
111 * blocking requests (like xcb_connect_to_fd) until
112 * it's done with that. */
113 signal(SIGUSR1, SIG_IGN);
Maksim Melnikau92de1442013-08-14 22:33:10 +0300114
115 if (execl(xserver,
116 xserver,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400117 display,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400118 "-rootless",
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700119 "-listen", abstract_fd,
120 "-listen", unix_fd,
121 "-wm", wm_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400122 "-terminate",
123 NULL) < 0)
Arnout Engelen7da71ee2014-06-20 21:36:54 +0200124 weston_log("exec of '%s %s -rootless "
125 "-listen %s -listen %s -wm %s "
126 "-terminate' failed: %m\n",
127 xserver, display,
128 abstract_fd, unix_fd, wm_fd);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700129 fail:
Kristian Høgsberga58290b2013-06-18 01:03:02 -0400130 _exit(EXIT_FAILURE);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400131
132 default:
Tiago Vignattif2684462012-11-30 17:19:56 -0200133 weston_log("forked X server, pid %d\n", wxs->process.pid);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400134
135 close(sv[1]);
Tiago Vignattif2684462012-11-30 17:19:56 -0200136 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400137
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700138 close(wm[1]);
139 wxs->wm_fd = wm[0];
140
Tiago Vignattif2684462012-11-30 17:19:56 -0200141 weston_watch_process(&wxs->process);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400142
Tiago Vignattif2684462012-11-30 17:19:56 -0200143 wl_event_source_remove(wxs->abstract_source);
144 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400145 break;
146
147 case -1:
Martin Minarik6d118362012-06-07 18:01:59 +0200148 weston_log( "failed to fork\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400149 break;
150 }
151
152 return 1;
153}
154
155static void
156weston_xserver_shutdown(struct weston_xserver *wxs)
157{
158 char path[256];
159
160 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
161 unlink(path);
162 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
163 unlink(path);
164 if (wxs->process.pid == 0) {
165 wl_event_source_remove(wxs->abstract_source);
166 wl_event_source_remove(wxs->unix_source);
167 }
168 close(wxs->abstract_fd);
169 close(wxs->unix_fd);
Dima Ryazanov434cc232014-06-19 01:03:31 -0700170 if (wxs->wm) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400171 weston_wm_destroy(wxs->wm);
Dima Ryazanov434cc232014-06-19 01:03:31 -0700172 wxs->wm = NULL;
173 }
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400174 wxs->loop = NULL;
175}
176
177static void
178weston_xserver_cleanup(struct weston_process *process, int status)
179{
Tiago Vignattif2684462012-11-30 17:19:56 -0200180 struct weston_xserver *wxs =
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400181 container_of(process, struct weston_xserver, process);
182
Tiago Vignattif2684462012-11-30 17:19:56 -0200183 wxs->process.pid = 0;
184 wxs->client = NULL;
185 wxs->resource = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400186
Tiago Vignattif2684462012-11-30 17:19:56 -0200187 wxs->abstract_source =
188 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400189 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200190 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400191
Tiago Vignattif2684462012-11-30 17:19:56 -0200192 wxs->unix_source =
193 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400194 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200195 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400196
Tiago Vignattif2684462012-11-30 17:19:56 -0200197 if (wxs->wm) {
Martin Minarik6d118362012-06-07 18:01:59 +0200198 weston_log("xserver exited, code %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200199 weston_wm_destroy(wxs->wm);
200 wxs->wm = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400201 } else {
202 /* If the X server crashes before it binds to the
203 * xserver interface, shut down and don't try
204 * again. */
Martin Minarik6d118362012-06-07 18:01:59 +0200205 weston_log("xserver crashing too fast: %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200206 weston_xserver_shutdown(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400207 }
208}
209
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400210static int
211bind_to_abstract_socket(int display)
212{
213 struct sockaddr_un addr;
214 socklen_t size, name_size;
215 int fd;
216
217 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
218 if (fd < 0)
219 return -1;
220
221 addr.sun_family = AF_LOCAL;
222 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
223 "%c/tmp/.X11-unix/X%d", 0, display);
224 size = offsetof(struct sockaddr_un, sun_path) + name_size;
225 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400226 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400227 close(fd);
228 return -1;
229 }
230
231 if (listen(fd, 1) < 0) {
232 close(fd);
233 return -1;
234 }
235
236 return fd;
237}
238
239static int
240bind_to_unix_socket(int display)
241{
242 struct sockaddr_un addr;
243 socklen_t size, name_size;
244 int fd;
245
246 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
247 if (fd < 0)
248 return -1;
249
250 addr.sun_family = AF_LOCAL;
251 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
252 "/tmp/.X11-unix/X%d", display) + 1;
253 size = offsetof(struct sockaddr_un, sun_path) + name_size;
254 unlink(addr.sun_path);
255 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400256 weston_log("failed to bind to %s: %m\n", addr.sun_path);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400257 close(fd);
258 return -1;
259 }
260
261 if (listen(fd, 1) < 0) {
262 unlink(addr.sun_path);
263 close(fd);
264 return -1;
265 }
266
267 return fd;
268}
269
270static int
271create_lockfile(int display, char *lockfile, size_t lsize)
272{
273 char pid[16], *end;
274 int fd, size;
275 pid_t other;
276
277 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
278 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
279 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800280 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400281 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200282 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400283 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000284 if (fd >= 0)
285 close (fd);
286
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400287 errno = EEXIST;
288 return -1;
289 }
290
291 other = strtol(pid, &end, 0);
292 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200293 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400294 lockfile);
295 close(fd);
296 errno = EEXIST;
297 return -1;
298 }
299
300 if (kill(other, 0) < 0 && errno == ESRCH) {
301 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400303 close(fd);
304 if (unlink(lockfile))
305 /* If we fail to unlink, return EEXIST
306 so we try the next display number.*/
307 errno = EEXIST;
308 else
309 errno = EAGAIN;
310 return -1;
311 }
312
Martin Olsson19721412012-07-08 03:03:45 +0200313 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400314 errno = EEXIST;
315 return -1;
316 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200317 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400318 lockfile, strerror(errno));
319 return -1;
320 }
321
322 /* Subtle detail: we use the pid of the wayland
323 * compositor, not the xserver in the lock file. */
324 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
325 if (write(fd, pid, size) != size) {
326 unlink(lockfile);
327 close(fd);
328 return -1;
329 }
330
331 close(fd);
332
333 return 0;
334}
335
336static void
337weston_xserver_destroy(struct wl_listener *l, void *data)
338{
339 struct weston_xserver *wxs =
340 container_of(l, struct weston_xserver, destroy_listener);
341
342 if (!wxs)
343 return;
344
345 if (wxs->loop)
346 weston_xserver_shutdown(wxs);
347
348 free(wxs);
349}
350
351WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500352module_init(struct weston_compositor *compositor,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700353 int *argc, char *argv[])
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500354
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400355{
356 struct wl_display *display = compositor->wl_display;
Tiago Vignattif2684462012-11-30 17:19:56 -0200357 struct weston_xserver *wxs;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400358 char lockfile[256], display_name[8];
359
Peter Huttererf3d62272013-08-08 11:57:05 +1000360 wxs = zalloc(sizeof *wxs);
Bryce W. Harringtona212cbb2014-04-21 23:51:03 +0000361 if (wxs == NULL)
362 return -1;
Tiago Vignattif2684462012-11-30 17:19:56 -0200363 wxs->process.cleanup = weston_xserver_cleanup;
364 wxs->wl_display = display;
365 wxs->compositor = compositor;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400366
Tiago Vignattif2684462012-11-30 17:19:56 -0200367 wxs->display = 0;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400368
369 retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200370 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400371 if (errno == EAGAIN) {
372 goto retry;
373 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200374 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400375 goto retry;
376 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200377 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400378 return -1;
379 }
380 }
381
Tiago Vignattif2684462012-11-30 17:19:56 -0200382 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
383 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
384 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400385 unlink(lockfile);
386 goto retry;
387 }
388
Tiago Vignattif2684462012-11-30 17:19:56 -0200389 wxs->unix_fd = bind_to_unix_socket(wxs->display);
390 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400391 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200392 close(wxs->abstract_fd);
393 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400394 return -1;
395 }
396
Tiago Vignattif2684462012-11-30 17:19:56 -0200397 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200398 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400399 setenv("DISPLAY", display_name, 1);
400
Tiago Vignattif2684462012-11-30 17:19:56 -0200401 wxs->loop = wl_display_get_event_loop(display);
402 wxs->abstract_source =
403 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400404 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200405 weston_xserver_handle_event, wxs);
406 wxs->unix_source =
407 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400408 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200409 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400410
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700411 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
412 handle_sigusr1, wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200413 wxs->destroy_listener.notify = weston_xserver_destroy;
414 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400415
416 return 0;
417}