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 | |
Adrian Negreanu | 908e6d0 | 2013-09-27 20:58:45 +0300 | [diff] [blame] | 43 | #ifdef BUILD_DRM_COMPOSITOR |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 44 | #include <xf86drm.h> |
Adrian Negreanu | 908e6d0 | 2013-09-27 20:58:45 +0300 | [diff] [blame] | 45 | #endif |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 46 | |
| 47 | #include "compositor.h" |
| 48 | #include "launcher-util.h" |
| 49 | #include "weston-launch.h" |
| 50 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 51 | #define DRM_MAJOR 226 |
| 52 | |
| 53 | #ifndef KDSKBMUTE |
| 54 | #define KDSKBMUTE 0x4B51 |
| 55 | #endif |
| 56 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 57 | union cmsg_data { unsigned char b[4]; int fd; }; |
| 58 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 59 | struct weston_launcher { |
| 60 | struct weston_compositor *compositor; |
| 61 | int fd; |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 62 | struct wl_event_source *source; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 63 | |
| 64 | int kb_mode, tty, drm_fd; |
| 65 | struct wl_event_source *vt_source; |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 68 | int |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 69 | weston_launcher_open(struct weston_launcher *launcher, |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 70 | const char *path, int flags) |
| 71 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 72 | int n, fd, ret = -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 73 | struct msghdr msg; |
| 74 | struct cmsghdr *cmsg; |
| 75 | struct iovec iov; |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 76 | union cmsg_data *data; |
| 77 | char control[CMSG_SPACE(sizeof data->fd)]; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 78 | ssize_t len; |
| 79 | struct weston_launcher_open *message; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 80 | struct stat s; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 81 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 82 | if (launcher->fd == -1) { |
| 83 | fd = open(path, flags | O_CLOEXEC); |
| 84 | |
| 85 | if (fd == -1) |
| 86 | return -1; |
| 87 | |
| 88 | if (fstat(fd, &s) == -1) { |
| 89 | close(fd); |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | if (major(s.st_rdev) == DRM_MAJOR) |
| 94 | launcher->drm_fd = fd; |
| 95 | |
| 96 | return fd; |
| 97 | } |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 98 | |
| 99 | n = sizeof(*message) + strlen(path) + 1; |
| 100 | message = malloc(n); |
| 101 | if (!message) |
| 102 | return -1; |
| 103 | |
| 104 | message->header.opcode = WESTON_LAUNCHER_OPEN; |
| 105 | message->flags = flags; |
| 106 | strcpy(message->path, path); |
| 107 | |
| 108 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 109 | len = send(launcher->fd, message, n, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 110 | } while (len < 0 && errno == EINTR); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 111 | free(message); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 112 | |
| 113 | memset(&msg, 0, sizeof msg); |
| 114 | iov.iov_base = &ret; |
| 115 | iov.iov_len = sizeof ret; |
| 116 | msg.msg_iov = &iov; |
| 117 | msg.msg_iovlen = 1; |
| 118 | msg.msg_control = control; |
| 119 | msg.msg_controllen = sizeof control; |
| 120 | |
| 121 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 122 | len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 123 | } while (len < 0 && errno == EINTR); |
| 124 | |
| 125 | if (len != sizeof ret || |
| 126 | ret < 0) |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 127 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 128 | |
| 129 | cmsg = CMSG_FIRSTHDR(&msg); |
| 130 | if (!cmsg || |
| 131 | cmsg->cmsg_level != SOL_SOCKET || |
| 132 | cmsg->cmsg_type != SCM_RIGHTS) { |
| 133 | fprintf(stderr, "invalid control message\n"); |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 134 | return -1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Kristian Høgsberg | 9e14091 | 2012-04-10 01:26:18 -0400 | [diff] [blame] | 137 | data = (union cmsg_data *) CMSG_DATA(cmsg); |
| 138 | if (data->fd == -1) { |
Martin Andersson | 566b464 | 2013-02-13 00:11:12 +0100 | [diff] [blame] | 139 | fprintf(stderr, "missing drm fd in socket request\n"); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 140 | return -1; |
| 141 | } |
| 142 | |
Kristian Høgsberg | ba25bd7 | 2012-04-10 01:31:09 -0400 | [diff] [blame] | 143 | return data->fd; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 146 | void |
| 147 | weston_launcher_restore(struct weston_launcher *launcher) |
| 148 | { |
| 149 | struct vt_mode mode = { 0 }; |
| 150 | |
| 151 | if (ioctl(launcher->tty, KDSKBMUTE, 0) && |
| 152 | ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode)) |
| 153 | weston_log("failed to restore kb mode: %m\n"); |
| 154 | |
| 155 | if (ioctl(launcher->tty, KDSETMODE, KD_TEXT)) |
| 156 | weston_log("failed to set KD_TEXT mode on tty: %m\n"); |
| 157 | |
| 158 | mode.mode = VT_AUTO; |
| 159 | if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) |
| 160 | weston_log("could not reset vt handling\n"); |
| 161 | } |
| 162 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 163 | static int |
| 164 | weston_launcher_data(int fd, uint32_t mask, void *data) |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 165 | { |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 166 | struct weston_launcher *launcher = data; |
| 167 | int len, ret; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 168 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 169 | if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { |
| 170 | weston_log("launcher socket closed, exiting\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 171 | /* Normally the weston-launch will reset the tty, but |
| 172 | * in this case it died or something, so do it here so |
| 173 | * we don't end up with a stuck vt. */ |
| 174 | weston_launcher_restore(launcher); |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 175 | exit(-1); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 178 | do { |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 179 | len = recv(launcher->fd, &ret, sizeof ret, 0); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 180 | } while (len < 0 && errno == EINTR); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 181 | |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 182 | switch (ret) { |
| 183 | case WESTON_LAUNCHER_ACTIVATE: |
| 184 | launcher->compositor->session_active = 1; |
| 185 | wl_signal_emit(&launcher->compositor->session_signal, |
| 186 | launcher->compositor); |
| 187 | break; |
| 188 | case WESTON_LAUNCHER_DEACTIVATE: |
| 189 | launcher->compositor->session_active = 0; |
| 190 | wl_signal_emit(&launcher->compositor->session_signal, |
| 191 | launcher->compositor); |
| 192 | break; |
| 193 | default: |
| 194 | weston_log("unexpected event from weston-launch\n"); |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | return 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Adrian Negreanu | 908e6d0 | 2013-09-27 20:58:45 +0300 | [diff] [blame] | 201 | #ifdef BUILD_DRM_COMPOSITOR |
| 202 | static int |
| 203 | drm_drop_master(int drm_fd) |
| 204 | { |
| 205 | if (drm_fd != -1) |
| 206 | return drmDropMaster(drm_fd); |
| 207 | return -EBADF; |
| 208 | } |
| 209 | static int |
| 210 | drm_set_master(int drm_fd) |
| 211 | { |
| 212 | if (drm_fd != -1) |
| 213 | return drmSetMaster(drm_fd); |
| 214 | return -EBADF; |
| 215 | } |
| 216 | #else |
| 217 | static int drm_drop_master(int drm_fd) {return 0;} |
| 218 | static int drm_set_master(int drm_fd) {return 0;} |
| 219 | #endif |
| 220 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 221 | static int |
| 222 | vt_handler(int signal_number, void *data) |
| 223 | { |
| 224 | struct weston_launcher *launcher = data; |
| 225 | struct weston_compositor *compositor = launcher->compositor; |
| 226 | |
| 227 | if (compositor->session_active) { |
| 228 | compositor->session_active = 0; |
| 229 | wl_signal_emit(&compositor->session_signal, compositor); |
Adrian Negreanu | 908e6d0 | 2013-09-27 20:58:45 +0300 | [diff] [blame] | 230 | drm_drop_master(launcher->drm_fd); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 231 | ioctl(launcher->tty, VT_RELDISP, 1); |
| 232 | } else { |
| 233 | ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ); |
Adrian Negreanu | 908e6d0 | 2013-09-27 20:58:45 +0300 | [diff] [blame] | 234 | drm_set_master(launcher->drm_fd); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 235 | compositor->session_active = 1; |
| 236 | wl_signal_emit(&compositor->session_signal, compositor); |
| 237 | } |
| 238 | |
| 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | static int |
| 243 | setup_tty(struct weston_launcher *launcher) |
| 244 | { |
| 245 | struct wl_event_loop *loop; |
| 246 | struct vt_mode mode = { 0 }; |
| 247 | struct stat buf; |
| 248 | int ret; |
| 249 | |
| 250 | if (fstat(STDIN_FILENO, &buf) == -1 || |
| 251 | major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) { |
| 252 | weston_log("stdin not a vt\n"); |
Kristian Høgsberg | 19ec77a | 2013-10-01 12:54:55 -0700 | [diff] [blame^] | 253 | weston_log("if running weston from ssh, " |
| 254 | "use --tty to specify a tty\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | launcher->tty = STDIN_FILENO; |
| 259 | if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) { |
| 260 | weston_log("failed to read keyboard mode: %m\n"); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | if (ioctl(launcher->tty, KDSKBMUTE, 1) && |
| 265 | ioctl(launcher->tty, KDSKBMODE, K_OFF)) { |
| 266 | weston_log("failed to set K_OFF keyboard mode: %m\n"); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS); |
| 271 | if (ret) { |
| 272 | weston_log("failed to set KD_GRAPHICS mode on tty: %m\n"); |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | mode.mode = VT_PROCESS; |
| 277 | mode.relsig = SIGUSR1; |
| 278 | mode.acqsig = SIGUSR1; |
| 279 | if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) { |
| 280 | weston_log("failed to take control of vt handling\n"); |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | loop = wl_display_get_event_loop(launcher->compositor->wl_display); |
| 285 | launcher->vt_source = |
| 286 | wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher); |
| 287 | if (!launcher->vt_source) |
| 288 | return -1; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | int |
| 294 | weston_launcher_activate_vt(struct weston_launcher *launcher, int vt) |
| 295 | { |
| 296 | return ioctl(launcher->tty, VT_ACTIVATE, vt); |
| 297 | } |
| 298 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 299 | struct weston_launcher * |
| 300 | weston_launcher_connect(struct weston_compositor *compositor) |
| 301 | { |
| 302 | struct weston_launcher *launcher; |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 303 | struct wl_event_loop *loop; |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 304 | |
| 305 | launcher = malloc(sizeof *launcher); |
| 306 | if (launcher == NULL) |
| 307 | return NULL; |
| 308 | |
| 309 | launcher->compositor = compositor; |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 310 | launcher->drm_fd = -1; |
| 311 | launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK"); |
| 312 | if (launcher->fd != -1) { |
| 313 | launcher->tty = weston_environment_get_fd("WESTON_TTY_FD"); |
| 314 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 315 | launcher->source = wl_event_loop_add_fd(loop, launcher->fd, |
| 316 | WL_EVENT_READABLE, |
| 317 | weston_launcher_data, |
| 318 | launcher); |
| 319 | if (launcher->source == NULL) { |
| 320 | free(launcher); |
| 321 | return NULL; |
| 322 | } |
| 323 | } else if (geteuid() == 0) { |
Kristian Høgsberg | 19ec77a | 2013-10-01 12:54:55 -0700 | [diff] [blame^] | 324 | if (setup_tty(launcher) == -1) { |
| 325 | free(launcher); |
| 326 | return NULL; |
| 327 | } |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 328 | } else { |
Kristian Høgsberg | 1eb482d | 2013-09-17 22:15:37 -0700 | [diff] [blame] | 329 | free(launcher); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 333 | return launcher; |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | weston_launcher_destroy(struct weston_launcher *launcher) |
| 338 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 339 | if (launcher->fd != -1) { |
| 340 | close(launcher->fd); |
| 341 | wl_event_source_remove(launcher->source); |
| 342 | } else { |
| 343 | weston_launcher_restore(launcher); |
| 344 | wl_event_source_remove(launcher->vt_source); |
| 345 | } |
| 346 | |
Kristian Høgsberg | 05ad1e4 | 2013-09-17 14:41:03 -0700 | [diff] [blame] | 347 | free(launcher); |
| 348 | } |