This patch follows a similar approach taken to detach the backends from weston. But instead of passing a configuration struct when loading the plugin, we use the plugin API registry to register an API, and to get it in the compositor side. This API allows to spawn the Xwayland process in the compositor side, and to deal with signal handling. A new function is added in compositor.c to load and init the xwayland.so plugin. Also make sure to re-arm the SIGUSR1 when the X server quits. Signed-off-by: Giulio Camuffo <giuliocamuffo@gmail.com> [Pekka: moved xwayland/weston-xwayland.c -> compositor/xwayland.c] Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>dev
parent
827b5d225d
commit
9c764df043
@ -0,0 +1,207 @@ |
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
#include <signal.h> |
||||
#include <sys/socket.h> |
||||
|
||||
#include "compositor.h" |
||||
#include "compositor/weston.h" |
||||
#include "xwayland/xwayland-api.h" |
||||
#include "shared/helpers.h" |
||||
|
||||
struct wet_xwayland { |
||||
struct weston_compositor *compositor; |
||||
const struct weston_xwayland_api *api; |
||||
struct weston_xwayland *xwayland; |
||||
struct wl_event_source *sigusr1_source; |
||||
struct wl_client *client; |
||||
int wm_fd; |
||||
struct weston_process process; |
||||
}; |
||||
|
||||
static int |
||||
handle_sigusr1(int signal_number, void *data) |
||||
{ |
||||
struct wet_xwayland *wxw = data; |
||||
|
||||
/* We'd be safer if we actually had the struct
|
||||
* signalfd_siginfo from the signalfd data and could verify |
||||
* this came from Xwayland.*/ |
||||
wxw->api->xserver_loaded(wxw->xwayland, wxw->client, wxw->wm_fd); |
||||
wl_event_source_remove(wxw->sigusr1_source); |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
static pid_t |
||||
spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd) |
||||
{ |
||||
struct wet_xwayland *wxw = user_data; |
||||
pid_t pid; |
||||
char s[8], abstract_fd_str[8], unix_fd_str[8], wm_fd_str[8]; |
||||
int sv[2], wm[2], fd; |
||||
char *xserver = NULL; |
||||
struct weston_config *config = wet_get_config(wxw->compositor); |
||||
struct weston_config_section *section; |
||||
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
||||
weston_log("wl connection socketpair failed\n"); |
||||
return 1; |
||||
} |
||||
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) { |
||||
weston_log("X wm connection socketpair failed\n"); |
||||
return 1; |
||||
} |
||||
|
||||
pid = fork(); |
||||
switch (pid) { |
||||
case 0: |
||||
/* SOCK_CLOEXEC closes both ends, so we need to unset
|
||||
* the flag on the client fd. */ |
||||
fd = dup(sv[1]); |
||||
if (fd < 0) |
||||
goto fail; |
||||
snprintf(s, sizeof s, "%d", fd); |
||||
setenv("WAYLAND_SOCKET", s, 1); |
||||
|
||||
fd = dup(abstract_fd); |
||||
if (fd < 0) |
||||
goto fail; |
||||
snprintf(abstract_fd_str, sizeof abstract_fd_str, "%d", fd); |
||||
fd = dup(unix_fd); |
||||
if (fd < 0) |
||||
goto fail; |
||||
snprintf(unix_fd_str, sizeof unix_fd_str, "%d", fd); |
||||
fd = dup(wm[1]); |
||||
if (fd < 0) |
||||
goto fail; |
||||
snprintf(wm_fd_str, sizeof wm_fd_str, "%d", fd); |
||||
|
||||
section = weston_config_get_section(config, |
||||
"xwayland", NULL, NULL); |
||||
weston_config_section_get_string(section, "path", |
||||
&xserver, XSERVER_PATH); |
||||
|
||||
/* Ignore SIGUSR1 in the child, which will make the X
|
||||
* server send SIGUSR1 to the parent (weston) when |
||||
* it's done with initialization. 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. */ |
||||
signal(SIGUSR1, SIG_IGN); |
||||
|
||||
if (execl(xserver, |
||||
xserver, |
||||
display, |
||||
"-rootless", |
||||
"-listen", abstract_fd_str, |
||||
"-listen", unix_fd_str, |
||||
"-wm", wm_fd_str, |
||||
"-terminate", |
||||
NULL) < 0) |
||||
weston_log("exec of '%s %s -rootless " |
||||
"-listen %s -listen %s -wm %s " |
||||
"-terminate' failed: %m\n", |
||||
xserver, display, |
||||
abstract_fd_str, unix_fd_str, wm_fd_str); |
||||
fail: |
||||
_exit(EXIT_FAILURE); |
||||
|
||||
default: |
||||
close(sv[1]); |
||||
wxw->client = wl_client_create(wxw->compositor->wl_display, sv[0]); |
||||
|
||||
close(wm[1]); |
||||
wxw->wm_fd = wm[0]; |
||||
|
||||
wxw->process.pid = pid; |
||||
weston_watch_process(&wxw->process); |
||||
break; |
||||
|
||||
case -1: |
||||
weston_log( "failed to fork\n"); |
||||
break; |
||||
} |
||||
|
||||
return pid; |
||||
} |
||||
|
||||
static void |
||||
xserver_cleanup(struct weston_process *process, int status) |
||||
{ |
||||
struct wet_xwayland *wxw = |
||||
container_of(process, struct wet_xwayland, process); |
||||
struct wl_event_loop *loop = |
||||
wl_display_get_event_loop(wxw->compositor->wl_display); |
||||
|
||||
wxw->api->xserver_exited(wxw->xwayland, status); |
||||
wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, |
||||
handle_sigusr1, wxw); |
||||
wxw->client = NULL; |
||||
} |
||||
|
||||
int |
||||
wet_load_xwayland(struct weston_compositor *comp) |
||||
{ |
||||
const struct weston_xwayland_api *api; |
||||
struct weston_xwayland *xwayland; |
||||
struct wet_xwayland *wxw; |
||||
struct wl_event_loop *loop; |
||||
|
||||
if (weston_compositor_load_xwayland(comp) < 0) |
||||
return -1; |
||||
|
||||
api = weston_xwayland_get_api(comp); |
||||
if (!api) { |
||||
weston_log("Failed to get the xwayland module API.\n"); |
||||
return -1; |
||||
} |
||||
|
||||
xwayland = api->get(comp); |
||||
if (!xwayland) { |
||||
weston_log("Failed to get the xwayland object.\n"); |
||||
return -1; |
||||
} |
||||
|
||||
wxw = zalloc(sizeof *wxw); |
||||
if (!wxw) |
||||
return -1; |
||||
|
||||
wxw->compositor = comp; |
||||
wxw->api = api; |
||||
wxw->xwayland = xwayland; |
||||
wxw->process.cleanup = xserver_cleanup; |
||||
if (api->listen(xwayland, wxw, spawn_xserver) < 0) |
||||
return -1; |
||||
|
||||
loop = wl_display_get_event_loop(comp->wl_display); |
||||
wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, |
||||
handle_sigusr1, wxw); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,134 @@ |
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
#ifndef XWAYLAND_API_H |
||||
#define XWAYLAND_API_H |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
#include <unistd.h> |
||||
|
||||
#include "plugin-registry.h" |
||||
|
||||
struct weston_compositor; |
||||
struct weston_xwayland; |
||||
|
||||
#define WESTON_XWAYLAND_API_NAME "weston_xwayland_v1" |
||||
|
||||
typedef pid_t |
||||
(*weston_xwayland_spawn_xserver_func_t)( |
||||
void *user_data, const char *display, int abstract_fd, int unix_fd); |
||||
|
||||
/** The libweston Xwayland API
|
||||
* |
||||
* This API allows to control the Xwayland libweston module. |
||||
* The module must be loaded at runtime with \a weston_compositor_load_xwayland, |
||||
* after which the API can be retrieved by using \a weston_xwayland_get_api. |
||||
*/ |
||||
struct weston_xwayland_api { |
||||
/** Retrieve the Xwayland context object.
|
||||
* |
||||
* Note that this function does not create a new object, but returns |
||||
* always the same object per compositor instance. |
||||
* This function cannot fail, if this API object is valid. |
||||
* |
||||
* \param compositor The compositor instance. |
||||
*/ |
||||
struct weston_xwayland * |
||||
(*get)(struct weston_compositor *compositor); |
||||
|
||||
/** Listen for X connections.
|
||||
* |
||||
* This function tells the Xwayland module to start create a X socket |
||||
* and to listen for client connections. When one such connection is |
||||
* detected the given \a spawn_func callback will be called to start |
||||
* the Xwayland process. |
||||
* |
||||
* \param xwayland The Xwayland context object. |
||||
* \param user_data The user data pointer to be passed to \a spawn_func. |
||||
* \param spawn_func The callback function called to start the Xwayland |
||||
* server process. |
||||
* |
||||
* \return 0 on success, a negative number otherwise. |
||||
*/ |
||||
int |
||||
(*listen)(struct weston_xwayland *xwayland, void *user_data, |
||||
weston_xwayland_spawn_xserver_func_t spawn_func); |
||||
|
||||
/** Notify the Xwayland module the Xwayland server is loaded.
|
||||
* |
||||
* After the Xwayland server process has been spawned it will notify |
||||
* the parent that is has finished the initialization by sending a |
||||
* SIGUSR1 signal. |
||||
* The caller should listen for that signal and call this function |
||||
* when it is received. |
||||
* |
||||
* \param xwayland The Xwayland context object. |
||||
* \param client The wl_client object representing the connection of |
||||
* the Xwayland server process. |
||||
* \param wm_fd The file descriptor for the wm. |
||||
*/ |
||||
void |
||||
(*xserver_loaded)(struct weston_xwayland *xwayland, |
||||
struct wl_client *client, int wm_fd); |
||||
|
||||
/** Notify the Xwayland module the Xwayland server has exited.
|
||||
* |
||||
* Whenever the Xwayland server process quits this function should be |
||||
* called. |
||||
* The Xwayland module will keep listening for X connections on the |
||||
* socket, and may call the spawn function again. |
||||
* |
||||
* \param xwayland The Xwayland context object. |
||||
* \param exit_status The exit status of the Xwayland server process. |
||||
*/ |
||||
void |
||||
(*xserver_exited)(struct weston_xwayland *xwayland, int exit_status); |
||||
}; |
||||
|
||||
/** Retrieve the API object for the libweston Xwayland module.
|
||||
* |
||||
* The module must have been previously loaded by calling |
||||
* \a weston_compositor_load_xwayland. |
||||
* |
||||
* \param compositor The compositor instance. |
||||
*/ |
||||
static inline const struct weston_xwayland_api * |
||||
weston_xwayland_get_api(struct weston_compositor *compositor) |
||||
{ |
||||
const void *api; |
||||
api = weston_plugin_api_get(compositor, WESTON_XWAYLAND_API_NAME, |
||||
sizeof(struct weston_xwayland_api)); |
||||
/* The cast is necessary to use this function in C++ code */ |
||||
return (const struct weston_xwayland_api *)api; |
||||
} |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif |
Loading…
Reference in new issue