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