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>
39 handle_sigusr1(int signal_number, void *data)
41 struct weston_xserver *wxs = data;
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.*/
46 wxs->wm = weston_wm_create(wxs, wxs->wm_fd);
47 wl_event_source_remove(wxs->sigusr1_source);
53 weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
55 struct weston_xserver *wxs = data;
56 char display[8], s[8], abstract_fd[8], unix_fd[8], wm_fd[8];
59 struct weston_config_section *section;
61 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
62 weston_log("wl connection socketpair failed\n");
66 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
67 weston_log("X wm connection socketpair failed\n");
71 wxs->process.pid = fork();
72 switch (wxs->process.pid) {
74 /* SOCK_CLOEXEC closes both ends, so we need to unset
75 * the flag on the client fd. */
79 snprintf(s, sizeof s, "%d", fd);
80 setenv("WAYLAND_SOCKET", s, 1);
82 snprintf(display, sizeof display, ":%d", wxs->display);
84 fd = dup(wxs->abstract_fd);
87 snprintf(abstract_fd, sizeof abstract_fd, "%d", fd);
88 fd = dup(wxs->unix_fd);
91 snprintf(unix_fd, sizeof unix_fd, "%d", fd);
95 snprintf(wm_fd, sizeof wm_fd, "%d", fd);
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);
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);
115 "-listen", abstract_fd,
120 weston_log("exec of '%s %s -rootless "
121 "-listen %s -listen %s -wm %s "
122 "-terminate' failed: %m\n",
124 abstract_fd, unix_fd, wm_fd);
129 weston_log("forked X server, pid %d\n", wxs->process.pid);
132 wxs->client = wl_client_create(wxs->wl_display, sv[0]);
137 weston_watch_process(&wxs->process);
139 wl_event_source_remove(wxs->abstract_source);
140 wl_event_source_remove(wxs->unix_source);
144 weston_log( "failed to fork\n");
152 weston_xserver_shutdown(struct weston_xserver *wxs)
156 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
158 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
160 if (wxs->process.pid == 0) {
161 wl_event_source_remove(wxs->abstract_source);
162 wl_event_source_remove(wxs->unix_source);
164 close(wxs->abstract_fd);
167 weston_wm_destroy(wxs->wm);
174 weston_xserver_cleanup(struct weston_process *process, int status)
176 struct weston_xserver *wxs =
177 container_of(process, struct weston_xserver, process);
179 wxs->process.pid = 0;
181 wxs->resource = NULL;
183 wxs->abstract_source =
184 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
186 weston_xserver_handle_event, wxs);
189 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
191 weston_xserver_handle_event, wxs);
194 weston_log("xserver exited, code %d\n", status);
195 weston_wm_destroy(wxs->wm);
198 /* If the X server crashes before it binds to the
199 * xserver interface, shut down and don't try
201 weston_log("xserver crashing too fast: %d\n", status);
202 weston_xserver_shutdown(wxs);
207 bind_to_abstract_socket(int display)
209 struct sockaddr_un addr;
210 socklen_t size, name_size;
213 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
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) {
222 weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
227 if (listen(fd, 1) < 0) {
236 bind_to_unix_socket(int display)
238 struct sockaddr_un addr;
239 socklen_t size, name_size;
242 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
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) {
252 weston_log("failed to bind to %s: %m\n", addr.sun_path);
257 if (listen(fd, 1) < 0) {
258 unlink(addr.sun_path);
267 create_lockfile(int display, char *lockfile, size_t lsize)
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) {
276 fd = open(lockfile, O_CLOEXEC | O_RDONLY);
277 if (fd < 0 || read(fd, pid, 11) != 11) {
278 weston_log("can't read lock file %s: %s\n",
279 lockfile, strerror(errno));
287 if (!weston_strtoi(pid, &end, 0, &other) || end != pid + 10) {
288 weston_log("can't parse lock file %s\n",
295 if (kill(other, 0) < 0 && errno == ESRCH) {
296 /* stale lock file; unlink and try again */
297 weston_log("unlinking stale lock file %s\n", lockfile);
299 if (unlink(lockfile))
300 /* If we fail to unlink, return EEXIST
301 so we try the next display number.*/
312 weston_log("failed to create lock file %s: %s\n",
313 lockfile, strerror(errno));
317 /* Subtle detail: we use the pid of the wayland
318 * compositor, not the xserver in the lock file. */
319 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
320 if (write(fd, pid, size) != size) {
332 weston_xserver_destroy(struct wl_listener *l, void *data)
334 struct weston_xserver *wxs =
335 container_of(l, struct weston_xserver, destroy_listener);
341 weston_xserver_shutdown(wxs);
347 module_init(struct weston_compositor *compositor,
348 int *argc, char *argv[])
351 struct wl_display *display = compositor->wl_display;
352 struct weston_xserver *wxs;
353 char lockfile[256], display_name[8];
355 wxs = zalloc(sizeof *wxs);
358 wxs->process.cleanup = weston_xserver_cleanup;
359 wxs->wl_display = display;
360 wxs->compositor = compositor;
365 if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
366 if (errno == EAGAIN) {
368 } else if (errno == EEXIST) {
377 wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
378 if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
384 wxs->unix_fd = bind_to_unix_socket(wxs->display);
385 if (wxs->unix_fd < 0) {
387 close(wxs->abstract_fd);
392 snprintf(display_name, sizeof display_name, ":%d", wxs->display);
393 weston_log("xserver listening on display %s\n", display_name);
394 setenv("DISPLAY", display_name, 1);
396 wxs->loop = wl_display_get_event_loop(display);
397 wxs->abstract_source =
398 wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
400 weston_xserver_handle_event, wxs);
402 wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
404 weston_xserver_handle_event, wxs);
406 wxs->sigusr1_source = wl_event_loop_add_signal(wxs->loop, SIGUSR1,
407 handle_sigusr1, wxs);
408 wxs->destroy_listener.notify = weston_xserver_destroy;
409 wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);