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