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