Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 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 | |
Bryce Harrington | 9335ca5 | 2016-07-06 15:14:20 -0700 | [diff] [blame] | 27 | #include "config.h" |
| 28 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 30 | #include <string.h> |
| 31 | #include <errno.h> |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 34 | #include <libweston/libweston.h> |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 35 | #include "compositor/weston.h" |
Pekka Paalanen | eebb7dc | 2019-04-04 15:52:47 +0300 | [diff] [blame] | 36 | #include <libweston/xwayland-api.h> |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 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 |
| 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 |
| 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; |
Alexandros Frantzis | 7e2d4be | 2018-10-12 12:52:42 +0300 | [diff] [blame] | 68 | char s[12], abstract_fd_str[12], unix_fd_str[12], wm_fd_str[12]; |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 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", |
Vlad Zahorodnii | 022ea43 | 2021-02-09 10:43:22 +0200 | [diff] [blame] | 126 | #ifdef HAVE_XWAYLAND_LISTENFD |
| 127 | "-listenfd", abstract_fd_str, |
| 128 | "-listenfd", unix_fd_str, |
| 129 | #else |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 130 | "-listen", abstract_fd_str, |
| 131 | "-listen", unix_fd_str, |
Vlad Zahorodnii | 022ea43 | 2021-02-09 10:43:22 +0200 | [diff] [blame] | 132 | #endif |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 133 | "-wm", wm_fd_str, |
| 134 | "-terminate", |
| 135 | NULL) < 0) |
| 136 | weston_log("exec of '%s %s -rootless " |
Vlad Zahorodnii | 022ea43 | 2021-02-09 10:43:22 +0200 | [diff] [blame] | 137 | #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 Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 143 | xserver, display, |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 144 | abstract_fd_str, unix_fd_str, wm_fd_str, |
| 145 | strerror(errno)); |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 146 | 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; |
Alvarito050506 | 1f57a1f | 2021-06-29 14:33:04 -0300 | [diff] [blame] | 157 | wet_watch_process(wxw->compositor, &wxw->process); |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 158 | break; |
| 159 | |
| 160 | case -1: |
Bryce Harrington | cff0b1d | 2016-07-06 15:18:46 -0700 | [diff] [blame] | 161 | weston_log("Failed to fork to spawn xserver process\n"); |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 162 | break; |
| 163 | } |
| 164 | |
| 165 | return pid; |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | xserver_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 Peyrot | eff793a | 2021-07-31 17:25:41 +0200 | [diff] [blame] | 178 | handle_sigusr1, wxw); |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 179 | wxw->client = NULL; |
| 180 | } |
| 181 | |
| 182 | int |
| 183 | wet_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 | } |