blob: 8cdf05556feb17bdecf508c5a52f52c0a55fab0f [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
Bryce Harrington9335ca52016-07-06 15:14:20 -070027#include "config.h"
28
Giulio Camuffo9c764df2016-06-29 11:54:27 +020029#include <signal.h>
Antonio Borneo39578632019-04-26 23:57:31 +020030#include <string.h>
31#include <errno.h>
Giulio Camuffo9c764df2016-06-29 11:54:27 +020032#include <sys/socket.h>
33
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020034#include <libweston/libweston.h>
Giulio Camuffo9c764df2016-06-29 11:54:27 +020035#include "compositor/weston.h"
Pekka Paalaneneebb7dc2019-04-04 15:52:47 +030036#include <libweston/xwayland-api.h>
Giulio Camuffo9c764df2016-06-29 11:54:27 +020037#include "shared/helpers.h"
38
39struct wet_xwayland {
40 struct weston_compositor *compositor;
41 const struct weston_xwayland_api *api;
42 struct weston_xwayland *xwayland;
43 struct wl_event_source *sigusr1_source;
44 struct wl_client *client;
45 int wm_fd;
46 struct weston_process process;
47};
48
49static int
50handle_sigusr1(int signal_number, void *data)
51{
52 struct wet_xwayland *wxw = data;
53
54 /* We'd be safer if we actually had the struct
55 * signalfd_siginfo from the signalfd data and could verify
56 * this came from Xwayland.*/
57 wxw->api->xserver_loaded(wxw->xwayland, wxw->client, wxw->wm_fd);
58 wl_event_source_remove(wxw->sigusr1_source);
59
60 return 1;
61}
62
63static pid_t
64spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
65{
66 struct wet_xwayland *wxw = user_data;
67 pid_t pid;
Alexandros Frantzis7e2d4be2018-10-12 12:52:42 +030068 char s[12], abstract_fd_str[12], unix_fd_str[12], wm_fd_str[12];
Giulio Camuffo9c764df2016-06-29 11:54:27 +020069 int sv[2], wm[2], fd;
70 char *xserver = NULL;
71 struct weston_config *config = wet_get_config(wxw->compositor);
72 struct weston_config_section *section;
73
74 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
75 weston_log("wl connection socketpair failed\n");
76 return 1;
77 }
78
79 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
80 weston_log("X wm connection socketpair failed\n");
81 return 1;
82 }
83
84 pid = fork();
85 switch (pid) {
86 case 0:
87 /* SOCK_CLOEXEC closes both ends, so we need to unset
88 * the flag on the client fd. */
89 fd = dup(sv[1]);
90 if (fd < 0)
91 goto fail;
92 snprintf(s, sizeof s, "%d", fd);
93 setenv("WAYLAND_SOCKET", s, 1);
94
95 fd = dup(abstract_fd);
96 if (fd < 0)
97 goto fail;
98 snprintf(abstract_fd_str, sizeof abstract_fd_str, "%d", fd);
99 fd = dup(unix_fd);
100 if (fd < 0)
101 goto fail;
102 snprintf(unix_fd_str, sizeof unix_fd_str, "%d", fd);
103 fd = dup(wm[1]);
104 if (fd < 0)
105 goto fail;
106 snprintf(wm_fd_str, sizeof wm_fd_str, "%d", fd);
107
108 section = weston_config_get_section(config,
109 "xwayland", NULL, NULL);
110 weston_config_section_get_string(section, "path",
111 &xserver, XSERVER_PATH);
112
113 /* Ignore SIGUSR1 in the child, which will make the X
114 * server send SIGUSR1 to the parent (weston) when
115 * it's done with initialization. During
116 * initialization the X server will round trip and
117 * block on the wayland compositor, so avoid making
118 * blocking requests (like xcb_connect_to_fd) until
119 * it's done with that. */
120 signal(SIGUSR1, SIG_IGN);
121
122 if (execl(xserver,
123 xserver,
124 display,
125 "-rootless",
Vlad Zahorodnii022ea432021-02-09 10:43:22 +0200126#ifdef HAVE_XWAYLAND_LISTENFD
127 "-listenfd", abstract_fd_str,
128 "-listenfd", unix_fd_str,
129#else
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200130 "-listen", abstract_fd_str,
131 "-listen", unix_fd_str,
Vlad Zahorodnii022ea432021-02-09 10:43:22 +0200132#endif
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200133 "-wm", wm_fd_str,
134 "-terminate",
135 NULL) < 0)
136 weston_log("exec of '%s %s -rootless "
Vlad Zahorodnii022ea432021-02-09 10:43:22 +0200137#ifdef HAVE_XWAYLAND_LISTENFD
138 "-listenfd %s -listenfd %s "
139#else
140 "-listen %s -listen %s "
141#endif
142 "-wm %s -terminate' failed: %s\n",
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200143 xserver, display,
Antonio Borneo39578632019-04-26 23:57:31 +0200144 abstract_fd_str, unix_fd_str, wm_fd_str,
145 strerror(errno));
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200146 fail:
147 _exit(EXIT_FAILURE);
148
149 default:
150 close(sv[1]);
151 wxw->client = wl_client_create(wxw->compositor->wl_display, sv[0]);
152
153 close(wm[1]);
154 wxw->wm_fd = wm[0];
155
156 wxw->process.pid = pid;
Alvarito0505061f57a1f2021-06-29 14:33:04 -0300157 wet_watch_process(wxw->compositor, &wxw->process);
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200158 break;
159
160 case -1:
Bryce Harringtoncff0b1d2016-07-06 15:18:46 -0700161 weston_log("Failed to fork to spawn xserver process\n");
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200162 break;
163 }
164
165 return pid;
166}
167
168static void
169xserver_cleanup(struct weston_process *process, int status)
170{
171 struct wet_xwayland *wxw =
172 container_of(process, struct wet_xwayland, process);
173 struct wl_event_loop *loop =
174 wl_display_get_event_loop(wxw->compositor->wl_display);
175
176 wxw->api->xserver_exited(wxw->xwayland, status);
177 wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200178 handle_sigusr1, wxw);
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200179 wxw->client = NULL;
180}
181
182int
183wet_load_xwayland(struct weston_compositor *comp)
184{
185 const struct weston_xwayland_api *api;
186 struct weston_xwayland *xwayland;
187 struct wet_xwayland *wxw;
188 struct wl_event_loop *loop;
189
190 if (weston_compositor_load_xwayland(comp) < 0)
191 return -1;
192
193 api = weston_xwayland_get_api(comp);
194 if (!api) {
195 weston_log("Failed to get the xwayland module API.\n");
196 return -1;
197 }
198
199 xwayland = api->get(comp);
200 if (!xwayland) {
201 weston_log("Failed to get the xwayland object.\n");
202 return -1;
203 }
204
205 wxw = zalloc(sizeof *wxw);
206 if (!wxw)
207 return -1;
208
209 wxw->compositor = comp;
210 wxw->api = api;
211 wxw->xwayland = xwayland;
212 wxw->process.cleanup = xserver_cleanup;
213 if (api->listen(xwayland, wxw, spawn_xserver) < 0)
214 return -1;
215
216 loop = wl_display_get_event_loop(comp->wl_display);
217 wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
218 handle_sigusr1, wxw);
219
220 return 0;
221}