1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * 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:
11 *
12 * 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.
24 */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <stdint.h>
30 #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"
40 #include <libweston/xwayland-api.h>
41 #include "shared/helpers.h"
42 #include "shared/string-helpers.h"
43
44 static int
weston_xserver_handle_event(int listen_fd,uint32_t mask,void * data)45 weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
46 {
47 struct weston_xserver *wxs = data;
48 char display[8];
49
50 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");
55 return 1;
56 }
57
58 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);
61
62 return 1;
63 }
64
65 static void
weston_xserver_shutdown(struct weston_xserver * wxs)66 weston_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);
74 if (wxs->pid == 0) {
75 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);
80 if (wxs->wm) {
81 weston_wm_destroy(wxs->wm);
82 wxs->wm = NULL;
83 }
84 wxs->loop = NULL;
85 }
86
87 static int
bind_to_abstract_socket(int display)88 bind_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) {
103 weston_log("failed to bind to @%s: %s\n", addr.sun_path + 1,
104 strerror(errno));
105 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
117 static int
bind_to_unix_socket(int display)118 bind_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) {
134 weston_log("failed to bind to %s: %s\n", addr.sun_path,
135 strerror(errno));
136 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
149 static int
create_lockfile(int display,char * lockfile,size_t lsize)150 create_lockfile(int display, char *lockfile, size_t lsize)
151 {
152 /* 10 decimal characters, trailing LF and NUL byte; see comment
153 * at end of function. */
154 char pid[11];
155 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) {
161 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
162 if (fd < 0 || read(fd, pid, 11) != 11) {
163 weston_log("can't read lock file %s: %s\n",
164 lockfile, strerror(errno));
165 if (fd >= 0)
166 close (fd);
167
168 errno = EEXIST;
169 return -1;
170 }
171
172 /* Trim the trailing LF, or at least ensure it's NULL. */
173 pid[10] = '\0';
174
175 if (!safe_strtoint(pid, &other)) {
176 weston_log("can't parse lock file %s\n",
177 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 */
185 weston_log("unlinking stale lock file %s\n", lockfile);
186 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
196 close(fd);
197 errno = EEXIST;
198 return -1;
199 } else if (fd < 0) {
200 weston_log("failed to create lock file %s: %s\n",
201 lockfile, strerror(errno));
202 return -1;
203 }
204
205 /* 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) {
211 unlink(lockfile);
212 close(fd);
213 return -1;
214 }
215
216 close(fd);
217
218 return 0;
219 }
220
221 static void
weston_xserver_destroy(struct wl_listener * l,void * data)222 weston_xserver_destroy(struct wl_listener *l, void *data)
223 {
224 struct weston_xserver *wxs =
225 container_of(l, struct weston_xserver, destroy_listener);
226
227 if (!wxs)
228 return;
229
230 if (wxs->loop)
231 weston_xserver_shutdown(wxs);
232
233 weston_log_scope_destroy(wxs->wm_debug);
234
235 free(wxs);
236 }
237
238 static struct weston_xwayland *
weston_xwayland_get(struct weston_compositor * compositor)239 weston_xwayland_get(struct weston_compositor *compositor)
240 {
241 struct wl_listener *listener;
242 struct weston_xserver *wxs;
243
244 listener = wl_signal_get(&compositor->destroy_signal,
245 weston_xserver_destroy);
246 if (!listener)
247 return NULL;
248
249 wxs = wl_container_of(listener, wxs, destroy_listener);
250 return (struct weston_xwayland *)wxs;
251 }
252
253 static int
weston_xwayland_listen(struct weston_xwayland * xwayland,void * user_data,weston_xwayland_spawn_xserver_func_t spawn_func)254 weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
255 weston_xwayland_spawn_xserver_func_t spawn_func)
256 {
257 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
258 char lockfile[256], display_name[8];
259
260 wxs->user_data = user_data;
261 wxs->spawn_func = spawn_func;
262
263 retry:
264 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
265 if (errno == EAGAIN) {
266 goto retry;
267 } else if (errno == EEXIST) {
268 wxs->display++;
269 goto retry;
270 } else {
271 free(wxs);
272 return -1;
273 }
274 }
275
276 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
277 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
278 wxs->display++;
279 unlink(lockfile);
280 goto retry;
281 }
282
283 wxs->unix_fd = bind_to_unix_socket(wxs->display);
284 if (wxs->unix_fd < 0) {
285 unlink(lockfile);
286 close(wxs->abstract_fd);
287 free(wxs);
288 return -1;
289 }
290
291 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
292 weston_log("xserver listening on display %s\n", display_name);
293 setenv("DISPLAY", display_name, 1);
294
295 wxs->loop = wl_display_get_event_loop(wxs->wl_display);
296 wxs->abstract_source =
297 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
298 WL_EVENT_READABLE,
299 weston_xserver_handle_event, wxs);
300 wxs->unix_source =
301 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
302 WL_EVENT_READABLE,
303 weston_xserver_handle_event, wxs);
304
305 return 0;
306 }
307
308 static void
weston_xwayland_xserver_loaded(struct weston_xwayland * xwayland,struct wl_client * client,int wm_fd)309 weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
310 struct wl_client *client, int wm_fd)
311 {
312 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
313 wxs->wm = weston_wm_create(wxs, wm_fd);
314 wxs->client = client;
315 }
316
317 static void
weston_xwayland_xserver_exited(struct weston_xwayland * xwayland,int exit_status)318 weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
319 int exit_status)
320 {
321 struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
322
323 wxs->pid = 0;
324 wxs->client = NULL;
325
326 wxs->abstract_source =
327 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
328 WL_EVENT_READABLE,
329 weston_xserver_handle_event, wxs);
330 wxs->unix_source =
331 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
332 WL_EVENT_READABLE,
333 weston_xserver_handle_event, wxs);
334
335 if (wxs->wm) {
336 weston_log("xserver exited, code %d\n", exit_status);
337 weston_wm_destroy(wxs->wm);
338 wxs->wm = NULL;
339 } else {
340 /* If the X server crashes before it binds to the
341 * xserver interface, shut down and don't try
342 * again. */
343 weston_log("xserver crashing too fast: %d\n", exit_status);
344 weston_xserver_shutdown(wxs);
345 }
346 }
347
348 const struct weston_xwayland_api api = {
349 weston_xwayland_get,
350 weston_xwayland_listen,
351 weston_xwayland_xserver_loaded,
352 weston_xwayland_xserver_exited,
353 };
354 extern const struct weston_xwayland_surface_api surface_api;
355
356 WL_EXPORT int
weston_module_init(struct weston_compositor * compositor)357 weston_module_init(struct weston_compositor *compositor)
358
359 {
360 struct wl_display *display = compositor->wl_display;
361 struct weston_xserver *wxs;
362 int ret;
363
364 wxs = zalloc(sizeof *wxs);
365 if (wxs == NULL)
366 return -1;
367 wxs->wl_display = display;
368 wxs->compositor = compositor;
369
370 if (!weston_compositor_add_destroy_listener_once(compositor,
371 &wxs->destroy_listener,
372 weston_xserver_destroy)) {
373 free(wxs);
374 return 0;
375 }
376
377 if (weston_xwayland_get_api(compositor) != NULL ||
378 weston_xwayland_surface_get_api(compositor) != NULL) {
379 weston_log("The xwayland module APIs are already loaded.\n");
380 goto out_free;
381 }
382
383 ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
384 &api, sizeof(api));
385 if (ret < 0) {
386 weston_log("Failed to register the xwayland module API.\n");
387 goto out_free;
388 }
389
390 ret = weston_plugin_api_register(compositor,
391 WESTON_XWAYLAND_SURFACE_API_NAME,
392 &surface_api, sizeof(surface_api));
393 if (ret < 0) {
394 weston_log("Failed to register the xwayland surface API.\n");
395 goto out_free;
396 }
397
398 wxs->wm_debug =
399 weston_compositor_add_log_scope(wxs->compositor, "xwm-wm-x11",
400 "XWM's window management X11 events\n",
401 NULL, NULL, NULL);
402
403 return 0;
404
405 out_free:
406 wl_list_remove(&wxs->destroy_listener.link);
407 free(wxs);
408 return -1;
409 }
410