blob: 4fd25537b703195f4b935a14246f9a470ff1d3fd [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"
Giulio Camuffod52f3b72016-06-02 21:48:11 +030040#include "weston.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040041
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;
Giulio Camuffod52f3b72016-06-02 21:48:11 +030063 struct weston_config *config = wet_get_config(wxs->compositor);
Maksim Melnikau92de1442013-08-14 22:33:10 +030064 struct weston_config_section *section;
Kristian Høgsberge10b1242012-05-21 16:48:05 -040065
66 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070067 weston_log("wl connection socketpair failed\n");
68 return 1;
69 }
70
71 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
72 weston_log("X wm connection socketpair failed\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040073 return 1;
74 }
75
Tiago Vignattif2684462012-11-30 17:19:56 -020076 wxs->process.pid = fork();
77 switch (wxs->process.pid) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040078 case 0:
79 /* SOCK_CLOEXEC closes both ends, so we need to unset
80 * the flag on the client fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070081 fd = dup(sv[1]);
82 if (fd < 0)
83 goto fail;
84 snprintf(s, sizeof s, "%d", fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040085 setenv("WAYLAND_SOCKET", s, 1);
86
Tiago Vignattif2684462012-11-30 17:19:56 -020087 snprintf(display, sizeof display, ":%d", wxs->display);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040088
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070089 fd = dup(wxs->abstract_fd);
90 if (fd < 0)
91 goto fail;
92 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
93 fd = dup(wxs->unix_fd);
94 if (fd < 0)
95 goto fail;
96 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
97 fd = dup(wm[1]);
98 if (fd < 0)
99 goto fail;
100 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
101
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300102 section = weston_config_get_section(config,
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700103 "xwayland", NULL, NULL);
104 weston_config_section_get_string(section, "path",
105 &xserver, XSERVER_PATH);
106
107 /* Ignore SIGUSR1 in the child, which will make the X
108 * server send SIGUSR1 to the parent (weston) when
109 * it's done with initialization. During
110 * initialization the X server will round trip and
111 * block on the wayland compositor, so avoid making
112 * blocking requests (like xcb_connect_to_fd) until
113 * it's done with that. */
114 signal(SIGUSR1, SIG_IGN);
Maksim Melnikau92de1442013-08-14 22:33:10 +0300115
116 if (execl(xserver,
117 xserver,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400118 display,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400119 "-rootless",
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700120 "-listen", abstract_fd,
121 "-listen", unix_fd,
122 "-wm", wm_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400123 "-terminate",
124 NULL) < 0)
Arnout Engelen7da71ee2014-06-20 21:36:54 +0200125 weston_log("exec of '%s %s -rootless "
126 "-listen %s -listen %s -wm %s "
127 "-terminate' failed: %m\n",
128 xserver, display,
129 abstract_fd, unix_fd, wm_fd);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700130 fail:
Kristian Høgsberga58290b2013-06-18 01:03:02 -0400131 _exit(EXIT_FAILURE);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400132
133 default:
Tiago Vignattif2684462012-11-30 17:19:56 -0200134 weston_log("forked X server, pid %d\n", wxs->process.pid);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400135
136 close(sv[1]);
Tiago Vignattif2684462012-11-30 17:19:56 -0200137 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400138
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700139 close(wm[1]);
140 wxs->wm_fd = wm[0];
141
Tiago Vignattif2684462012-11-30 17:19:56 -0200142 weston_watch_process(&wxs->process);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400143
Tiago Vignattif2684462012-11-30 17:19:56 -0200144 wl_event_source_remove(wxs->abstract_source);
145 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400146 break;
147
148 case -1:
Martin Minarik6d118362012-06-07 18:01:59 +0200149 weston_log( "failed to fork\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400150 break;
151 }
152
153 return 1;
154}
155
156static void
157weston_xserver_shutdown(struct weston_xserver *wxs)
158{
159 char path[256];
160
161 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
162 unlink(path);
163 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
164 unlink(path);
165 if (wxs->process.pid == 0) {
166 wl_event_source_remove(wxs->abstract_source);
167 wl_event_source_remove(wxs->unix_source);
168 }
169 close(wxs->abstract_fd);
170 close(wxs->unix_fd);
Dima Ryazanov434cc232014-06-19 01:03:31 -0700171 if (wxs->wm) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400172 weston_wm_destroy(wxs->wm);
Dima Ryazanov434cc232014-06-19 01:03:31 -0700173 wxs->wm = NULL;
174 }
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400175 wxs->loop = NULL;
176}
177
178static void
179weston_xserver_cleanup(struct weston_process *process, int status)
180{
Tiago Vignattif2684462012-11-30 17:19:56 -0200181 struct weston_xserver *wxs =
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400182 container_of(process, struct weston_xserver, process);
183
Tiago Vignattif2684462012-11-30 17:19:56 -0200184 wxs->process.pid = 0;
185 wxs->client = NULL;
186 wxs->resource = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400187
Tiago Vignattif2684462012-11-30 17:19:56 -0200188 wxs->abstract_source =
189 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400190 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200191 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400192
Tiago Vignattif2684462012-11-30 17:19:56 -0200193 wxs->unix_source =
194 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400195 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200196 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400197
Tiago Vignattif2684462012-11-30 17:19:56 -0200198 if (wxs->wm) {
Martin Minarik6d118362012-06-07 18:01:59 +0200199 weston_log("xserver exited, code %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200200 weston_wm_destroy(wxs->wm);
201 wxs->wm = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400202 } else {
203 /* If the X server crashes before it binds to the
204 * xserver interface, shut down and don't try
205 * again. */
Martin Minarik6d118362012-06-07 18:01:59 +0200206 weston_log("xserver crashing too fast: %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200207 weston_xserver_shutdown(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400208 }
209}
210
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400211static int
212bind_to_abstract_socket(int display)
213{
214 struct sockaddr_un addr;
215 socklen_t size, name_size;
216 int fd;
217
218 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
219 if (fd < 0)
220 return -1;
221
222 addr.sun_family = AF_LOCAL;
223 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
224 "%c/tmp/.X11-unix/X%d", 0, display);
225 size = offsetof(struct sockaddr_un, sun_path) + name_size;
226 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400227 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400228 close(fd);
229 return -1;
230 }
231
232 if (listen(fd, 1) < 0) {
233 close(fd);
234 return -1;
235 }
236
237 return fd;
238}
239
240static int
241bind_to_unix_socket(int display)
242{
243 struct sockaddr_un addr;
244 socklen_t size, name_size;
245 int fd;
246
247 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
248 if (fd < 0)
249 return -1;
250
251 addr.sun_family = AF_LOCAL;
252 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
253 "/tmp/.X11-unix/X%d", display) + 1;
254 size = offsetof(struct sockaddr_un, sun_path) + name_size;
255 unlink(addr.sun_path);
256 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400257 weston_log("failed to bind to %s: %m\n", addr.sun_path);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400258 close(fd);
259 return -1;
260 }
261
262 if (listen(fd, 1) < 0) {
263 unlink(addr.sun_path);
264 close(fd);
265 return -1;
266 }
267
268 return fd;
269}
270
271static int
272create_lockfile(int display, char *lockfile, size_t lsize)
273{
274 char pid[16], *end;
275 int fd, size;
276 pid_t other;
277
278 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
279 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
280 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800281 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400282 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200283 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400284 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000285 if (fd >= 0)
286 close (fd);
287
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400288 errno = EEXIST;
289 return -1;
290 }
291
292 other = strtol(pid, &end, 0);
293 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400295 lockfile);
296 close(fd);
297 errno = EEXIST;
298 return -1;
299 }
300
301 if (kill(other, 0) < 0 && errno == ESRCH) {
302 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200303 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400304 close(fd);
305 if (unlink(lockfile))
306 /* If we fail to unlink, return EEXIST
307 so we try the next display number.*/
308 errno = EEXIST;
309 else
310 errno = EAGAIN;
311 return -1;
312 }
313
Martin Olsson19721412012-07-08 03:03:45 +0200314 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400315 errno = EEXIST;
316 return -1;
317 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200318 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400319 lockfile, strerror(errno));
320 return -1;
321 }
322
323 /* Subtle detail: we use the pid of the wayland
324 * compositor, not the xserver in the lock file. */
325 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
326 if (write(fd, pid, size) != size) {
327 unlink(lockfile);
328 close(fd);
329 return -1;
330 }
331
332 close(fd);
333
334 return 0;
335}
336
337static void
338weston_xserver_destroy(struct wl_listener *l, void *data)
339{
340 struct weston_xserver *wxs =
341 container_of(l, struct weston_xserver, destroy_listener);
342
343 if (!wxs)
344 return;
345
346 if (wxs->loop)
347 weston_xserver_shutdown(wxs);
348
349 free(wxs);
350}
351
352WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500353module_init(struct weston_compositor *compositor,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700354 int *argc, char *argv[])
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500355
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400356{
357 struct wl_display *display = compositor->wl_display;
Tiago Vignattif2684462012-11-30 17:19:56 -0200358 struct weston_xserver *wxs;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400359 char lockfile[256], display_name[8];
360
Peter Huttererf3d62272013-08-08 11:57:05 +1000361 wxs = zalloc(sizeof *wxs);
Bryce W. Harringtona212cbb2014-04-21 23:51:03 +0000362 if (wxs == NULL)
363 return -1;
Tiago Vignattif2684462012-11-30 17:19:56 -0200364 wxs->process.cleanup = weston_xserver_cleanup;
365 wxs->wl_display = display;
366 wxs->compositor = compositor;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400367
Tiago Vignattif2684462012-11-30 17:19:56 -0200368 wxs->display = 0;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400369
370 retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200371 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400372 if (errno == EAGAIN) {
373 goto retry;
374 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200375 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400376 goto retry;
377 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200378 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400379 return -1;
380 }
381 }
382
Tiago Vignattif2684462012-11-30 17:19:56 -0200383 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
384 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
385 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400386 unlink(lockfile);
387 goto retry;
388 }
389
Tiago Vignattif2684462012-11-30 17:19:56 -0200390 wxs->unix_fd = bind_to_unix_socket(wxs->display);
391 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400392 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200393 close(wxs->abstract_fd);
394 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400395 return -1;
396 }
397
Tiago Vignattif2684462012-11-30 17:19:56 -0200398 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200399 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400400 setenv("DISPLAY", display_name, 1);
401
Tiago Vignattif2684462012-11-30 17:19:56 -0200402 wxs->loop = wl_display_get_event_loop(display);
403 wxs->abstract_source =
404 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400405 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200406 weston_xserver_handle_event, wxs);
407 wxs->unix_source =
408 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400409 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200410 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400411
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700412 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
413 handle_sigusr1, wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200414 wxs->destroy_listener.notify = weston_xserver_destroy;
415 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400416
417 return 0;
418}