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