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