blob: 7702b4fb12b0ed34c8d5986ad91ebd25edc2f143 [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"
Pekka Paalaneneebb7dc2019-04-04 15:52:47 +030040#include <libweston/xwayland-api.h>
Jon Cruz867d50e2015-06-15 15:37:10 -070041#include "shared/helpers.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070042#include "shared/string-helpers.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) {
Antonio Borneo39578632019-04-26 23:57:31 +0200103 weston_log("failed to bind to @%s: %s\n", addr.sun_path + 1,
104 strerror(errno));
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400105 close(fd);
106 return -1;
107 }
108
109 if (listen(fd, 1) < 0) {
110 close(fd);
111 return -1;
112 }
113
114 return fd;
115}
116
117static int
118bind_to_unix_socket(int display)
119{
120 struct sockaddr_un addr;
121 socklen_t size, name_size;
122 int fd;
123
124 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
125 if (fd < 0)
126 return -1;
127
128 addr.sun_family = AF_LOCAL;
129 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
130 "/tmp/.X11-unix/X%d", display) + 1;
131 size = offsetof(struct sockaddr_un, sun_path) + name_size;
132 unlink(addr.sun_path);
133 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200134 weston_log("failed to bind to %s: %s\n", addr.sun_path,
135 strerror(errno));
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400136 close(fd);
137 return -1;
138 }
139
140 if (listen(fd, 1) < 0) {
141 unlink(addr.sun_path);
142 close(fd);
143 return -1;
144 }
145
146 return fd;
147}
148
149static int
150create_lockfile(int display, char *lockfile, size_t lsize)
151{
Daniel Stone08a35d32016-11-16 16:37:05 +0000152 /* 10 decimal characters, trailing LF and NUL byte; see comment
153 * at end of function. */
154 char pid[11];
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400155 int fd, size;
156 pid_t other;
157
158 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
159 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
160 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800161 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400162 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200163 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400164 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000165 if (fd >= 0)
166 close (fd);
167
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400168 errno = EEXIST;
169 return -1;
170 }
171
Daniel Stone08a35d32016-11-16 16:37:05 +0000172 /* Trim the trailing LF, or at least ensure it's NULL. */
Pekka Paalanen61beda62016-11-14 15:05:28 +0200173 pid[10] = '\0';
174
Bryce Harrington25a2bdd2016-08-03 17:40:52 -0700175 if (!safe_strtoint(pid, &other)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200176 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400177 lockfile);
178 close(fd);
179 errno = EEXIST;
180 return -1;
181 }
182
183 if (kill(other, 0) < 0 && errno == ESRCH) {
184 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200185 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400186 close(fd);
187 if (unlink(lockfile))
188 /* If we fail to unlink, return EEXIST
189 so we try the next display number.*/
190 errno = EEXIST;
191 else
192 errno = EAGAIN;
193 return -1;
194 }
195
Martin Olsson19721412012-07-08 03:03:45 +0200196 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400197 errno = EEXIST;
198 return -1;
199 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200200 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400201 lockfile, strerror(errno));
202 return -1;
203 }
204
Daniel Stone08a35d32016-11-16 16:37:05 +0000205 /* Subtle detail: we use the pid of the wayland compositor, not the
206 * xserver in the lock file.
207 * Also subtle is that we don't emit a trailing NUL to the file, so
208 * our size here is 11 rather than 12. */
209 size = dprintf(fd, "%10d\n", getpid());
210 if (size != 11) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400211 unlink(lockfile);
212 close(fd);
213 return -1;
214 }
215
216 close(fd);
217
218 return 0;
219}
220
221static void
222weston_xserver_destroy(struct wl_listener *l, void *data)
223{
224 struct weston_xserver *wxs =
225 container_of(l, struct weston_xserver, destroy_listener);
226
Derek Foremanc826d8f2022-01-18 16:50:39 -0600227 wl_list_remove(&wxs->destroy_listener.link);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400228
229 if (wxs->loop)
230 weston_xserver_shutdown(wxs);
231
Leandro Ribeirof0149642019-12-18 15:52:18 -0300232 weston_log_scope_destroy(wxs->wm_debug);
Pekka Paalanen9b72eb72017-10-12 13:18:14 +0200233
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400234 free(wxs);
235}
236
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200237static struct weston_xwayland *
238weston_xwayland_get(struct weston_compositor *compositor)
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400239{
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200240 struct wl_listener *listener;
Tiago Vignattif2684462012-11-30 17:19:56 -0200241 struct weston_xserver *wxs;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200242
243 listener = wl_signal_get(&compositor->destroy_signal,
244 weston_xserver_destroy);
245 if (!listener)
246 return NULL;
247
248 wxs = wl_container_of(listener, wxs, destroy_listener);
249 return (struct weston_xwayland *)wxs;
250}
251
252static int
253weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
254 weston_xwayland_spawn_xserver_func_t spawn_func)
255{
256 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400257 char lockfile[256], display_name[8];
258
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200259 wxs->user_data = user_data;
260 wxs->spawn_func = spawn_func;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400261
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200262retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200263 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400264 if (errno == EAGAIN) {
265 goto retry;
266 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200267 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400268 goto retry;
269 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200270 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400271 return -1;
272 }
273 }
274
Tiago Vignattif2684462012-11-30 17:19:56 -0200275 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
276 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
277 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400278 unlink(lockfile);
279 goto retry;
280 }
281
Tiago Vignattif2684462012-11-30 17:19:56 -0200282 wxs->unix_fd = bind_to_unix_socket(wxs->display);
283 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400284 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200285 close(wxs->abstract_fd);
286 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400287 return -1;
288 }
289
Tiago Vignattif2684462012-11-30 17:19:56 -0200290 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200291 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400292 setenv("DISPLAY", display_name, 1);
293
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200294 wxs->loop = wl_display_get_event_loop(wxs->wl_display);
Tiago Vignattif2684462012-11-30 17:19:56 -0200295 wxs->abstract_source =
296 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400297 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200298 weston_xserver_handle_event, wxs);
299 wxs->unix_source =
300 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400301 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200302 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400303
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200304 return 0;
305}
306
307static void
308weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
309 struct wl_client *client, int wm_fd)
310{
311 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
312 wxs->wm = weston_wm_create(wxs, wm_fd);
313 wxs->client = client;
314}
315
316static void
317weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
318 int exit_status)
319{
320 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
321
322 wxs->pid = 0;
323 wxs->client = NULL;
324
325 wxs->abstract_source =
326 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
327 WL_EVENT_READABLE,
328 weston_xserver_handle_event, wxs);
329 wxs->unix_source =
330 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
331 WL_EVENT_READABLE,
332 weston_xserver_handle_event, wxs);
333
334 if (wxs->wm) {
335 weston_log("xserver exited, code %d\n", exit_status);
336 weston_wm_destroy(wxs->wm);
337 wxs->wm = NULL;
338 } else {
339 /* If the X server crashes before it binds to the
340 * xserver interface, shut down and don't try
341 * again. */
342 weston_log("xserver crashing too fast: %d\n", exit_status);
343 weston_xserver_shutdown(wxs);
344 }
345}
346
347const struct weston_xwayland_api api = {
348 weston_xwayland_get,
349 weston_xwayland_listen,
350 weston_xwayland_xserver_loaded,
351 weston_xwayland_xserver_exited,
352};
Quentin Glidic955cec02016-08-12 10:41:35 +0200353extern const struct weston_xwayland_surface_api surface_api;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200354
355WL_EXPORT int
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +0100356weston_module_init(struct weston_compositor *compositor)
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200357
358{
359 struct wl_display *display = compositor->wl_display;
360 struct weston_xserver *wxs;
361 int ret;
362
363 wxs = zalloc(sizeof *wxs);
364 if (wxs == NULL)
365 return -1;
366 wxs->wl_display = display;
367 wxs->compositor = compositor;
368
Pekka Paalanen6ffbba32019-11-06 12:59:32 +0200369 if (!weston_compositor_add_destroy_listener_once(compositor,
370 &wxs->destroy_listener,
371 weston_xserver_destroy)) {
372 free(wxs);
373 return 0;
374 }
375
Quentin Glidic955cec02016-08-12 10:41:35 +0200376 if (weston_xwayland_get_api(compositor) != NULL ||
377 weston_xwayland_surface_get_api(compositor) != NULL) {
378 weston_log("The xwayland module APIs are already loaded.\n");
Pekka Paalanen6ffbba32019-11-06 12:59:32 +0200379 goto out_free;
Quentin Glidic955cec02016-08-12 10:41:35 +0200380 }
381
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200382 ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
383 &api, sizeof(api));
384 if (ret < 0) {
385 weston_log("Failed to register the xwayland module API.\n");
Pekka Paalanen6ffbba32019-11-06 12:59:32 +0200386 goto out_free;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200387 }
388
Quentin Glidic955cec02016-08-12 10:41:35 +0200389 ret = weston_plugin_api_register(compositor,
390 WESTON_XWAYLAND_SURFACE_API_NAME,
391 &surface_api, sizeof(surface_api));
392 if (ret < 0) {
393 weston_log("Failed to register the xwayland surface API.\n");
Pekka Paalanen6ffbba32019-11-06 12:59:32 +0200394 goto out_free;
Quentin Glidic955cec02016-08-12 10:41:35 +0200395 }
396
Marius Vlad1e2fda22019-04-07 19:07:16 +0300397 wxs->wm_debug =
Leandro Ribeirobdd45d62019-12-26 16:46:16 -0300398 weston_compositor_add_log_scope(wxs->compositor, "xwm-wm-x11",
399 "XWM's window management X11 events\n",
400 NULL, NULL, NULL);
Pekka Paalanen9b72eb72017-10-12 13:18:14 +0200401
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400402 return 0;
Pekka Paalanen6ffbba32019-11-06 12:59:32 +0200403
404out_free:
405 wl_list_remove(&wxs->destroy_listener.link);
406 free(wxs);
407 return -1;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400408}