8ac5edc105b5959be3b81e853d338d351ff8c11c
[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
35 #include <libweston/libweston.h>
36 #include <weston.h>
37 #include <libweston/xwayland-api.h>
38 #include "shared/helpers.h"
39
40 struct wet_xwayland {
41         struct ivi_compositor *ivi_compositor;
42         const struct weston_xwayland_api *api;
43         struct weston_xwayland *xwayland;
44         struct wl_event_source *sigusr1_source;
45         struct wl_client *client;
46         int wm_fd;
47         struct weston_process process;
48 };
49
50 static int
51 handle_sigusr1(int signal_number, void *data)
52 {
53         struct wet_xwayland *wxw = data;
54
55         /* We'd be safer if we actually had the struct
56          * signalfd_siginfo from the signalfd data and could verify
57          * this came from Xwayland.*/
58         wxw->api->xserver_loaded(wxw->xwayland, wxw->client, wxw->wm_fd);
59         wl_event_source_remove(wxw->sigusr1_source);
60
61         return 1;
62 }
63
64 static pid_t
65 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
66 {
67         struct wet_xwayland *wxw = user_data;
68         pid_t pid;
69         char s[12], abstract_fd_str[12], unix_fd_str[12], wm_fd_str[12];
70         int sv[2], wm[2], fd;
71         char *xserver = NULL;
72         struct weston_config *config = wxw->ivi_compositor->config;
73         struct weston_config_section *section;
74
75         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
76                 weston_log("wl connection socketpair failed\n");
77                 return 1;
78         }
79
80         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
81                 weston_log("X wm connection socketpair failed\n");
82                 return 1;
83         }
84
85         pid = fork();
86         switch (pid) {
87         case 0:
88                 /* SOCK_CLOEXEC closes both ends, so we need to unset
89                  * the flag on the client fd. */
90                 fd = dup(sv[1]);
91                 if (fd < 0)
92                         goto fail;
93                 snprintf(s, sizeof s, "%d", fd);
94                 setenv("WAYLAND_SOCKET", s, 1);
95
96                 fd = dup(abstract_fd);
97                 if (fd < 0)
98                         goto fail;
99                 snprintf(abstract_fd_str, sizeof abstract_fd_str, "%d", fd);
100                 fd = dup(unix_fd);
101                 if (fd < 0)
102                         goto fail;
103                 snprintf(unix_fd_str, sizeof unix_fd_str, "%d", fd);
104                 fd = dup(wm[1]);
105                 if (fd < 0)
106                         goto fail;
107                 snprintf(wm_fd_str, sizeof wm_fd_str, "%d", fd);
108
109                 section = weston_config_get_section(config,
110                                                     "xwayland", NULL, NULL);
111                 weston_config_section_get_string(section, "path",
112                                                  &xserver, XSERVER_PATH);
113
114                 /* Ignore SIGUSR1 in the child, which will make the X
115                  * server send SIGUSR1 to the parent (weston) when
116                  * it's done with initialization.  During
117                  * initialization the X server will round trip and
118                  * block on the wayland compositor, so avoid making
119                  * blocking requests (like xcb_connect_to_fd) until
120                  * it's done with that. */
121                 signal(SIGUSR1, SIG_IGN);
122
123                 if (execl(xserver,
124                           xserver,
125                           display,
126                           "-rootless",
127 #ifdef HAVE_XWAYLAND_LISTENFD
128                           "-listenfd", abstract_fd_str,
129                           "-listenfd", unix_fd_str,
130 #else
131                           "-listen", abstract_fd_str,
132                           "-listen", unix_fd_str,
133 #endif
134                           "-wm", wm_fd_str,
135                           "-terminate",
136                           NULL) < 0)
137                         weston_log("exec of '%s %s -rootless "
138 #ifdef HAVE_XWAYLAND_LISTENFD
139                                    "-listenfd %s -listenfd %s "
140 #else
141                                    "-listen %s -listen %s "
142 #endif
143                                    "-wm %s -terminate' failed: %s\n",
144                                    xserver, display,
145                                    abstract_fd_str, unix_fd_str, wm_fd_str,
146                                    strerror(errno));
147         fail:
148                 _exit(EXIT_FAILURE);
149
150         default:
151                 close(sv[1]);
152                 wxw->client = wl_client_create(wxw->ivi_compositor->compositor->wl_display, sv[0]);
153
154                 close(wm[1]);
155                 wxw->wm_fd = wm[0];
156
157                 wxw->process.pid = pid;
158                 wl_list_insert(&wxw->ivi_compositor->child_process_list,
159                                &wxw->process.link);
160                 break;
161
162         case -1:
163                 weston_log("Failed to fork to spawn xserver process\n");
164                 break;
165         }
166
167         return pid;
168 }
169
170 static void
171 xserver_cleanup(struct weston_process *process, int status)
172 {
173         struct wet_xwayland *wxw =
174                 container_of(process, struct wet_xwayland, process);
175         struct wl_event_loop *loop =
176                 wl_display_get_event_loop(wxw->ivi_compositor->compositor->wl_display);
177
178         wxw->api->xserver_exited(wxw->xwayland, status);
179         wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
180                                                        handle_sigusr1, wxw);
181         wxw->client = NULL;
182 }
183
184 int
185 wet_load_xwayland(struct weston_compositor *comp)
186 {
187         const struct weston_xwayland_api *api;
188         struct weston_xwayland *xwayland;
189         struct wet_xwayland *wxw;
190         struct wl_event_loop *loop;
191         struct ivi_compositor *ivi = to_ivi_compositor(comp);
192
193         if (weston_compositor_load_xwayland(comp) < 0)
194                 return -1;
195
196         api = weston_xwayland_get_api(comp);
197         if (!api) {
198                 weston_log("Failed to get the xwayland module API.\n");
199                 return -1;
200         }
201
202         xwayland = api->get(comp);
203         if (!xwayland) {
204                 weston_log("Failed to get the xwayland object.\n");
205                 return -1;
206         }
207
208         wxw = zalloc(sizeof *wxw);
209         if (!wxw)
210                 return -1;
211
212         wxw->ivi_compositor = ivi;
213         wxw->api = api;
214         wxw->xwayland = xwayland;
215         wxw->process.cleanup = xserver_cleanup;
216         if (api->listen(xwayland, wxw, spawn_xserver) < 0)
217                 return -1;
218
219         loop = wl_display_get_event_loop(comp->wl_display);
220         wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
221                                                        handle_sigusr1, wxw);
222
223         return 0;
224 }