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
28 #include "ivi-compositor.h"
33 #include <sys/socket.h>
36 #include <libweston/libweston.h>
38 #include <libweston/xwayland-api.h>
39 #include "shared/helpers.h"
40 #include "shared/process-util.h"
41 #include "shared/xalloc.h"
43 #ifdef HAVE_XWAYLAND_LISTENFD
44 # define LISTEN_STR "-listenfd"
46 # define LISTEN_STR "-listen"
50 struct weston_compositor *compositor;
51 const struct weston_xwayland_api *api;
52 struct weston_xwayland *xwayland;
53 struct wl_event_source *display_fd_source;
55 struct wet_process *process;
59 handle_display_fd(int fd, uint32_t mask, void *data)
61 struct wet_xwayland *wxw = data;
65 /* xwayland exited before being ready, don't finish initialization,
66 * the process watcher will cleanup */
67 if (!(mask & WL_EVENT_READABLE))
70 /* Xwayland writes to the pipe twice, so if we close it too early
71 * it's possible the second write will fail and Xwayland shuts down.
72 * Make sure we read until end of line marker to avoid this. */
73 n = read(fd, buf, sizeof buf);
74 if (n < 0 && errno != EAGAIN) {
75 weston_log("read from Xwayland display_fd failed: %s\n",
79 /* Returning 1 here means recheck and call us again if required. */
80 if (n <= 0 || (n > 0 && buf[n - 1] != '\n'))
83 wxw->api->xserver_loaded(wxw->xwayland, wxw->wm_fd);
86 wl_event_source_remove(wxw->display_fd_source);
94 xserver_cleanup(struct wet_process *process, int status, void *data)
96 struct wet_xwayland *wxw = data;
98 /* We only have one Xwayland process active, so make sure it's the
100 assert(process == wxw->process);
102 wxw->api->xserver_exited(wxw->xwayland);
106 cleanup_for_child_process() {
109 /* Put the client in a new session so it won't catch signals
110 * intended for the parent. Sharing a session can be
111 * confusing when launching weston under gdb, as the ctrl-c
112 * intended for gdb will pass to the child, and weston
113 * will cleanly shut down when the child exits.
117 /* do not give our signal mask to the new process */
118 sigfillset(&allsigs);
119 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
122 static struct wet_process *
123 client_launch(struct weston_compositor *compositor,
124 struct custom_env *child_env,
126 size_t num_no_cloexec_fds,
127 wet_process_cleanup_func_t cleanup,
130 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
131 struct wet_process *proc = NULL;
132 const char *fail_cloexec = "Couldn't unset CLOEXEC on child FDs";
133 const char *fail_seteuid = "Couldn't call seteuid";
140 size_t written __attribute__((unused));
142 argp = custom_env_get_argp(child_env);
143 envp = custom_env_get_envp(child_env);
145 weston_log("launching '%s'\n", argp[0]);
146 str_printf(&fail_exec, "Error: Couldn't launch client '%s'\n", argp[0]);
151 cleanup_for_child_process();
153 /* Launch clients as the user. Do not launch clients with wrong euid. */
154 if (seteuid(getuid()) == -1) {
155 written = write(STDERR_FILENO, fail_seteuid,
156 strlen(fail_seteuid));
160 for (i = 0; i < num_no_cloexec_fds; i++) {
161 err = os_fd_clear_cloexec(no_cloexec_fds[i]);
163 written = write(STDERR_FILENO, fail_cloexec,
164 strlen(fail_cloexec));
169 execve(argp[0], argp, envp);
172 written = write(STDERR_FILENO, fail_exec,
177 proc = xzalloc(sizeof(*proc));
179 proc->cleanup = cleanup;
180 proc->cleanup_data = cleanup_data;
181 proc->path = strdup(argp[0]);
182 wl_list_insert(&ivi->child_process_list, &proc->link);
186 weston_log("weston_client_launch: "
187 "fork failed while launching '%s': %s\n", argp[0],
192 custom_env_fini(child_env);
197 static struct weston_config *
198 ivi_get_config(struct weston_compositor *ec)
200 struct ivi_compositor *ivi = to_ivi_compositor(ec);
206 static struct wl_client *
207 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
209 struct wet_xwayland *wxw = user_data;
210 struct fdstr wayland_socket = FDSTR_INIT;
211 struct fdstr x11_abstract_socket = FDSTR_INIT;
212 struct fdstr x11_unix_socket = FDSTR_INIT;
213 struct fdstr x11_wm_socket = FDSTR_INIT;
214 struct fdstr display_pipe = FDSTR_INIT;
216 char *xserver = NULL;
217 struct weston_config *config = ivi_get_config(wxw->compositor);
218 struct weston_config_section *section;
219 struct wl_client *client;
220 struct wl_event_loop *loop;
221 struct custom_env child_env;
222 int no_cloexec_fds[5];
223 size_t num_no_cloexec_fds = 0;
224 size_t written __attribute__ ((unused));
226 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, wayland_socket.fds) < 0) {
227 weston_log("wl connection socketpair failed\n");
230 fdstr_update_str1(&wayland_socket);
231 no_cloexec_fds[num_no_cloexec_fds++] = wayland_socket.fds[1];
233 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, x11_wm_socket.fds) < 0) {
234 weston_log("X wm connection socketpair failed\n");
237 fdstr_update_str1(&x11_wm_socket);
238 no_cloexec_fds[num_no_cloexec_fds++] = x11_wm_socket.fds[1];
240 if (pipe2(display_pipe.fds, O_CLOEXEC) < 0) {
241 weston_log("pipe creation for displayfd failed\n");
244 fdstr_update_str1(&display_pipe);
245 no_cloexec_fds[num_no_cloexec_fds++] = display_pipe.fds[1];
247 fdstr_set_fd1(&x11_abstract_socket, abstract_fd);
248 no_cloexec_fds[num_no_cloexec_fds++] = abstract_fd;
250 fdstr_set_fd1(&x11_unix_socket, unix_fd);
251 no_cloexec_fds[num_no_cloexec_fds++] = unix_fd;
253 assert(num_no_cloexec_fds <= ARRAY_LENGTH(no_cloexec_fds));
255 section = weston_config_get_section(config, "xwayland", NULL, NULL);
256 weston_config_section_get_string(section, "path",
257 &xserver, XSERVER_PATH);
258 custom_env_init_from_environ(&child_env);
259 custom_env_set_env_var(&child_env, "WAYLAND_SOCKET", wayland_socket.str1);
261 custom_env_add_arg(&child_env, xserver);
262 custom_env_add_arg(&child_env, display);
263 custom_env_add_arg(&child_env, "-rootless");
264 custom_env_add_arg(&child_env, LISTEN_STR);
265 custom_env_add_arg(&child_env, x11_abstract_socket.str1);
266 custom_env_add_arg(&child_env, LISTEN_STR);
267 custom_env_add_arg(&child_env, x11_unix_socket.str1);
268 custom_env_add_arg(&child_env, "-displayfd");
269 custom_env_add_arg(&child_env, display_pipe.str1);
270 custom_env_add_arg(&child_env, "-wm");
271 custom_env_add_arg(&child_env, x11_wm_socket.str1);
272 custom_env_add_arg(&child_env, "-terminate");
274 wxw->process = client_launch(wxw->compositor, &child_env,
275 no_cloexec_fds, num_no_cloexec_fds,
276 xserver_cleanup, wxw);
278 weston_log("Couldn't start Xwayland\n");
282 client = wl_client_create(wxw->compositor->wl_display,
283 wayland_socket.fds[0]);
285 weston_log("Couldn't create client for Xwayland\n");
289 wxw->wm_fd = x11_wm_socket.fds[0];
291 /* Now we can no longer fail, close the child end of our sockets */
292 close(wayland_socket.fds[1]);
293 close(x11_wm_socket.fds[1]);
294 close(display_pipe.fds[1]);
296 /* During initialization the X server will round trip
297 * and block on the wayland compositor, so avoid making
298 * blocking requests (like xcb_connect_to_fd) until
299 * it's done with that. */
300 loop = wl_display_get_event_loop(wxw->compositor->wl_display);
301 wxw->display_fd_source =
302 wl_event_loop_add_fd(loop, display_pipe.fds[0],
304 handle_display_fd, wxw);
312 wl_list_remove(&wxw->process->link);
315 fdstr_close_all(&display_pipe);
316 fdstr_close_all(&x11_wm_socket);
317 fdstr_close_all(&wayland_socket);
323 wet_xwayland_destroy(struct weston_compositor *compositor, void *data)
325 struct wet_xwayland *wxw = data;
327 /* Calling this will call the process cleanup, in turn cleaning up the
328 * client and the core Xwayland state */
330 ivi_process_destroy(wxw->process, 0, true);
336 wet_load_xwayland(struct weston_compositor *compositor)
338 const struct weston_xwayland_api *api;
339 struct weston_xwayland *xwayland;
340 struct wet_xwayland *wxw;
342 if (weston_compositor_load_xwayland(compositor) < 0)
345 api = weston_xwayland_get_api(compositor);
347 weston_log("Failed to get the xwayland module API.\n");
351 xwayland = api->get(compositor);
353 weston_log("Failed to get the xwayland object.\n");
357 wxw = zalloc(sizeof *wxw);
361 wxw->compositor = compositor;
363 wxw->xwayland = xwayland;
364 if (api->listen(xwayland, wxw, spawn_xserver) < 0)