Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
Bryce Harrington | 0a007dd | 2015-06-11 16:22:34 -0700 | [diff] [blame] | 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: |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 0a007dd | 2015-06-11 16:22:34 -0700 | [diff] [blame] | 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. |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 27 | |
| 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 Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 39 | #include "xwayland-api.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 40 | #include "shared/helpers.h" |
Pekka Paalanen | 58f98c9 | 2016-06-03 16:45:21 +0300 | [diff] [blame] | 41 | #include "compositor/weston.h" |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 42 | |
| 43 | static int |
| 44 | weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data) |
| 45 | { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 46 | struct weston_xserver *wxs = data; |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 47 | char display[8]; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 48 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 49 | 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øgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 57 | 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øgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 60 | |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | weston_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 Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 73 | if (wxs->pid == 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 74 | 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 Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 79 | if (wxs->wm) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 80 | weston_wm_destroy(wxs->wm); |
Dima Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 81 | wxs->wm = NULL; |
| 82 | } |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 83 | wxs->loop = NULL; |
| 84 | } |
| 85 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 86 | static int |
| 87 | bind_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. Pierre | 4783739 | 2014-04-01 19:55:26 -0400 | [diff] [blame] | 102 | weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 103 | 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 | |
| 115 | static int |
| 116 | bind_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. Pierre | 4783739 | 2014-04-01 19:55:26 -0400 | [diff] [blame] | 132 | weston_log("failed to bind to %s: %m\n", addr.sun_path); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 133 | 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 | |
| 146 | static int |
| 147 | create_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øgsberg | 5e647ca | 2014-02-01 20:44:54 -0800 | [diff] [blame] | 156 | fd = open(lockfile, O_CLOEXEC | O_RDONLY); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 157 | if (fd < 0 || read(fd, pid, 11) != 11) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 158 | weston_log("can't read lock file %s: %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 159 | lockfile, strerror(errno)); |
Rob Bradford | ef94085 | 2012-12-05 18:47:07 +0000 | [diff] [blame] | 160 | if (fd >= 0) |
| 161 | close (fd); |
| 162 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 163 | errno = EEXIST; |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | other = strtol(pid, &end, 0); |
| 168 | if (end != pid + 10) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 169 | weston_log("can't parse lock file %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 170 | 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 Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 178 | weston_log("unlinking stale lock file %s\n", lockfile); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 179 | 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 Olsson | 1972141 | 2012-07-08 03:03:45 +0200 | [diff] [blame] | 189 | close(fd); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 190 | errno = EEXIST; |
| 191 | return -1; |
| 192 | } else if (fd < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 193 | weston_log("failed to create lock file %s: %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 194 | 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 | |
| 212 | static void |
| 213 | weston_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 Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 227 | static struct weston_xwayland * |
| 228 | weston_xwayland_get(struct weston_compositor *compositor) |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 229 | { |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 230 | struct wl_listener *listener; |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 231 | struct weston_xserver *wxs; |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 232 | |
| 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 | |
| 242 | static int |
| 243 | weston_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øgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 247 | char lockfile[256], display_name[8]; |
| 248 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 249 | wxs->user_data = user_data; |
| 250 | wxs->spawn_func = spawn_func; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 251 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 252 | retry: |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 253 | if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 254 | if (errno == EAGAIN) { |
| 255 | goto retry; |
| 256 | } else if (errno == EEXIST) { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 257 | wxs->display++; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 258 | goto retry; |
| 259 | } else { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 260 | free(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | } |
| 264 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 265 | wxs->abstract_fd = bind_to_abstract_socket(wxs->display); |
| 266 | if (wxs->abstract_fd < 0 && errno == EADDRINUSE) { |
| 267 | wxs->display++; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 268 | unlink(lockfile); |
| 269 | goto retry; |
| 270 | } |
| 271 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 272 | wxs->unix_fd = bind_to_unix_socket(wxs->display); |
| 273 | if (wxs->unix_fd < 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 274 | unlink(lockfile); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 275 | close(wxs->abstract_fd); |
| 276 | free(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 280 | snprintf(display_name, sizeof display_name, ":%d", wxs->display); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 281 | weston_log("xserver listening on display %s\n", display_name); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 282 | setenv("DISPLAY", display_name, 1); |
| 283 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 284 | wxs->loop = wl_display_get_event_loop(wxs->wl_display); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 285 | wxs->abstract_source = |
| 286 | wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 287 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 288 | weston_xserver_handle_event, wxs); |
| 289 | wxs->unix_source = |
| 290 | wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 291 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 292 | weston_xserver_handle_event, wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 293 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame^] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | weston_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 | |
| 306 | static void |
| 307 | weston_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 | |
| 337 | const 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 | |
| 344 | WL_EXPORT int |
| 345 | module_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 Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 367 | wxs->destroy_listener.notify = weston_xserver_destroy; |
| 368 | wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | } |