blob: 35a5698e813910d00c054f50083ec3c29d2480e9 [file] [log] [blame]
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001/*
2 * Copyright © 2012 Benjamin Franzke
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -07003 * Copyright © 2013 Intel Corporation
Benjamin Franzkebfeda132012-01-30 14:04:04 +01004 *
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 Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Benjamin Franzkebfeda132012-01-30 14:04:04 +010026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <errno.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070031#include <signal.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010032#include <sys/socket.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/uio.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070036#include <sys/ioctl.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010037#include <fcntl.h>
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070038#include <unistd.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070039#include <linux/vt.h>
40#include <linux/kd.h>
41#include <linux/major.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010042
Adrian Negreanu908e6d02013-09-27 20:58:45 +030043#ifdef BUILD_DRM_COMPOSITOR
Benjamin Franzkebfeda132012-01-30 14:04:04 +010044#include <xf86drm.h>
Adrian Negreanu908e6d02013-09-27 20:58:45 +030045#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +010046
47#include "compositor.h"
48#include "launcher-util.h"
49#include "weston-launch.h"
50
Kristian Høgsberg3f495872013-09-18 23:00:17 -070051#define DRM_MAJOR 226
52
53#ifndef KDSKBMUTE
54#define KDSKBMUTE 0x4B51
55#endif
56
Kristian Høgsberg9e140912012-04-10 01:26:18 -040057union cmsg_data { unsigned char b[4]; int fd; };
58
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070059struct weston_launcher {
60 struct weston_compositor *compositor;
61 int fd;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -070062 struct wl_event_source *source;
Kristian Høgsberg3f495872013-09-18 23:00:17 -070063
64 int kb_mode, tty, drm_fd;
65 struct wl_event_source *vt_source;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070066};
67
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070068#ifdef BUILD_DRM_COMPOSITOR
69static int
70drm_drop_master(int drm_fd)
71{
72 if (drm_fd != -1)
73 return drmDropMaster(drm_fd);
74 return -EBADF;
75}
76static int
77drm_set_master(int drm_fd)
78{
79 if (drm_fd != -1)
80 return drmSetMaster(drm_fd);
81 return -EBADF;
82}
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020083static int
84drm_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øgsberg57a10e42013-10-01 15:37:09 -070092#else
93static int drm_drop_master(int drm_fd) {return 0;}
94static int drm_set_master(int drm_fd) {return 0;}
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020095static int drm_check_master(int drm_fd) {return 1;}
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070096#endif
97
Benjamin Franzkebfeda132012-01-30 14:04:04 +010098int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070099weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100100 const char *path, int flags)
101{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700102 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100103 struct msghdr msg;
104 struct cmsghdr *cmsg;
105 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400106 union cmsg_data *data;
107 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100108 ssize_t len;
109 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700110 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100111
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700112 if (launcher->fd == -1) {
113 fd = open(path, flags | O_CLOEXEC);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700114 if (fd == -1)
115 return -1;
116
117 if (fstat(fd, &s) == -1) {
118 close(fd);
119 return -1;
120 }
121
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700122 if (major(s.st_rdev) == DRM_MAJOR) {
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700123 launcher->drm_fd = fd;
Tomeu Vizoso0b12db52013-10-09 11:30:57 +0200124 if (!drm_check_master(fd)) {
Kristian Høgsberg1468e602013-10-02 10:49:05 -0700125 weston_log("drm fd not master\n");
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700126 close(fd);
127 return -1;
128 }
129 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700130
131 return fd;
132 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100133
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øgsberg05ad1e42013-09-17 14:41:03 -0700144 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100145 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400146 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100147
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øgsberg05ad1e42013-09-17 14:41:03 -0700157 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100158 } while (len < 0 && errno == EINTR);
159
160 if (len != sizeof ret ||
161 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400162 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100163
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øgsbergba25bd72012-04-10 01:31:09 -0400169 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100170 }
171
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400172 data = (union cmsg_data *) CMSG_DATA(cmsg);
173 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100174 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100175 return -1;
176 }
177
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400178 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100179}
180
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700181void
182weston_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øgsberg1eb482d2013-09-17 22:15:37 -0700198static int
199weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100200{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700201 struct weston_launcher *launcher = data;
202 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100203
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700204 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
205 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700206 /* 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øgsberg1eb482d2013-09-17 22:15:37 -0700210 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100211 }
212
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100213 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700214 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100215 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100216
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700217 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 Franzkebfeda132012-01-30 14:04:04 +0100234}
235
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700236static int
237vt_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 Negreanu908e6d02013-09-27 20:58:45 +0300245 drm_drop_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700246 ioctl(launcher->tty, VT_RELDISP, 1);
247 } else {
248 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300249 drm_set_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700250 compositor->session_active = 1;
251 wl_signal_emit(&compositor->session_signal, compositor);
252 }
253
254 return 1;
255}
256
257static int
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700258setup_tty(struct weston_launcher *launcher, int tty)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700259{
260 struct wl_event_loop *loop;
261 struct vt_mode mode = { 0 };
262 struct stat buf;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700263 char tty_device[32] ="<stdin>";
264 int ret, kd_mode;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700265
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700266 if (tty == 0) {
Kristian Høgsberg325390e2013-10-09 11:03:39 -0700267 launcher->tty = dup(tty);
268 if (launcher->tty == -1) {
269 weston_log("couldn't dup stdin: %m\n");
270 return -1;
271 }
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700272 } 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øgsberg3f495872013-09-18 23:00:17 -0700282 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700283 weston_log("%s not a vt\n", tty_device);
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700284 weston_log("if running weston from ssh, "
285 "use --tty to specify a tty\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700286 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700287 }
288
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700289 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øgsberg4a74d5a2013-10-09 11:19:11 -0700297 goto err_close;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700298 }
299
Kristian Høgsberg74b0d722013-10-09 13:10:42 -0700300 ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
301 ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700302
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700303 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
304 weston_log("failed to read keyboard mode: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700305 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700306 }
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øgsberg4a74d5a2013-10-09 11:19:11 -0700311 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700312 }
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øgsberg4a74d5a2013-10-09 11:19:11 -0700317 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700318 }
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øgsberg4a74d5a2013-10-09 11:19:11 -0700325 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700326 }
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øgsberg4a74d5a2013-10-09 11:19:11 -0700332 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700333
334 return 0;
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700335
336 err_close:
337 close(launcher->tty);
338 return -1;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700339}
340
341int
342weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
343{
344 return ioctl(launcher->tty, VT_ACTIVATE, vt);
345}
346
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700347struct weston_launcher *
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700348weston_launcher_connect(struct weston_compositor *compositor, int tty)
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700349{
350 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700351 struct wl_event_loop *loop;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700352
353 launcher = malloc(sizeof *launcher);
354 if (launcher == NULL)
355 return NULL;
356
357 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700358 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øgsberg6ff3ff52013-10-02 10:53:33 -0700372 if (setup_tty(launcher, tty) == -1) {
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700373 free(launcher);
374 return NULL;
375 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700376 } else {
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700377 free(launcher);
378 return NULL;
379 }
380
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700381 return launcher;
382}
383
384void
385weston_launcher_destroy(struct weston_launcher *launcher)
386{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700387 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øgsberg325390e2013-10-09 11:03:39 -0700395 close(launcher->tty);
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700396 free(launcher);
397}