blob: 8b496c85dc9e30b7cfcb8fac0cd51b251454fef8 [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{
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070072 return drmDropMaster(drm_fd);
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070073}
74static int
75drm_set_master(int drm_fd)
76{
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070077 return drmSetMaster(drm_fd);
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070078}
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020079static int
Kristian Høgsberg876c75f2013-10-14 13:57:44 -070080drm_is_master(int drm_fd)
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020081{
82 drm_magic_t magic;
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070083
84 return drmGetMagic(drm_fd, &magic) == 0 &&
85 drmAuthMagic(drm_fd, magic) == 0;
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020086}
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070087#else
88static int drm_drop_master(int drm_fd) {return 0;}
89static int drm_set_master(int drm_fd) {return 0;}
Kristian Høgsberg876c75f2013-10-14 13:57:44 -070090static int drm_is_master(int drm_fd) {return 1;}
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070091#endif
92
Benjamin Franzkebfeda132012-01-30 14:04:04 +010093int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070094weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +010095 const char *path, int flags)
96{
Kristian Høgsberg3f495872013-09-18 23:00:17 -070097 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010098 struct msghdr msg;
99 struct cmsghdr *cmsg;
100 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400101 union cmsg_data *data;
102 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100103 ssize_t len;
104 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700105 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100106
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700107 if (launcher->fd == -1) {
108 fd = open(path, flags | O_CLOEXEC);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700109 if (fd == -1)
110 return -1;
111
112 if (fstat(fd, &s) == -1) {
113 close(fd);
114 return -1;
115 }
116
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700117 if (major(s.st_rdev) == DRM_MAJOR) {
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700118 launcher->drm_fd = fd;
Kristian Høgsberg876c75f2013-10-14 13:57:44 -0700119 if (!drm_is_master(fd)) {
Kristian Høgsberg1468e602013-10-02 10:49:05 -0700120 weston_log("drm fd not master\n");
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700121 close(fd);
122 return -1;
123 }
124 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700125
126 return fd;
127 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100128
129 n = sizeof(*message) + strlen(path) + 1;
130 message = malloc(n);
131 if (!message)
132 return -1;
133
134 message->header.opcode = WESTON_LAUNCHER_OPEN;
135 message->flags = flags;
136 strcpy(message->path, path);
137
138 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700139 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100140 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400141 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100142
143 memset(&msg, 0, sizeof msg);
144 iov.iov_base = &ret;
145 iov.iov_len = sizeof ret;
146 msg.msg_iov = &iov;
147 msg.msg_iovlen = 1;
148 msg.msg_control = control;
149 msg.msg_controllen = sizeof control;
150
151 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700152 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100153 } while (len < 0 && errno == EINTR);
154
155 if (len != sizeof ret ||
156 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400157 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100158
159 cmsg = CMSG_FIRSTHDR(&msg);
160 if (!cmsg ||
161 cmsg->cmsg_level != SOL_SOCKET ||
162 cmsg->cmsg_type != SCM_RIGHTS) {
163 fprintf(stderr, "invalid control message\n");
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400164 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100165 }
166
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400167 data = (union cmsg_data *) CMSG_DATA(cmsg);
168 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100169 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100170 return -1;
171 }
172
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400173 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100174}
175
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700176void
David Herrmanne461f852013-10-22 00:28:08 +0200177weston_launcher_close(struct weston_launcher *launcher, int fd)
178{
179 close(fd);
180}
181
182void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700183weston_launcher_restore(struct weston_launcher *launcher)
184{
185 struct vt_mode mode = { 0 };
186
187 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
188 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
189 weston_log("failed to restore kb mode: %m\n");
190
191 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
192 weston_log("failed to set KD_TEXT mode on tty: %m\n");
193
194 mode.mode = VT_AUTO;
195 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
196 weston_log("could not reset vt handling\n");
197}
198
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700199static int
200weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100201{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700202 struct weston_launcher *launcher = data;
203 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100204
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700205 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
206 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700207 /* Normally the weston-launch will reset the tty, but
208 * in this case it died or something, so do it here so
209 * we don't end up with a stuck vt. */
210 weston_launcher_restore(launcher);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700211 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100212 }
213
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100214 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700215 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100216 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100217
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700218 switch (ret) {
219 case WESTON_LAUNCHER_ACTIVATE:
220 launcher->compositor->session_active = 1;
221 wl_signal_emit(&launcher->compositor->session_signal,
222 launcher->compositor);
223 break;
224 case WESTON_LAUNCHER_DEACTIVATE:
225 launcher->compositor->session_active = 0;
226 wl_signal_emit(&launcher->compositor->session_signal,
227 launcher->compositor);
228 break;
229 default:
230 weston_log("unexpected event from weston-launch\n");
231 break;
232 }
233
234 return 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100235}
236
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700237static int
238vt_handler(int signal_number, void *data)
239{
240 struct weston_launcher *launcher = data;
241 struct weston_compositor *compositor = launcher->compositor;
242
243 if (compositor->session_active) {
244 compositor->session_active = 0;
245 wl_signal_emit(&compositor->session_signal, compositor);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300246 drm_drop_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700247 ioctl(launcher->tty, VT_RELDISP, 1);
248 } else {
249 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300250 drm_set_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700251 compositor->session_active = 1;
252 wl_signal_emit(&compositor->session_signal, compositor);
253 }
254
255 return 1;
256}
257
258static int
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700259setup_tty(struct weston_launcher *launcher, int tty)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700260{
261 struct wl_event_loop *loop;
262 struct vt_mode mode = { 0 };
263 struct stat buf;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700264 char tty_device[32] ="<stdin>";
265 int ret, kd_mode;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700266
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700267 if (tty == 0) {
Kristian Høgsberg325390e2013-10-09 11:03:39 -0700268 launcher->tty = dup(tty);
269 if (launcher->tty == -1) {
270 weston_log("couldn't dup stdin: %m\n");
271 return -1;
272 }
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700273 } else {
274 snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty);
275 launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC);
276 if (launcher->tty == -1) {
277 weston_log("couldn't open tty %s: %m\n", tty_device);
278 return -1;
279 }
280 }
281
282 if (fstat(launcher->tty, &buf) == -1 ||
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700283 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700284 weston_log("%s not a vt\n", tty_device);
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700285 weston_log("if running weston from ssh, "
286 "use --tty to specify a tty\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700287 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700288 }
289
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700290 ret = ioctl(launcher->tty, KDGETMODE, &kd_mode);
291 if (ret) {
292 weston_log("failed to get VT mode: %m\n");
293 return -1;
294 }
295 if (kd_mode != KD_TEXT) {
296 weston_log("%s is already in graphics mode, "
297 "is another display server running?\n", tty_device);
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700298 goto err_close;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700299 }
300
Kristian Høgsberg74b0d722013-10-09 13:10:42 -0700301 ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
302 ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700303
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700304 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
305 weston_log("failed to read keyboard mode: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700306 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700307 }
308
309 if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
310 ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
311 weston_log("failed to set K_OFF keyboard mode: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700312 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700313 }
314
315 ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
316 if (ret) {
317 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700318 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700319 }
320
321 mode.mode = VT_PROCESS;
322 mode.relsig = SIGUSR1;
323 mode.acqsig = SIGUSR1;
324 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
325 weston_log("failed to take control of vt handling\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700326 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700327 }
328
329 loop = wl_display_get_event_loop(launcher->compositor->wl_display);
330 launcher->vt_source =
331 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
332 if (!launcher->vt_source)
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700333 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700334
335 return 0;
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700336
337 err_close:
338 close(launcher->tty);
339 return -1;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700340}
341
342int
343weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
344{
345 return ioctl(launcher->tty, VT_ACTIVATE, vt);
346}
347
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700348struct weston_launcher *
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700349weston_launcher_connect(struct weston_compositor *compositor, int tty)
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700350{
351 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700352 struct wl_event_loop *loop;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700353
354 launcher = malloc(sizeof *launcher);
355 if (launcher == NULL)
356 return NULL;
357
358 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700359 launcher->drm_fd = -1;
360 launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
361 if (launcher->fd != -1) {
362 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
363 loop = wl_display_get_event_loop(compositor->wl_display);
364 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
365 WL_EVENT_READABLE,
366 weston_launcher_data,
367 launcher);
368 if (launcher->source == NULL) {
369 free(launcher);
370 return NULL;
371 }
372 } else if (geteuid() == 0) {
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700373 if (setup_tty(launcher, tty) == -1) {
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700374 free(launcher);
375 return NULL;
376 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700377 } else {
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700378 free(launcher);
379 return NULL;
380 }
381
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700382 return launcher;
383}
384
385void
386weston_launcher_destroy(struct weston_launcher *launcher)
387{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700388 if (launcher->fd != -1) {
389 close(launcher->fd);
390 wl_event_source_remove(launcher->source);
391 } else {
392 weston_launcher_restore(launcher);
393 wl_event_source_remove(launcher->vt_source);
394 }
395
Kristian Høgsberg325390e2013-10-09 11:03:39 -0700396 close(launcher->tty);
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700397 free(launcher);
398}