229ce75d879616cee25e8f241496739ba9bc0c77
[src/agl-compositor.git] / src / xwayland.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  * Copyright © 2016 Giulio Camuffo
4  *
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:
12  *
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.
16  *
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
24  * SOFTWARE.
25  */
26
27 #include "config.h"
28 #include "ivi-compositor.h"
29
30 #include <signal.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/socket.h>
34 #include <fcntl.h>
35
36 #include <libweston/libweston.h>
37 #include <weston.h>
38 #include <libweston/xwayland-api.h>
39 #include "shared/helpers.h"
40 #include "shared/process-util.h"
41 #include "shared/xalloc.h"
42
43 #ifdef HAVE_XWAYLAND_LISTENFD
44 #  define LISTEN_STR "-listenfd"
45 #else
46 #  define LISTEN_STR "-listen"
47 #endif
48
49 struct wet_xwayland {
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;
54         int wm_fd;
55         struct wet_process *process;
56 };
57
58 static void
59 process_destroy(struct wet_process *process, int status, bool call_cleanup)
60 {
61         wl_list_remove(&process->link);
62         if (call_cleanup && process->cleanup)
63                 process->cleanup(process, status, process->cleanup_data);
64         free(process->path);
65         free(process);
66 }
67
68 static int
69 handle_display_fd(int fd, uint32_t mask, void *data)
70 {
71         struct wet_xwayland *wxw = data;
72         char buf[64];
73         ssize_t n;
74
75         /* xwayland exited before being ready, don't finish initialization,
76          * the process watcher will cleanup */
77         if (!(mask & WL_EVENT_READABLE))
78                 goto out;
79
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",
86                                 strerror(errno));
87                 goto out;
88         }
89         /* Returning 1 here means recheck and call us again if required. */
90         if (n <= 0 || (n > 0 && buf[n - 1] != '\n'))
91                 return 1;
92
93         wxw->api->xserver_loaded(wxw->xwayland, wxw->wm_fd);
94
95 out:
96         wl_event_source_remove(wxw->display_fd_source);
97         close(fd);
98
99         return 0;
100 }
101
102
103 static void
104 xserver_cleanup(struct wet_process *process, int status, void *data)
105 {
106         struct wet_xwayland *wxw = data;
107
108         /* We only have one Xwayland process active, so make sure it's the
109          * right one */
110         assert(process == wxw->process);
111
112         wxw->api->xserver_exited(wxw->xwayland);
113         wxw->process = NULL;
114 }
115 static void
116 cleanup_for_child_process() {
117         sigset_t allsigs;
118
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.
124         */
125         setsid();
126
127         /* do not give our signal mask to the new process */
128         sigfillset(&allsigs);
129         sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
130 }
131
132 static struct wet_process *
133 client_launch(struct weston_compositor *compositor,
134                   struct custom_env *child_env,
135                   int *no_cloexec_fds,
136                   size_t num_no_cloexec_fds,
137                   wet_process_cleanup_func_t cleanup,
138                   void *cleanup_data)
139 {
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";
144         char *fail_exec;
145         char * const *argp;
146         char * const *envp;
147         pid_t pid;
148         int err;
149         size_t i;
150         size_t written __attribute__((unused));
151
152         argp = custom_env_get_argp(child_env);
153         envp = custom_env_get_envp(child_env);
154
155         weston_log("launching '%s'\n", argp[0]);
156         str_printf(&fail_exec, "Error: Couldn't launch client '%s'\n", argp[0]);
157
158         pid = fork();
159         switch (pid) {
160         case 0:
161                 cleanup_for_child_process();
162
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));
167                         _exit(EXIT_FAILURE);
168                 }
169
170                 for (i = 0; i < num_no_cloexec_fds; i++) {
171                         err = os_fd_clear_cloexec(no_cloexec_fds[i]);
172                         if (err < 0) {
173                                 written = write(STDERR_FILENO, fail_cloexec,
174                                                 strlen(fail_cloexec));
175                                 _exit(EXIT_FAILURE);
176                         }
177                 }
178
179                 execve(argp[0], argp, envp);
180
181                 if (fail_exec)
182                         written = write(STDERR_FILENO, fail_exec,
183                                         strlen(fail_exec));
184                 _exit(EXIT_FAILURE);
185
186         default:
187                 proc = xzalloc(sizeof(*proc));
188                 proc->pid = pid;
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);
193                 break;
194
195         case -1:
196                 weston_log("weston_client_launch: "
197                            "fork failed while launching '%s': %s\n", argp[0],
198                            strerror(errno));
199                 break;
200         }
201
202         custom_env_fini(child_env);
203         free(fail_exec);
204         return proc;
205 }
206
207 static struct weston_config *
208 ivi_get_config(struct weston_compositor *ec)
209 {
210         struct ivi_compositor *ivi = to_ivi_compositor(ec);
211
212         return ivi->config;
213 }
214
215
216 static struct wl_client *
217 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
218 {
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;
225
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));
235
236         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, wayland_socket.fds) < 0) {
237                 weston_log("wl connection socketpair failed\n");
238                 goto err;
239         }
240         fdstr_update_str1(&wayland_socket);
241         no_cloexec_fds[num_no_cloexec_fds++] = wayland_socket.fds[1];
242
243         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, x11_wm_socket.fds) < 0) {
244                 weston_log("X wm connection socketpair failed\n");
245                 goto err;
246         }
247         fdstr_update_str1(&x11_wm_socket);
248         no_cloexec_fds[num_no_cloexec_fds++] = x11_wm_socket.fds[1];
249
250         if (pipe2(display_pipe.fds, O_CLOEXEC) < 0) {
251                 weston_log("pipe creation for displayfd failed\n");
252                 goto err;
253         }
254         fdstr_update_str1(&display_pipe);
255         no_cloexec_fds[num_no_cloexec_fds++] = display_pipe.fds[1];
256
257         fdstr_set_fd1(&x11_abstract_socket, abstract_fd);
258         no_cloexec_fds[num_no_cloexec_fds++] = abstract_fd;
259
260         fdstr_set_fd1(&x11_unix_socket, unix_fd);
261         no_cloexec_fds[num_no_cloexec_fds++] = unix_fd;
262
263         assert(num_no_cloexec_fds <= ARRAY_LENGTH(no_cloexec_fds));
264
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);
270
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");
283
284         wxw->process = client_launch(wxw->compositor, &child_env,
285                                      no_cloexec_fds, num_no_cloexec_fds,
286                                      xserver_cleanup, wxw);
287         if (!wxw->process) {
288                 weston_log("Couldn't start Xwayland\n");
289                 goto err;
290         }
291
292         client = wl_client_create(wxw->compositor->wl_display,
293                                   wayland_socket.fds[0]);
294         if (!client) {
295                 weston_log("Couldn't create client for Xwayland\n");
296                 goto err_proc;
297         }
298
299         wxw->wm_fd = x11_wm_socket.fds[0];
300
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]);
305
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],
313                                      WL_EVENT_READABLE,
314                                      handle_display_fd, wxw);
315
316         free(xserver);
317
318         return client;
319
320
321 err_proc:
322         wl_list_remove(&wxw->process->link);
323 err:
324         free(xserver);
325         fdstr_close_all(&display_pipe);
326         fdstr_close_all(&x11_wm_socket);
327         fdstr_close_all(&wayland_socket);
328         free(wxw->process);
329         return NULL;
330 }
331
332 void
333 wet_xwayland_destroy(struct weston_compositor *compositor, void *data)
334 {
335         struct wet_xwayland *wxw = data;
336
337         /* Calling this will call the process cleanup, in turn cleaning up the
338          * client and the core Xwayland state */
339         if (wxw->process)
340                 process_destroy(wxw->process, 0, true);
341
342         free(wxw);
343 }
344
345 void *
346 wet_load_xwayland(struct weston_compositor *compositor)
347 {
348         const struct weston_xwayland_api *api;
349         struct weston_xwayland *xwayland;
350         struct wet_xwayland *wxw;
351
352         if (weston_compositor_load_xwayland(compositor) < 0)
353                 return NULL;
354
355         api = weston_xwayland_get_api(compositor);
356         if (!api) {
357                 weston_log("Failed to get the xwayland module API.\n");
358                 return NULL;
359         }
360
361         xwayland = api->get(compositor);
362         if (!xwayland) {
363                 weston_log("Failed to get the xwayland object.\n");
364                 return NULL;
365         }
366
367         wxw = zalloc(sizeof *wxw);
368         if (!wxw)
369                 return NULL;
370
371         wxw->compositor = compositor;
372         wxw->api = api;
373         wxw->xwayland = xwayland;
374         if (api->listen(xwayland, wxw, spawn_xserver) < 0)
375                 return NULL;
376
377         return wxw;
378 }