blob: 97d7c6ee4f781c34487b61932880c847e9804e92 [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"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070042#include "shared/string-helpers.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030043#include "compositor/weston.h"
Kristian Høgsberge10b1242012-05-21 16:48:05 -040044
45static int
46weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
47{
Tiago Vignattif2684462012-11-30 17:19:56 -020048 struct weston_xserver *wxs = data;
Giulio Camuffo9c764df2016-06-29 11:54:27 +020049 char display[8];
Kristian Høgsberge10b1242012-05-21 16:48:05 -040050
Giulio Camuffo9c764df2016-06-29 11:54:27 +020051 snprintf(display, sizeof display, ":%d", wxs->display);
52
53 wxs->pid = wxs->spawn_func(wxs->user_data, display, wxs->abstract_fd, wxs->unix_fd);
54 if (wxs->pid == -1) {
55 weston_log("Failed to spawn the Xwayland server\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -070056 return 1;
57 }
58
Giulio Camuffo9c764df2016-06-29 11:54:27 +020059 weston_log("Spawned Xwayland server, pid %d\n", wxs->pid);
60 wl_event_source_remove(wxs->abstract_source);
61 wl_event_source_remove(wxs->unix_source);
Kristian Høgsberge10b1242012-05-21 16:48:05 -040062
63 return 1;
64}
65
66static void
67weston_xserver_shutdown(struct weston_xserver *wxs)
68{
69 char path[256];
70
71 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
72 unlink(path);
73 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
74 unlink(path);
Giulio Camuffo9c764df2016-06-29 11:54:27 +020075 if (wxs->pid == 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040076 wl_event_source_remove(wxs->abstract_source);
77 wl_event_source_remove(wxs->unix_source);
78 }
79 close(wxs->abstract_fd);
80 close(wxs->unix_fd);
Dima Ryazanov434cc232014-06-19 01:03:31 -070081 if (wxs->wm) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -040082 weston_wm_destroy(wxs->wm);
Dima Ryazanov434cc232014-06-19 01:03:31 -070083 wxs->wm = NULL;
84 }
Kristian Høgsberge10b1242012-05-21 16:48:05 -040085 wxs->loop = NULL;
86}
87
Kristian Høgsberge10b1242012-05-21 16:48:05 -040088static int
89bind_to_abstract_socket(int display)
90{
91 struct sockaddr_un addr;
92 socklen_t size, name_size;
93 int fd;
94
95 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
96 if (fd < 0)
97 return -1;
98
99 addr.sun_family = AF_LOCAL;
100 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
101 "%c/tmp/.X11-unix/X%d", 0, display);
102 size = offsetof(struct sockaddr_un, sun_path) + name_size;
103 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400104 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
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) {
Jasper St. Pierre47837392014-04-01 19:55:26 -0400134 weston_log("failed to bind to %s: %m\n", addr.sun_path);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400135 close(fd);
136 return -1;
137 }
138
139 if (listen(fd, 1) < 0) {
140 unlink(addr.sun_path);
141 close(fd);
142 return -1;
143 }
144
145 return fd;
146}
147
148static int
149create_lockfile(int display, char *lockfile, size_t lsize)
150{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -0700151 char pid[16];
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400152 int fd, size;
153 pid_t other;
154
155 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
156 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
157 if (fd < 0 && errno == EEXIST) {
Kristian Høgsberg5e647ca2014-02-01 20:44:54 -0800158 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400159 if (fd < 0 || read(fd, pid, 11) != 11) {
Martin Minarik6d118362012-06-07 18:01:59 +0200160 weston_log("can't read lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400161 lockfile, strerror(errno));
Rob Bradfordef940852012-12-05 18:47:07 +0000162 if (fd >= 0)
163 close (fd);
164
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400165 errno = EEXIST;
166 return -1;
167 }
168
Pekka Paalanen61beda62016-11-14 15:05:28 +0200169 /* Trim the newline, ensure terminated string. */
170 pid[10] = '\0';
171
Bryce Harrington25a2bdd2016-08-03 17:40:52 -0700172 if (!safe_strtoint(pid, &other)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200173 weston_log("can't parse lock file %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400174 lockfile);
175 close(fd);
176 errno = EEXIST;
177 return -1;
178 }
179
180 if (kill(other, 0) < 0 && errno == ESRCH) {
181 /* stale lock file; unlink and try again */
Martin Minarik6d118362012-06-07 18:01:59 +0200182 weston_log("unlinking stale lock file %s\n", lockfile);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400183 close(fd);
184 if (unlink(lockfile))
185 /* If we fail to unlink, return EEXIST
186 so we try the next display number.*/
187 errno = EEXIST;
188 else
189 errno = EAGAIN;
190 return -1;
191 }
192
Martin Olsson19721412012-07-08 03:03:45 +0200193 close(fd);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400194 errno = EEXIST;
195 return -1;
196 } else if (fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200197 weston_log("failed to create lock file %s: %s\n",
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400198 lockfile, strerror(errno));
199 return -1;
200 }
201
202 /* Subtle detail: we use the pid of the wayland
203 * compositor, not the xserver in the lock file. */
204 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
205 if (write(fd, pid, size) != size) {
206 unlink(lockfile);
207 close(fd);
208 return -1;
209 }
210
211 close(fd);
212
213 return 0;
214}
215
216static void
217weston_xserver_destroy(struct wl_listener *l, void *data)
218{
219 struct weston_xserver *wxs =
220 container_of(l, struct weston_xserver, destroy_listener);
221
222 if (!wxs)
223 return;
224
225 if (wxs->loop)
226 weston_xserver_shutdown(wxs);
227
228 free(wxs);
229}
230
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200231static struct weston_xwayland *
232weston_xwayland_get(struct weston_compositor *compositor)
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400233{
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200234 struct wl_listener *listener;
Tiago Vignattif2684462012-11-30 17:19:56 -0200235 struct weston_xserver *wxs;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200236
237 listener = wl_signal_get(&compositor->destroy_signal,
238 weston_xserver_destroy);
239 if (!listener)
240 return NULL;
241
242 wxs = wl_container_of(listener, wxs, destroy_listener);
243 return (struct weston_xwayland *)wxs;
244}
245
246static int
247weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
248 weston_xwayland_spawn_xserver_func_t spawn_func)
249{
250 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400251 char lockfile[256], display_name[8];
252
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200253 wxs->user_data = user_data;
254 wxs->spawn_func = spawn_func;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400255
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200256retry:
Tiago Vignattif2684462012-11-30 17:19:56 -0200257 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400258 if (errno == EAGAIN) {
259 goto retry;
260 } else if (errno == EEXIST) {
Tiago Vignattif2684462012-11-30 17:19:56 -0200261 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400262 goto retry;
263 } else {
Tiago Vignattif2684462012-11-30 17:19:56 -0200264 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400265 return -1;
266 }
267 }
268
Tiago Vignattif2684462012-11-30 17:19:56 -0200269 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
270 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
271 wxs->display++;
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400272 unlink(lockfile);
273 goto retry;
274 }
275
Tiago Vignattif2684462012-11-30 17:19:56 -0200276 wxs->unix_fd = bind_to_unix_socket(wxs->display);
277 if (wxs->unix_fd < 0) {
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400278 unlink(lockfile);
Tiago Vignattif2684462012-11-30 17:19:56 -0200279 close(wxs->abstract_fd);
280 free(wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400281 return -1;
282 }
283
Tiago Vignattif2684462012-11-30 17:19:56 -0200284 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("xserver listening on display %s\n", display_name);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400286 setenv("DISPLAY", display_name, 1);
287
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200288 wxs->loop = wl_display_get_event_loop(wxs->wl_display);
Tiago Vignattif2684462012-11-30 17:19:56 -0200289 wxs->abstract_source =
290 wl_event_loop_add_fd(wxs->loop, wxs->abstract_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);
293 wxs->unix_source =
294 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400295 WL_EVENT_READABLE,
Tiago Vignattif2684462012-11-30 17:19:56 -0200296 weston_xserver_handle_event, wxs);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400297
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200298 return 0;
299}
300
301static void
302weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
303 struct wl_client *client, int wm_fd)
304{
305 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
306 wxs->wm = weston_wm_create(wxs, wm_fd);
307 wxs->client = client;
308}
309
310static void
311weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
312 int exit_status)
313{
314 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
315
316 wxs->pid = 0;
317 wxs->client = NULL;
318
319 wxs->abstract_source =
320 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
321 WL_EVENT_READABLE,
322 weston_xserver_handle_event, wxs);
323 wxs->unix_source =
324 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
325 WL_EVENT_READABLE,
326 weston_xserver_handle_event, wxs);
327
328 if (wxs->wm) {
329 weston_log("xserver exited, code %d\n", exit_status);
330 weston_wm_destroy(wxs->wm);
331 wxs->wm = NULL;
332 } else {
333 /* If the X server crashes before it binds to the
334 * xserver interface, shut down and don't try
335 * again. */
336 weston_log("xserver crashing too fast: %d\n", exit_status);
337 weston_xserver_shutdown(wxs);
338 }
339}
340
341const struct weston_xwayland_api api = {
342 weston_xwayland_get,
343 weston_xwayland_listen,
344 weston_xwayland_xserver_loaded,
345 weston_xwayland_xserver_exited,
346};
Quentin Glidic955cec02016-08-12 10:41:35 +0200347extern const struct weston_xwayland_surface_api surface_api;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200348
349WL_EXPORT int
350module_init(struct weston_compositor *compositor,
351 int *argc, char *argv[])
352
353{
354 struct wl_display *display = compositor->wl_display;
355 struct weston_xserver *wxs;
356 int ret;
357
358 wxs = zalloc(sizeof *wxs);
359 if (wxs == NULL)
360 return -1;
361 wxs->wl_display = display;
362 wxs->compositor = compositor;
363
Quentin Glidic955cec02016-08-12 10:41:35 +0200364 if (weston_xwayland_get_api(compositor) != NULL ||
365 weston_xwayland_surface_get_api(compositor) != NULL) {
366 weston_log("The xwayland module APIs are already loaded.\n");
367 free(wxs);
368 return -1;
369 }
370
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200371 ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
372 &api, sizeof(api));
373 if (ret < 0) {
374 weston_log("Failed to register the xwayland module API.\n");
375 free(wxs);
376 return -1;
377 }
378
Quentin Glidic955cec02016-08-12 10:41:35 +0200379 ret = weston_plugin_api_register(compositor,
380 WESTON_XWAYLAND_SURFACE_API_NAME,
381 &surface_api, sizeof(surface_api));
382 if (ret < 0) {
383 weston_log("Failed to register the xwayland surface API.\n");
384 free(wxs);
385 return -1;
386 }
387
Tiago Vignattif2684462012-11-30 17:19:56 -0200388 wxs->destroy_listener.notify = weston_xserver_destroy;
389 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
Kristian Høgsberge10b1242012-05-21 16:48:05 -0400390
391 return 0;
392}