Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Benjamin Franzke |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 3 | * Copyright © 2013 Intel Corporation |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include <errno.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 31 | #include <signal.h> |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/uio.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 36 | #include <sys/ioctl.h> |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 37 | #include <fcntl.h> |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 38 | #include <unistd.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 39 | #include <linux/vt.h> |
| 40 | #include <linux/kd.h> |
| 41 | #include <linux/major.h> |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 42 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 43 | #include "compositor.h" |
| 44 | #include "launcher-util.h" |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 45 | #include "logind-util.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 46 | #include "weston-launch.h" |
| 47 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 48 | #define DRM_MAJOR 226 |
| 49 | |
| 50 | #ifndef KDSKBMUTE |
| 51 | #define KDSKBMUTE 0x4B51 |
| 52 | #endif |
| 53 | |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 54 | #ifdef HAVE_LIBDRM |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 55 | |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 56 | #include <xf86drm.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 57 | |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 58 | static inline int |
| 59 | is_drm_master(int drm_fd) |
Tomeu Vizoso | 0b12db5 | 2013-10-09 11:30:57 +0200 | [diff] [blame] | 60 | { |
| 61 | drm_magic_t magic; |
Kristian Høgsberg | 891a16d | 2013-10-14 13:59:53 -0700 | [diff] [blame] | 62 | |
| 63 | return drmGetMagic(drm_fd, &magic) == 0 && |
| 64 | drmAuthMagic(drm_fd, magic) == 0; |
Tomeu Vizoso | 0b12db5 | 2013-10-09 11:30:57 +0200 | [diff] [blame] | 65 | } |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 66 | |
Kristian Høgsberg | 57a10e4 | 2013-10-01 15:37:09 -0700 | [diff] [blame] | 67 | #else |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 68 | |
| 69 | static inline int |
| 70 | drmDropMaster(int drm_fd) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static inline int |
| 76 | drmSetMaster(int drm_fd) |
| 77 | { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static inline int |
| 82 | is_drm_master(int drm_fd) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | |
Kristian Høgsberg | 57a10e4 | 2013-10-01 15:37:09 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 89 | |
| 90 | union cmsg_data { unsigned char b[4]; int fd; }; |
| 91 | |
| 92 | struct weston_launcher { |
| 93 | struct weston_compositor *compositor; |
| 94 | struct weston_logind *logind; |
| 95 | struct wl_event_loop *loop; |
| 96 | int fd; |
| 97 | struct wl_event_source *source; |
| 98 | |
| 99 | int kb_mode, tty, drm_fd; |
| 100 | struct wl_event_source *vt_source; |
| 101 | }; |
| 102 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 103 | int |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 104 | weston_launcher_open(struct weston_launcher *launcher, |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 105 | const char *path, int flags) |
| 106 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 107 | int n, fd, ret = -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 108 | struct msghdr msg; |
| 109 | struct cmsghdr *cmsg; |
| 110 | struct iovec iov; |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 111 | union cmsg_data *data; |
| 112 | char control[CMSG_SPACE(sizeof data->fd)]; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 113 | ssize_t len; |
| 114 | struct weston_launcher_open *message; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 115 | struct stat s; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 116 | |
Derek Foreman | 2663c68 | 2015-05-01 11:46:36 -0500 | [diff] [blame^] | 117 | /* We really don't want to be leaking fds to child processes so |
| 118 | * we force this flag here. If someone comes up with a legitimate |
| 119 | * reason to not CLOEXEC they'll need to unset the flag manually. |
| 120 | */ |
| 121 | flags |= O_CLOEXEC; |
| 122 | |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 123 | if (launcher->logind) |
| 124 | return weston_logind_open(launcher->logind, path, flags); |
| 125 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 126 | if (launcher->fd == -1) { |
Derek Foreman | 2663c68 | 2015-05-01 11:46:36 -0500 | [diff] [blame^] | 127 | fd = open(path, flags); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 128 | if (fd == -1) |
| 129 | return -1; |
| 130 | |
| 131 | if (fstat(fd, &s) == -1) { |
| 132 | close(fd); |
| 133 | return -1; |
| 134 | } |
| 135 | |
Kristian Høgsberg | 57a10e4 | 2013-10-01 15:37:09 -0700 | [diff] [blame] | 136 | if (major(s.st_rdev) == DRM_MAJOR) { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 137 | launcher->drm_fd = fd; |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 138 | if (!is_drm_master(fd)) { |
Kristian Høgsberg | 1468e60 | 2013-10-02 10:49:05 -0700 | [diff] [blame] | 139 | weston_log("drm fd not master\n"); |
Kristian Høgsberg | 57a10e4 | 2013-10-01 15:37:09 -0700 | [diff] [blame] | 140 | close(fd); |
| 141 | return -1; |
| 142 | } |
| 143 | } |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 144 | |
| 145 | return fd; |
| 146 | } |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 147 | |
| 148 | n = sizeof(*message) + strlen(path) + 1; |
| 149 | message = malloc(n); |
| 150 | if (!message) |
| 151 | return -1; |
| 152 | |
| 153 | message->header.opcode = WESTON_LAUNCHER_OPEN; |
| 154 | message->flags = flags; |
| 155 | strcpy(message->path, path); |
| 156 | |
| 157 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 158 | len = send(launcher->fd, message, n, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 159 | } while (len < 0 && errno == EINTR); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 160 | free(message); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 161 | |
| 162 | memset(&msg, 0, sizeof msg); |
| 163 | iov.iov_base = &ret; |
| 164 | iov.iov_len = sizeof ret; |
| 165 | msg.msg_iov = &iov; |
| 166 | msg.msg_iovlen = 1; |
| 167 | msg.msg_control = control; |
| 168 | msg.msg_controllen = sizeof control; |
| 169 | |
| 170 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 171 | len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 172 | } while (len < 0 && errno == EINTR); |
| 173 | |
| 174 | if (len != sizeof ret || |
| 175 | ret < 0) |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 176 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 177 | |
| 178 | cmsg = CMSG_FIRSTHDR(&msg); |
| 179 | if (!cmsg || |
| 180 | cmsg->cmsg_level != SOL_SOCKET || |
| 181 | cmsg->cmsg_type != SCM_RIGHTS) { |
| 182 | fprintf(stderr, "invalid control message\n"); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 183 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 186 | data = (union cmsg_data *) CMSG_DATA(cmsg); |
| 187 | if (data->fd == -1) { |
Martin Andersson | 566b464 | 2013-02-13 00:11:12 +0100 | [diff] [blame] | 188 | fprintf(stderr, "missing drm fd in socket request\n"); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 189 | return -1; |
| 190 | } |
| 191 | |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 192 | return data->fd; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 195 | void |
David Herrmann | e461f85 | 2013-10-22 00:28:08 +0200 | [diff] [blame] | 196 | weston_launcher_close(struct weston_launcher *launcher, int fd) |
| 197 | { |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 198 | if (launcher->logind) |
Derek Foreman | 8f5acc2 | 2015-04-30 14:37:03 -0500 | [diff] [blame] | 199 | weston_logind_close(launcher->logind, fd); |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 200 | |
David Herrmann | e461f85 | 2013-10-22 00:28:08 +0200 | [diff] [blame] | 201 | close(fd); |
| 202 | } |
| 203 | |
| 204 | void |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 205 | weston_launcher_restore(struct weston_launcher *launcher) |
| 206 | { |
| 207 | struct vt_mode mode = { 0 }; |
| 208 | |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 209 | if (launcher->logind) |
| 210 | return weston_logind_restore(launcher->logind); |
| 211 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 212 | if (ioctl(launcher->tty, KDSKBMUTE, 0) && |
| 213 | ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode)) |
| 214 | weston_log("failed to restore kb mode: %m\n"); |
| 215 | |
| 216 | if (ioctl(launcher->tty, KDSETMODE, KD_TEXT)) |
| 217 | weston_log("failed to set KD_TEXT mode on tty: %m\n"); |
| 218 | |
Kristian Høgsberg | a28ba55 | 2013-10-30 16:27:16 -0700 | [diff] [blame] | 219 | /* We have to drop master before we switch the VT back in |
| 220 | * VT_AUTO, so we don't risk switching to a VT with another |
| 221 | * display server, that will then fail to set drm master. */ |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 222 | drmDropMaster(launcher->drm_fd); |
Kristian Høgsberg | a28ba55 | 2013-10-30 16:27:16 -0700 | [diff] [blame] | 223 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 224 | mode.mode = VT_AUTO; |
| 225 | if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) |
| 226 | weston_log("could not reset vt handling\n"); |
| 227 | } |
| 228 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 229 | static int |
| 230 | weston_launcher_data(int fd, uint32_t mask, void *data) |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 231 | { |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 232 | struct weston_launcher *launcher = data; |
| 233 | int len, ret; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 234 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 235 | if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { |
| 236 | weston_log("launcher socket closed, exiting\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 237 | /* Normally the weston-launch will reset the tty, but |
| 238 | * in this case it died or something, so do it here so |
| 239 | * we don't end up with a stuck vt. */ |
| 240 | weston_launcher_restore(launcher); |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 241 | exit(-1); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 244 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 245 | len = recv(launcher->fd, &ret, sizeof ret, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 246 | } while (len < 0 && errno == EINTR); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 247 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 248 | switch (ret) { |
| 249 | case WESTON_LAUNCHER_ACTIVATE: |
| 250 | launcher->compositor->session_active = 1; |
| 251 | wl_signal_emit(&launcher->compositor->session_signal, |
| 252 | launcher->compositor); |
| 253 | break; |
| 254 | case WESTON_LAUNCHER_DEACTIVATE: |
| 255 | launcher->compositor->session_active = 0; |
| 256 | wl_signal_emit(&launcher->compositor->session_signal, |
| 257 | launcher->compositor); |
| 258 | break; |
| 259 | default: |
| 260 | weston_log("unexpected event from weston-launch\n"); |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | return 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 267 | static int |
| 268 | vt_handler(int signal_number, void *data) |
| 269 | { |
| 270 | struct weston_launcher *launcher = data; |
| 271 | struct weston_compositor *compositor = launcher->compositor; |
| 272 | |
| 273 | if (compositor->session_active) { |
| 274 | compositor->session_active = 0; |
| 275 | wl_signal_emit(&compositor->session_signal, compositor); |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 276 | drmDropMaster(launcher->drm_fd); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 277 | ioctl(launcher->tty, VT_RELDISP, 1); |
| 278 | } else { |
| 279 | ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ); |
Kristian Høgsberg | d2c9d8a | 2013-11-24 14:37:07 -0800 | [diff] [blame] | 280 | drmSetMaster(launcher->drm_fd); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 281 | compositor->session_active = 1; |
| 282 | wl_signal_emit(&compositor->session_signal, compositor); |
| 283 | } |
| 284 | |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | static int |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 289 | setup_tty(struct weston_launcher *launcher, int tty) |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 290 | { |
| 291 | struct wl_event_loop *loop; |
| 292 | struct vt_mode mode = { 0 }; |
| 293 | struct stat buf; |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 294 | char tty_device[32] ="<stdin>"; |
| 295 | int ret, kd_mode; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 296 | |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 297 | if (tty == 0) { |
Kristian Høgsberg | 325390e | 2013-10-09 11:03:39 -0700 | [diff] [blame] | 298 | launcher->tty = dup(tty); |
| 299 | if (launcher->tty == -1) { |
| 300 | weston_log("couldn't dup stdin: %m\n"); |
| 301 | return -1; |
| 302 | } |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 303 | } else { |
| 304 | snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty); |
| 305 | launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC); |
| 306 | if (launcher->tty == -1) { |
| 307 | weston_log("couldn't open tty %s: %m\n", tty_device); |
| 308 | return -1; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (fstat(launcher->tty, &buf) == -1 || |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 313 | major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) { |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 314 | weston_log("%s not a vt\n", tty_device); |
Kristian Høgsberg | 19ec77a | 2013-10-01 12:54:55 -0700 | [diff] [blame] | 315 | weston_log("if running weston from ssh, " |
| 316 | "use --tty to specify a tty\n"); |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 317 | goto err_close; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 320 | ret = ioctl(launcher->tty, KDGETMODE, &kd_mode); |
| 321 | if (ret) { |
| 322 | weston_log("failed to get VT mode: %m\n"); |
| 323 | return -1; |
| 324 | } |
| 325 | if (kd_mode != KD_TEXT) { |
| 326 | weston_log("%s is already in graphics mode, " |
| 327 | "is another display server running?\n", tty_device); |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 328 | goto err_close; |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Kristian Høgsberg | 74b0d72 | 2013-10-09 13:10:42 -0700 | [diff] [blame] | 331 | ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev)); |
| 332 | ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev)); |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 333 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 334 | if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) { |
| 335 | weston_log("failed to read keyboard mode: %m\n"); |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 336 | goto err_close; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (ioctl(launcher->tty, KDSKBMUTE, 1) && |
| 340 | ioctl(launcher->tty, KDSKBMODE, K_OFF)) { |
| 341 | weston_log("failed to set K_OFF keyboard mode: %m\n"); |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 342 | goto err_close; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS); |
| 346 | if (ret) { |
| 347 | weston_log("failed to set KD_GRAPHICS mode on tty: %m\n"); |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 348 | goto err_close; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 349 | } |
| 350 | |
David Herrmann | 541b604 | 2014-12-30 14:33:21 +0100 | [diff] [blame] | 351 | /* |
| 352 | * SIGRTMIN is used as global VT-acquire+release signal. Note that |
| 353 | * SIGRT* must be tested on runtime, as their exact values are not |
| 354 | * known at compile-time. POSIX requires 32 of them to be available. |
| 355 | */ |
| 356 | if (SIGRTMIN > SIGRTMAX) { |
| 357 | weston_log("not enough RT signals available: %u-%u\n", |
| 358 | SIGRTMIN, SIGRTMAX); |
| 359 | ret = -EINVAL; |
| 360 | goto err_close; |
| 361 | } |
| 362 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 363 | mode.mode = VT_PROCESS; |
David Herrmann | 541b604 | 2014-12-30 14:33:21 +0100 | [diff] [blame] | 364 | mode.relsig = SIGRTMIN; |
| 365 | mode.acqsig = SIGRTMIN; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 366 | if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) { |
| 367 | weston_log("failed to take control of vt handling\n"); |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 368 | goto err_close; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | loop = wl_display_get_event_loop(launcher->compositor->wl_display); |
| 372 | launcher->vt_source = |
David Herrmann | 541b604 | 2014-12-30 14:33:21 +0100 | [diff] [blame] | 373 | wl_event_loop_add_signal(loop, SIGRTMIN, vt_handler, launcher); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 374 | if (!launcher->vt_source) |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 375 | goto err_close; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 376 | |
| 377 | return 0; |
Kristian Høgsberg | 4a74d5a | 2013-10-09 11:19:11 -0700 | [diff] [blame] | 378 | |
| 379 | err_close: |
| 380 | close(launcher->tty); |
| 381 | return -1; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | int |
| 385 | weston_launcher_activate_vt(struct weston_launcher *launcher, int vt) |
| 386 | { |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 387 | if (launcher->logind) |
| 388 | return weston_logind_activate_vt(launcher->logind, vt); |
| 389 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 390 | return ioctl(launcher->tty, VT_ACTIVATE, vt); |
| 391 | } |
| 392 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 393 | struct weston_launcher * |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 394 | weston_launcher_connect(struct weston_compositor *compositor, int tty, |
David Herrmann | 2ecb84a | 2014-12-30 14:33:22 +0100 | [diff] [blame] | 395 | const char *seat_id, bool sync_drm) |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 396 | { |
| 397 | struct weston_launcher *launcher; |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 398 | struct wl_event_loop *loop; |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 399 | int r; |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 400 | |
| 401 | launcher = malloc(sizeof *launcher); |
| 402 | if (launcher == NULL) |
| 403 | return NULL; |
| 404 | |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 405 | launcher->logind = NULL; |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 406 | launcher->compositor = compositor; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 407 | launcher->drm_fd = -1; |
| 408 | launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK"); |
| 409 | if (launcher->fd != -1) { |
| 410 | launcher->tty = weston_environment_get_fd("WESTON_TTY_FD"); |
Kristian Høgsberg | 9a14b8f | 2014-04-30 10:40:39 -0700 | [diff] [blame] | 411 | /* We don't get a chance to read out the original kb |
| 412 | * mode for the tty, so just hard code K_UNICODE here |
| 413 | * in case we have to clean if weston-launch dies. */ |
| 414 | launcher->kb_mode = K_UNICODE; |
| 415 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 416 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 417 | launcher->source = wl_event_loop_add_fd(loop, launcher->fd, |
| 418 | WL_EVENT_READABLE, |
| 419 | weston_launcher_data, |
| 420 | launcher); |
| 421 | if (launcher->source == NULL) { |
| 422 | free(launcher); |
| 423 | return NULL; |
| 424 | } |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 425 | } else { |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 426 | r = weston_logind_connect(&launcher->logind, compositor, |
David Herrmann | 2ecb84a | 2014-12-30 14:33:22 +0100 | [diff] [blame] | 427 | seat_id, tty, sync_drm); |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 428 | if (r < 0) { |
| 429 | launcher->logind = NULL; |
| 430 | if (geteuid() == 0) { |
| 431 | if (setup_tty(launcher, tty) == -1) { |
| 432 | free(launcher); |
| 433 | return NULL; |
| 434 | } |
| 435 | } else { |
| 436 | free(launcher); |
| 437 | return NULL; |
| 438 | } |
| 439 | } |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 442 | return launcher; |
| 443 | } |
| 444 | |
| 445 | void |
| 446 | weston_launcher_destroy(struct weston_launcher *launcher) |
| 447 | { |
Kristian Høgsberg | d4c1cd7 | 2013-10-22 13:19:23 -0700 | [diff] [blame] | 448 | if (launcher->logind) { |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 449 | weston_logind_destroy(launcher->logind); |
Kristian Høgsberg | d4c1cd7 | 2013-10-22 13:19:23 -0700 | [diff] [blame] | 450 | } else if (launcher->fd != -1) { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 451 | close(launcher->fd); |
| 452 | wl_event_source_remove(launcher->source); |
| 453 | } else { |
| 454 | weston_launcher_restore(launcher); |
| 455 | wl_event_source_remove(launcher->vt_source); |
| 456 | } |
| 457 | |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 458 | if (launcher->tty >= 0) |
| 459 | close(launcher->tty); |
| 460 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 461 | free(launcher); |
| 462 | } |