blob: 237f8c58fa15087841c06d0b70d4dc721f73cf2b [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
Benjamin Franzkebfeda132012-01-30 14:04:04 +010068int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070069weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +010070 const char *path, int flags)
71{
Kristian Høgsberg3f495872013-09-18 23:00:17 -070072 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010073 struct msghdr msg;
74 struct cmsghdr *cmsg;
75 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -040076 union cmsg_data *data;
77 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +010078 ssize_t len;
79 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -070080 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010081
Kristian Høgsberg3f495872013-09-18 23:00:17 -070082 if (launcher->fd == -1) {
83 fd = open(path, flags | O_CLOEXEC);
84
85 if (fd == -1)
86 return -1;
87
88 if (fstat(fd, &s) == -1) {
89 close(fd);
90 return -1;
91 }
92
93 if (major(s.st_rdev) == DRM_MAJOR)
94 launcher->drm_fd = fd;
95
96 return fd;
97 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +010098
99 n = sizeof(*message) + strlen(path) + 1;
100 message = malloc(n);
101 if (!message)
102 return -1;
103
104 message->header.opcode = WESTON_LAUNCHER_OPEN;
105 message->flags = flags;
106 strcpy(message->path, path);
107
108 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700109 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100110 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400111 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100112
113 memset(&msg, 0, sizeof msg);
114 iov.iov_base = &ret;
115 iov.iov_len = sizeof ret;
116 msg.msg_iov = &iov;
117 msg.msg_iovlen = 1;
118 msg.msg_control = control;
119 msg.msg_controllen = sizeof control;
120
121 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700122 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100123 } while (len < 0 && errno == EINTR);
124
125 if (len != sizeof ret ||
126 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400127 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100128
129 cmsg = CMSG_FIRSTHDR(&msg);
130 if (!cmsg ||
131 cmsg->cmsg_level != SOL_SOCKET ||
132 cmsg->cmsg_type != SCM_RIGHTS) {
133 fprintf(stderr, "invalid control message\n");
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400134 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100135 }
136
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400137 data = (union cmsg_data *) CMSG_DATA(cmsg);
138 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100139 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100140 return -1;
141 }
142
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400143 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100144}
145
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700146void
147weston_launcher_restore(struct weston_launcher *launcher)
148{
149 struct vt_mode mode = { 0 };
150
151 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
152 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
153 weston_log("failed to restore kb mode: %m\n");
154
155 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
156 weston_log("failed to set KD_TEXT mode on tty: %m\n");
157
158 mode.mode = VT_AUTO;
159 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
160 weston_log("could not reset vt handling\n");
161}
162
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700163static int
164weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100165{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700166 struct weston_launcher *launcher = data;
167 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100168
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700169 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
170 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700171 /* Normally the weston-launch will reset the tty, but
172 * in this case it died or something, so do it here so
173 * we don't end up with a stuck vt. */
174 weston_launcher_restore(launcher);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700175 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100176 }
177
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100178 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700179 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100180 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100181
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700182 switch (ret) {
183 case WESTON_LAUNCHER_ACTIVATE:
184 launcher->compositor->session_active = 1;
185 wl_signal_emit(&launcher->compositor->session_signal,
186 launcher->compositor);
187 break;
188 case WESTON_LAUNCHER_DEACTIVATE:
189 launcher->compositor->session_active = 0;
190 wl_signal_emit(&launcher->compositor->session_signal,
191 launcher->compositor);
192 break;
193 default:
194 weston_log("unexpected event from weston-launch\n");
195 break;
196 }
197
198 return 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100199}
200
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300201#ifdef BUILD_DRM_COMPOSITOR
202static int
203drm_drop_master(int drm_fd)
204{
205 if (drm_fd != -1)
206 return drmDropMaster(drm_fd);
207 return -EBADF;
208}
209static int
210drm_set_master(int drm_fd)
211{
212 if (drm_fd != -1)
213 return drmSetMaster(drm_fd);
214 return -EBADF;
215}
216#else
217static int drm_drop_master(int drm_fd) {return 0;}
218static int drm_set_master(int drm_fd) {return 0;}
219#endif
220
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700221static int
222vt_handler(int signal_number, void *data)
223{
224 struct weston_launcher *launcher = data;
225 struct weston_compositor *compositor = launcher->compositor;
226
227 if (compositor->session_active) {
228 compositor->session_active = 0;
229 wl_signal_emit(&compositor->session_signal, compositor);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300230 drm_drop_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700231 ioctl(launcher->tty, VT_RELDISP, 1);
232 } else {
233 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300234 drm_set_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700235 compositor->session_active = 1;
236 wl_signal_emit(&compositor->session_signal, compositor);
237 }
238
239 return 1;
240}
241
242static int
243setup_tty(struct weston_launcher *launcher)
244{
245 struct wl_event_loop *loop;
246 struct vt_mode mode = { 0 };
247 struct stat buf;
248 int ret;
249
250 if (fstat(STDIN_FILENO, &buf) == -1 ||
251 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
252 weston_log("stdin not a vt\n");
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700253 weston_log("if running weston from ssh, "
254 "use --tty to specify a tty\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700255 return -1;
256 }
257
258 launcher->tty = STDIN_FILENO;
259 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
260 weston_log("failed to read keyboard mode: %m\n");
261 return -1;
262 }
263
264 if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
265 ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
266 weston_log("failed to set K_OFF keyboard mode: %m\n");
267 return -1;
268 }
269
270 ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
271 if (ret) {
272 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
273 return -1;
274 }
275
276 mode.mode = VT_PROCESS;
277 mode.relsig = SIGUSR1;
278 mode.acqsig = SIGUSR1;
279 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
280 weston_log("failed to take control of vt handling\n");
281 return -1;
282 }
283
284 loop = wl_display_get_event_loop(launcher->compositor->wl_display);
285 launcher->vt_source =
286 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
287 if (!launcher->vt_source)
288 return -1;
289
290 return 0;
291}
292
293int
294weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
295{
296 return ioctl(launcher->tty, VT_ACTIVATE, vt);
297}
298
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700299struct weston_launcher *
300weston_launcher_connect(struct weston_compositor *compositor)
301{
302 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700303 struct wl_event_loop *loop;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700304
305 launcher = malloc(sizeof *launcher);
306 if (launcher == NULL)
307 return NULL;
308
309 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700310 launcher->drm_fd = -1;
311 launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
312 if (launcher->fd != -1) {
313 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
314 loop = wl_display_get_event_loop(compositor->wl_display);
315 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
316 WL_EVENT_READABLE,
317 weston_launcher_data,
318 launcher);
319 if (launcher->source == NULL) {
320 free(launcher);
321 return NULL;
322 }
323 } else if (geteuid() == 0) {
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700324 if (setup_tty(launcher) == -1) {
325 free(launcher);
326 return NULL;
327 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700328 } else {
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700329 free(launcher);
330 return NULL;
331 }
332
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700333 return launcher;
334}
335
336void
337weston_launcher_destroy(struct weston_launcher *launcher)
338{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700339 if (launcher->fd != -1) {
340 close(launcher->fd);
341 wl_event_source_remove(launcher->source);
342 } else {
343 weston_launcher_restore(launcher);
344 wl_event_source_remove(launcher->vt_source);
345 }
346
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700347 free(launcher);
348}