blob: d90271f2d921a44107ab89c4d38819a36dea8160 [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}
83#else
84static int drm_drop_master(int drm_fd) {return 0;}
85static int drm_set_master(int drm_fd) {return 0;}
86#endif
87
Benjamin Franzkebfeda132012-01-30 14:04:04 +010088int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070089weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +010090 const char *path, int flags)
91{
Kristian Høgsberg3f495872013-09-18 23:00:17 -070092 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010093 struct msghdr msg;
94 struct cmsghdr *cmsg;
95 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -040096 union cmsg_data *data;
97 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +010098 ssize_t len;
99 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700100 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100101
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700102 if (launcher->fd == -1) {
103 fd = open(path, flags | O_CLOEXEC);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700104 if (fd == -1)
105 return -1;
106
107 if (fstat(fd, &s) == -1) {
108 close(fd);
109 return -1;
110 }
111
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700112 if (major(s.st_rdev) == DRM_MAJOR) {
Kristian Høgsberg1468e602013-10-02 10:49:05 -0700113 drm_magic_t magic;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700114 launcher->drm_fd = fd;
Kristian Høgsberg1468e602013-10-02 10:49:05 -0700115 if (drmGetMagic(fd, &magic) != 0 ||
116 drmAuthMagic(fd, magic) != 0) {
117 weston_log("drm fd not master\n");
Kristian Høgsberg57a10e42013-10-01 15:37:09 -0700118 close(fd);
119 return -1;
120 }
121 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700122
123 return fd;
124 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100125
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øgsberg05ad1e42013-09-17 14:41:03 -0700136 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100137 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400138 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100139
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øgsberg05ad1e42013-09-17 14:41:03 -0700149 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100150 } while (len < 0 && errno == EINTR);
151
152 if (len != sizeof ret ||
153 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400154 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100155
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øgsbergba25bd72012-04-10 01:31:09 -0400161 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100162 }
163
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400164 data = (union cmsg_data *) CMSG_DATA(cmsg);
165 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100166 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100167 return -1;
168 }
169
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400170 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100171}
172
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700173void
174weston_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øgsberg1eb482d2013-09-17 22:15:37 -0700190static int
191weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100192{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700193 struct weston_launcher *launcher = data;
194 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100195
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700196 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
197 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700198 /* 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øgsberg1eb482d2013-09-17 22:15:37 -0700202 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100203 }
204
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100205 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700206 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100207 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100208
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700209 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 Franzkebfeda132012-01-30 14:04:04 +0100226}
227
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700228static int
229vt_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 Negreanu908e6d02013-09-27 20:58:45 +0300237 drm_drop_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700238 ioctl(launcher->tty, VT_RELDISP, 1);
239 } else {
240 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300241 drm_set_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700242 compositor->session_active = 1;
243 wl_signal_emit(&compositor->session_signal, compositor);
244 }
245
246 return 1;
247}
248
249static int
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700250setup_tty(struct weston_launcher *launcher, int tty)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700251{
252 struct wl_event_loop *loop;
253 struct vt_mode mode = { 0 };
254 struct stat buf;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700255 char tty_device[32] ="<stdin>";
256 int ret, kd_mode;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700257
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700258 if (tty == 0) {
Kristian Høgsberg325390e2013-10-09 11:03:39 -0700259 launcher->tty = dup(tty);
260 if (launcher->tty == -1) {
261 weston_log("couldn't dup stdin: %m\n");
262 return -1;
263 }
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700264 } 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øgsberg3f495872013-09-18 23:00:17 -0700274 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700275 weston_log("%s not a vt\n", tty_device);
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700276 weston_log("if running weston from ssh, "
277 "use --tty to specify a tty\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700278 return -1;
279 }
280
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700281 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øgsberg3f495872013-09-18 23:00:17 -0700298 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
332int
333weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
334{
335 return ioctl(launcher->tty, VT_ACTIVATE, vt);
336}
337
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700338struct weston_launcher *
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700339weston_launcher_connect(struct weston_compositor *compositor, int tty)
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700340{
341 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700342 struct wl_event_loop *loop;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700343
344 launcher = malloc(sizeof *launcher);
345 if (launcher == NULL)
346 return NULL;
347
348 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700349 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øgsberg6ff3ff52013-10-02 10:53:33 -0700363 if (setup_tty(launcher, tty) == -1) {
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700364 free(launcher);
365 return NULL;
366 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700367 } else {
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700368 free(launcher);
369 return NULL;
370 }
371
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700372 return launcher;
373}
374
375void
376weston_launcher_destroy(struct weston_launcher *launcher)
377{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700378 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øgsberg325390e2013-10-09 11:03:39 -0700386 close(launcher->tty);
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700387 free(launcher);
388}