blob: b7aee3b46d8580e5e51359ab2884ffd9ecdf1cbd [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"
Giulio Camuffo9c764df2016-06-29 11:54:27 +020039#include "xwayland-api.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070040#include "shared/helpers.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030041#include "compositor/weston.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040042
43static int
44weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
45{
Tiago Vignattif2684462012-11-30 17:19:56 -020046 struct weston_xserver *wxs = data;
Giulio Camuffo9c764df2016-06-29 11:54:27 +020047 char display[8];
Kristian Høgsberge10b1242012-05-21 16:48:05 -040048
Giulio Camuffo9c764df2016-06-29 11:54:27 +020049 snprintf(display, sizeof display, ":%d", wxs->display);
50
51 wxs->pid = wxs->spawn_func(wxs->user_data, display, wxs->abstract_fd, wxs->unix_fd);
52 if (wxs->pid == -1) {
53 weston_log("Failed to spawn the Xwayland server\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070054 return 1;
55 }
56
Giulio Camuffo9c764df2016-06-29 11:54:27 +020057 weston_log("Spawned Xwayland server, pid %d\n", wxs->pid);
58 wl_event_source_remove(wxs->abstract_source);
59 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040060
61 return 1;
62}
63
64static void
65weston_xserver_shutdown(struct weston_xserver *wxs)
66{
67 char path[256];
68
69 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
70 unlink(path);
71 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
72 unlink(path);
Giulio Camuffo9c764df2016-06-29 11:54:27 +020073 if (wxs->pid == 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040074 wl_event_source_remove(wxs->abstract_source);
75 wl_event_source_remove(wxs->unix_source);
76 }
77 close(wxs->abstract_fd);
78 close(wxs->unix_fd);
Dima Ryazanov434cc232014-06-19 01:03:31 -070079 if (wxs->wm) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040080 weston_wm_destroy(wxs->wm);
Dima Ryazanov434cc232014-06-19 01:03:31 -070081 wxs->wm = NULL;
82 }
Kristian Høgsberge10b1242012-05-21 16:48:05 -040083 wxs->loop = NULL;
84}
85
Kristian Høgsberge10b1242012-05-21 16:48:05 -040086static int
87bind_to_abstract_socket(int display)
88{
89 struct sockaddr_un addr;
90 socklen_t size, name_size;
91 int fd;
92
93 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
94 if (fd < 0)
95 return -1;
96
97 addr.sun_family = AF_LOCAL;
98 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
99 "%c/tmp/.X11-unix/X%d", 0, display);
100 size = offsetof(struct sockaddr_un, sun_path) + name_size;
101 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400102 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400103 close(fd);
104 return -1;
105 }
106
107 if (listen(fd, 1) < 0) {
108 close(fd);
109 return -1;
110 }
111
112 return fd;
113}
114
115static int
116bind_to_unix_socket(int display)
117{
118 struct sockaddr_un addr;
119 socklen_t size, name_size;
120 int fd;
121
122 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
123 if (fd < 0)
124 return -1;
125
126 addr.sun_family = AF_LOCAL;
127 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
128 "/tmp/.X11-unix/X%d", display) + 1;
129 size = offsetof(struct sockaddr_un, sun_path) + name_size;
130 unlink(addr.sun_path);
131 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400132 weston_log("failed to bind to %s: %m\n", addr.sun_path);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400133 close(fd);
134 return -1;
135 }
136
137 if (listen(fd, 1) < 0) {
138 unlink(addr.sun_path);
139 close(fd);
140 return -1;
141 }
142
143 return fd;
144}
145
146static int
147create_lockfile(int display, char *lockfile, size_t lsize)
148{
149 char pid[16], *end;
150 int fd, size;
151 pid_t other;
152
153 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
154 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
155 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800156 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400157 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200158 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400159 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000160 if (fd >= 0)
161 close (fd);
162
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400163 errno = EEXIST;
164 return -1;
165 }
166
Bryce Harrington375759e2016-07-12 16:51:27 -0700167 other = strtol(pid, &end, 10);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400168 if (end != pid + 10) {
Martin Minarik6d118362012-06-07 18:01:59 +0200169 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400170 lockfile);
171 close(fd);
172 errno = EEXIST;
173 return -1;
174 }
175
176 if (kill(other, 0) < 0 && errno == ESRCH) {
177 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200178 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400179 close(fd);
180 if (unlink(lockfile))
181 /* If we fail to unlink, return EEXIST
182 so we try the next display number.*/
183 errno = EEXIST;
184 else
185 errno = EAGAIN;
186 return -1;
187 }
188
Martin Olsson19721412012-07-08 03:03:45 +0200189 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400190 errno = EEXIST;
191 return -1;
192 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200193 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400194 lockfile, strerror(errno));
195 return -1;
196 }
197
198 /* Subtle detail: we use the pid of the wayland
199 * compositor, not the xserver in the lock file. */
200 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
201 if (write(fd, pid, size) != size) {
202 unlink(lockfile);
203 close(fd);
204 return -1;
205 }
206
207 close(fd);
208
209 return 0;
210}
211
212static void
213weston_xserver_destroy(struct wl_listener *l, void *data)
214{
215 struct weston_xserver *wxs =
216 container_of(l, struct weston_xserver, destroy_listener);
217
218 if (!wxs)
219 return;
220
221 if (wxs->loop)
222 weston_xserver_shutdown(wxs);
223
224 free(wxs);
225}
226
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200227static struct weston_xwayland *
228weston_xwayland_get(struct weston_compositor *compositor)
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400229{
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200230 struct wl_listener *listener;
Tiago Vignattif2684462012-11-30 17:19:56 -0200231 struct weston_xserver *wxs;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200232
233 listener = wl_signal_get(&compositor->destroy_signal,
234 weston_xserver_destroy);
235 if (!listener)
236 return NULL;
237
238 wxs = wl_container_of(listener, wxs, destroy_listener);
239 return (struct weston_xwayland *)wxs;
240}
241
242static int
243weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
244 weston_xwayland_spawn_xserver_func_t spawn_func)
245{
246 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400247 char lockfile[256], display_name[8];
248
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200249 wxs->user_data = user_data;
250 wxs->spawn_func = spawn_func;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400251
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200252retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200253 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400254 if (errno == EAGAIN) {
255 goto retry;
256 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200257 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400258 goto retry;
259 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200260 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400261 return -1;
262 }
263 }
264
Tiago Vignattif2684462012-11-30 17:19:56 -0200265 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
266 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
267 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400268 unlink(lockfile);
269 goto retry;
270 }
271
Tiago Vignattif2684462012-11-30 17:19:56 -0200272 wxs->unix_fd = bind_to_unix_socket(wxs->display);
273 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400274 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200275 close(wxs->abstract_fd);
276 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400277 return -1;
278 }
279
Tiago Vignattif2684462012-11-30 17:19:56 -0200280 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200281 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400282 setenv("DISPLAY", display_name, 1);
283
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200284 wxs->loop = wl_display_get_event_loop(wxs->wl_display);
Tiago Vignattif2684462012-11-30 17:19:56 -0200285 wxs->abstract_source =
286 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400287 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200288 weston_xserver_handle_event, wxs);
289 wxs->unix_source =
290 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400291 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200292 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400293
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200294 return 0;
295}
296
297static void
298weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
299 struct wl_client *client, int wm_fd)
300{
301 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
302 wxs->wm = weston_wm_create(wxs, wm_fd);
303 wxs->client = client;
304}
305
306static void
307weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
308 int exit_status)
309{
310 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
311
312 wxs->pid = 0;
313 wxs->client = NULL;
314
315 wxs->abstract_source =
316 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
317 WL_EVENT_READABLE,
318 weston_xserver_handle_event, wxs);
319 wxs->unix_source =
320 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
321 WL_EVENT_READABLE,
322 weston_xserver_handle_event, wxs);
323
324 if (wxs->wm) {
325 weston_log("xserver exited, code %d\n", exit_status);
326 weston_wm_destroy(wxs->wm);
327 wxs->wm = NULL;
328 } else {
329 /* If the X server crashes before it binds to the
330 * xserver interface, shut down and don't try
331 * again. */
332 weston_log("xserver crashing too fast: %d\n", exit_status);
333 weston_xserver_shutdown(wxs);
334 }
335}
336
337const struct weston_xwayland_api api = {
338 weston_xwayland_get,
339 weston_xwayland_listen,
340 weston_xwayland_xserver_loaded,
341 weston_xwayland_xserver_exited,
342};
343
344WL_EXPORT int
345module_init(struct weston_compositor *compositor,
346 int *argc, char *argv[])
347
348{
349 struct wl_display *display = compositor->wl_display;
350 struct weston_xserver *wxs;
351 int ret;
352
353 wxs = zalloc(sizeof *wxs);
354 if (wxs == NULL)
355 return -1;
356 wxs->wl_display = display;
357 wxs->compositor = compositor;
358
359 ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
360 &api, sizeof(api));
361 if (ret < 0) {
362 weston_log("Failed to register the xwayland module API.\n");
363 free(wxs);
364 return -1;
365 }
366
Tiago Vignattif2684462012-11-30 17:19:56 -0200367 wxs->destroy_listener.notify = weston_xserver_destroy;
368 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400369
370 return 0;
371}