blob: aad8b16e210777ecbd3b15ae9a5cf6132900bbef [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"
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -050050#include "shared/string-helpers.h"
Jasper St. Pierre72dea062015-09-23 10:46:47 -070051
52#define DRM_MAJOR 226
53
54#ifndef KDSKBMUTE
55#define KDSKBMUTE 0x4B51
56#endif
57
Pekka Paalanen2667e9e2017-04-06 13:18:59 +030058#ifdef BUILD_DRM_COMPOSITOR
Jasper St. Pierre72dea062015-09-23 10:46:47 -070059
60#include <xf86drm.h>
61
Jasper St. Pierre72dea062015-09-23 10:46:47 -070062#else
63
64static inline int
65drmDropMaster(int drm_fd)
66{
67 return 0;
68}
69
70static inline int
71drmSetMaster(int drm_fd)
72{
73 return 0;
74}
75
Jasper St. Pierre72dea062015-09-23 10:46:47 -070076#endif
77
Sergei Trofimovich43c5a652017-05-31 22:17:50 +010078/* major()/minor() */
79#ifdef MAJOR_IN_MKDEV
80#include <sys/mkdev.h>
81#endif
82#ifdef MAJOR_IN_SYSMACROS
83#include <sys/sysmacros.h>
84#endif
Jasper St. Pierre72dea062015-09-23 10:46:47 -070085
86union cmsg_data { unsigned char b[4]; int fd; };
87
88struct launcher_weston_launch {
89 struct weston_launcher base;
90 struct weston_compositor *compositor;
91 struct wl_event_loop *loop;
92 int fd;
93 struct wl_event_source *source;
94
95 int kb_mode, tty, drm_fd;
Jasper St. Pierre72dea062015-09-23 10:46:47 -070096};
97
Stefan Agner10356a22020-01-22 22:40:19 +010098static ssize_t
99launcher_weston_launch_send(int sockfd, void *buf, size_t buflen)
100{
101 ssize_t len;
102
103 do {
104 len = send(sockfd, buf, buflen, 0);
105 } while (len < 0 && errno == EINTR);
106
107 return len;
108}
109
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700110static int
111launcher_weston_launch_open(struct weston_launcher *launcher_base,
112 const char *path, int flags)
113{
114 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
115 int n, ret;
116 struct msghdr msg;
117 struct cmsghdr *cmsg;
118 struct iovec iov;
119 union cmsg_data *data;
120 char control[CMSG_SPACE(sizeof data->fd)];
121 ssize_t len;
122 struct weston_launcher_open *message;
123
124 n = sizeof(*message) + strlen(path) + 1;
125 message = malloc(n);
126 if (!message)
127 return -1;
128
129 message->header.opcode = WESTON_LAUNCHER_OPEN;
130 message->flags = flags;
131 strcpy(message->path, path);
132
Stefan Agner10356a22020-01-22 22:40:19 +0100133 launcher_weston_launch_send(launcher->fd, message, n);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700134 free(message);
135
136 memset(&msg, 0, sizeof msg);
137 iov.iov_base = &ret;
138 iov.iov_len = sizeof ret;
139 msg.msg_iov = &iov;
140 msg.msg_iovlen = 1;
141 msg.msg_control = control;
142 msg.msg_controllen = sizeof control;
Yong Bakos53361532017-01-23 06:17:44 -0800143
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700144 do {
145 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
146 } while (len < 0 && errno == EINTR);
147
148 if (len != sizeof ret ||
149 ret < 0)
150 return -1;
151
152 cmsg = CMSG_FIRSTHDR(&msg);
153 if (!cmsg ||
154 cmsg->cmsg_level != SOL_SOCKET ||
155 cmsg->cmsg_type != SCM_RIGHTS) {
156 fprintf(stderr, "invalid control message\n");
157 return -1;
158 }
159
160 data = (union cmsg_data *) CMSG_DATA(cmsg);
161 if (data->fd == -1) {
162 fprintf(stderr, "missing drm fd in socket request\n");
163 return -1;
164 }
165
166 return data->fd;
167}
168
169static void
170launcher_weston_launch_close(struct weston_launcher *launcher_base, int fd)
171{
172 close(fd);
173}
174
175static void
176launcher_weston_launch_restore(struct weston_launcher *launcher_base)
177{
178 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
179 struct vt_mode mode = { 0 };
180
181 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
182 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
Antonio Borneo39578632019-04-26 23:57:31 +0200183 weston_log("failed to restore kb mode: %s\n",
184 strerror(errno));
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700185
186 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
Antonio Borneo39578632019-04-26 23:57:31 +0200187 weston_log("failed to set KD_TEXT mode on tty: %s\n",
188 strerror(errno));
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700189
190 /* We have to drop master before we switch the VT back in
191 * VT_AUTO, so we don't risk switching to a VT with another
192 * display server, that will then fail to set drm master. */
193 drmDropMaster(launcher->drm_fd);
194
195 mode.mode = VT_AUTO;
196 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
197 weston_log("could not reset vt handling\n");
198}
199
200static int
201launcher_weston_launch_data(int fd, uint32_t mask, void *data)
202{
203 struct launcher_weston_launch *launcher = data;
204 int len, ret;
205
206 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
207 weston_log("launcher socket closed, exiting\n");
208 /* Normally the weston-launch will reset the tty, but
209 * in this case it died or something, so do it here so
210 * we don't end up with a stuck vt. */
211 launcher_weston_launch_restore(&launcher->base);
212 exit(-1);
213 }
214
215 do {
216 len = recv(launcher->fd, &ret, sizeof ret, 0);
217 } while (len < 0 && errno == EINTR);
218
219 switch (ret) {
220 case WESTON_LAUNCHER_ACTIVATE:
Robert Beckettc569bdc2019-07-10 16:40:12 +0100221 launcher->compositor->session_active = true;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700222 wl_signal_emit(&launcher->compositor->session_signal,
223 launcher->compositor);
224 break;
225 case WESTON_LAUNCHER_DEACTIVATE:
Robert Beckettc569bdc2019-07-10 16:40:12 +0100226 launcher->compositor->session_active = false;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700227 wl_signal_emit(&launcher->compositor->session_signal,
228 launcher->compositor);
229 break;
230 default:
231 weston_log("unexpected event from weston-launch\n");
232 break;
233 }
234
235 return 1;
236}
237
238static int
239launcher_weston_launch_activate_vt(struct weston_launcher *launcher_base, int vt)
240{
241 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
242 return ioctl(launcher->tty, VT_ACTIVATE, vt);
243}
244
245static int
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500246launcher_weston_environment_get_fd(const char *env)
247{
248 char *e;
249 int fd, flags;
250
251 e = getenv(env);
252 if (!e || !safe_strtoint(e, &fd))
253 return -1;
254
255 flags = fcntl(fd, F_GETFD);
256 if (flags == -1)
257 return -1;
258
259 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
260 unsetenv(env);
261
262 return fd;
263}
264
265
266static int
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700267launcher_weston_launch_connect(struct weston_launcher **out, struct weston_compositor *compositor,
268 int tty, const char *seat_id, bool sync_drm)
269{
270 struct launcher_weston_launch *launcher;
271 struct wl_event_loop *loop;
272
273 launcher = malloc(sizeof *launcher);
274 if (launcher == NULL)
275 return -ENOMEM;
276
277 launcher->base.iface = &launcher_weston_launch_iface;
278 * (struct launcher_weston_launch **) out = launcher;
279 launcher->compositor = compositor;
280 launcher->drm_fd = -1;
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500281 launcher->fd = launcher_weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700282 if (launcher->fd != -1) {
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500283 launcher->tty = launcher_weston_environment_get_fd("WESTON_TTY_FD");
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700284 /* We don't get a chance to read out the original kb
285 * mode for the tty, so just hard code K_UNICODE here
286 * in case we have to clean if weston-launch dies. */
287 launcher->kb_mode = K_UNICODE;
288
289 loop = wl_display_get_event_loop(compositor->wl_display);
290 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
291 WL_EVENT_READABLE,
292 launcher_weston_launch_data,
293 launcher);
294 if (launcher->source == NULL) {
295 free(launcher);
296 return -ENOMEM;
297 }
298
299 return 0;
300 } else {
301 return -1;
302 }
303}
304
305static void
306launcher_weston_launch_destroy(struct weston_launcher *launcher_base)
307{
308 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
309
310 if (launcher->fd != -1) {
311 close(launcher->fd);
312 wl_event_source_remove(launcher->source);
313 } else {
314 launcher_weston_launch_restore(&launcher->base);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700315 }
316
317 if (launcher->tty >= 0)
318 close(launcher->tty);
319
320 free(launcher);
321}
322
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100323static int
324launcher_weston_launch_get_vt(struct weston_launcher *base)
325{
326 struct launcher_weston_launch *launcher = wl_container_of(base, launcher, base);
327 struct stat s;
328 if (fstat(launcher->tty, &s) < 0)
329 return -1;
330
331 return minor(s.st_rdev);
332}
333
Emil Velikov8f7201e2017-02-10 20:14:22 +0000334const struct launcher_interface launcher_weston_launch_iface = {
Emil Velikov4d6eb172017-02-10 20:14:23 +0000335 .connect = launcher_weston_launch_connect,
336 .destroy = launcher_weston_launch_destroy,
337 .open = launcher_weston_launch_open,
338 .close = launcher_weston_launch_close,
339 .activate_vt = launcher_weston_launch_activate_vt,
Emil Velikov4d6eb172017-02-10 20:14:23 +0000340 .get_vt = launcher_weston_launch_get_vt,
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700341};