blob: f37cbe4a4ccb7bb7386b0d931e53abb3d271b406 [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"
36#include "xserver-server-protocol.h"
37
38
39static int
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070040handle_sigusr1(int signal_number, void *data)
41{
42 struct weston_xserver *wxs = data;
43
44 /* We'd be safer if we actually had the struct
45 * signalfd_siginfo from the signalfd data and could verify
46 * this came from Xwayland.*/
47 weston_wm_create(wxs, wxs->wm_fd);
48 wl_event_source_remove(wxs->sigusr1_source);
49
50 return 1;
51}
52
53static int
Kristian Høgsberge10b1242012-05-21 16:48:05 -040054weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
55{
Tiago Vignattif2684462012-11-30 17:19:56 -020056 struct weston_xserver *wxs = data;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070057 char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8];
58 int sv[2], wm[2], fd;
Maksim Melnikau92de1442013-08-14 22:33:10 +030059 char *xserver = NULL;
60 struct weston_config_section *section;
Kristian Høgsberge10b1242012-05-21 16:48:05 -040061
62 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070063 weston_log("wl connection socketpair failed\n");
64 return 1;
65 }
66
67 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
68 weston_log("X wm connection socketpair failed\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040069 return 1;
70 }
71
Tiago Vignattif2684462012-11-30 17:19:56 -020072 wxs->process.pid = fork();
73 switch (wxs->process.pid) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040074 case 0:
75 /* SOCK_CLOEXEC closes both ends, so we need to unset
76 * the flag on the client fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070077 fd = dup(sv[1]);
78 if (fd < 0)
79 goto fail;
80 snprintf(s, sizeof s, "%d", fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040081 setenv("WAYLAND_SOCKET", s, 1);
82
Tiago Vignattif2684462012-11-30 17:19:56 -020083 snprintf(display, sizeof display, ":%d", wxs->display);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040084
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070085 fd = dup(wxs->abstract_fd);
86 if (fd < 0)
87 goto fail;
88 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
89 fd = dup(wxs->unix_fd);
90 if (fd < 0)
91 goto fail;
92 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
93 fd = dup(wm[1]);
94 if (fd < 0)
95 goto fail;
96 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
97
98 section = weston_config_get_section(wxs->compositor->config,
99 "xwayland", NULL, NULL);
100 weston_config_section_get_string(section, "path",
101 &xserver, XSERVER_PATH);
102
103 /* Ignore SIGUSR1 in the child, which will make the X
104 * server send SIGUSR1 to the parent (weston) when
105 * it's done with initialization. During
106 * initialization the X server will round trip and
107 * block on the wayland compositor, so avoid making
108 * blocking requests (like xcb_connect_to_fd) until
109 * it's done with that. */
110 signal(SIGUSR1, SIG_IGN);
Maksim Melnikau92de1442013-08-14 22:33:10 +0300111
112 if (execl(xserver,
113 xserver,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400114 display,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400115 "-rootless",
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700116 "-listen", abstract_fd,
117 "-listen", unix_fd,
118 "-wm", wm_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400119 "-terminate",
120 NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200121 weston_log("exec failed: %m\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700122 fail:
Kristian Høgsberga58290b2013-06-18 01:03:02 -0400123 _exit(EXIT_FAILURE);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400124
125 default:
Tiago Vignattif2684462012-11-30 17:19:56 -0200126 weston_log("forked X server, pid %d\n", wxs->process.pid);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400127
128 close(sv[1]);
Tiago Vignattif2684462012-11-30 17:19:56 -0200129 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400130
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700131 close(wm[1]);
132 wxs->wm_fd = wm[0];
133
Tiago Vignattif2684462012-11-30 17:19:56 -0200134 weston_watch_process(&wxs->process);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400135
Tiago Vignattif2684462012-11-30 17:19:56 -0200136 wl_event_source_remove(wxs->abstract_source);
137 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400138 break;
139
140 case -1:
Martin Minarik6d118362012-06-07 18:01:59 +0200141 weston_log( "failed to fork\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400142 break;
143 }
144
145 return 1;
146}
147
148static void
149weston_xserver_shutdown(struct weston_xserver *wxs)
150{
151 char path[256];
152
153 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
154 unlink(path);
155 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
156 unlink(path);
157 if (wxs->process.pid == 0) {
158 wl_event_source_remove(wxs->abstract_source);
159 wl_event_source_remove(wxs->unix_source);
160 }
161 close(wxs->abstract_fd);
162 close(wxs->unix_fd);
163 if (wxs->wm)
164 weston_wm_destroy(wxs->wm);
165 wxs->loop = NULL;
166}
167
168static void
169weston_xserver_cleanup(struct weston_process *process, int status)
170{
Tiago Vignattif2684462012-11-30 17:19:56 -0200171 struct weston_xserver *wxs =
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400172 container_of(process, struct weston_xserver, process);
173
Tiago Vignattif2684462012-11-30 17:19:56 -0200174 wxs->process.pid = 0;
175 wxs->client = NULL;
176 wxs->resource = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400177
Tiago Vignattif2684462012-11-30 17:19:56 -0200178 wxs->abstract_source =
179 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400180 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200181 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400182
Tiago Vignattif2684462012-11-30 17:19:56 -0200183 wxs->unix_source =
184 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400185 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200186 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400187
Tiago Vignattif2684462012-11-30 17:19:56 -0200188 if (wxs->wm) {
Martin Minarik6d118362012-06-07 18:01:59 +0200189 weston_log("xserver exited, code %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200190 weston_wm_destroy(wxs->wm);
191 wxs->wm = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400192 } else {
193 /* If the X server crashes before it binds to the
194 * xserver interface, shut down and don't try
195 * again. */
Martin Minarik6d118362012-06-07 18:01:59 +0200196 weston_log("xserver crashing too fast: %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200197 weston_xserver_shutdown(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400198 }
199}
200
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400201static int
202bind_to_abstract_socket(int display)
203{
204 struct sockaddr_un addr;
205 socklen_t size, name_size;
206 int fd;
207
208 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
209 if (fd < 0)
210 return -1;
211
212 addr.sun_family = AF_LOCAL;
213 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
214 "%c/tmp/.X11-unix/X%d", 0, display);
215 size = offsetof(struct sockaddr_un, sun_path) + name_size;
216 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200217 weston_log("failed to bind to @%s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400218 addr.sun_path + 1, strerror(errno));
219 close(fd);
220 return -1;
221 }
222
223 if (listen(fd, 1) < 0) {
224 close(fd);
225 return -1;
226 }
227
228 return fd;
229}
230
231static int
232bind_to_unix_socket(int display)
233{
234 struct sockaddr_un addr;
235 socklen_t size, name_size;
236 int fd;
237
238 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
239 if (fd < 0)
240 return -1;
241
242 addr.sun_family = AF_LOCAL;
243 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
244 "/tmp/.X11-unix/X%d", display) + 1;
245 size = offsetof(struct sockaddr_un, sun_path) + name_size;
246 unlink(addr.sun_path);
247 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200248 weston_log("failed to bind to %s (%s)\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400249 addr.sun_path, strerror(errno));
250 close(fd);
251 return -1;
252 }
253
254 if (listen(fd, 1) < 0) {
255 unlink(addr.sun_path);
256 close(fd);
257 return -1;
258 }
259
260 return fd;
261}
262
263static int
264create_lockfile(int display, char *lockfile, size_t lsize)
265{
266 char pid[16], *end;
267 int fd, size;
268 pid_t other;
269
270 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
271 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
272 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800273 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400274 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200275 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400276 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000277 if (fd >= 0)
278 close (fd);
279
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400280 errno = EEXIST;
281 return -1;
282 }
283
284 other = strtol(pid, &end, 0);
285 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200286 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400287 lockfile);
288 close(fd);
289 errno = EEXIST;
290 return -1;
291 }
292
293 if (kill(other, 0) < 0 && errno == ESRCH) {
294 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400296 close(fd);
297 if (unlink(lockfile))
298 /* If we fail to unlink, return EEXIST
299 so we try the next display number.*/
300 errno = EEXIST;
301 else
302 errno = EAGAIN;
303 return -1;
304 }
305
Martin Olsson19721412012-07-08 03:03:45 +0200306 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400307 errno = EEXIST;
308 return -1;
309 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200310 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400311 lockfile, strerror(errno));
312 return -1;
313 }
314
315 /* Subtle detail: we use the pid of the wayland
316 * compositor, not the xserver in the lock file. */
317 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
318 if (write(fd, pid, size) != size) {
319 unlink(lockfile);
320 close(fd);
321 return -1;
322 }
323
324 close(fd);
325
326 return 0;
327}
328
329static void
330weston_xserver_destroy(struct wl_listener *l, void *data)
331{
332 struct weston_xserver *wxs =
333 container_of(l, struct weston_xserver, destroy_listener);
334
335 if (!wxs)
336 return;
337
338 if (wxs->loop)
339 weston_xserver_shutdown(wxs);
340
341 free(wxs);
342}
343
344WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500345module_init(struct weston_compositor *compositor,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700346 int *argc, char *argv[])
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500347
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400348{
349 struct wl_display *display = compositor->wl_display;
Tiago Vignattif2684462012-11-30 17:19:56 -0200350 struct weston_xserver *wxs;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400351 char lockfile[256], display_name[8];
352
Peter Huttererf3d62272013-08-08 11:57:05 +1000353 wxs = zalloc(sizeof *wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200354 wxs->process.cleanup = weston_xserver_cleanup;
355 wxs->wl_display = display;
356 wxs->compositor = compositor;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400357
Tiago Vignattif2684462012-11-30 17:19:56 -0200358 wxs->display = 0;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400359
360 retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200361 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400362 if (errno == EAGAIN) {
363 goto retry;
364 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200365 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400366 goto retry;
367 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200368 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400369 return -1;
370 }
371 }
372
Tiago Vignattif2684462012-11-30 17:19:56 -0200373 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
374 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
375 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400376 unlink(lockfile);
377 goto retry;
378 }
379
Tiago Vignattif2684462012-11-30 17:19:56 -0200380 wxs->unix_fd = bind_to_unix_socket(wxs->display);
381 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400382 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200383 close(wxs->abstract_fd);
384 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400385 return -1;
386 }
387
Tiago Vignattif2684462012-11-30 17:19:56 -0200388 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200389 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400390 setenv("DISPLAY", display_name, 1);
391
Tiago Vignattif2684462012-11-30 17:19:56 -0200392 wxs->loop = wl_display_get_event_loop(display);
393 wxs->abstract_source =
394 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400395 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200396 weston_xserver_handle_event, wxs);
397 wxs->unix_source =
398 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400399 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200400 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400401
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700402 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
403 handle_sigusr1, wxs);
Tiago Vignattif2684462012-11-30 17:19:56 -0200404 wxs->destroy_listener.notify = weston_xserver_destroy;
405 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400406
407 return 0;
408}