2 * Copyright © 2011 Intel Corporation
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.
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.
28 #include <sys/socket.h>
36 #include "xserver-server-protocol.h"
40 handle_sigusr1(int signal_number, void *data)
42 struct weston_xserver *wxs = data;
44 /* We'd be safer if we actually had the struct
45 * signalfd_siginfo from the signalfd data and could verify
46 * this came from Xwayland.*/
47 weston_wm_create(wxs, wxs->wm_fd);
48 wl_event_source_remove(wxs->sigusr1_source);
54 weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
56 struct weston_xserver *wxs = data;
57 char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8];
60 struct weston_config_section *section;
62 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
63 weston_log("wl connection socketpair failed\n");
67 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
68 weston_log("X wm connection socketpair failed\n");
72 wxs->process.pid = fork();
73 switch (wxs->process.pid) {
75 /* SOCK_CLOEXEC closes both ends, so we need to unset
76 * the flag on the client fd. */
80 snprintf(s, sizeof s, "%d", fd);
81 setenv("WAYLAND_SOCKET", s, 1);
83 snprintf(display, sizeof display, ":%d", wxs->display);
85 fd = dup(wxs->abstract_fd);
88 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
89 fd = dup(wxs->unix_fd);
92 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
96 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
98 section = weston_config_get_section(wxs->compositor->config,
99 "xwayland", NULL, NULL);
100 weston_config_section_get_string(section, "path",
101 &xserver, XSERVER_PATH);
103 /* Ignore SIGUSR1 in the child, which will make the X
104 * server send SIGUSR1 to the parent (weston) when
105 * it's done with initialization. During
106 * initialization the X server will round trip and
107 * block on the wayland compositor, so avoid making
108 * blocking requests (like xcb_connect_to_fd) until
109 * it's done with that. */
110 signal(SIGUSR1, SIG_IGN);
116 "-listen", abstract_fd,
121 weston_log("exec failed: %m\n");
126 weston_log("forked X server, pid %d\n", wxs->process.pid);
129 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
134 weston_watch_process(&wxs->process);
136 wl_event_source_remove(wxs->abstract_source);
137 wl_event_source_remove(wxs->unix_source);
141 weston_log( "failed to fork\n");
149 weston_xserver_shutdown(struct weston_xserver *wxs)
153 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
155 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
157 if (wxs->process.pid == 0) {
158 wl_event_source_remove(wxs->abstract_source);
159 wl_event_source_remove(wxs->unix_source);
161 close(wxs->abstract_fd);
164 weston_wm_destroy(wxs->wm);
169 weston_xserver_cleanup(struct weston_process *process, int status)
171 struct weston_xserver *wxs =
172 container_of(process, struct weston_xserver, process);
174 wxs->process.pid = 0;
176 wxs->resource = NULL;
178 wxs->abstract_source =
179 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
181 weston_xserver_handle_event, wxs);
184 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
186 weston_xserver_handle_event, wxs);
189 weston_log("xserver exited, code %d\n", status);
190 weston_wm_destroy(wxs->wm);
193 /* If the X server crashes before it binds to the
194 * xserver interface, shut down and don't try
196 weston_log("xserver crashing too fast: %d\n", status);
197 weston_xserver_shutdown(wxs);
202 bind_to_abstract_socket(int display)
204 struct sockaddr_un addr;
205 socklen_t size, name_size;
208 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
212 addr.sun_family = AF_LOCAL;
213 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
214 "%c/tmp/.X11-unix/X%d", 0, display);
215 size = offsetof(struct sockaddr_un, sun_path) + name_size;
216 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
217 weston_log("failed to bind to @%s: %s\n",
218 addr.sun_path + 1, strerror(errno));
223 if (listen(fd, 1) < 0) {
232 bind_to_unix_socket(int display)
234 struct sockaddr_un addr;
235 socklen_t size, name_size;
238 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
242 addr.sun_family = AF_LOCAL;
243 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
244 "/tmp/.X11-unix/X%d", display) + 1;
245 size = offsetof(struct sockaddr_un, sun_path) + name_size;
246 unlink(addr.sun_path);
247 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
248 weston_log("failed to bind to %s (%s)\n",
249 addr.sun_path, strerror(errno));
254 if (listen(fd, 1) < 0) {
255 unlink(addr.sun_path);
264 create_lockfile(int display, char *lockfile, size_t lsize)
270 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
271 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
272 if (fd < 0 && errno == EEXIST) {
273 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
274 if (fd < 0 || read(fd, pid, 11) != 11) {
275 weston_log("can't read lock file %s: %s\n",
276 lockfile, strerror(errno));
284 other = strtol(pid, &end, 0);
285 if (end != pid + 10) {
286 weston_log("can't parse lock file %s\n",
293 if (kill(other, 0) < 0 && errno == ESRCH) {
294 /* stale lock file; unlink and try again */
295 weston_log("unlinking stale lock file %s\n", lockfile);
297 if (unlink(lockfile))
298 /* If we fail to unlink, return EEXIST
299 so we try the next display number.*/
310 weston_log("failed to create lock file %s: %s\n",
311 lockfile, strerror(errno));
315 /* Subtle detail: we use the pid of the wayland
316 * compositor, not the xserver in the lock file. */
317 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
318 if (write(fd, pid, size) != size) {
330 weston_xserver_destroy(struct wl_listener *l, void *data)
332 struct weston_xserver *wxs =
333 container_of(l, struct weston_xserver, destroy_listener);
339 weston_xserver_shutdown(wxs);
345 module_init(struct weston_compositor *compositor,
346 int *argc, char *argv[])
349 struct wl_display *display = compositor->wl_display;
350 struct weston_xserver *wxs;
351 char lockfile[256], display_name[8];
353 wxs = zalloc(sizeof *wxs);
354 wxs->process.cleanup = weston_xserver_cleanup;
355 wxs->wl_display = display;
356 wxs->compositor = compositor;
361 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
362 if (errno == EAGAIN) {
364 } else if (errno == EEXIST) {
373 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
374 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
380 wxs->unix_fd = bind_to_unix_socket(wxs->display);
381 if (wxs->unix_fd < 0) {
383 close(wxs->abstract_fd);
388 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
389 weston_log("xserver listening on display %s\n", display_name);
390 setenv("DISPLAY", display_name, 1);
392 wxs->loop = wl_display_get_event_loop(display);
393 wxs->abstract_source =
394 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
396 weston_xserver_handle_event, wxs);
398 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
400 weston_xserver_handle_event, wxs);
402 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
403 handle_sigusr1, wxs);
404 wxs->destroy_listener.notify = weston_xserver_destroy;
405 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);