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" |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 39 | |
| 40 | |
| 41 | static int |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 42 | handle_sigusr1(int signal_number, void *data) |
| 43 | { |
| 44 | struct weston_xserver *wxs = data; |
| 45 | |
| 46 | /* We'd be safer if we actually had the struct |
| 47 | * signalfd_siginfo from the signalfd data and could verify |
| 48 | * this came from Xwayland.*/ |
Dima Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 49 | wxs->wm = weston_wm_create(wxs, wxs->wm_fd); |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 50 | wl_event_source_remove(wxs->sigusr1_source); |
| 51 | |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static int |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 56 | weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data) |
| 57 | { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 58 | struct weston_xserver *wxs = data; |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 59 | char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8]; |
| 60 | int sv[2], wm[2], fd; |
Maksim Melnikau | 92de144 | 2013-08-14 22:33:10 +0300 | [diff] [blame] | 61 | char *xserver = NULL; |
| 62 | struct weston_config_section *section; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 63 | |
| 64 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 65 | weston_log("wl connection socketpair failed\n"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) { |
| 70 | weston_log("X wm connection socketpair failed\n"); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 74 | wxs->process.pid = fork(); |
| 75 | switch (wxs->process.pid) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 76 | case 0: |
| 77 | /* SOCK_CLOEXEC closes both ends, so we need to unset |
| 78 | * the flag on the client fd. */ |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 79 | fd = dup(sv[1]); |
| 80 | if (fd < 0) |
| 81 | goto fail; |
| 82 | snprintf(s, sizeof s, "%d", fd); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 83 | setenv("WAYLAND_SOCKET", s, 1); |
| 84 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 85 | snprintf(display, sizeof display, ":%d", wxs->display); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 86 | |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 87 | fd = dup(wxs->abstract_fd); |
| 88 | if (fd < 0) |
| 89 | goto fail; |
| 90 | snprintf(abstract_fd, sizeof abstract_fd, "%d", fd); |
| 91 | fd = dup(wxs->unix_fd); |
| 92 | if (fd < 0) |
| 93 | goto fail; |
| 94 | snprintf(unix_fd, sizeof unix_fd, "%d", fd); |
| 95 | fd = dup(wm[1]); |
| 96 | if (fd < 0) |
| 97 | goto fail; |
| 98 | snprintf(wm_fd, sizeof wm_fd, "%d", fd); |
| 99 | |
| 100 | section = weston_config_get_section(wxs->compositor->config, |
| 101 | "xwayland", NULL, NULL); |
| 102 | weston_config_section_get_string(section, "path", |
| 103 | &xserver, XSERVER_PATH); |
| 104 | |
| 105 | /* Ignore SIGUSR1 in the child, which will make the X |
| 106 | * server send SIGUSR1 to the parent (weston) when |
| 107 | * it's done with initialization. During |
| 108 | * initialization the X server will round trip and |
| 109 | * block on the wayland compositor, so avoid making |
| 110 | * blocking requests (like xcb_connect_to_fd) until |
| 111 | * it's done with that. */ |
| 112 | signal(SIGUSR1, SIG_IGN); |
Maksim Melnikau | 92de144 | 2013-08-14 22:33:10 +0300 | [diff] [blame] | 113 | |
| 114 | if (execl(xserver, |
| 115 | xserver, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 116 | display, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 117 | "-rootless", |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 118 | "-listen", abstract_fd, |
| 119 | "-listen", unix_fd, |
| 120 | "-wm", wm_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 121 | "-terminate", |
| 122 | NULL) < 0) |
Arnout Engelen | 7da71ee | 2014-06-20 21:36:54 +0200 | [diff] [blame] | 123 | weston_log("exec of '%s %s -rootless " |
| 124 | "-listen %s -listen %s -wm %s " |
| 125 | "-terminate' failed: %m\n", |
| 126 | xserver, display, |
| 127 | abstract_fd, unix_fd, wm_fd); |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 128 | fail: |
Kristian Høgsberg | a58290b | 2013-06-18 01:03:02 -0400 | [diff] [blame] | 129 | _exit(EXIT_FAILURE); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 130 | |
| 131 | default: |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 132 | weston_log("forked X server, pid %d\n", wxs->process.pid); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 133 | |
| 134 | close(sv[1]); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 135 | wxs->client = wl_client_create(wxs->wl_display, sv[0]); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 136 | |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 137 | close(wm[1]); |
| 138 | wxs->wm_fd = wm[0]; |
| 139 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 140 | weston_watch_process(&wxs->process); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 141 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 142 | wl_event_source_remove(wxs->abstract_source); |
| 143 | wl_event_source_remove(wxs->unix_source); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | case -1: |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 147 | weston_log( "failed to fork\n"); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | weston_xserver_shutdown(struct weston_xserver *wxs) |
| 156 | { |
| 157 | char path[256]; |
| 158 | |
| 159 | snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display); |
| 160 | unlink(path); |
| 161 | snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display); |
| 162 | unlink(path); |
| 163 | if (wxs->process.pid == 0) { |
| 164 | wl_event_source_remove(wxs->abstract_source); |
| 165 | wl_event_source_remove(wxs->unix_source); |
| 166 | } |
| 167 | close(wxs->abstract_fd); |
| 168 | close(wxs->unix_fd); |
Dima Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 169 | if (wxs->wm) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 170 | weston_wm_destroy(wxs->wm); |
Dima Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 171 | wxs->wm = NULL; |
| 172 | } |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 173 | wxs->loop = NULL; |
| 174 | } |
| 175 | |
| 176 | static void |
| 177 | weston_xserver_cleanup(struct weston_process *process, int status) |
| 178 | { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 179 | struct weston_xserver *wxs = |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 180 | container_of(process, struct weston_xserver, process); |
| 181 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 182 | wxs->process.pid = 0; |
| 183 | wxs->client = NULL; |
| 184 | wxs->resource = NULL; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 185 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 186 | wxs->abstract_source = |
| 187 | wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 188 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 189 | weston_xserver_handle_event, wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 190 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 191 | wxs->unix_source = |
| 192 | wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 193 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 194 | weston_xserver_handle_event, wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 195 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 196 | if (wxs->wm) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 197 | weston_log("xserver exited, code %d\n", status); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 198 | weston_wm_destroy(wxs->wm); |
| 199 | wxs->wm = NULL; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 200 | } else { |
| 201 | /* If the X server crashes before it binds to the |
| 202 | * xserver interface, shut down and don't try |
| 203 | * again. */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 204 | weston_log("xserver crashing too fast: %d\n", status); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 205 | weston_xserver_shutdown(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 209 | static int |
| 210 | bind_to_abstract_socket(int display) |
| 211 | { |
| 212 | struct sockaddr_un addr; |
| 213 | socklen_t size, name_size; |
| 214 | int fd; |
| 215 | |
| 216 | fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 217 | if (fd < 0) |
| 218 | return -1; |
| 219 | |
| 220 | addr.sun_family = AF_LOCAL; |
| 221 | name_size = snprintf(addr.sun_path, sizeof addr.sun_path, |
| 222 | "%c/tmp/.X11-unix/X%d", 0, display); |
| 223 | size = offsetof(struct sockaddr_un, sun_path) + name_size; |
| 224 | if (bind(fd, (struct sockaddr *) &addr, size) < 0) { |
Jasper St. Pierre | 4783739 | 2014-04-01 19:55:26 -0400 | [diff] [blame] | 225 | 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] | 226 | close(fd); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | if (listen(fd, 1) < 0) { |
| 231 | close(fd); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | return fd; |
| 236 | } |
| 237 | |
| 238 | static int |
| 239 | bind_to_unix_socket(int display) |
| 240 | { |
| 241 | struct sockaddr_un addr; |
| 242 | socklen_t size, name_size; |
| 243 | int fd; |
| 244 | |
| 245 | fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 246 | if (fd < 0) |
| 247 | return -1; |
| 248 | |
| 249 | addr.sun_family = AF_LOCAL; |
| 250 | name_size = snprintf(addr.sun_path, sizeof addr.sun_path, |
| 251 | "/tmp/.X11-unix/X%d", display) + 1; |
| 252 | size = offsetof(struct sockaddr_un, sun_path) + name_size; |
| 253 | unlink(addr.sun_path); |
| 254 | if (bind(fd, (struct sockaddr *) &addr, size) < 0) { |
Jasper St. Pierre | 4783739 | 2014-04-01 19:55:26 -0400 | [diff] [blame] | 255 | 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] | 256 | close(fd); |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | if (listen(fd, 1) < 0) { |
| 261 | unlink(addr.sun_path); |
| 262 | close(fd); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | return fd; |
| 267 | } |
| 268 | |
| 269 | static int |
| 270 | create_lockfile(int display, char *lockfile, size_t lsize) |
| 271 | { |
| 272 | char pid[16], *end; |
| 273 | int fd, size; |
| 274 | pid_t other; |
| 275 | |
| 276 | snprintf(lockfile, lsize, "/tmp/.X%d-lock", display); |
| 277 | fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444); |
| 278 | if (fd < 0 && errno == EEXIST) { |
Kristian Høgsberg | 5e647ca | 2014-02-01 20:44:54 -0800 | [diff] [blame] | 279 | fd = open(lockfile, O_CLOEXEC | O_RDONLY); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 280 | if (fd < 0 || read(fd, pid, 11) != 11) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 281 | weston_log("can't read lock file %s: %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 282 | lockfile, strerror(errno)); |
Rob Bradford | ef94085 | 2012-12-05 18:47:07 +0000 | [diff] [blame] | 283 | if (fd >= 0) |
| 284 | close (fd); |
| 285 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 286 | errno = EEXIST; |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | other = strtol(pid, &end, 0); |
| 291 | if (end != pid + 10) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 292 | weston_log("can't parse lock file %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 293 | lockfile); |
| 294 | close(fd); |
| 295 | errno = EEXIST; |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | if (kill(other, 0) < 0 && errno == ESRCH) { |
| 300 | /* stale lock file; unlink and try again */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 301 | weston_log("unlinking stale lock file %s\n", lockfile); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 302 | close(fd); |
| 303 | if (unlink(lockfile)) |
| 304 | /* If we fail to unlink, return EEXIST |
| 305 | so we try the next display number.*/ |
| 306 | errno = EEXIST; |
| 307 | else |
| 308 | errno = EAGAIN; |
| 309 | return -1; |
| 310 | } |
| 311 | |
Martin Olsson | 1972141 | 2012-07-08 03:03:45 +0200 | [diff] [blame] | 312 | close(fd); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 313 | errno = EEXIST; |
| 314 | return -1; |
| 315 | } else if (fd < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 316 | weston_log("failed to create lock file %s: %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 317 | lockfile, strerror(errno)); |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | /* Subtle detail: we use the pid of the wayland |
| 322 | * compositor, not the xserver in the lock file. */ |
| 323 | size = snprintf(pid, sizeof pid, "%10d\n", getpid()); |
| 324 | if (write(fd, pid, size) != size) { |
| 325 | unlink(lockfile); |
| 326 | close(fd); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | close(fd); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static void |
| 336 | weston_xserver_destroy(struct wl_listener *l, void *data) |
| 337 | { |
| 338 | struct weston_xserver *wxs = |
| 339 | container_of(l, struct weston_xserver, destroy_listener); |
| 340 | |
| 341 | if (!wxs) |
| 342 | return; |
| 343 | |
| 344 | if (wxs->loop) |
| 345 | weston_xserver_shutdown(wxs); |
| 346 | |
| 347 | free(wxs); |
| 348 | } |
| 349 | |
| 350 | WL_EXPORT int |
Kristian Høgsberg | cb4685b | 2013-02-20 15:37:49 -0500 | [diff] [blame] | 351 | module_init(struct weston_compositor *compositor, |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 352 | int *argc, char *argv[]) |
Kristian Høgsberg | cb4685b | 2013-02-20 15:37:49 -0500 | [diff] [blame] | 353 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 354 | { |
| 355 | struct wl_display *display = compositor->wl_display; |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 356 | struct weston_xserver *wxs; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 357 | char lockfile[256], display_name[8]; |
| 358 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 359 | wxs = zalloc(sizeof *wxs); |
Bryce W. Harrington | a212cbb | 2014-04-21 23:51:03 +0000 | [diff] [blame] | 360 | if (wxs == NULL) |
| 361 | return -1; |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 362 | wxs->process.cleanup = weston_xserver_cleanup; |
| 363 | wxs->wl_display = display; |
| 364 | wxs->compositor = compositor; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 365 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 366 | wxs->display = 0; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 367 | |
| 368 | retry: |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 369 | if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 370 | if (errno == EAGAIN) { |
| 371 | goto retry; |
| 372 | } else if (errno == EEXIST) { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 373 | wxs->display++; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 374 | goto retry; |
| 375 | } else { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 376 | free(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 377 | return -1; |
| 378 | } |
| 379 | } |
| 380 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 381 | wxs->abstract_fd = bind_to_abstract_socket(wxs->display); |
| 382 | if (wxs->abstract_fd < 0 && errno == EADDRINUSE) { |
| 383 | wxs->display++; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 384 | unlink(lockfile); |
| 385 | goto retry; |
| 386 | } |
| 387 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 388 | wxs->unix_fd = bind_to_unix_socket(wxs->display); |
| 389 | if (wxs->unix_fd < 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 390 | unlink(lockfile); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 391 | close(wxs->abstract_fd); |
| 392 | free(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 396 | snprintf(display_name, sizeof display_name, ":%d", wxs->display); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 397 | weston_log("xserver listening on display %s\n", display_name); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 398 | setenv("DISPLAY", display_name, 1); |
| 399 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 400 | wxs->loop = wl_display_get_event_loop(display); |
| 401 | wxs->abstract_source = |
| 402 | wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 403 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 404 | weston_xserver_handle_event, wxs); |
| 405 | wxs->unix_source = |
| 406 | wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 407 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 408 | weston_xserver_handle_event, wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 409 | |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 410 | wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1, |
| 411 | handle_sigusr1, wxs); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 412 | wxs->destroy_listener.notify = weston_xserver_destroy; |
| 413 | wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |