blob: a1ff832e1c9f0cf87ccce3763c7b9acb94a6c5b8 [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"
David Herrmanncc5b2ed2013-10-22 00:28:09 +020049#include "logind-util.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010050#include "weston-launch.h"
51
Kristian Høgsberg3f495872013-09-18 23:00:17 -070052#define DRM_MAJOR 226
53
54#ifndef KDSKBMUTE
55#define KDSKBMUTE 0x4B51
56#endif
57
Kristian Høgsberg9e140912012-04-10 01:26:18 -040058union cmsg_data { unsigned char b[4]; int fd; };
59
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070060struct weston_launcher {
David Herrmanncc5b2ed2013-10-22 00:28:09 +020061 struct weston_logind *logind;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070062 struct weston_compositor *compositor;
63 int fd;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -070064 struct wl_event_source *source;
Kristian Høgsberg3f495872013-09-18 23:00:17 -070065
66 int kb_mode, tty, drm_fd;
67 struct wl_event_source *vt_source;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070068};
69
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070070#ifdef BUILD_DRM_COMPOSITOR
71static int
72drm_drop_master(int drm_fd)
73{
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070074 return drmDropMaster(drm_fd);
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070075}
76static int
77drm_set_master(int drm_fd)
78{
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070079 return drmSetMaster(drm_fd);
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070080}
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020081static int
Kristian Høgsberg876c75f2013-10-14 13:57:44 -070082drm_is_master(int drm_fd)
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020083{
84 drm_magic_t magic;
Kristian Høgsberg891a16d2013-10-14 13:59:53 -070085
86 return drmGetMagic(drm_fd, &magic) == 0 &&
87 drmAuthMagic(drm_fd, magic) == 0;
Tomeu Vizoso0b12db52013-10-09 11:30:57 +020088}
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070089#else
90static int drm_drop_master(int drm_fd) {return 0;}
91static int drm_set_master(int drm_fd) {return 0;}
Kristian Høgsberg876c75f2013-10-14 13:57:44 -070092static int drm_is_master(int drm_fd) {return 1;}
Kristian Høgsberg57a10e42013-10-01 15:37:09 -070093#endif
94
Benjamin Franzkebfeda132012-01-30 14:04:04 +010095int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070096weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +010097 const char *path, int flags)
98{
Kristian Høgsberg3f495872013-09-18 23:00:17 -070099 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100100 struct msghdr msg;
101 struct cmsghdr *cmsg;
102 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400103 union cmsg_data *data;
104 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100105 ssize_t len;
106 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700107 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100108
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200109 if (launcher->logind)
110 return weston_logind_open(launcher->logind, path, flags);
111
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;
Kristian Høgsberg876c75f2013-10-14 13:57:44 -0700124 if (!drm_is_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
David Herrmanne461f852013-10-22 00:28:08 +0200182weston_launcher_close(struct weston_launcher *launcher, int fd)
183{
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200184 if (launcher->logind)
185 return weston_logind_close(launcher->logind, fd);
186
David Herrmanne461f852013-10-22 00:28:08 +0200187 close(fd);
188}
189
190void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700191weston_launcher_restore(struct weston_launcher *launcher)
192{
193 struct vt_mode mode = { 0 };
194
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200195 if (launcher->logind)
196 return weston_logind_restore(launcher->logind);
197
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700198 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
199 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
200 weston_log("failed to restore kb mode: %m\n");
201
202 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
203 weston_log("failed to set KD_TEXT mode on tty: %m\n");
204
205 mode.mode = VT_AUTO;
206 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
207 weston_log("could not reset vt handling\n");
208}
209
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700210static int
211weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100212{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700213 struct weston_launcher *launcher = data;
214 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100215
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700216 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
217 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700218 /* Normally the weston-launch will reset the tty, but
219 * in this case it died or something, so do it here so
220 * we don't end up with a stuck vt. */
221 weston_launcher_restore(launcher);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700222 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100223 }
224
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100225 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700226 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100227 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100228
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700229 switch (ret) {
230 case WESTON_LAUNCHER_ACTIVATE:
231 launcher->compositor->session_active = 1;
232 wl_signal_emit(&launcher->compositor->session_signal,
233 launcher->compositor);
234 break;
235 case WESTON_LAUNCHER_DEACTIVATE:
236 launcher->compositor->session_active = 0;
237 wl_signal_emit(&launcher->compositor->session_signal,
238 launcher->compositor);
239 break;
240 default:
241 weston_log("unexpected event from weston-launch\n");
242 break;
243 }
244
245 return 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100246}
247
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700248static int
249vt_handler(int signal_number, void *data)
250{
251 struct weston_launcher *launcher = data;
252 struct weston_compositor *compositor = launcher->compositor;
253
254 if (compositor->session_active) {
255 compositor->session_active = 0;
256 wl_signal_emit(&compositor->session_signal, compositor);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300257 drm_drop_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700258 ioctl(launcher->tty, VT_RELDISP, 1);
259 } else {
260 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
Adrian Negreanu908e6d02013-09-27 20:58:45 +0300261 drm_set_master(launcher->drm_fd);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700262 compositor->session_active = 1;
263 wl_signal_emit(&compositor->session_signal, compositor);
264 }
265
266 return 1;
267}
268
269static int
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700270setup_tty(struct weston_launcher *launcher, int tty)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700271{
272 struct wl_event_loop *loop;
273 struct vt_mode mode = { 0 };
274 struct stat buf;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700275 char tty_device[32] ="<stdin>";
276 int ret, kd_mode;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700277
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700278 if (tty == 0) {
Kristian Høgsberg325390e2013-10-09 11:03:39 -0700279 launcher->tty = dup(tty);
280 if (launcher->tty == -1) {
281 weston_log("couldn't dup stdin: %m\n");
282 return -1;
283 }
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700284 } else {
285 snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty);
286 launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC);
287 if (launcher->tty == -1) {
288 weston_log("couldn't open tty %s: %m\n", tty_device);
289 return -1;
290 }
291 }
292
293 if (fstat(launcher->tty, &buf) == -1 ||
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700294 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700295 weston_log("%s not a vt\n", tty_device);
Kristian Høgsberg19ec77a2013-10-01 12:54:55 -0700296 weston_log("if running weston from ssh, "
297 "use --tty to specify a tty\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700298 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700299 }
300
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700301 ret = ioctl(launcher->tty, KDGETMODE, &kd_mode);
302 if (ret) {
303 weston_log("failed to get VT mode: %m\n");
304 return -1;
305 }
306 if (kd_mode != KD_TEXT) {
307 weston_log("%s is already in graphics mode, "
308 "is another display server running?\n", tty_device);
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700309 goto err_close;
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700310 }
311
Kristian Høgsberg74b0d722013-10-09 13:10:42 -0700312 ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
313 ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700314
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700315 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
316 weston_log("failed to read keyboard mode: %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 if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
321 ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
322 weston_log("failed to set K_OFF keyboard mode: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700323 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700324 }
325
326 ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
327 if (ret) {
328 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700329 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700330 }
331
332 mode.mode = VT_PROCESS;
333 mode.relsig = SIGUSR1;
334 mode.acqsig = SIGUSR1;
335 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
336 weston_log("failed to take control of vt handling\n");
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700337 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700338 }
339
340 loop = wl_display_get_event_loop(launcher->compositor->wl_display);
341 launcher->vt_source =
342 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
343 if (!launcher->vt_source)
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700344 goto err_close;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700345
346 return 0;
Kristian Høgsberg4a74d5a2013-10-09 11:19:11 -0700347
348 err_close:
349 close(launcher->tty);
350 return -1;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700351}
352
353int
354weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
355{
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200356 if (launcher->logind)
357 return weston_logind_activate_vt(launcher->logind, vt);
358
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700359 return ioctl(launcher->tty, VT_ACTIVATE, vt);
360}
361
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700362struct weston_launcher *
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200363weston_launcher_connect(struct weston_compositor *compositor, int tty,
364 const char *seat_id)
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700365{
366 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700367 struct wl_event_loop *loop;
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200368 int r;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700369
370 launcher = malloc(sizeof *launcher);
371 if (launcher == NULL)
372 return NULL;
373
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200374 launcher->logind = NULL;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700375 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700376 launcher->drm_fd = -1;
377 launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
378 if (launcher->fd != -1) {
379 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
380 loop = wl_display_get_event_loop(compositor->wl_display);
381 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
382 WL_EVENT_READABLE,
383 weston_launcher_data,
384 launcher);
385 if (launcher->source == NULL) {
386 free(launcher);
387 return NULL;
388 }
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700389 } else {
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200390 r = weston_logind_connect(&launcher->logind, compositor,
391 seat_id, tty);
392 if (r < 0) {
393 launcher->logind = NULL;
394 if (geteuid() == 0) {
395 if (setup_tty(launcher, tty) == -1) {
396 free(launcher);
397 return NULL;
398 }
399 } else {
400 free(launcher);
401 return NULL;
402 }
403 }
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700404 }
405
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700406 return launcher;
407}
408
409void
410weston_launcher_destroy(struct weston_launcher *launcher)
411{
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200412 if (launcher->logind)
413 weston_logind_destroy(launcher->logind);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700414 if (launcher->fd != -1) {
415 close(launcher->fd);
416 wl_event_source_remove(launcher->source);
417 } else {
418 weston_launcher_restore(launcher);
419 wl_event_source_remove(launcher->vt_source);
420 }
421
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200422 if (launcher->tty >= 0)
423 close(launcher->tty);
424
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700425 free(launcher);
426}