2016-06-29 12:54:27 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Intel Corporation
|
|
|
|
* Copyright © 2016 Giulio Camuffo
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-07-07 01:14:20 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-06-29 12:54:27 +03:00
|
|
|
#include <signal.h>
|
2019-04-27 00:57:31 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2016-06-29 12:54:27 +03:00
|
|
|
#include <sys/socket.h>
|
2022-07-07 14:30:17 +03:00
|
|
|
#include <fcntl.h>
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2019-03-28 17:28:47 +03:00
|
|
|
#include <libweston/libweston.h>
|
2016-06-29 12:54:27 +03:00
|
|
|
#include "compositor/weston.h"
|
2019-04-04 15:52:47 +03:00
|
|
|
#include <libweston/xwayland-api.h>
|
2016-06-29 12:54:27 +03:00
|
|
|
#include "shared/helpers.h"
|
2022-04-11 13:52:27 +03:00
|
|
|
#include "shared/os-compatibility.h"
|
2022-07-12 15:15:23 +03:00
|
|
|
#include "shared/process-util.h"
|
2022-07-07 13:42:39 +03:00
|
|
|
#include "shared/string-helpers.h"
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2022-03-23 23:48:05 +03:00
|
|
|
#ifdef HAVE_XWAYLAND_LISTENFD
|
|
|
|
# define LISTEN_STR "-listenfd"
|
|
|
|
#else
|
|
|
|
# define LISTEN_STR "-listen"
|
|
|
|
#endif
|
|
|
|
|
2016-06-29 12:54:27 +03:00
|
|
|
struct wet_xwayland {
|
|
|
|
struct weston_compositor *compositor;
|
|
|
|
const struct weston_xwayland_api *api;
|
|
|
|
struct weston_xwayland *xwayland;
|
2022-04-11 13:52:27 +03:00
|
|
|
struct wl_event_source *display_fd_source;
|
2016-06-29 12:54:27 +03:00
|
|
|
int wm_fd;
|
2023-05-01 17:19:40 +03:00
|
|
|
struct wet_process *process;
|
2016-06-29 12:54:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2022-04-11 13:52:27 +03:00
|
|
|
handle_display_fd(int fd, uint32_t mask, void *data)
|
2016-06-29 12:54:27 +03:00
|
|
|
{
|
|
|
|
struct wet_xwayland *wxw = data;
|
2022-04-11 13:52:27 +03:00
|
|
|
char buf[64];
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
/* xwayland exited before being ready, don't finish initialization,
|
|
|
|
* the process watcher will cleanup */
|
|
|
|
if (!(mask & WL_EVENT_READABLE))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Xwayland writes to the pipe twice, so if we close it too early
|
|
|
|
* it's possible the second write will fail and Xwayland shuts down.
|
|
|
|
* Make sure we read until end of line marker to avoid this. */
|
|
|
|
n = read(fd, buf, sizeof buf);
|
|
|
|
if (n < 0 && errno != EAGAIN) {
|
|
|
|
weston_log("read from Xwayland display_fd failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Returning 1 here means recheck and call us again if required. */
|
|
|
|
if (n <= 0 || (n > 0 && buf[n - 1] != '\n'))
|
|
|
|
return 1;
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2023-05-01 17:54:52 +03:00
|
|
|
wxw->api->xserver_loaded(wxw->xwayland, wxw->wm_fd);
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2022-04-11 13:52:27 +03:00
|
|
|
out:
|
|
|
|
wl_event_source_remove(wxw->display_fd_source);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
|
|
|
|
2022-07-21 19:32:51 +03:00
|
|
|
static void
|
2023-05-01 17:14:51 +03:00
|
|
|
xserver_cleanup(struct wet_process *process, int status, void *data)
|
2022-07-21 19:32:51 +03:00
|
|
|
{
|
2023-05-01 17:14:51 +03:00
|
|
|
struct wet_xwayland *wxw = data;
|
2022-07-21 19:32:51 +03:00
|
|
|
|
2023-05-01 17:19:40 +03:00
|
|
|
/* We only have one Xwayland process active, so make sure it's the
|
|
|
|
* right one */
|
|
|
|
assert(process == wxw->process);
|
|
|
|
|
2023-05-01 19:29:01 +03:00
|
|
|
wxw->api->xserver_exited(wxw->xwayland);
|
2023-05-01 17:19:40 +03:00
|
|
|
wxw->process = NULL;
|
2022-07-21 19:32:51 +03:00
|
|
|
}
|
|
|
|
|
2023-05-01 17:54:52 +03:00
|
|
|
static struct wl_client *
|
2016-06-29 12:54:27 +03:00
|
|
|
spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
|
|
|
|
{
|
|
|
|
struct wet_xwayland *wxw = user_data;
|
2023-04-18 15:02:38 +03:00
|
|
|
struct fdstr wayland_socket = FDSTR_INIT;
|
|
|
|
struct fdstr x11_abstract_socket = FDSTR_INIT;
|
|
|
|
struct fdstr x11_unix_socket = FDSTR_INIT;
|
|
|
|
struct fdstr x11_wm_socket = FDSTR_INIT;
|
|
|
|
struct fdstr display_pipe = FDSTR_INIT;
|
2016-06-29 12:54:27 +03:00
|
|
|
char *xserver = NULL;
|
|
|
|
struct weston_config *config = wet_get_config(wxw->compositor);
|
|
|
|
struct weston_config_section *section;
|
2023-05-01 17:54:52 +03:00
|
|
|
struct wl_client *client;
|
2022-04-11 13:52:27 +03:00
|
|
|
struct wl_event_loop *loop;
|
2022-07-07 15:41:48 +03:00
|
|
|
struct custom_env child_env;
|
2022-07-21 19:25:49 +03:00
|
|
|
int no_cloexec_fds[5];
|
|
|
|
size_t num_no_cloexec_fds = 0;
|
2022-09-21 11:00:21 +03:00
|
|
|
size_t written __attribute__ ((unused));
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2022-07-12 19:18:40 +03:00
|
|
|
if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, wayland_socket.fds) < 0) {
|
2016-06-29 12:54:27 +03:00
|
|
|
weston_log("wl connection socketpair failed\n");
|
2022-07-21 19:32:51 +03:00
|
|
|
goto err;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
2022-07-07 14:18:54 +03:00
|
|
|
fdstr_update_str1(&wayland_socket);
|
2022-07-21 19:25:49 +03:00
|
|
|
no_cloexec_fds[num_no_cloexec_fds++] = wayland_socket.fds[1];
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2022-07-12 19:18:40 +03:00
|
|
|
if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, x11_wm_socket.fds) < 0) {
|
2016-06-29 12:54:27 +03:00
|
|
|
weston_log("X wm connection socketpair failed\n");
|
2022-07-21 19:32:51 +03:00
|
|
|
goto err;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
2022-07-07 14:18:54 +03:00
|
|
|
fdstr_update_str1(&x11_wm_socket);
|
2022-07-21 19:25:49 +03:00
|
|
|
no_cloexec_fds[num_no_cloexec_fds++] = x11_wm_socket.fds[1];
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2022-07-07 14:30:17 +03:00
|
|
|
if (pipe2(display_pipe.fds, O_CLOEXEC) < 0) {
|
2022-04-11 13:52:27 +03:00
|
|
|
weston_log("pipe creation for displayfd failed\n");
|
2022-07-21 19:32:51 +03:00
|
|
|
goto err;
|
2022-04-11 13:52:27 +03:00
|
|
|
}
|
2022-07-07 14:18:54 +03:00
|
|
|
fdstr_update_str1(&display_pipe);
|
2022-07-21 19:25:49 +03:00
|
|
|
no_cloexec_fds[num_no_cloexec_fds++] = display_pipe.fds[1];
|
2022-07-07 14:18:54 +03:00
|
|
|
|
|
|
|
fdstr_set_fd1(&x11_abstract_socket, abstract_fd);
|
2022-07-21 19:25:49 +03:00
|
|
|
no_cloexec_fds[num_no_cloexec_fds++] = abstract_fd;
|
|
|
|
|
2022-07-07 14:18:54 +03:00
|
|
|
fdstr_set_fd1(&x11_unix_socket, unix_fd);
|
2022-07-21 19:25:49 +03:00
|
|
|
no_cloexec_fds[num_no_cloexec_fds++] = unix_fd;
|
|
|
|
|
|
|
|
assert(num_no_cloexec_fds <= ARRAY_LENGTH(no_cloexec_fds));
|
2022-07-07 14:18:54 +03:00
|
|
|
|
2022-07-07 11:15:42 +03:00
|
|
|
section = weston_config_get_section(config, "xwayland", NULL, NULL);
|
|
|
|
weston_config_section_get_string(section, "path",
|
|
|
|
&xserver, XSERVER_PATH);
|
2022-07-07 15:41:48 +03:00
|
|
|
custom_env_init_from_environ(&child_env);
|
2022-07-12 16:40:56 +03:00
|
|
|
custom_env_set_env_var(&child_env, "WAYLAND_SOCKET", wayland_socket.str1);
|
2022-07-07 11:15:42 +03:00
|
|
|
|
2022-07-12 18:45:57 +03:00
|
|
|
custom_env_add_arg(&child_env, xserver);
|
|
|
|
custom_env_add_arg(&child_env, display);
|
|
|
|
custom_env_add_arg(&child_env, "-rootless");
|
|
|
|
custom_env_add_arg(&child_env, LISTEN_STR);
|
|
|
|
custom_env_add_arg(&child_env, x11_abstract_socket.str1);
|
|
|
|
custom_env_add_arg(&child_env, LISTEN_STR);
|
|
|
|
custom_env_add_arg(&child_env, x11_unix_socket.str1);
|
|
|
|
custom_env_add_arg(&child_env, "-displayfd");
|
|
|
|
custom_env_add_arg(&child_env, display_pipe.str1);
|
|
|
|
custom_env_add_arg(&child_env, "-wm");
|
|
|
|
custom_env_add_arg(&child_env, x11_wm_socket.str1);
|
|
|
|
custom_env_add_arg(&child_env, "-terminate");
|
|
|
|
|
2023-05-01 17:31:56 +03:00
|
|
|
wxw->process =
|
|
|
|
wet_client_launch(wxw->compositor, &child_env,
|
|
|
|
no_cloexec_fds, num_no_cloexec_fds,
|
|
|
|
xserver_cleanup, wxw);
|
|
|
|
if (!wxw->process) {
|
2022-07-21 19:32:51 +03:00
|
|
|
weston_log("Couldn't start Xwayland\n");
|
|
|
|
goto err;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
|
|
|
|
2023-05-01 17:54:52 +03:00
|
|
|
client = wl_client_create(wxw->compositor->wl_display,
|
|
|
|
wayland_socket.fds[0]);
|
|
|
|
if (!client) {
|
2022-07-21 19:32:51 +03:00
|
|
|
weston_log("Couldn't create client for Xwayland\n");
|
2023-05-01 17:19:40 +03:00
|
|
|
goto err_proc;
|
2022-07-21 19:32:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wxw->wm_fd = x11_wm_socket.fds[0];
|
|
|
|
|
|
|
|
/* Now we can no longer fail, close the child end of our sockets */
|
|
|
|
close(wayland_socket.fds[1]);
|
|
|
|
close(x11_wm_socket.fds[1]);
|
|
|
|
close(display_pipe.fds[1]);
|
|
|
|
|
|
|
|
/* During initialization the X server will round trip
|
|
|
|
* and block on the wayland compositor, so avoid making
|
|
|
|
* blocking requests (like xcb_connect_to_fd) until
|
|
|
|
* it's done with that. */
|
|
|
|
loop = wl_display_get_event_loop(wxw->compositor->wl_display);
|
|
|
|
wxw->display_fd_source =
|
|
|
|
wl_event_loop_add_fd(loop, display_pipe.fds[0],
|
|
|
|
WL_EVENT_READABLE,
|
|
|
|
handle_display_fd, wxw);
|
|
|
|
|
2022-07-07 11:15:42 +03:00
|
|
|
free(xserver);
|
|
|
|
|
2023-05-01 17:54:52 +03:00
|
|
|
return client;
|
2023-05-01 17:19:40 +03:00
|
|
|
|
2022-07-21 19:15:18 +03:00
|
|
|
|
2023-05-01 17:19:40 +03:00
|
|
|
err_proc:
|
|
|
|
wl_list_remove(&wxw->process->link);
|
2022-07-21 19:32:51 +03:00
|
|
|
err:
|
|
|
|
free(xserver);
|
|
|
|
fdstr_close_all(&display_pipe);
|
2022-07-21 19:15:18 +03:00
|
|
|
fdstr_close_all(&x11_wm_socket);
|
|
|
|
fdstr_close_all(&wayland_socket);
|
2023-05-01 17:19:40 +03:00
|
|
|
free(wxw->process);
|
2023-05-01 17:54:52 +03:00
|
|
|
return NULL;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
|
|
|
|
2023-05-01 21:20:32 +03:00
|
|
|
void
|
|
|
|
wet_xwayland_destroy(struct weston_compositor *comp, void *data)
|
2022-06-24 18:10:48 +03:00
|
|
|
{
|
2023-05-01 21:20:32 +03:00
|
|
|
struct wet_xwayland *wxw = data;
|
2022-06-24 18:10:48 +03:00
|
|
|
|
2023-05-01 21:20:32 +03:00
|
|
|
/* Calling this will call the process cleanup, in turn cleaning up the
|
|
|
|
* client and the core Xwayland state */
|
|
|
|
if (wxw->process)
|
|
|
|
wet_process_destroy(wxw->process, 0, true);
|
2022-06-24 18:10:48 +03:00
|
|
|
|
|
|
|
free(wxw);
|
|
|
|
}
|
|
|
|
|
2023-05-01 21:15:52 +03:00
|
|
|
void *
|
2016-06-29 12:54:27 +03:00
|
|
|
wet_load_xwayland(struct weston_compositor *comp)
|
|
|
|
{
|
|
|
|
const struct weston_xwayland_api *api;
|
|
|
|
struct weston_xwayland *xwayland;
|
|
|
|
struct wet_xwayland *wxw;
|
|
|
|
|
|
|
|
if (weston_compositor_load_xwayland(comp) < 0)
|
2023-05-01 21:15:52 +03:00
|
|
|
return NULL;
|
2016-06-29 12:54:27 +03:00
|
|
|
|
|
|
|
api = weston_xwayland_get_api(comp);
|
|
|
|
if (!api) {
|
|
|
|
weston_log("Failed to get the xwayland module API.\n");
|
2023-05-01 21:15:52 +03:00
|
|
|
return NULL;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
xwayland = api->get(comp);
|
|
|
|
if (!xwayland) {
|
|
|
|
weston_log("Failed to get the xwayland object.\n");
|
2023-05-01 21:15:52 +03:00
|
|
|
return NULL;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wxw = zalloc(sizeof *wxw);
|
|
|
|
if (!wxw)
|
2023-05-01 21:15:52 +03:00
|
|
|
return NULL;
|
2016-06-29 12:54:27 +03:00
|
|
|
|
|
|
|
wxw->compositor = comp;
|
|
|
|
wxw->api = api;
|
|
|
|
wxw->xwayland = xwayland;
|
|
|
|
if (api->listen(xwayland, wxw, spawn_xserver) < 0)
|
2023-05-01 21:15:52 +03:00
|
|
|
return NULL;
|
2016-06-29 12:54:27 +03:00
|
|
|
|
2023-05-01 21:15:52 +03:00
|
|
|
return wxw;
|
2016-06-29 12:54:27 +03:00
|
|
|
}
|