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 weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
42 struct weston_xserver *mxs = data;
43 char display[8], s[8];
46 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
47 fprintf(stderr, "socketpair failed\n");
51 mxs->process.pid = fork();
52 switch (mxs->process.pid) {
54 /* SOCK_CLOEXEC closes both ends, so we need to unset
55 * the flag on the client fd. */
56 client_fd = dup(sv[1]);
60 snprintf(s, sizeof s, "%d", client_fd);
61 setenv("WAYLAND_SOCKET", s, 1);
63 snprintf(display, sizeof display, ":%d", mxs->display);
65 if (execl(XSERVER_PATH,
74 fprintf(stderr, "exec failed: %m\n");
78 fprintf(stderr, "forked X server, pid %d\n", mxs->process.pid);
81 mxs->client = wl_client_create(mxs->wl_display, sv[0]);
83 weston_watch_process(&mxs->process);
85 wl_event_source_remove(mxs->abstract_source);
86 wl_event_source_remove(mxs->unix_source);
90 fprintf(stderr, "failed to fork\n");
98 weston_xserver_shutdown(struct weston_xserver *wxs)
102 snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
104 snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
106 if (wxs->process.pid == 0) {
107 wl_event_source_remove(wxs->abstract_source);
108 wl_event_source_remove(wxs->unix_source);
110 close(wxs->abstract_fd);
113 weston_wm_destroy(wxs->wm);
118 weston_xserver_cleanup(struct weston_process *process, int status)
120 struct weston_xserver *mxs =
121 container_of(process, struct weston_xserver, process);
123 mxs->process.pid = 0;
125 mxs->resource = NULL;
127 mxs->abstract_source =
128 wl_event_loop_add_fd(mxs->loop, mxs->abstract_fd,
130 weston_xserver_handle_event, mxs);
133 wl_event_loop_add_fd(mxs->loop, mxs->unix_fd,
135 weston_xserver_handle_event, mxs);
138 fprintf(stderr, "xserver exited, code %d\n", status);
139 weston_wm_destroy(mxs->wm);
142 /* If the X server crashes before it binds to the
143 * xserver interface, shut down and don't try
145 fprintf(stderr, "xserver crashing too fast: %d\n", status);
146 weston_xserver_shutdown(mxs);
151 bind_xserver(struct wl_client *client,
152 void *data, uint32_t version, uint32_t id)
154 struct weston_xserver *wxs = data;
156 /* If it's a different client than the xserver we launched,
157 * don't start the wm. */
158 if (client != wxs->client)
162 wl_client_add_object(client, &xserver_interface,
163 &xserver_implementation, id, wxs);
165 wxs->wm = weston_wm_create(wxs);
166 if (wxs->wm == NULL) {
167 fprintf(stderr, "failed to create wm\n");
170 xserver_send_listen_socket(wxs->resource, wxs->abstract_fd);
171 xserver_send_listen_socket(wxs->resource, wxs->unix_fd);
175 bind_to_abstract_socket(int display)
177 struct sockaddr_un addr;
178 socklen_t size, name_size;
181 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
185 addr.sun_family = AF_LOCAL;
186 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
187 "%c/tmp/.X11-unix/X%d", 0, display);
188 size = offsetof(struct sockaddr_un, sun_path) + name_size;
189 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
190 fprintf(stderr, "failed to bind to @%s: %s\n",
191 addr.sun_path + 1, strerror(errno));
196 if (listen(fd, 1) < 0) {
205 bind_to_unix_socket(int display)
207 struct sockaddr_un addr;
208 socklen_t size, name_size;
211 fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
215 addr.sun_family = AF_LOCAL;
216 name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
217 "/tmp/.X11-unix/X%d", display) + 1;
218 size = offsetof(struct sockaddr_un, sun_path) + name_size;
219 unlink(addr.sun_path);
220 if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
221 fprintf(stderr, "failed to bind to %s (%s)\n",
222 addr.sun_path, strerror(errno));
227 if (listen(fd, 1) < 0) {
228 unlink(addr.sun_path);
237 create_lockfile(int display, char *lockfile, size_t lsize)
243 snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
244 fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
245 if (fd < 0 && errno == EEXIST) {
246 fd = open(lockfile, O_CLOEXEC, O_RDONLY);
247 if (fd < 0 || read(fd, pid, 11) != 11) {
248 fprintf(stderr, "can't read lock file %s: %s\n",
249 lockfile, strerror(errno));
254 other = strtol(pid, &end, 0);
255 if (end != pid + 10) {
256 fprintf(stderr, "can't parse lock file %s\n",
263 if (kill(other, 0) < 0 && errno == ESRCH) {
264 /* stale lock file; unlink and try again */
266 "unlinking stale lock file %s\n", lockfile);
268 if (unlink(lockfile))
269 /* If we fail to unlink, return EEXIST
270 so we try the next display number.*/
280 fprintf(stderr, "failed to create lock file %s: %s\n",
281 lockfile, strerror(errno));
285 /* Subtle detail: we use the pid of the wayland
286 * compositor, not the xserver in the lock file. */
287 size = snprintf(pid, sizeof pid, "%10d\n", getpid());
288 if (write(fd, pid, size) != size) {
300 weston_xserver_destroy(struct wl_listener *l, void *data)
302 struct weston_xserver *wxs =
303 container_of(l, struct weston_xserver, destroy_listener);
309 weston_xserver_shutdown(wxs);
315 weston_xserver_init(struct weston_compositor *compositor)
317 struct wl_display *display = compositor->wl_display;
318 struct weston_xserver *mxs;
319 char lockfile[256], display_name[8];
321 mxs = malloc(sizeof *mxs);
322 memset(mxs, 0, sizeof *mxs);
324 mxs->process.cleanup = weston_xserver_cleanup;
325 mxs->wl_display = display;
326 mxs->compositor = compositor;
331 if (create_lockfile(mxs->display, lockfile, sizeof lockfile) < 0) {
332 if (errno == EAGAIN) {
334 } else if (errno == EEXIST) {
343 mxs->abstract_fd = bind_to_abstract_socket(mxs->display);
344 if (mxs->abstract_fd < 0 && errno == EADDRINUSE) {
350 mxs->unix_fd = bind_to_unix_socket(mxs->display);
351 if (mxs->unix_fd < 0) {
353 close(mxs->abstract_fd);
358 snprintf(display_name, sizeof display_name, ":%d", mxs->display);
359 fprintf(stderr, "xserver listening on display %s\n", display_name);
360 setenv("DISPLAY", display_name, 1);
362 mxs->loop = wl_display_get_event_loop(display);
363 mxs->abstract_source =
364 wl_event_loop_add_fd(mxs->loop, mxs->abstract_fd,
366 weston_xserver_handle_event, mxs);
368 wl_event_loop_add_fd(mxs->loop, mxs->unix_fd,
370 weston_xserver_handle_event, mxs);
372 wl_display_add_global(display, &xserver_interface, mxs, bind_xserver);
374 mxs->destroy_listener.notify = weston_xserver_destroy;
375 wl_signal_add(&compositor->destroy_signal, &mxs->destroy_listener);