Jasper St. Pierre | 72dea06 | 2015-09-23 10:46:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Benjamin Franzke |
| 3 | * Copyright © 2013 Intel Corporation |
| 4 | * |
Yong Bakos | 5336153 | 2017-01-23 06:17:44 -0800 | [diff] [blame] | 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: |
Jasper St. Pierre | 72dea06 | 2015-09-23 10:46:47 -0700 | [diff] [blame] | 12 | * |
Yong Bakos | 5336153 | 2017-01-23 06:17:44 -0800 | [diff] [blame] | 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. |
Jasper St. Pierre | 72dea06 | 2015-09-23 10:46:47 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
| 29 | #include "compositor.h" |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <unistd.h> |
| 34 | #include <signal.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <linux/vt.h> |
| 38 | #include <linux/kd.h> |
| 39 | #include <linux/major.h> |
| 40 | |
| 41 | #include "launcher-impl.h" |
| 42 | |
| 43 | #define DRM_MAJOR 226 |
| 44 | |
| 45 | #ifndef KDSKBMUTE |
| 46 | #define KDSKBMUTE 0x4B51 |
| 47 | #endif |
| 48 | |
| 49 | #ifdef HAVE_LIBDRM |
| 50 | |
| 51 | #include <xf86drm.h> |
| 52 | |
| 53 | static inline int |
| 54 | is_drm_master(int drm_fd) |
| 55 | { |
| 56 | drm_magic_t magic; |
| 57 | |
| 58 | return drmGetMagic(drm_fd, &magic) == 0 && |
| 59 | drmAuthMagic(drm_fd, magic) == 0; |
| 60 | } |
| 61 | |
| 62 | #else |
| 63 | |
| 64 | static inline int |
| 65 | drmDropMaster(int drm_fd) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static inline int |
| 71 | drmSetMaster(int drm_fd) |
| 72 | { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static inline int |
| 77 | is_drm_master(int drm_fd) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | #endif |
| 83 | |
| 84 | struct launcher_direct { |
| 85 | struct weston_launcher base; |
| 86 | struct weston_compositor *compositor; |
| 87 | int kb_mode, tty, drm_fd; |
| 88 | struct wl_event_source *vt_source; |
| 89 | }; |
| 90 | |
| 91 | static int |
| 92 | vt_handler(int signal_number, void *data) |
| 93 | { |
| 94 | struct launcher_direct *launcher = data; |
| 95 | struct weston_compositor *compositor = launcher->compositor; |
| 96 | |
| 97 | if (compositor->session_active) { |
| 98 | compositor->session_active = 0; |
| 99 | wl_signal_emit(&compositor->session_signal, compositor); |
| 100 | drmDropMaster(launcher->drm_fd); |
| 101 | ioctl(launcher->tty, VT_RELDISP, 1); |
| 102 | } else { |
| 103 | ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ); |
| 104 | drmSetMaster(launcher->drm_fd); |
| 105 | compositor->session_active = 1; |
| 106 | wl_signal_emit(&compositor->session_signal, compositor); |
| 107 | } |
| 108 | |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | setup_tty(struct launcher_direct *launcher, int tty) |
| 114 | { |
| 115 | struct wl_event_loop *loop; |
| 116 | struct vt_mode mode = { 0 }; |
| 117 | struct stat buf; |
| 118 | char tty_device[32] ="<stdin>"; |
| 119 | int ret, kd_mode; |
| 120 | |
| 121 | if (tty == 0) { |
| 122 | launcher->tty = dup(tty); |
| 123 | if (launcher->tty == -1) { |
| 124 | weston_log("couldn't dup stdin: %m\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | } else { |
| 128 | snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty); |
| 129 | launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC); |
| 130 | if (launcher->tty == -1) { |
| 131 | weston_log("couldn't open tty %s: %m\n", tty_device); |
| 132 | return -1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (fstat(launcher->tty, &buf) == -1 || |
| 137 | major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) { |
| 138 | weston_log("%s not a vt\n", tty_device); |
| 139 | weston_log("if running weston from ssh, " |
| 140 | "use --tty to specify a tty\n"); |
| 141 | goto err_close; |
| 142 | } |
| 143 | |
| 144 | ret = ioctl(launcher->tty, KDGETMODE, &kd_mode); |
| 145 | if (ret) { |
| 146 | weston_log("failed to get VT mode: %m\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | if (kd_mode != KD_TEXT) { |
| 150 | weston_log("%s is already in graphics mode, " |
| 151 | "is another display server running?\n", tty_device); |
| 152 | goto err_close; |
| 153 | } |
| 154 | |
| 155 | ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev)); |
| 156 | ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev)); |
| 157 | |
| 158 | if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) { |
| 159 | weston_log("failed to read keyboard mode: %m\n"); |
| 160 | goto err_close; |
| 161 | } |
| 162 | |
| 163 | if (ioctl(launcher->tty, KDSKBMUTE, 1) && |
| 164 | ioctl(launcher->tty, KDSKBMODE, K_OFF)) { |
| 165 | weston_log("failed to set K_OFF keyboard mode: %m\n"); |
| 166 | goto err_close; |
| 167 | } |
| 168 | |
| 169 | ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS); |
| 170 | if (ret) { |
| 171 | weston_log("failed to set KD_GRAPHICS mode on tty: %m\n"); |
| 172 | goto err_close; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * SIGRTMIN is used as global VT-acquire+release signal. Note that |
| 177 | * SIGRT* must be tested on runtime, as their exact values are not |
| 178 | * known at compile-time. POSIX requires 32 of them to be available. |
| 179 | */ |
| 180 | if (SIGRTMIN > SIGRTMAX) { |
| 181 | weston_log("not enough RT signals available: %u-%u\n", |
| 182 | SIGRTMIN, SIGRTMAX); |
| 183 | ret = -EINVAL; |
| 184 | goto err_close; |
| 185 | } |
| 186 | |
| 187 | mode.mode = VT_PROCESS; |
| 188 | mode.relsig = SIGRTMIN; |
| 189 | mode.acqsig = SIGRTMIN; |
| 190 | if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) { |
| 191 | weston_log("failed to take control of vt handling\n"); |
| 192 | goto err_close; |
| 193 | } |
| 194 | |
| 195 | loop = wl_display_get_event_loop(launcher->compositor->wl_display); |
| 196 | launcher->vt_source = |
| 197 | wl_event_loop_add_signal(loop, SIGRTMIN, vt_handler, launcher); |
| 198 | if (!launcher->vt_source) |
| 199 | goto err_close; |
| 200 | |
| 201 | return 0; |
| 202 | |
| 203 | err_close: |
| 204 | close(launcher->tty); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | static int |
| 209 | launcher_direct_open(struct weston_launcher *launcher_base, const char *path, int flags) |
| 210 | { |
| 211 | struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base); |
| 212 | struct stat s; |
| 213 | int fd; |
| 214 | |
| 215 | fd = open(path, flags | O_CLOEXEC); |
| 216 | if (fd == -1) |
| 217 | return -1; |
| 218 | |
| 219 | if (fstat(fd, &s) == -1) { |
| 220 | close(fd); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | if (major(s.st_rdev) == DRM_MAJOR) { |
| 225 | launcher->drm_fd = fd; |
| 226 | if (!is_drm_master(fd)) { |
| 227 | weston_log("drm fd not master\n"); |
| 228 | close(fd); |
| 229 | return -1; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return fd; |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | launcher_direct_close(struct weston_launcher *launcher_base, int fd) |
| 238 | { |
| 239 | close(fd); |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | launcher_direct_restore(struct weston_launcher *launcher_base) |
| 244 | { |
| 245 | struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base); |
| 246 | struct vt_mode mode = { 0 }; |
| 247 | |
| 248 | if (ioctl(launcher->tty, KDSKBMUTE, 0) && |
| 249 | ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode)) |
| 250 | weston_log("failed to restore kb mode: %m\n"); |
| 251 | |
| 252 | if (ioctl(launcher->tty, KDSETMODE, KD_TEXT)) |
| 253 | weston_log("failed to set KD_TEXT mode on tty: %m\n"); |
| 254 | |
| 255 | /* We have to drop master before we switch the VT back in |
| 256 | * VT_AUTO, so we don't risk switching to a VT with another |
| 257 | * display server, that will then fail to set drm master. */ |
| 258 | drmDropMaster(launcher->drm_fd); |
| 259 | |
| 260 | mode.mode = VT_AUTO; |
| 261 | if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) |
| 262 | weston_log("could not reset vt handling\n"); |
| 263 | } |
| 264 | |
| 265 | static int |
| 266 | launcher_direct_activate_vt(struct weston_launcher *launcher_base, int vt) |
| 267 | { |
| 268 | struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base); |
| 269 | return ioctl(launcher->tty, VT_ACTIVATE, vt); |
| 270 | } |
| 271 | |
| 272 | static int |
| 273 | launcher_direct_connect(struct weston_launcher **out, struct weston_compositor *compositor, |
| 274 | int tty, const char *seat_id, bool sync_drm) |
| 275 | { |
| 276 | struct launcher_direct *launcher; |
| 277 | |
| 278 | if (geteuid() != 0) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | launcher = zalloc(sizeof(*launcher)); |
| 282 | if (launcher == NULL) |
| 283 | return -ENOMEM; |
| 284 | |
| 285 | launcher->base.iface = &launcher_direct_iface; |
| 286 | launcher->compositor = compositor; |
| 287 | |
| 288 | if (setup_tty(launcher, tty) == -1) { |
| 289 | free(launcher); |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | * (struct launcher_direct **) out = launcher; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | launcher_direct_destroy(struct weston_launcher *launcher_base) |
| 299 | { |
| 300 | struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base); |
| 301 | |
| 302 | launcher_direct_restore(&launcher->base); |
| 303 | wl_event_source_remove(launcher->vt_source); |
| 304 | |
| 305 | if (launcher->tty >= 0) |
| 306 | close(launcher->tty); |
| 307 | |
| 308 | free(launcher); |
| 309 | } |
| 310 | |
Giulio Camuffo | a32986e | 2016-12-05 14:50:36 +0100 | [diff] [blame] | 311 | static int |
| 312 | launcher_direct_get_vt(struct weston_launcher *base) |
| 313 | { |
| 314 | struct launcher_direct *launcher = wl_container_of(base, launcher, base); |
| 315 | struct stat s; |
| 316 | if (fstat(launcher->tty, &s) < 0) |
| 317 | return -1; |
| 318 | |
| 319 | return minor(s.st_rdev); |
| 320 | } |
| 321 | |
Emil Velikov | 8f7201e | 2017-02-10 20:14:22 +0000 | [diff] [blame^] | 322 | const struct launcher_interface launcher_direct_iface = { |
Jasper St. Pierre | 72dea06 | 2015-09-23 10:46:47 -0700 | [diff] [blame] | 323 | launcher_direct_connect, |
| 324 | launcher_direct_destroy, |
| 325 | launcher_direct_open, |
| 326 | launcher_direct_close, |
| 327 | launcher_direct_activate_vt, |
| 328 | launcher_direct_restore, |
Giulio Camuffo | a32986e | 2016-12-05 14:50:36 +0100 | [diff] [blame] | 329 | launcher_direct_get_vt, |
Jasper St. Pierre | 72dea06 | 2015-09-23 10:46:47 -0700 | [diff] [blame] | 330 | }; |