1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* Copyright © 2011 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include "xwayland.h"
#include "xwayland-api.h"
#include "shared/helpers.h"
#include "compositor/weston.h"
static int
weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data)
{
struct weston_xserver *wxs = data;
char display[8];
snprintf(display, sizeof display, ":%d", wxs->display);
wxs->pid = wxs->spawn_func(wxs->user_data, display, wxs->abstract_fd, wxs->unix_fd);
if (wxs->pid == -1) {
weston_log("Failed to spawn the Xwayland server\n");
return 1;
}
weston_log("Spawned Xwayland server, pid %d\n", wxs->pid);
wl_event_source_remove(wxs->abstract_source);
wl_event_source_remove(wxs->unix_source);
return 1;
}
static void
weston_xserver_shutdown(struct weston_xserver *wxs)
{
char path[256];
snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display);
unlink(path);
snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display);
unlink(path);
if (wxs->pid == 0) {
wl_event_source_remove(wxs->abstract_source);
wl_event_source_remove(wxs->unix_source);
}
close(wxs->abstract_fd);
close(wxs->unix_fd);
if (wxs->wm) {
weston_wm_destroy(wxs->wm);
wxs->wm = NULL;
}
wxs->loop = NULL;
}
static int
bind_to_abstract_socket(int display)
{
struct sockaddr_un addr;
socklen_t size, name_size;
int fd;
fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0)
return -1;
addr.sun_family = AF_LOCAL;
name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
"%c/tmp/.X11-unix/X%d", 0, display);
size = offsetof(struct sockaddr_un, sun_path) + name_size;
if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1);
close(fd);
return -1;
}
if (listen(fd, 1) < 0) {
close(fd);
return -1;
}
return fd;
}
static int
bind_to_unix_socket(int display)
{
struct sockaddr_un addr;
socklen_t size, name_size;
int fd;
fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0)
return -1;
addr.sun_family = AF_LOCAL;
name_size = snprintf(addr.sun_path, sizeof addr.sun_path,
"/tmp/.X11-unix/X%d", display) + 1;
size = offsetof(struct sockaddr_un, sun_path) + name_size;
unlink(addr.sun_path);
if (bind(fd, (struct sockaddr *) &addr, size) < 0) {
weston_log("failed to bind to %s: %m\n", addr.sun_path);
close(fd);
return -1;
}
if (listen(fd, 1) < 0) {
unlink(addr.sun_path);
close(fd);
return -1;
}
return fd;
}
static int
create_lockfile(int display, char *lockfile, size_t lsize)
{
char pid[16], *end;
int fd, size;
pid_t other;
snprintf(lockfile, lsize, "/tmp/.X%d-lock", display);
fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
if (fd < 0 && errno == EEXIST) {
fd = open(lockfile, O_CLOEXEC | O_RDONLY);
if (fd < 0 || read(fd, pid, 11) != 11) {
weston_log("can't read lock file %s: %s\n",
lockfile, strerror(errno));
if (fd >= 0)
close (fd);
errno = EEXIST;
return -1;
}
other = strtol(pid, &end, 10);
if (end != pid + 10) {
weston_log("can't parse lock file %s\n",
lockfile);
close(fd);
errno = EEXIST;
return -1;
}
if (kill(other, 0) < 0 && errno == ESRCH) {
/* stale lock file; unlink and try again */
weston_log("unlinking stale lock file %s\n", lockfile);
close(fd);
if (unlink(lockfile))
/* If we fail to unlink, return EEXIST
so we try the next display number.*/
errno = EEXIST;
else
errno = EAGAIN;
return -1;
}
close(fd);
errno = EEXIST;
return -1;
} else if (fd < 0) {
weston_log("failed to create lock file %s: %s\n",
lockfile, strerror(errno));
return -1;
}
/* Subtle detail: we use the pid of the wayland
* compositor, not the xserver in the lock file. */
size = snprintf(pid, sizeof pid, "%10d\n", getpid());
if (write(fd, pid, size) != size) {
unlink(lockfile);
close(fd);
return -1;
}
close(fd);
return 0;
}
static void
weston_xserver_destroy(struct wl_listener *l, void *data)
{
struct weston_xserver *wxs =
container_of(l, struct weston_xserver, destroy_listener);
if (!wxs)
return;
if (wxs->loop)
weston_xserver_shutdown(wxs);
free(wxs);
}
static struct weston_xwayland *
weston_xwayland_get(struct weston_compositor *compositor)
{
struct wl_listener *listener;
struct weston_xserver *wxs;
listener = wl_signal_get(&compositor->destroy_signal,
weston_xserver_destroy);
if (!listener)
return NULL;
wxs = wl_container_of(listener, wxs, destroy_listener);
return (struct weston_xwayland *)wxs;
}
static int
weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data,
weston_xwayland_spawn_xserver_func_t spawn_func)
{
struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
char lockfile[256], display_name[8];
wxs->user_data = user_data;
wxs->spawn_func = spawn_func;
retry:
if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) {
if (errno == EAGAIN) {
goto retry;
} else if (errno == EEXIST) {
wxs->display++;
goto retry;
} else {
free(wxs);
return -1;
}
}
wxs->abstract_fd = bind_to_abstract_socket(wxs->display);
if (wxs->abstract_fd < 0 && errno == EADDRINUSE) {
wxs->display++;
unlink(lockfile);
goto retry;
}
wxs->unix_fd = bind_to_unix_socket(wxs->display);
if (wxs->unix_fd < 0) {
unlink(lockfile);
close(wxs->abstract_fd);
free(wxs);
return -1;
}
snprintf(display_name, sizeof display_name, ":%d", wxs->display);
weston_log("xserver listening on display %s\n", display_name);
setenv("DISPLAY", display_name, 1);
wxs->loop = wl_display_get_event_loop(wxs->wl_display);
wxs->abstract_source =
wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
WL_EVENT_READABLE,
weston_xserver_handle_event, wxs);
wxs->unix_source =
wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
WL_EVENT_READABLE,
weston_xserver_handle_event, wxs);
return 0;
}
static void
weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland,
struct wl_client *client, int wm_fd)
{
struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
wxs->wm = weston_wm_create(wxs, wm_fd);
wxs->client = client;
}
static void
weston_xwayland_xserver_exited(struct weston_xwayland *xwayland,
int exit_status)
{
struct weston_xserver *wxs = (struct weston_xserver *)xwayland;
wxs->pid = 0;
wxs->client = NULL;
wxs->abstract_source =
wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd,
WL_EVENT_READABLE,
weston_xserver_handle_event, wxs);
wxs->unix_source =
wl_event_loop_add_fd(wxs->loop, wxs->unix_fd,
WL_EVENT_READABLE,
weston_xserver_handle_event, wxs);
if (wxs->wm) {
weston_log("xserver exited, code %d\n", exit_status);
weston_wm_destroy(wxs->wm);
wxs->wm = NULL;
} else {
/* If the X server crashes before it binds to the
* xserver interface, shut down and don't try
* again. */
weston_log("xserver crashing too fast: %d\n", exit_status);
weston_xserver_shutdown(wxs);
}
}
const struct weston_xwayland_api api = {
weston_xwayland_get,
weston_xwayland_listen,
weston_xwayland_xserver_loaded,
weston_xwayland_xserver_exited,
};
WL_EXPORT int
module_init(struct weston_compositor *compositor,
int *argc, char *argv[])
{
struct wl_display *display = compositor->wl_display;
struct weston_xserver *wxs;
int ret;
wxs = zalloc(sizeof *wxs);
if (wxs == NULL)
return -1;
wxs->wl_display = display;
wxs->compositor = compositor;
ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME,
&api, sizeof(api));
if (ret < 0) {
weston_log("Failed to register the xwayland module API.\n");
free(wxs);
return -1;
}
wxs->destroy_listener.notify = weston_xserver_destroy;
wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener);
return 0;
}
|