blob: c7db8da79a5a6bca171dd8f1733e86ce0217b03a [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;
Stefan Agnerda0cd682019-11-14 23:55:35 +0100204 int len, ret, reply;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700205
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);
Stefan Agnerda0cd682019-11-14 23:55:35 +0100229
230 reply = WESTON_LAUNCHER_DEACTIVATE_DONE;
231 launcher_weston_launch_send(launcher->fd, &reply, sizeof reply);
232
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700233 break;
234 default:
235 weston_log("unexpected event from weston-launch\n");
236 break;
237 }
238
239 return 1;
240}
241
242static int
243launcher_weston_launch_activate_vt(struct weston_launcher *launcher_base, int vt)
244{
245 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
246 return ioctl(launcher->tty, VT_ACTIVATE, vt);
247}
248
249static int
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500250launcher_weston_environment_get_fd(const char *env)
251{
252 char *e;
253 int fd, flags;
254
255 e = getenv(env);
Anurup Maa7de332020-12-03 07:55:04 +0530256 if (!e || !safe_strtoint(e, &fd)) {
257 weston_log("could not get launcher fd from env\n");
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500258 return -1;
Anurup Maa7de332020-12-03 07:55:04 +0530259 }
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500260
261 flags = fcntl(fd, F_GETFD);
Anurup Maa7de332020-12-03 07:55:04 +0530262 if (flags == -1) {
263 weston_log("could not get fd flags!, env: %s, error: %s\n",
264 env, strerror(errno));
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500265 return -1;
Anurup Maa7de332020-12-03 07:55:04 +0530266 }
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500267
268 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
269 unsetenv(env);
270
271 return fd;
272}
273
274
275static int
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700276launcher_weston_launch_connect(struct weston_launcher **out, struct weston_compositor *compositor,
277 int tty, const char *seat_id, bool sync_drm)
278{
279 struct launcher_weston_launch *launcher;
280 struct wl_event_loop *loop;
281
282 launcher = malloc(sizeof *launcher);
283 if (launcher == NULL)
284 return -ENOMEM;
285
286 launcher->base.iface = &launcher_weston_launch_iface;
287 * (struct launcher_weston_launch **) out = launcher;
288 launcher->compositor = compositor;
289 launcher->drm_fd = -1;
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500290 launcher->fd = launcher_weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700291 if (launcher->fd != -1) {
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500292 launcher->tty = launcher_weston_environment_get_fd("WESTON_TTY_FD");
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700293 /* We don't get a chance to read out the original kb
294 * mode for the tty, so just hard code K_UNICODE here
295 * in case we have to clean if weston-launch dies. */
296 launcher->kb_mode = K_UNICODE;
297
298 loop = wl_display_get_event_loop(compositor->wl_display);
299 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
300 WL_EVENT_READABLE,
301 launcher_weston_launch_data,
302 launcher);
303 if (launcher->source == NULL) {
304 free(launcher);
Anurup Maa7de332020-12-03 07:55:04 +0530305 weston_log("failed to get weston-launcher socket fd event source\n");
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700306 return -ENOMEM;
307 }
308
309 return 0;
310 } else {
311 return -1;
312 }
313}
314
315static void
316launcher_weston_launch_destroy(struct weston_launcher *launcher_base)
317{
318 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
319
320 if (launcher->fd != -1) {
321 close(launcher->fd);
322 wl_event_source_remove(launcher->source);
323 } else {
324 launcher_weston_launch_restore(&launcher->base);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700325 }
326
327 if (launcher->tty >= 0)
328 close(launcher->tty);
329
330 free(launcher);
331}
332
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100333static int
334launcher_weston_launch_get_vt(struct weston_launcher *base)
335{
336 struct launcher_weston_launch *launcher = wl_container_of(base, launcher, base);
337 struct stat s;
Anurup Maa7de332020-12-03 07:55:04 +0530338 if (fstat(launcher->tty, &s) < 0) {
339 weston_log("could not fstat launcher tty: %s\n", strerror(errno));
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100340 return -1;
Anurup Maa7de332020-12-03 07:55:04 +0530341 }
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100342
343 return minor(s.st_rdev);
344}
345
Emil Velikov8f7201e2017-02-10 20:14:22 +0000346const struct launcher_interface launcher_weston_launch_iface = {
Anurup Maa7de332020-12-03 07:55:04 +0530347 .name = "weston_launch",
Emil Velikov4d6eb172017-02-10 20:14:23 +0000348 .connect = launcher_weston_launch_connect,
349 .destroy = launcher_weston_launch_destroy,
350 .open = launcher_weston_launch_open,
351 .close = launcher_weston_launch_close,
352 .activate_vt = launcher_weston_launch_activate_vt,
Emil Velikov4d6eb172017-02-10 20:14:23 +0000353 .get_vt = launcher_weston_launch_get_vt,
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700354};