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