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