blob: 15443cd1befe474df85625b6d3a7ae739b34fd18 [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 Harrington375759e2016-07-12 16:51:27 -0700168 other = strtol(pid, &end, 10);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400169 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200170 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400171 lockfile);
172 close(fd);
173 errno = EEXIST;
174 return -1;
175 }
176
177 if (kill(other, 0) < 0 && errno == ESRCH) {
178 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200179 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400180 close(fd);
181 if (unlink(lockfile))
182 /* If we fail to unlink, return EEXIST
183 so we try the next display number.*/
184 errno = EEXIST;
185 else
186 errno = EAGAIN;
187 return -1;
188 }
189
Martin Olsson19721412012-07-08 03:03:45 +0200190 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400191 errno = EEXIST;
192 return -1;
193 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200194 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400195 lockfile, strerror(errno));
196 return -1;
197 }
198
199 /* Subtle detail: we use the pid of the wayland
200 * compositor, not the xserver in the lock file. */
201 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
202 if (write(fd, pid, size) != size) {
203 unlink(lockfile);
204 close(fd);
205 return -1;
206 }
207
208 close(fd);
209
210 return 0;
211}
212
213static void
214weston_xserver_destroy(struct wl_listener *l, void *data)
215{
216 struct weston_xserver *wxs =
217 container_of(l, struct weston_xserver, destroy_listener);
218
219 if (!wxs)
220 return;
221
222 if (wxs->loop)
223 weston_xserver_shutdown(wxs);
224
225 free(wxs);
226}
227
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200228static struct weston_xwayland *
229weston_xwayland_get(struct weston_compositor *compositor)
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400230{
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200231 struct wl_listener *listener;
Tiago Vignattif2684462012-11-30 17:19:56 -0200232 struct weston_xserver *wxs;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200233
234 listener = wl_signal_get(&compositor->destroy_signal,
235 weston_xserver_destroy);
236 if (!listener)
237 return NULL;
238
239 wxs = wl_container_of(listener, wxs, destroy_listener);
240 return (struct weston_xwayland *)wxs;
241}
242
243static int
244weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
245 weston_xwayland_spawn_xserver_func_t spawn_func)
246{
247 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400248 char lockfile[256], display_name[8];
249
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200250 wxs->user_data = user_data;
251 wxs->spawn_func = spawn_func;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400252
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200253retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200254 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400255 if (errno == EAGAIN) {
256 goto retry;
257 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200258 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400259 goto retry;
260 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200261 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400262 return -1;
263 }
264 }
265
Tiago Vignattif2684462012-11-30 17:19:56 -0200266 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
267 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
268 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400269 unlink(lockfile);
270 goto retry;
271 }
272
Tiago Vignattif2684462012-11-30 17:19:56 -0200273 wxs->unix_fd = bind_to_unix_socket(wxs->display);
274 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400275 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200276 close(wxs->abstract_fd);
277 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400278 return -1;
279 }
280
Tiago Vignattif2684462012-11-30 17:19:56 -0200281 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200282 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400283 setenv("DISPLAY", display_name, 1);
284
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200285 wxs->loop = wl_display_get_event_loop(wxs->wl_display);
Tiago Vignattif2684462012-11-30 17:19:56 -0200286 wxs->abstract_source =
287 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400288 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200289 weston_xserver_handle_event, wxs);
290 wxs->unix_source =
291 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400292 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200293 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400294
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200295 return 0;
296}
297
298static void
299weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
300 struct wl_client *client, int wm_fd)
301{
302 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
303 wxs->wm = weston_wm_create(wxs, wm_fd);
304 wxs->client = client;
305}
306
307static void
308weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
309 int exit_status)
310{
311 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
312
313 wxs->pid = 0;
314 wxs->client = NULL;
315
316 wxs->abstract_source =
317 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
318 WL_EVENT_READABLE,
319 weston_xserver_handle_event, wxs);
320 wxs->unix_source =
321 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
322 WL_EVENT_READABLE,
323 weston_xserver_handle_event, wxs);
324
325 if (wxs->wm) {
326 weston_log("xserver exited, code %d\n", exit_status);
327 weston_wm_destroy(wxs->wm);
328 wxs->wm = NULL;
329 } else {
330 /* If the X server crashes before it binds to the
331 * xserver interface, shut down and don't try
332 * again. */
333 weston_log("xserver crashing too fast: %d\n", exit_status);
334 weston_xserver_shutdown(wxs);
335 }
336}
337
338const struct weston_xwayland_api api = {
339 weston_xwayland_get,
340 weston_xwayland_listen,
341 weston_xwayland_xserver_loaded,
342 weston_xwayland_xserver_exited,
343};
344
345WL_EXPORT int
346module_init(struct weston_compositor *compositor,
347 int *argc, char *argv[])
348
349{
350 struct wl_display *display = compositor->wl_display;
351 struct weston_xserver *wxs;
352 int ret;
353
354 wxs = zalloc(sizeof *wxs);
355 if (wxs == NULL)
356 return -1;
357 wxs->wl_display = display;
358 wxs->compositor = compositor;
359
360 ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
361 &api, sizeof(api));
362 if (ret < 0) {
363 weston_log("Failed to register the xwayland module API.\n");
364 free(wxs);
365 return -1;
366 }
367
Tiago Vignattif2684462012-11-30 17:19:56 -0200368 wxs->destroy_listener.notify = weston_xserver_destroy;
369 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400370
371 return 0;
372}