blob: a2ad0ec289d2452cd1b005adc90daea520e0de7e [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
43#include <xf86drm.h>
44
45#include "compositor.h"
46#include "launcher-util.h"
47#include "weston-launch.h"
48
Kristian Høgsberg3f495872013-09-18 23:00:17 -070049#define DRM_MAJOR 226
50
51#ifndef KDSKBMUTE
52#define KDSKBMUTE 0x4B51
53#endif
54
Kristian Høgsberg9e140912012-04-10 01:26:18 -040055union cmsg_data { unsigned char b[4]; int fd; };
56
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070057struct weston_launcher {
58 struct weston_compositor *compositor;
59 int fd;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -070060 struct wl_event_source *source;
Kristian Høgsberg3f495872013-09-18 23:00:17 -070061
62 int kb_mode, tty, drm_fd;
63 struct wl_event_source *vt_source;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070064};
65
Benjamin Franzkebfeda132012-01-30 14:04:04 +010066int
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -070067weston_launcher_open(struct weston_launcher *launcher,
Benjamin Franzkebfeda132012-01-30 14:04:04 +010068 const char *path, int flags)
69{
Kristian Høgsberg3f495872013-09-18 23:00:17 -070070 int n, fd, ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010071 struct msghdr msg;
72 struct cmsghdr *cmsg;
73 struct iovec iov;
Kristian Høgsberg9e140912012-04-10 01:26:18 -040074 union cmsg_data *data;
75 char control[CMSG_SPACE(sizeof data->fd)];
Benjamin Franzkebfeda132012-01-30 14:04:04 +010076 ssize_t len;
77 struct weston_launcher_open *message;
Kristian Høgsberg3f495872013-09-18 23:00:17 -070078 struct stat s;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010079
Kristian Høgsberg3f495872013-09-18 23:00:17 -070080 if (launcher->fd == -1) {
81 fd = open(path, flags | O_CLOEXEC);
82
83 if (fd == -1)
84 return -1;
85
86 if (fstat(fd, &s) == -1) {
87 close(fd);
88 return -1;
89 }
90
91 if (major(s.st_rdev) == DRM_MAJOR)
92 launcher->drm_fd = fd;
93
94 return fd;
95 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +010096
97 n = sizeof(*message) + strlen(path) + 1;
98 message = malloc(n);
99 if (!message)
100 return -1;
101
102 message->header.opcode = WESTON_LAUNCHER_OPEN;
103 message->flags = flags;
104 strcpy(message->path, path);
105
106 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700107 len = send(launcher->fd, message, n, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100108 } while (len < 0 && errno == EINTR);
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400109 free(message);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100110
111 memset(&msg, 0, sizeof msg);
112 iov.iov_base = &ret;
113 iov.iov_len = sizeof ret;
114 msg.msg_iov = &iov;
115 msg.msg_iovlen = 1;
116 msg.msg_control = control;
117 msg.msg_controllen = sizeof control;
118
119 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700120 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100121 } while (len < 0 && errno == EINTR);
122
123 if (len != sizeof ret ||
124 ret < 0)
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400125 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100126
127 cmsg = CMSG_FIRSTHDR(&msg);
128 if (!cmsg ||
129 cmsg->cmsg_level != SOL_SOCKET ||
130 cmsg->cmsg_type != SCM_RIGHTS) {
131 fprintf(stderr, "invalid control message\n");
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400132 return -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100133 }
134
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400135 data = (union cmsg_data *) CMSG_DATA(cmsg);
136 if (data->fd == -1) {
Martin Andersson566b4642013-02-13 00:11:12 +0100137 fprintf(stderr, "missing drm fd in socket request\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100138 return -1;
139 }
140
Kristian Høgsbergba25bd72012-04-10 01:31:09 -0400141 return data->fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100142}
143
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700144void
145weston_launcher_restore(struct weston_launcher *launcher)
146{
147 struct vt_mode mode = { 0 };
148
149 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
150 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
151 weston_log("failed to restore kb mode: %m\n");
152
153 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
154 weston_log("failed to set KD_TEXT mode on tty: %m\n");
155
156 mode.mode = VT_AUTO;
157 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
158 weston_log("could not reset vt handling\n");
159}
160
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700161static int
162weston_launcher_data(int fd, uint32_t mask, void *data)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100163{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700164 struct weston_launcher *launcher = data;
165 int len, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100166
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700167 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
168 weston_log("launcher socket closed, exiting\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700169 /* Normally the weston-launch will reset the tty, but
170 * in this case it died or something, so do it here so
171 * we don't end up with a stuck vt. */
172 weston_launcher_restore(launcher);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700173 exit(-1);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100174 }
175
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100176 do {
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700177 len = recv(launcher->fd, &ret, sizeof ret, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100178 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100179
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700180 switch (ret) {
181 case WESTON_LAUNCHER_ACTIVATE:
182 launcher->compositor->session_active = 1;
183 wl_signal_emit(&launcher->compositor->session_signal,
184 launcher->compositor);
185 break;
186 case WESTON_LAUNCHER_DEACTIVATE:
187 launcher->compositor->session_active = 0;
188 wl_signal_emit(&launcher->compositor->session_signal,
189 launcher->compositor);
190 break;
191 default:
192 weston_log("unexpected event from weston-launch\n");
193 break;
194 }
195
196 return 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100197}
198
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700199static int
200vt_handler(int signal_number, void *data)
201{
202 struct weston_launcher *launcher = data;
203 struct weston_compositor *compositor = launcher->compositor;
204
205 if (compositor->session_active) {
206 compositor->session_active = 0;
207 wl_signal_emit(&compositor->session_signal, compositor);
208 if (launcher->drm_fd != -1)
209 drmDropMaster(launcher->drm_fd);
210 ioctl(launcher->tty, VT_RELDISP, 1);
211 } else {
212 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
213 if (launcher->drm_fd != -1)
214 drmSetMaster(launcher->drm_fd);
215 compositor->session_active = 1;
216 wl_signal_emit(&compositor->session_signal, compositor);
217 }
218
219 return 1;
220}
221
222static int
223setup_tty(struct weston_launcher *launcher)
224{
225 struct wl_event_loop *loop;
226 struct vt_mode mode = { 0 };
227 struct stat buf;
228 int ret;
229
230 if (fstat(STDIN_FILENO, &buf) == -1 ||
231 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
232 weston_log("stdin not a vt\n");
233 return -1;
234 }
235
236 launcher->tty = STDIN_FILENO;
237 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
238 weston_log("failed to read keyboard mode: %m\n");
239 return -1;
240 }
241
242 if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
243 ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
244 weston_log("failed to set K_OFF keyboard mode: %m\n");
245 return -1;
246 }
247
248 ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
249 if (ret) {
250 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
251 return -1;
252 }
253
254 mode.mode = VT_PROCESS;
255 mode.relsig = SIGUSR1;
256 mode.acqsig = SIGUSR1;
257 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
258 weston_log("failed to take control of vt handling\n");
259 return -1;
260 }
261
262 loop = wl_display_get_event_loop(launcher->compositor->wl_display);
263 launcher->vt_source =
264 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
265 if (!launcher->vt_source)
266 return -1;
267
268 return 0;
269}
270
271int
272weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
273{
274 return ioctl(launcher->tty, VT_ACTIVATE, vt);
275}
276
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700277struct weston_launcher *
278weston_launcher_connect(struct weston_compositor *compositor)
279{
280 struct weston_launcher *launcher;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700281 struct wl_event_loop *loop;
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700282
283 launcher = malloc(sizeof *launcher);
284 if (launcher == NULL)
285 return NULL;
286
287 launcher->compositor = compositor;
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700288 launcher->drm_fd = -1;
289 launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
290 if (launcher->fd != -1) {
291 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
292 loop = wl_display_get_event_loop(compositor->wl_display);
293 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
294 WL_EVENT_READABLE,
295 weston_launcher_data,
296 launcher);
297 if (launcher->source == NULL) {
298 free(launcher);
299 return NULL;
300 }
301 } else if (geteuid() == 0) {
302 setup_tty(launcher);
303 } else {
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700304 free(launcher);
305 return NULL;
306 }
307
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700308 return launcher;
309}
310
311void
312weston_launcher_destroy(struct weston_launcher *launcher)
313{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700314 if (launcher->fd != -1) {
315 close(launcher->fd);
316 wl_event_source_remove(launcher->source);
317 } else {
318 weston_launcher_restore(launcher);
319 wl_event_source_remove(launcher->vt_source);
320 }
321
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700322 free(launcher);
323}