blob: a83784c67ce5f7fcace50139cc67c03f14812f70 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberge10b1242012-05-21 16:48:05 -040030#include <stdio.h>
31#include <string.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <unistd.h>
37#include <signal.h>
38
39#include "xwayland.h"
Giulio Camuffo9c764df2016-06-29 11:54:27 +020040#include "xwayland-api.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070041#include "shared/helpers.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030042#include "compositor/weston.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040043
44static int
45weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
46{
Tiago Vignattif2684462012-11-30 17:19:56 -020047 struct weston_xserver *wxs = data;
Giulio Camuffo9c764df2016-06-29 11:54:27 +020048 char display[8];
Kristian Høgsberge10b1242012-05-21 16:48:05 -040049
Giulio Camuffo9c764df2016-06-29 11:54:27 +020050 snprintf(display, sizeof display, ":%d", wxs->display);
51
52 wxs->pid = wxs->spawn_func(wxs->user_data, display, wxs->abstract_fd, wxs->unix_fd);
53 if (wxs->pid == -1) {
54 weston_log("Failed to spawn the Xwayland server\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070055 return 1;
56 }
57
Giulio Camuffo9c764df2016-06-29 11:54:27 +020058 weston_log("Spawned Xwayland server, pid %d\n", wxs->pid);
59 wl_event_source_remove(wxs->abstract_source);
60 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040061
62 return 1;
63}
64
65static void
66weston_xserver_shutdown(struct weston_xserver *wxs)
67{
68 char path[256];
69
70 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
71 unlink(path);
72 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
73 unlink(path);
Giulio Camuffo9c764df2016-06-29 11:54:27 +020074 if (wxs->pid == 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040075 wl_event_source_remove(wxs->abstract_source);
76 wl_event_source_remove(wxs->unix_source);
77 }
78 close(wxs->abstract_fd);
79 close(wxs->unix_fd);
Dima Ryazanov434cc232014-06-19 01:03:31 -070080 if (wxs->wm) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040081 weston_wm_destroy(wxs->wm);
Dima Ryazanov434cc232014-06-19 01:03:31 -070082 wxs->wm = NULL;
83 }
Kristian Høgsberge10b1242012-05-21 16:48:05 -040084 wxs->loop = NULL;
85}
86
Kristian Høgsberge10b1242012-05-21 16:48:05 -040087static int
88bind_to_abstract_socket(int display)
89{
90 struct sockaddr_un addr;
91 socklen_t size, name_size;
92 int fd;
93
94 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
95 if (fd < 0)
96 return -1;
97
98 addr.sun_family = AF_LOCAL;
99 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
100 "%c/tmp/.X11-unix/X%d", 0, display);
101 size = offsetof(struct sockaddr_un, sun_path) + name_size;
102 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400103 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400104 close(fd);
105 return -1;
106 }
107
108 if (listen(fd, 1) < 0) {
109 close(fd);
110 return -1;
111 }
112
113 return fd;
114}
115
116static int
117bind_to_unix_socket(int display)
118{
119 struct sockaddr_un addr;
120 socklen_t size, name_size;
121 int fd;
122
123 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
124 if (fd < 0)
125 return -1;
126
127 addr.sun_family = AF_LOCAL;
128 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
129 "/tmp/.X11-unix/X%d", display) + 1;
130 size = offsetof(struct sockaddr_un, sun_path) + name_size;
131 unlink(addr.sun_path);
132 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400133 weston_log("failed to bind to %s: %m\n", addr.sun_path);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400134 close(fd);
135 return -1;
136 }
137
138 if (listen(fd, 1) < 0) {
139 unlink(addr.sun_path);
140 close(fd);
141 return -1;
142 }
143
144 return fd;
145}
146
147static int
148create_lockfile(int display, char *lockfile, size_t lsize)
149{
150 char pid[16], *end;
151 int fd, size;
152 pid_t other;
153
154 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
155 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
156 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800157 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400158 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200159 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400160 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000161 if (fd >= 0)
162 close (fd);
163
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400164 errno = EEXIST;
165 return -1;
166 }
167
Bryce Harrington139fcab2016-08-03 17:40:49 -0700168 errno = 0;
Bryce Harrington375759e2016-07-12 16:51:27 -0700169 other = strtol(pid, &end, 10);
Bryce Harrington139fcab2016-08-03 17:40:49 -0700170 if (errno != 0 || end == pid || *end != '\0') {
Martin Minarik6d118362012-06-07 18:01:59 +0200171 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400172 lockfile);
173 close(fd);
174 errno = EEXIST;
175 return -1;
176 }
177
178 if (kill(other, 0) < 0 && errno == ESRCH) {
179 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200180 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400181 close(fd);
182 if (unlink(lockfile))
183 /* If we fail to unlink, return EEXIST
184 so we try the next display number.*/
185 errno = EEXIST;
186 else
187 errno = EAGAIN;
188 return -1;
189 }
190
Martin Olsson19721412012-07-08 03:03:45 +0200191 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400192 errno = EEXIST;
193 return -1;
194 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200195 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400196 lockfile, strerror(errno));
197 return -1;
198 }
199
200 /* Subtle detail: we use the pid of the wayland
201 * compositor, not the xserver in the lock file. */
202 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
203 if (write(fd, pid, size) != size) {
204 unlink(lockfile);
205 close(fd);
206 return -1;
207 }
208
209 close(fd);
210
211 return 0;
212}
213
214static void
215weston_xserver_destroy(struct wl_listener *l, void *data)
216{
217 struct weston_xserver *wxs =
218 container_of(l, struct weston_xserver, destroy_listener);
219
220 if (!wxs)
221 return;
222
223 if (wxs->loop)
224 weston_xserver_shutdown(wxs);
225
226 free(wxs);
227}
228
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200229static struct weston_xwayland *
230weston_xwayland_get(struct weston_compositor *compositor)
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400231{
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200232 struct wl_listener *listener;
Tiago Vignattif2684462012-11-30 17:19:56 -0200233 struct weston_xserver *wxs;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200234
235 listener = wl_signal_get(&compositor->destroy_signal,
236 weston_xserver_destroy);
237 if (!listener)
238 return NULL;
239
240 wxs = wl_container_of(listener, wxs, destroy_listener);
241 return (struct weston_xwayland *)wxs;
242}
243
244static int
245weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
246 weston_xwayland_spawn_xserver_func_t spawn_func)
247{
248 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400249 char lockfile[256], display_name[8];
250
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200251 wxs->user_data = user_data;
252 wxs->spawn_func = spawn_func;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400253
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200254retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200255 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400256 if (errno == EAGAIN) {
257 goto retry;
258 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200259 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400260 goto retry;
261 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200262 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400263 return -1;
264 }
265 }
266
Tiago Vignattif2684462012-11-30 17:19:56 -0200267 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
268 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
269 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400270 unlink(lockfile);
271 goto retry;
272 }
273
Tiago Vignattif2684462012-11-30 17:19:56 -0200274 wxs->unix_fd = bind_to_unix_socket(wxs->display);
275 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400276 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200277 close(wxs->abstract_fd);
278 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400279 return -1;
280 }
281
Tiago Vignattif2684462012-11-30 17:19:56 -0200282 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200283 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400284 setenv("DISPLAY", display_name, 1);
285
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200286 wxs->loop = wl_display_get_event_loop(wxs->wl_display);
Tiago Vignattif2684462012-11-30 17:19:56 -0200287 wxs->abstract_source =
288 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400289 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200290 weston_xserver_handle_event, wxs);
291 wxs->unix_source =
292 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400293 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200294 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400295
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200296 return 0;
297}
298
299static void
300weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
301 struct wl_client *client, int wm_fd)
302{
303 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
304 wxs->wm = weston_wm_create(wxs, wm_fd);
305 wxs->client = client;
306}
307
308static void
309weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
310 int exit_status)
311{
312 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
313
314 wxs->pid = 0;
315 wxs->client = NULL;
316
317 wxs->abstract_source =
318 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
319 WL_EVENT_READABLE,
320 weston_xserver_handle_event, wxs);
321 wxs->unix_source =
322 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
323 WL_EVENT_READABLE,
324 weston_xserver_handle_event, wxs);
325
326 if (wxs->wm) {
327 weston_log("xserver exited, code %d\n", exit_status);
328 weston_wm_destroy(wxs->wm);
329 wxs->wm = NULL;
330 } else {
331 /* If the X server crashes before it binds to the
332 * xserver interface, shut down and don't try
333 * again. */
334 weston_log("xserver crashing too fast: %d\n", exit_status);
335 weston_xserver_shutdown(wxs);
336 }
337}
338
339const struct weston_xwayland_api api = {
340 weston_xwayland_get,
341 weston_xwayland_listen,
342 weston_xwayland_xserver_loaded,
343 weston_xwayland_xserver_exited,
344};
345
346WL_EXPORT int
347module_init(struct weston_compositor *compositor,
348 int *argc, char *argv[])
349
350{
351 struct wl_display *display = compositor->wl_display;
352 struct weston_xserver *wxs;
353 int ret;
354
355 wxs = zalloc(sizeof *wxs);
356 if (wxs == NULL)
357 return -1;
358 wxs->wl_display = display;
359 wxs->compositor = compositor;
360
361 ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
362 &api, sizeof(api));
363 if (ret < 0) {
364 weston_log("Failed to register the xwayland module API.\n");
365 free(wxs);
366 return -1;
367 }
368
Tiago Vignattif2684462012-11-30 17:19:56 -0200369 wxs->destroy_listener.notify = weston_xserver_destroy;
370 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400371
372 return 0;
373}