blob: c811a5008a842c0e983e29eb3207ade845f1064e [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
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Jasper St. Pierre72dea062015-09-23 10:46:47 -070030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include <errno.h>
35#include <signal.h>
36#include <sys/socket.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <sys/uio.h>
40#include <sys/ioctl.h>
41#include <fcntl.h>
42#include <unistd.h>
43#include <linux/vt.h>
44#include <linux/kd.h>
45#include <linux/major.h>
46
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020047#include <libweston/libweston.h>
Jasper St. Pierre72dea062015-09-23 10:46:47 -070048#include "weston-launch.h"
49#include "launcher-impl.h"
50
51#define DRM_MAJOR 226
52
53#ifndef KDSKBMUTE
54#define KDSKBMUTE 0x4B51
55#endif
56
Pekka Paalanen2667e9e2017-04-06 13:18:59 +030057#ifdef BUILD_DRM_COMPOSITOR
Jasper St. Pierre72dea062015-09-23 10:46:47 -070058
59#include <xf86drm.h>
60
Jasper St. Pierre72dea062015-09-23 10:46:47 -070061#else
62
63static inline int
64drmDropMaster(int drm_fd)
65{
66 return 0;
67}
68
69static inline int
70drmSetMaster(int drm_fd)
71{
72 return 0;
73}
74
Jasper St. Pierre72dea062015-09-23 10:46:47 -070075#endif
76
Sergei Trofimovich43c5a652017-05-31 22:17:50 +010077/* major()/minor() */
78#ifdef MAJOR_IN_MKDEV
79#include <sys/mkdev.h>
80#endif
81#ifdef MAJOR_IN_SYSMACROS
82#include <sys/sysmacros.h>
83#endif
Jasper St. Pierre72dea062015-09-23 10:46:47 -070084
85union cmsg_data { unsigned char b[4]; int fd; };
86
87struct launcher_weston_launch {
88 struct weston_launcher base;
89 struct weston_compositor *compositor;
90 struct wl_event_loop *loop;
91 int fd;
92 struct wl_event_source *source;
93
94 int kb_mode, tty, drm_fd;
Jasper St. Pierre72dea062015-09-23 10:46:47 -070095};
96
97static int
98launcher_weston_launch_open(struct weston_launcher *launcher_base,
99 const char *path, int flags)
100{
101 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
102 int n, ret;
103 struct msghdr msg;
104 struct cmsghdr *cmsg;
105 struct iovec iov;
106 union cmsg_data *data;
107 char control[CMSG_SPACE(sizeof data->fd)];
108 ssize_t len;
109 struct weston_launcher_open *message;
110
111 n = sizeof(*message) + strlen(path) + 1;
112 message = malloc(n);
113 if (!message)
114 return -1;
115
116 message->header.opcode = WESTON_LAUNCHER_OPEN;
117 message->flags = flags;
118 strcpy(message->path, path);
119
120 do {
121 len = send(launcher->fd, message, n, 0);
122 } while (len < 0 && errno == EINTR);
123 free(message);
124
125 memset(&msg, 0, sizeof msg);
126 iov.iov_base = &ret;
127 iov.iov_len = sizeof ret;
128 msg.msg_iov = &iov;
129 msg.msg_iovlen = 1;
130 msg.msg_control = control;
131 msg.msg_controllen = sizeof control;
Yong Bakos53361532017-01-23 06:17:44 -0800132
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700133 do {
134 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
135 } while (len < 0 && errno == EINTR);
136
137 if (len != sizeof ret ||
138 ret < 0)
139 return -1;
140
141 cmsg = CMSG_FIRSTHDR(&msg);
142 if (!cmsg ||
143 cmsg->cmsg_level != SOL_SOCKET ||
144 cmsg->cmsg_type != SCM_RIGHTS) {
145 fprintf(stderr, "invalid control message\n");
146 return -1;
147 }
148
149 data = (union cmsg_data *) CMSG_DATA(cmsg);
150 if (data->fd == -1) {
151 fprintf(stderr, "missing drm fd in socket request\n");
152 return -1;
153 }
154
155 return data->fd;
156}
157
158static void
159launcher_weston_launch_close(struct weston_launcher *launcher_base, int fd)
160{
161 close(fd);
162}
163
164static void
165launcher_weston_launch_restore(struct weston_launcher *launcher_base)
166{
167 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
168 struct vt_mode mode = { 0 };
169
170 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
171 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
172 weston_log("failed to restore kb mode: %m\n");
173
174 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
175 weston_log("failed to set KD_TEXT mode on tty: %m\n");
176
177 /* We have to drop master before we switch the VT back in
178 * VT_AUTO, so we don't risk switching to a VT with another
179 * display server, that will then fail to set drm master. */
180 drmDropMaster(launcher->drm_fd);
181
182 mode.mode = VT_AUTO;
183 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
184 weston_log("could not reset vt handling\n");
185}
186
187static int
188launcher_weston_launch_data(int fd, uint32_t mask, void *data)
189{
190 struct launcher_weston_launch *launcher = data;
191 int len, ret;
192
193 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
194 weston_log("launcher socket closed, exiting\n");
195 /* Normally the weston-launch will reset the tty, but
196 * in this case it died or something, so do it here so
197 * we don't end up with a stuck vt. */
198 launcher_weston_launch_restore(&launcher->base);
199 exit(-1);
200 }
201
202 do {
203 len = recv(launcher->fd, &ret, sizeof ret, 0);
204 } while (len < 0 && errno == EINTR);
205
206 switch (ret) {
207 case WESTON_LAUNCHER_ACTIVATE:
208 launcher->compositor->session_active = 1;
209 wl_signal_emit(&launcher->compositor->session_signal,
210 launcher->compositor);
211 break;
212 case WESTON_LAUNCHER_DEACTIVATE:
213 launcher->compositor->session_active = 0;
214 wl_signal_emit(&launcher->compositor->session_signal,
215 launcher->compositor);
216 break;
217 default:
218 weston_log("unexpected event from weston-launch\n");
219 break;
220 }
221
222 return 1;
223}
224
225static int
226launcher_weston_launch_activate_vt(struct weston_launcher *launcher_base, int vt)
227{
228 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
229 return ioctl(launcher->tty, VT_ACTIVATE, vt);
230}
231
232static int
233launcher_weston_launch_connect(struct weston_launcher **out, struct weston_compositor *compositor,
234 int tty, const char *seat_id, bool sync_drm)
235{
236 struct launcher_weston_launch *launcher;
237 struct wl_event_loop *loop;
238
239 launcher = malloc(sizeof *launcher);
240 if (launcher == NULL)
241 return -ENOMEM;
242
243 launcher->base.iface = &launcher_weston_launch_iface;
244 * (struct launcher_weston_launch **) out = launcher;
245 launcher->compositor = compositor;
246 launcher->drm_fd = -1;
Derek Foremane7d868a2015-09-24 14:30:38 -0500247 launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700248 if (launcher->fd != -1) {
249 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
250 /* We don't get a chance to read out the original kb
251 * mode for the tty, so just hard code K_UNICODE here
252 * in case we have to clean if weston-launch dies. */
253 launcher->kb_mode = K_UNICODE;
254
255 loop = wl_display_get_event_loop(compositor->wl_display);
256 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
257 WL_EVENT_READABLE,
258 launcher_weston_launch_data,
259 launcher);
260 if (launcher->source == NULL) {
261 free(launcher);
262 return -ENOMEM;
263 }
264
265 return 0;
266 } else {
267 return -1;
268 }
269}
270
271static void
272launcher_weston_launch_destroy(struct weston_launcher *launcher_base)
273{
274 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
275
276 if (launcher->fd != -1) {
277 close(launcher->fd);
278 wl_event_source_remove(launcher->source);
279 } else {
280 launcher_weston_launch_restore(&launcher->base);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700281 }
282
283 if (launcher->tty >= 0)
284 close(launcher->tty);
285
286 free(launcher);
287}
288
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100289static int
290launcher_weston_launch_get_vt(struct weston_launcher *base)
291{
292 struct launcher_weston_launch *launcher = wl_container_of(base, launcher, base);
293 struct stat s;
294 if (fstat(launcher->tty, &s) < 0)
295 return -1;
296
297 return minor(s.st_rdev);
298}
299
Emil Velikov8f7201e2017-02-10 20:14:22 +0000300const struct launcher_interface launcher_weston_launch_iface = {
Emil Velikov4d6eb172017-02-10 20:14:23 +0000301 .connect = launcher_weston_launch_connect,
302 .destroy = launcher_weston_launch_destroy,
303 .open = launcher_weston_launch_open,
304 .close = launcher_weston_launch_close,
305 .activate_vt = launcher_weston_launch_activate_vt,
Emil Velikov4d6eb172017-02-10 20:14:23 +0000306 .get_vt = launcher_weston_launch_get_vt,
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700307};