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