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