compositor: Fix wet_process cleanup on compositor shuwdown
[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 int
59 handle_display_fd(int fd, uint32_t mask, void *data)
60 {
61         struct wet_xwayland *wxw = data;
62         char buf[64];
63         ssize_t n;
64
65         /* xwayland exited before being ready, don't finish initialization,
66          * the process watcher will cleanup */
67         if (!(mask & WL_EVENT_READABLE))
68                 goto out;
69
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",
76                                 strerror(errno));
77                 goto out;
78         }
79         /* Returning 1 here means recheck and call us again if required. */
80         if (n <= 0 || (n > 0 && buf[n - 1] != '\n'))
81                 return 1;
82
83         wxw->api->xserver_loaded(wxw->xwayland, wxw->wm_fd);
84
85 out:
86         wl_event_source_remove(wxw->display_fd_source);
87         close(fd);
88
89         return 0;
90 }
91
92
93 static void
94 xserver_cleanup(struct wet_process *process, int status, void *data)
95 {
96         struct wet_xwayland *wxw = data;
97
98         /* We only have one Xwayland process active, so make sure it's the
99          * right one */
100         assert(process == wxw->process);
101
102         wxw->api->xserver_exited(wxw->xwayland);
103         wxw->process = NULL;
104 }
105 static void
106 cleanup_for_child_process() {
107         sigset_t allsigs;
108
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.
114         */
115         setsid();
116
117         /* do not give our signal mask to the new process */
118         sigfillset(&allsigs);
119         sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
120 }
121
122 static struct wet_process *
123 client_launch(struct weston_compositor *compositor,
124                   struct custom_env *child_env,
125                   int *no_cloexec_fds,
126                   size_t num_no_cloexec_fds,
127                   wet_process_cleanup_func_t cleanup,
128                   void *cleanup_data)
129 {
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";
134         char *fail_exec;
135         char * const *argp;
136         char * const *envp;
137         pid_t pid;
138         int err;
139         size_t i;
140         size_t written __attribute__((unused));
141
142         argp = custom_env_get_argp(child_env);
143         envp = custom_env_get_envp(child_env);
144
145         weston_log("launching '%s'\n", argp[0]);
146         str_printf(&fail_exec, "Error: Couldn't launch client '%s'\n", argp[0]);
147
148         pid = fork();
149         switch (pid) {
150         case 0:
151                 cleanup_for_child_process();
152
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));
157                         _exit(EXIT_FAILURE);
158                 }
159
160                 for (i = 0; i < num_no_cloexec_fds; i++) {
161                         err = os_fd_clear_cloexec(no_cloexec_fds[i]);
162                         if (err < 0) {
163                                 written = write(STDERR_FILENO, fail_cloexec,
164                                                 strlen(fail_cloexec));
165                                 _exit(EXIT_FAILURE);
166                         }
167                 }
168
169                 execve(argp[0], argp, envp);
170
171                 if (fail_exec)
172                         written = write(STDERR_FILENO, fail_exec,
173                                         strlen(fail_exec));
174                 _exit(EXIT_FAILURE);
175
176         default:
177                 proc = xzalloc(sizeof(*proc));
178                 proc->pid = pid;
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);
183                 break;
184
185         case -1:
186                 weston_log("weston_client_launch: "
187                            "fork failed while launching '%s': %s\n", argp[0],
188                            strerror(errno));
189                 break;
190         }
191
192         custom_env_fini(child_env);
193         free(fail_exec);
194         return proc;
195 }
196
197 static struct weston_config *
198 ivi_get_config(struct weston_compositor *ec)
199 {
200         struct ivi_compositor *ivi = to_ivi_compositor(ec);
201
202         return ivi->config;
203 }
204
205
206 static struct wl_client *
207 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
208 {
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;
215
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));
225
226         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, wayland_socket.fds) < 0) {
227                 weston_log("wl connection socketpair failed\n");
228                 goto err;
229         }
230         fdstr_update_str1(&wayland_socket);
231         no_cloexec_fds[num_no_cloexec_fds++] = wayland_socket.fds[1];
232
233         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, x11_wm_socket.fds) < 0) {
234                 weston_log("X wm connection socketpair failed\n");
235                 goto err;
236         }
237         fdstr_update_str1(&x11_wm_socket);
238         no_cloexec_fds[num_no_cloexec_fds++] = x11_wm_socket.fds[1];
239
240         if (pipe2(display_pipe.fds, O_CLOEXEC) < 0) {
241                 weston_log("pipe creation for displayfd failed\n");
242                 goto err;
243         }
244         fdstr_update_str1(&display_pipe);
245         no_cloexec_fds[num_no_cloexec_fds++] = display_pipe.fds[1];
246
247         fdstr_set_fd1(&x11_abstract_socket, abstract_fd);
248         no_cloexec_fds[num_no_cloexec_fds++] = abstract_fd;
249
250         fdstr_set_fd1(&x11_unix_socket, unix_fd);
251         no_cloexec_fds[num_no_cloexec_fds++] = unix_fd;
252
253         assert(num_no_cloexec_fds <= ARRAY_LENGTH(no_cloexec_fds));
254
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);
260
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");
273
274         wxw->process = client_launch(wxw->compositor, &child_env,
275                                      no_cloexec_fds, num_no_cloexec_fds,
276                                      xserver_cleanup, wxw);
277         if (!wxw->process) {
278                 weston_log("Couldn't start Xwayland\n");
279                 goto err;
280         }
281
282         client = wl_client_create(wxw->compositor->wl_display,
283                                   wayland_socket.fds[0]);
284         if (!client) {
285                 weston_log("Couldn't create client for Xwayland\n");
286                 goto err_proc;
287         }
288
289         wxw->wm_fd = x11_wm_socket.fds[0];
290
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]);
295
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],
303                                      WL_EVENT_READABLE,
304                                      handle_display_fd, wxw);
305
306         free(xserver);
307
308         return client;
309
310
311 err_proc:
312         wl_list_remove(&wxw->process->link);
313 err:
314         free(xserver);
315         fdstr_close_all(&display_pipe);
316         fdstr_close_all(&x11_wm_socket);
317         fdstr_close_all(&wayland_socket);
318         free(wxw->process);
319         return NULL;
320 }
321
322 void
323 wet_xwayland_destroy(struct weston_compositor *compositor, void *data)
324 {
325         struct wet_xwayland *wxw = data;
326
327         /* Calling this will call the process cleanup, in turn cleaning up the
328          * client and the core Xwayland state */
329         if (wxw->process)
330                 ivi_process_destroy(wxw->process, 0, true);
331
332         free(wxw);
333 }
334
335 void *
336 wet_load_xwayland(struct weston_compositor *compositor)
337 {
338         const struct weston_xwayland_api *api;
339         struct weston_xwayland *xwayland;
340         struct wet_xwayland *wxw;
341
342         if (weston_compositor_load_xwayland(compositor) < 0)
343                 return NULL;
344
345         api = weston_xwayland_get_api(compositor);
346         if (!api) {
347                 weston_log("Failed to get the xwayland module API.\n");
348                 return NULL;
349         }
350
351         xwayland = api->get(compositor);
352         if (!xwayland) {
353                 weston_log("Failed to get the xwayland object.\n");
354                 return NULL;
355         }
356
357         wxw = zalloc(sizeof *wxw);
358         if (!wxw)
359                 return NULL;
360
361         wxw->compositor = compositor;
362         wxw->api = api;
363         wxw->xwayland = xwayland;
364         if (api->listen(xwayland, wxw, spawn_xserver) < 0)
365                 return NULL;
366
367         return wxw;
368 }