• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include <signal.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/socket.h>
33 
34 #include <libweston/libweston.h>
35 #include "compositor/weston.h"
36 #include <libweston/xwayland-api.h>
37 #include "shared/helpers.h"
38 
39 struct 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 
49 static int
handle_sigusr1(int signal_number,void * data)50 handle_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 
63 static pid_t
spawn_xserver(void * user_data,const char * display,int abstract_fd,int unix_fd)64 spawn_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;
68 	char s[12], abstract_fd_str[12], unix_fd_str[12], wm_fd_str[12];
69 	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",
126 			  "-listen", abstract_fd_str,
127 			  "-listen", unix_fd_str,
128 			  "-wm", wm_fd_str,
129 			  "-terminate",
130 			  NULL) < 0)
131 			weston_log("exec of '%s %s -rootless "
132 				   "-listen %s -listen %s -wm %s "
133 				   "-terminate' failed: %s\n",
134 				   xserver, display,
135 				   abstract_fd_str, unix_fd_str, wm_fd_str,
136 				   strerror(errno));
137 	fail:
138 		_exit(EXIT_FAILURE);
139 
140 	default:
141 		close(sv[1]);
142 		wxw->client = wl_client_create(wxw->compositor->wl_display, sv[0]);
143 
144 		close(wm[1]);
145 		wxw->wm_fd = wm[0];
146 
147 		wxw->process.pid = pid;
148 		weston_watch_process(&wxw->process);
149 		break;
150 
151 	case -1:
152 		weston_log("Failed to fork to spawn xserver process\n");
153 		break;
154 	}
155 
156 	return pid;
157 }
158 
159 static void
xserver_cleanup(struct weston_process * process,int status)160 xserver_cleanup(struct weston_process *process, int status)
161 {
162 	struct wet_xwayland *wxw =
163 		container_of(process, struct wet_xwayland, process);
164 	struct wl_event_loop *loop =
165 		wl_display_get_event_loop(wxw->compositor->wl_display);
166 
167 	wxw->api->xserver_exited(wxw->xwayland, status);
168 	wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
169                                                        handle_sigusr1, wxw);
170 	wxw->client = NULL;
171 }
172 
173 int
wet_load_xwayland(struct weston_compositor * comp)174 wet_load_xwayland(struct weston_compositor *comp)
175 {
176 	const struct weston_xwayland_api *api;
177 	struct weston_xwayland *xwayland;
178 	struct wet_xwayland *wxw;
179 	struct wl_event_loop *loop;
180 
181 	if (weston_compositor_load_xwayland(comp) < 0)
182 		return -1;
183 
184 	api = weston_xwayland_get_api(comp);
185 	if (!api) {
186 		weston_log("Failed to get the xwayland module API.\n");
187 		return -1;
188 	}
189 
190 	xwayland = api->get(comp);
191 	if (!xwayland) {
192 		weston_log("Failed to get the xwayland object.\n");
193 		return -1;
194 	}
195 
196 	wxw = zalloc(sizeof *wxw);
197 	if (!wxw)
198 		return -1;
199 
200 	wxw->compositor = comp;
201 	wxw->api = api;
202 	wxw->xwayland = xwayland;
203 	wxw->process.cleanup = xserver_cleanup;
204 	if (api->listen(xwayland, wxw, spawn_xserver) < 0)
205 		return -1;
206 
207 	loop = wl_display_get_event_loop(comp->wl_display);
208 	wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
209 						       handle_sigusr1, wxw);
210 
211 	return 0;
212 }
213