2 * Copyright © 2011 Intel Corporation
3 * Copyright © 2016 Giulio Camuffo
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <sys/socket.h>
32 #include "compositor.h"
33 #include "compositor/weston.h"
34 #include "xwayland/xwayland-api.h"
35 #include "shared/helpers.h"
38 struct weston_compositor *compositor;
39 const struct weston_xwayland_api *api;
40 struct weston_xwayland *xwayland;
41 struct wl_event_source *sigusr1_source;
42 struct wl_client *client;
44 struct weston_process process;
48 handle_sigusr1(int signal_number, void *data)
50 struct wet_xwayland *wxw = data;
52 /* We'd be safer if we actually had the struct
53 * signalfd_siginfo from the signalfd data and could verify
54 * this came from Xwayland.*/
55 wxw->api->xserver_loaded(wxw->xwayland, wxw->client, wxw->wm_fd);
56 wl_event_source_remove(wxw->sigusr1_source);
62 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
64 struct wet_xwayland *wxw = user_data;
66 char s[8], abstract_fd_str[8], unix_fd_str[8], wm_fd_str[8];
69 struct weston_config *config = wet_get_config(wxw->compositor);
70 struct weston_config_section *section;
72 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
73 weston_log("wl connection socketpair failed\n");
77 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
78 weston_log("X wm connection socketpair failed\n");
85 /* SOCK_CLOEXEC closes both ends, so we need to unset
86 * the flag on the client fd. */
90 snprintf(s, sizeof s, "%d", fd);
91 setenv("WAYLAND_SOCKET", s, 1);
93 fd = dup(abstract_fd);
96 snprintf(abstract_fd_str, sizeof abstract_fd_str, "%d", fd);
100 snprintf(unix_fd_str, sizeof unix_fd_str, "%d", fd);
104 snprintf(wm_fd_str, sizeof wm_fd_str, "%d", fd);
106 section = weston_config_get_section(config,
107 "xwayland", NULL, NULL);
108 weston_config_section_get_string(section, "path",
109 &xserver, XSERVER_PATH);
111 /* Ignore SIGUSR1 in the child, which will make the X
112 * server send SIGUSR1 to the parent (weston) when
113 * it's done with initialization. During
114 * initialization the X server will round trip and
115 * block on the wayland compositor, so avoid making
116 * blocking requests (like xcb_connect_to_fd) until
117 * it's done with that. */
118 signal(SIGUSR1, SIG_IGN);
124 "-listen", abstract_fd_str,
125 "-listen", unix_fd_str,
129 weston_log("exec of '%s %s -rootless "
130 "-listen %s -listen %s -wm %s "
131 "-terminate' failed: %m\n",
133 abstract_fd_str, unix_fd_str, wm_fd_str);
139 wxw->client = wl_client_create(wxw->compositor->wl_display, sv[0]);
144 wxw->process.pid = pid;
145 weston_watch_process(&wxw->process);
149 weston_log("Failed to fork to spawn xserver process\n");
157 xserver_cleanup(struct weston_process *process, int status)
159 struct wet_xwayland *wxw =
160 container_of(process, struct wet_xwayland, process);
161 struct wl_event_loop *loop =
162 wl_display_get_event_loop(wxw->compositor->wl_display);
164 wxw->api->xserver_exited(wxw->xwayland, status);
165 wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
166 handle_sigusr1, wxw);
171 wet_load_xwayland(struct weston_compositor *comp)
173 const struct weston_xwayland_api *api;
174 struct weston_xwayland *xwayland;
175 struct wet_xwayland *wxw;
176 struct wl_event_loop *loop;
178 if (weston_compositor_load_xwayland(comp) < 0)
181 api = weston_xwayland_get_api(comp);
183 weston_log("Failed to get the xwayland module API.\n");
187 xwayland = api->get(comp);
189 weston_log("Failed to get the xwayland object.\n");
193 wxw = zalloc(sizeof *wxw);
197 wxw->compositor = comp;
199 wxw->xwayland = xwayland;
200 wxw->process.cleanup = xserver_cleanup;
201 if (api->listen(xwayland, wxw, spawn_xserver) < 0)
204 loop = wl_display_get_event_loop(comp->wl_display);
205 wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
206 handle_sigusr1, wxw);