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 process_destroy(struct wet_process *process, int status, bool call_cleanup)
61 wl_list_remove(&process->link);
62 if (call_cleanup && process->cleanup)
63 process->cleanup(process, status, process->cleanup_data);
69 handle_display_fd(int fd, uint32_t mask, void *data)
71 struct wet_xwayland *wxw = data;
75 /* xwayland exited before being ready, don't finish initialization,
76 * the process watcher will cleanup */
77 if (!(mask & WL_EVENT_READABLE))
80 /* Xwayland writes to the pipe twice, so if we close it too early
81 * it's possible the second write will fail and Xwayland shuts down.
82 * Make sure we read until end of line marker to avoid this. */
83 n = read(fd, buf, sizeof buf);
84 if (n < 0 && errno != EAGAIN) {
85 weston_log("read from Xwayland display_fd failed: %s\n",
89 /* Returning 1 here means recheck and call us again if required. */
90 if (n <= 0 || (n > 0 && buf[n - 1] != '\n'))
93 wxw->api->xserver_loaded(wxw->xwayland, wxw->wm_fd);
96 wl_event_source_remove(wxw->display_fd_source);
104 xserver_cleanup(struct wet_process *process, int status, void *data)
106 struct wet_xwayland *wxw = data;
108 /* We only have one Xwayland process active, so make sure it's the
110 assert(process == wxw->process);
112 wxw->api->xserver_exited(wxw->xwayland);
116 cleanup_for_child_process() {
119 /* Put the client in a new session so it won't catch signals
120 * intended for the parent. Sharing a session can be
121 * confusing when launching weston under gdb, as the ctrl-c
122 * intended for gdb will pass to the child, and weston
123 * will cleanly shut down when the child exits.
127 /* do not give our signal mask to the new process */
128 sigfillset(&allsigs);
129 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
132 static struct wet_process *
133 client_launch(struct weston_compositor *compositor,
134 struct custom_env *child_env,
136 size_t num_no_cloexec_fds,
137 wet_process_cleanup_func_t cleanup,
140 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
141 struct wet_process *proc = NULL;
142 const char *fail_cloexec = "Couldn't unset CLOEXEC on child FDs";
143 const char *fail_seteuid = "Couldn't call seteuid";
150 size_t written __attribute__((unused));
152 argp = custom_env_get_argp(child_env);
153 envp = custom_env_get_envp(child_env);
155 weston_log("launching '%s'\n", argp[0]);
156 str_printf(&fail_exec, "Error: Couldn't launch client '%s'\n", argp[0]);
161 cleanup_for_child_process();
163 /* Launch clients as the user. Do not launch clients with wrong euid. */
164 if (seteuid(getuid()) == -1) {
165 written = write(STDERR_FILENO, fail_seteuid,
166 strlen(fail_seteuid));
170 for (i = 0; i < num_no_cloexec_fds; i++) {
171 err = os_fd_clear_cloexec(no_cloexec_fds[i]);
173 written = write(STDERR_FILENO, fail_cloexec,
174 strlen(fail_cloexec));
179 execve(argp[0], argp, envp);
182 written = write(STDERR_FILENO, fail_exec,
187 proc = xzalloc(sizeof(*proc));
189 proc->cleanup = cleanup;
190 proc->cleanup_data = cleanup_data;
191 proc->path = strdup(argp[0]);
192 wl_list_insert(&ivi->child_process_list, &proc->link);
196 weston_log("weston_client_launch: "
197 "fork failed while launching '%s': %s\n", argp[0],
202 custom_env_fini(child_env);
207 static struct weston_config *
208 ivi_get_config(struct weston_compositor *ec)
210 struct ivi_compositor *ivi = to_ivi_compositor(ec);
216 static struct wl_client *
217 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
219 struct wet_xwayland *wxw = user_data;
220 struct fdstr wayland_socket = FDSTR_INIT;
221 struct fdstr x11_abstract_socket = FDSTR_INIT;
222 struct fdstr x11_unix_socket = FDSTR_INIT;
223 struct fdstr x11_wm_socket = FDSTR_INIT;
224 struct fdstr display_pipe = FDSTR_INIT;
226 char *xserver = NULL;
227 struct weston_config *config = ivi_get_config(wxw->compositor);
228 struct weston_config_section *section;
229 struct wl_client *client;
230 struct wl_event_loop *loop;
231 struct custom_env child_env;
232 int no_cloexec_fds[5];
233 size_t num_no_cloexec_fds = 0;
234 size_t written __attribute__ ((unused));
236 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, wayland_socket.fds) < 0) {
237 weston_log("wl connection socketpair failed\n");
240 fdstr_update_str1(&wayland_socket);
241 no_cloexec_fds[num_no_cloexec_fds++] = wayland_socket.fds[1];
243 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, x11_wm_socket.fds) < 0) {
244 weston_log("X wm connection socketpair failed\n");
247 fdstr_update_str1(&x11_wm_socket);
248 no_cloexec_fds[num_no_cloexec_fds++] = x11_wm_socket.fds[1];
250 if (pipe2(display_pipe.fds, O_CLOEXEC) < 0) {
251 weston_log("pipe creation for displayfd failed\n");
254 fdstr_update_str1(&display_pipe);
255 no_cloexec_fds[num_no_cloexec_fds++] = display_pipe.fds[1];
257 fdstr_set_fd1(&x11_abstract_socket, abstract_fd);
258 no_cloexec_fds[num_no_cloexec_fds++] = abstract_fd;
260 fdstr_set_fd1(&x11_unix_socket, unix_fd);
261 no_cloexec_fds[num_no_cloexec_fds++] = unix_fd;
263 assert(num_no_cloexec_fds <= ARRAY_LENGTH(no_cloexec_fds));
265 section = weston_config_get_section(config, "xwayland", NULL, NULL);
266 weston_config_section_get_string(section, "path",
267 &xserver, XSERVER_PATH);
268 custom_env_init_from_environ(&child_env);
269 custom_env_set_env_var(&child_env, "WAYLAND_SOCKET", wayland_socket.str1);
271 custom_env_add_arg(&child_env, xserver);
272 custom_env_add_arg(&child_env, display);
273 custom_env_add_arg(&child_env, "-rootless");
274 custom_env_add_arg(&child_env, LISTEN_STR);
275 custom_env_add_arg(&child_env, x11_abstract_socket.str1);
276 custom_env_add_arg(&child_env, LISTEN_STR);
277 custom_env_add_arg(&child_env, x11_unix_socket.str1);
278 custom_env_add_arg(&child_env, "-displayfd");
279 custom_env_add_arg(&child_env, display_pipe.str1);
280 custom_env_add_arg(&child_env, "-wm");
281 custom_env_add_arg(&child_env, x11_wm_socket.str1);
282 custom_env_add_arg(&child_env, "-terminate");
284 wxw->process = client_launch(wxw->compositor, &child_env,
285 no_cloexec_fds, num_no_cloexec_fds,
286 xserver_cleanup, wxw);
288 weston_log("Couldn't start Xwayland\n");
292 client = wl_client_create(wxw->compositor->wl_display,
293 wayland_socket.fds[0]);
295 weston_log("Couldn't create client for Xwayland\n");
299 wxw->wm_fd = x11_wm_socket.fds[0];
301 /* Now we can no longer fail, close the child end of our sockets */
302 close(wayland_socket.fds[1]);
303 close(x11_wm_socket.fds[1]);
304 close(display_pipe.fds[1]);
306 /* During initialization the X server will round trip
307 * and block on the wayland compositor, so avoid making
308 * blocking requests (like xcb_connect_to_fd) until
309 * it's done with that. */
310 loop = wl_display_get_event_loop(wxw->compositor->wl_display);
311 wxw->display_fd_source =
312 wl_event_loop_add_fd(loop, display_pipe.fds[0],
314 handle_display_fd, wxw);
322 wl_list_remove(&wxw->process->link);
325 fdstr_close_all(&display_pipe);
326 fdstr_close_all(&x11_wm_socket);
327 fdstr_close_all(&wayland_socket);
333 wet_xwayland_destroy(struct weston_compositor *compositor, void *data)
335 struct wet_xwayland *wxw = data;
337 /* Calling this will call the process cleanup, in turn cleaning up the
338 * client and the core Xwayland state */
340 process_destroy(wxw->process, 0, true);
346 wet_load_xwayland(struct weston_compositor *compositor)
348 const struct weston_xwayland_api *api;
349 struct weston_xwayland *xwayland;
350 struct wet_xwayland *wxw;
352 if (weston_compositor_load_xwayland(compositor) < 0)
355 api = weston_xwayland_get_api(compositor);
357 weston_log("Failed to get the xwayland module API.\n");
361 xwayland = api->get(compositor);
363 weston_log("Failed to get the xwayland object.\n");
367 wxw = zalloc(sizeof *wxw);
371 wxw->compositor = compositor;
373 wxw->xwayland = xwayland;
374 if (api->listen(xwayland, wxw, spawn_xserver) < 0)