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