blob: 3d8f5f678d40db8cde5b9df405d6adc63b18ec93 [file] [log] [blame]
Jasper St. Pierre72dea062015-09-23 10:46:47 -07001/*
2 * Copyright © 2012 Benjamin Franzke
3 * Copyright © 2013 Intel Corporation
4 *
Yong Bakos53361532017-01-23 06:17:44 -08005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Jasper St. Pierre72dea062015-09-23 10:46:47 -070012 *
Yong Bakos53361532017-01-23 06:17:44 -080013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Jasper St. Pierre72dea062015-09-23 10:46:47 -070025 */
26
27#include "config.h"
28
29#include "compositor.h"
30
31#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <signal.h>
35#include <sys/stat.h>
Daniel Stonec4d7f662017-03-13 16:32:18 +000036#include <sys/sysmacros.h>
Jasper St. Pierre72dea062015-09-23 10:46:47 -070037#include <sys/ioctl.h>
38#include <linux/vt.h>
39#include <linux/kd.h>
40#include <linux/major.h>
41
42#include "launcher-impl.h"
43
44#define DRM_MAJOR 226
45
46#ifndef KDSKBMUTE
47#define KDSKBMUTE 0x4B51
48#endif
49
50#ifdef HAVE_LIBDRM
51
52#include <xf86drm.h>
53
54static inline int
55is_drm_master(int drm_fd)
56{
57 drm_magic_t magic;
58
59 return drmGetMagic(drm_fd, &magic) == 0 &&
60 drmAuthMagic(drm_fd, magic) == 0;
61}
62
63#else
64
65static inline int
66drmDropMaster(int drm_fd)
67{
68 return 0;
69}
70
71static inline int
72drmSetMaster(int drm_fd)
73{
74 return 0;
75}
76
77static inline int
78is_drm_master(int drm_fd)
79{
80 return 0;
81}
82
83#endif
84
85struct launcher_direct {
86 struct weston_launcher base;
87 struct weston_compositor *compositor;
88 int kb_mode, tty, drm_fd;
89 struct wl_event_source *vt_source;
90};
91
92static int
93vt_handler(int signal_number, void *data)
94{
95 struct launcher_direct *launcher = data;
96 struct weston_compositor *compositor = launcher->compositor;
97
98 if (compositor->session_active) {
99 compositor->session_active = 0;
100 wl_signal_emit(&compositor->session_signal, compositor);
101 drmDropMaster(launcher->drm_fd);
102 ioctl(launcher->tty, VT_RELDISP, 1);
103 } else {
104 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
105 drmSetMaster(launcher->drm_fd);
106 compositor->session_active = 1;
107 wl_signal_emit(&compositor->session_signal, compositor);
108 }
109
110 return 1;
111}
112
113static int
114setup_tty(struct launcher_direct *launcher, int tty)
115{
116 struct wl_event_loop *loop;
117 struct vt_mode mode = { 0 };
118 struct stat buf;
119 char tty_device[32] ="<stdin>";
120 int ret, kd_mode;
121
122 if (tty == 0) {
123 launcher->tty = dup(tty);
124 if (launcher->tty == -1) {
125 weston_log("couldn't dup stdin: %m\n");
126 return -1;
127 }
128 } else {
129 snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty);
130 launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC);
131 if (launcher->tty == -1) {
132 weston_log("couldn't open tty %s: %m\n", tty_device);
133 return -1;
134 }
135 }
136
137 if (fstat(launcher->tty, &buf) == -1 ||
138 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
139 weston_log("%s not a vt\n", tty_device);
140 weston_log("if running weston from ssh, "
141 "use --tty to specify a tty\n");
142 goto err_close;
143 }
144
145 ret = ioctl(launcher->tty, KDGETMODE, &kd_mode);
146 if (ret) {
147 weston_log("failed to get VT mode: %m\n");
148 return -1;
149 }
150 if (kd_mode != KD_TEXT) {
151 weston_log("%s is already in graphics mode, "
152 "is another display server running?\n", tty_device);
153 goto err_close;
154 }
155
156 ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
157 ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
158
159 if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
160 weston_log("failed to read keyboard mode: %m\n");
161 goto err_close;
162 }
163
164 if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
165 ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
166 weston_log("failed to set K_OFF keyboard mode: %m\n");
167 goto err_close;
168 }
169
170 ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
171 if (ret) {
172 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
173 goto err_close;
174 }
175
176 /*
177 * SIGRTMIN is used as global VT-acquire+release signal. Note that
178 * SIGRT* must be tested on runtime, as their exact values are not
179 * known at compile-time. POSIX requires 32 of them to be available.
180 */
181 if (SIGRTMIN > SIGRTMAX) {
182 weston_log("not enough RT signals available: %u-%u\n",
183 SIGRTMIN, SIGRTMAX);
184 ret = -EINVAL;
185 goto err_close;
186 }
187
188 mode.mode = VT_PROCESS;
189 mode.relsig = SIGRTMIN;
190 mode.acqsig = SIGRTMIN;
191 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
192 weston_log("failed to take control of vt handling\n");
193 goto err_close;
194 }
195
196 loop = wl_display_get_event_loop(launcher->compositor->wl_display);
197 launcher->vt_source =
198 wl_event_loop_add_signal(loop, SIGRTMIN, vt_handler, launcher);
199 if (!launcher->vt_source)
200 goto err_close;
201
202 return 0;
203
204 err_close:
205 close(launcher->tty);
206 return -1;
207}
208
209static int
210launcher_direct_open(struct weston_launcher *launcher_base, const char *path, int flags)
211{
212 struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
213 struct stat s;
214 int fd;
215
216 fd = open(path, flags | O_CLOEXEC);
217 if (fd == -1)
218 return -1;
219
220 if (fstat(fd, &s) == -1) {
221 close(fd);
222 return -1;
223 }
224
225 if (major(s.st_rdev) == DRM_MAJOR) {
226 launcher->drm_fd = fd;
227 if (!is_drm_master(fd)) {
228 weston_log("drm fd not master\n");
229 close(fd);
230 return -1;
231 }
232 }
233
234 return fd;
235}
236
237static void
238launcher_direct_close(struct weston_launcher *launcher_base, int fd)
239{
240 close(fd);
241}
242
243static void
244launcher_direct_restore(struct weston_launcher *launcher_base)
245{
246 struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
247 struct vt_mode mode = { 0 };
248
249 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
250 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
251 weston_log("failed to restore kb mode: %m\n");
252
253 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
254 weston_log("failed to set KD_TEXT mode on tty: %m\n");
255
256 /* We have to drop master before we switch the VT back in
257 * VT_AUTO, so we don't risk switching to a VT with another
258 * display server, that will then fail to set drm master. */
259 drmDropMaster(launcher->drm_fd);
260
261 mode.mode = VT_AUTO;
262 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
263 weston_log("could not reset vt handling\n");
264}
265
266static int
267launcher_direct_activate_vt(struct weston_launcher *launcher_base, int vt)
268{
269 struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
270 return ioctl(launcher->tty, VT_ACTIVATE, vt);
271}
272
273static int
274launcher_direct_connect(struct weston_launcher **out, struct weston_compositor *compositor,
275 int tty, const char *seat_id, bool sync_drm)
276{
277 struct launcher_direct *launcher;
278
279 if (geteuid() != 0)
280 return -EINVAL;
281
282 launcher = zalloc(sizeof(*launcher));
283 if (launcher == NULL)
284 return -ENOMEM;
285
286 launcher->base.iface = &launcher_direct_iface;
287 launcher->compositor = compositor;
288
289 if (setup_tty(launcher, tty) == -1) {
290 free(launcher);
291 return -1;
292 }
293
294 * (struct launcher_direct **) out = launcher;
295 return 0;
296}
297
298static void
299launcher_direct_destroy(struct weston_launcher *launcher_base)
300{
301 struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
302
303 launcher_direct_restore(&launcher->base);
304 wl_event_source_remove(launcher->vt_source);
305
306 if (launcher->tty >= 0)
307 close(launcher->tty);
308
309 free(launcher);
310}
311
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100312static int
313launcher_direct_get_vt(struct weston_launcher *base)
314{
315 struct launcher_direct *launcher = wl_container_of(base, launcher, base);
316 struct stat s;
317 if (fstat(launcher->tty, &s) < 0)
318 return -1;
319
320 return minor(s.st_rdev);
321}
322
Emil Velikov8f7201e2017-02-10 20:14:22 +0000323const struct launcher_interface launcher_direct_iface = {
Emil Velikov4d6eb172017-02-10 20:14:23 +0000324 .connect = launcher_direct_connect,
325 .destroy = launcher_direct_destroy,
326 .open = launcher_direct_open,
327 .close = launcher_direct_close,
328 .activate_vt = launcher_direct_activate_vt,
329 .restore = launcher_direct_restore,
330 .get_vt = launcher_direct_get_vt,
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700331};