blob: 32ec1ae6dfd4669228cbdcbc9826cdcdeb615298 [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
40weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
41{
Tiago Vignattif2684462012-11-30 17:19:56 -020042 struct weston_xserver *wxs = data;
Kristian Høgsberge10b1242012-05-21 16:48:05 -040043 char display[8], s[8];
44 int sv[2], client_fd;
45
46 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +020047 weston_log("socketpair failed\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040048 return 1;
49 }
50
Tiago Vignattif2684462012-11-30 17:19:56 -020051 wxs->process.pid = fork();
52 switch (wxs->process.pid) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040053 case 0:
54 /* SOCK_CLOEXEC closes both ends, so we need to unset
55 * the flag on the client fd. */
56 client_fd = dup(sv[1]);
57 if (client_fd < 0)
58 return 1;
59
60 snprintf(s, sizeof s, "%d", client_fd);
61 setenv("WAYLAND_SOCKET", s, 1);
62
Tiago Vignattif2684462012-11-30 17:19:56 -020063 snprintf(display, sizeof display, ":%d", wxs->display);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040064
65 if (execl(XSERVER_PATH,
66 XSERVER_PATH,
67 display,
68 "-wayland",
69 "-rootless",
70 "-retro",
71 "-nolisten", "all",
72 "-terminate",
73 NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +020074 weston_log("exec failed: %m\n");
Kristian Høgsberga58290b2013-06-18 01:03:02 -040075 _exit(EXIT_FAILURE);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040076
77 default:
Tiago Vignattif2684462012-11-30 17:19:56 -020078 weston_log("forked X server, pid %d\n", wxs->process.pid);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040079
80 close(sv[1]);
Tiago Vignattif2684462012-11-30 17:19:56 -020081 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040082
Tiago Vignattif2684462012-11-30 17:19:56 -020083 weston_watch_process(&wxs->process);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040084
Tiago Vignattif2684462012-11-30 17:19:56 -020085 wl_event_source_remove(wxs->abstract_source);
86 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040087 break;
88
89 case -1:
Martin Minarik6d118362012-06-07 18:01:59 +020090 weston_log( "failed to fork\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -040091 break;
92 }
93
94 return 1;
95}
96
97static void
98weston_xserver_shutdown(struct weston_xserver *wxs)
99{
100 char path[256];
101
102 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
103 unlink(path);
104 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
105 unlink(path);
106 if (wxs->process.pid == 0) {
107 wl_event_source_remove(wxs->abstract_source);
108 wl_event_source_remove(wxs->unix_source);
109 }
110 close(wxs->abstract_fd);
111 close(wxs->unix_fd);
112 if (wxs->wm)
113 weston_wm_destroy(wxs->wm);
114 wxs->loop = NULL;
115}
116
117static void
118weston_xserver_cleanup(struct weston_process *process, int status)
119{
Tiago Vignattif2684462012-11-30 17:19:56 -0200120 struct weston_xserver *wxs =
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400121 container_of(process, struct weston_xserver, process);
122
Tiago Vignattif2684462012-11-30 17:19:56 -0200123 wxs->process.pid = 0;
124 wxs->client = NULL;
125 wxs->resource = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400126
Tiago Vignattif2684462012-11-30 17:19:56 -0200127 wxs->abstract_source =
128 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400129 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200130 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400131
Tiago Vignattif2684462012-11-30 17:19:56 -0200132 wxs->unix_source =
133 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400134 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200135 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400136
Tiago Vignattif2684462012-11-30 17:19:56 -0200137 if (wxs->wm) {
Martin Minarik6d118362012-06-07 18:01:59 +0200138 weston_log("xserver exited, code %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200139 weston_wm_destroy(wxs->wm);
140 wxs->wm = NULL;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400141 } else {
142 /* If the X server crashes before it binds to the
143 * xserver interface, shut down and don't try
144 * again. */
Martin Minarik6d118362012-06-07 18:01:59 +0200145 weston_log("xserver crashing too fast: %d\n", status);
Tiago Vignattif2684462012-11-30 17:19:56 -0200146 weston_xserver_shutdown(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400147 }
148}
149
150static void
151bind_xserver(struct wl_client *client,
152 void *data, uint32_t version, uint32_t id)
153{
154 struct weston_xserver *wxs = data;
155
156 /* If it's a different client than the xserver we launched,
157 * don't start the wm. */
158 if (client != wxs->client)
159 return;
160
161 wxs->resource =
Jason Ekstranda85118c2013-06-27 20:17:02 -0500162 wl_resource_create(client, &xserver_interface,
163 1, id);
164 wl_resource_set_implementation(wxs->resource, &xserver_implementation,
165 wxs, NULL);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400166
167 wxs->wm = weston_wm_create(wxs);
168 if (wxs->wm == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200169 weston_log("failed to create wm\n");
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400170 }
171
172 xserver_send_listen_socket(wxs->resource, wxs->abstract_fd);
173 xserver_send_listen_socket(wxs->resource, wxs->unix_fd);
174}
175
176static int
177bind_to_abstract_socket(int display)
178{
179 struct sockaddr_un addr;
180 socklen_t size, name_size;
181 int fd;
182
183 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
184 if (fd < 0)
185 return -1;
186
187 addr.sun_family = AF_LOCAL;
188 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
189 "%c/tmp/.X11-unix/X%d", 0, display);
190 size = offsetof(struct sockaddr_un, sun_path) + name_size;
191 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200192 weston_log("failed to bind to @%s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400193 addr.sun_path + 1, strerror(errno));
194 close(fd);
195 return -1;
196 }
197
198 if (listen(fd, 1) < 0) {
199 close(fd);
200 return -1;
201 }
202
203 return fd;
204}
205
206static int
207bind_to_unix_socket(int display)
208{
209 struct sockaddr_un addr;
210 socklen_t size, name_size;
211 int fd;
212
213 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
214 if (fd < 0)
215 return -1;
216
217 addr.sun_family = AF_LOCAL;
218 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
219 "/tmp/.X11-unix/X%d", display) + 1;
220 size = offsetof(struct sockaddr_un, sun_path) + name_size;
221 unlink(addr.sun_path);
222 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200223 weston_log("failed to bind to %s (%s)\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400224 addr.sun_path, strerror(errno));
225 close(fd);
226 return -1;
227 }
228
229 if (listen(fd, 1) < 0) {
230 unlink(addr.sun_path);
231 close(fd);
232 return -1;
233 }
234
235 return fd;
236}
237
238static int
239create_lockfile(int display, char *lockfile, size_t lsize)
240{
241 char pid[16], *end;
242 int fd, size;
243 pid_t other;
244
245 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
246 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
247 if (fd < 0 && errno == EEXIST) {
248 fd = open(lockfile, O_CLOEXEC, O_RDONLY);
249 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200250 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400251 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000252 if (fd >= 0)
253 close (fd);
254
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400255 errno = EEXIST;
256 return -1;
257 }
258
259 other = strtol(pid, &end, 0);
260 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200261 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400262 lockfile);
263 close(fd);
264 errno = EEXIST;
265 return -1;
266 }
267
268 if (kill(other, 0) < 0 && errno == ESRCH) {
269 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200270 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400271 close(fd);
272 if (unlink(lockfile))
273 /* If we fail to unlink, return EEXIST
274 so we try the next display number.*/
275 errno = EEXIST;
276 else
277 errno = EAGAIN;
278 return -1;
279 }
280
Martin Olsson19721412012-07-08 03:03:45 +0200281 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400282 errno = EEXIST;
283 return -1;
284 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400286 lockfile, strerror(errno));
287 return -1;
288 }
289
290 /* Subtle detail: we use the pid of the wayland
291 * compositor, not the xserver in the lock file. */
292 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
293 if (write(fd, pid, size) != size) {
294 unlink(lockfile);
295 close(fd);
296 return -1;
297 }
298
299 close(fd);
300
301 return 0;
302}
303
304static void
305weston_xserver_destroy(struct wl_listener *l, void *data)
306{
307 struct weston_xserver *wxs =
308 container_of(l, struct weston_xserver, destroy_listener);
309
310 if (!wxs)
311 return;
312
313 if (wxs->loop)
314 weston_xserver_shutdown(wxs);
315
316 free(wxs);
317}
318
319WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500320module_init(struct weston_compositor *compositor,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700321 int *argc, char *argv[])
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500322
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400323{
324 struct wl_display *display = compositor->wl_display;
Tiago Vignattif2684462012-11-30 17:19:56 -0200325 struct weston_xserver *wxs;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400326 char lockfile[256], display_name[8];
327
Tiago Vignattif2684462012-11-30 17:19:56 -0200328 wxs = malloc(sizeof *wxs);
329 memset(wxs, 0, sizeof *wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400330
Tiago Vignattif2684462012-11-30 17:19:56 -0200331 wxs->process.cleanup = weston_xserver_cleanup;
332 wxs->wl_display = display;
333 wxs->compositor = compositor;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400334
Tiago Vignattif2684462012-11-30 17:19:56 -0200335 wxs->display = 0;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400336
337 retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200338 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400339 if (errno == EAGAIN) {
340 goto retry;
341 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200342 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400343 goto retry;
344 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200345 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400346 return -1;
347 }
348 }
349
Tiago Vignattif2684462012-11-30 17:19:56 -0200350 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
351 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
352 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400353 unlink(lockfile);
354 goto retry;
355 }
356
Tiago Vignattif2684462012-11-30 17:19:56 -0200357 wxs->unix_fd = bind_to_unix_socket(wxs->display);
358 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400359 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200360 close(wxs->abstract_fd);
361 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400362 return -1;
363 }
364
Tiago Vignattif2684462012-11-30 17:19:56 -0200365 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200366 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400367 setenv("DISPLAY", display_name, 1);
368
Tiago Vignattif2684462012-11-30 17:19:56 -0200369 wxs->loop = wl_display_get_event_loop(display);
370 wxs->abstract_source =
371 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400372 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200373 weston_xserver_handle_event, wxs);
374 wxs->unix_source =
375 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400376 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200377 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400378
Tiago Vignattif2684462012-11-30 17:19:56 -0200379 wl_display_add_global(display, &xserver_interface, wxs, bind_xserver);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400380
Tiago Vignattif2684462012-11-30 17:19:56 -0200381 wxs->destroy_listener.notify = weston_xserver_destroy;
382 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400383
384 return 0;
385}