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>
35 #include <libweston/libweston.h>
37 #include <libweston/xwayland-api.h>
38 #include "shared/helpers.h"
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;
47 struct weston_process process;
51 handle_sigusr1(int signal_number, void *data)
53 struct wet_xwayland *wxw = data;
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);
65 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
67 struct wet_xwayland *wxw = user_data;
69 char s[12], abstract_fd_str[12], unix_fd_str[12], wm_fd_str[12];
72 struct weston_config *config = wxw->ivi_compositor->config;
73 struct weston_config_section *section;
75 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
76 weston_log("wl connection socketpair failed\n");
80 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
81 weston_log("X wm connection socketpair failed\n");
88 /* SOCK_CLOEXEC closes both ends, so we need to unset
89 * the flag on the client fd. */
93 snprintf(s, sizeof s, "%d", fd);
94 setenv("WAYLAND_SOCKET", s, 1);
96 fd = dup(abstract_fd);
99 snprintf(abstract_fd_str, sizeof abstract_fd_str, "%d", fd);
103 snprintf(unix_fd_str, sizeof unix_fd_str, "%d", fd);
107 snprintf(wm_fd_str, sizeof wm_fd_str, "%d", fd);
109 section = weston_config_get_section(config,
110 "xwayland", NULL, NULL);
111 weston_config_section_get_string(section, "path",
112 &xserver, XSERVER_PATH);
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);
127 #ifdef HAVE_XWAYLAND_LISTENFD
128 "-listenfd", abstract_fd_str,
129 "-listenfd", unix_fd_str,
131 "-listen", abstract_fd_str,
132 "-listen", unix_fd_str,
137 weston_log("exec of '%s %s -rootless "
138 #ifdef HAVE_XWAYLAND_LISTENFD
139 "-listenfd %s -listenfd %s "
141 "-listen %s -listen %s "
143 "-wm %s -terminate' failed: %s\n",
145 abstract_fd_str, unix_fd_str, wm_fd_str,
152 wxw->client = wl_client_create(wxw->ivi_compositor->compositor->wl_display, sv[0]);
157 wxw->process.pid = pid;
158 wl_list_insert(&wxw->ivi_compositor->child_process_list,
163 weston_log("Failed to fork to spawn xserver process\n");
171 xserver_cleanup(struct weston_process *process, int status)
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);
178 wxw->api->xserver_exited(wxw->xwayland, status);
179 wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
180 handle_sigusr1, wxw);
185 wet_load_xwayland(struct weston_compositor *comp)
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);
193 if (weston_compositor_load_xwayland(comp) < 0)
196 api = weston_xwayland_get_api(comp);
198 weston_log("Failed to get the xwayland module API.\n");
202 xwayland = api->get(comp);
204 weston_log("Failed to get the xwayland object.\n");
208 wxw = zalloc(sizeof *wxw);
212 wxw->ivi_compositor = ivi;
214 wxw->xwayland = xwayland;
215 wxw->process.cleanup = xserver_cleanup;
216 if (api->listen(xwayland, wxw, spawn_xserver) < 0)
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);