blob: 109ac486670219db24cc989cbc547d78bba91e48 [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;
Jasper St. Pierre72dea062015-09-23 10:46:47 -070091 int fd;
92 struct wl_event_source *source;
93
94 int kb_mode, tty, drm_fd;
Jonathan Marlerf153c492021-03-27 09:55:59 -060095 int deferred_deactivate;
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
Jonathan Marlerf153c492021-03-27 09:55:59 -0600110static void
111handle_deactivate(struct launcher_weston_launch *launcher)
112{
113 int reply;
114
115 launcher->compositor->session_active = false;
116 wl_signal_emit(&launcher->compositor->session_signal,
117 launcher->compositor);
118
119 reply = WESTON_LAUNCHER_DEACTIVATE_DONE;
120 launcher_weston_launch_send(launcher->fd, &reply, sizeof reply);
121}
122
123static void
124idle_deactivate(void *data)
125{
126 struct launcher_weston_launch *launcher = data;
127
128 if (launcher->deferred_deactivate) {
129 launcher->deferred_deactivate = 0;
130 handle_deactivate((struct launcher_weston_launch*)data);
131 }
132}
133
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700134static int
135launcher_weston_launch_open(struct weston_launcher *launcher_base,
136 const char *path, int flags)
137{
138 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
Jonathan Marlerf153c492021-03-27 09:55:59 -0600139 int n;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700140 struct msghdr msg;
141 struct cmsghdr *cmsg;
142 struct iovec iov;
143 union cmsg_data *data;
144 char control[CMSG_SPACE(sizeof data->fd)];
145 ssize_t len;
146 struct weston_launcher_open *message;
Jonathan Marlerf153c492021-03-27 09:55:59 -0600147 struct { int id; int ret; } event;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700148
149 n = sizeof(*message) + strlen(path) + 1;
150 message = malloc(n);
151 if (!message)
152 return -1;
153
154 message->header.opcode = WESTON_LAUNCHER_OPEN;
155 message->flags = flags;
156 strcpy(message->path, path);
157
Stefan Agner10356a22020-01-22 22:40:19 +0100158 launcher_weston_launch_send(launcher->fd, message, n);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700159 free(message);
160
161 memset(&msg, 0, sizeof msg);
Jonathan Marlerf153c492021-03-27 09:55:59 -0600162 iov.iov_base = &event;
163 iov.iov_len = sizeof event;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700164 msg.msg_iov = &iov;
165 msg.msg_iovlen = 1;
166 msg.msg_control = control;
Yong Bakos53361532017-01-23 06:17:44 -0800167
Jonathan Marlerf153c492021-03-27 09:55:59 -0600168 while (1) {
169 msg.msg_controllen = sizeof control;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700170
Jonathan Marlerf153c492021-03-27 09:55:59 -0600171 do {
172 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
173 } while (len < 0 && errno == EINTR);
174
175 // Only OPEN_REPLY and up to one DEACTIVATE message should be possible here
176 if ((len == sizeof event) && (event.id == WESTON_LAUNCHER_OPEN_REPLY))
177 break;
178
179 if ((len == sizeof event.id) && (event.id == WESTON_LAUNCHER_DEACTIVATE) && (launcher->deferred_deactivate == 0)) {
180 wl_event_loop_add_idle(wl_display_get_event_loop(launcher->compositor->wl_display), idle_deactivate, launcher);
181 launcher->deferred_deactivate = 1;
182 } else {
183 weston_log("unexpected event %d (len=%zd) from weston-launch\n", event.id, len);
184 return -1;
185 }
186 }
187
188 if (event.ret < 0)
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700189 return -1;
190
191 cmsg = CMSG_FIRSTHDR(&msg);
192 if (!cmsg ||
193 cmsg->cmsg_level != SOL_SOCKET ||
194 cmsg->cmsg_type != SCM_RIGHTS) {
195 fprintf(stderr, "invalid control message\n");
196 return -1;
197 }
198
199 data = (union cmsg_data *) CMSG_DATA(cmsg);
200 if (data->fd == -1) {
201 fprintf(stderr, "missing drm fd in socket request\n");
202 return -1;
203 }
204
205 return data->fd;
206}
207
208static void
209launcher_weston_launch_close(struct weston_launcher *launcher_base, int fd)
210{
211 close(fd);
212}
213
214static void
215launcher_weston_launch_restore(struct weston_launcher *launcher_base)
216{
217 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
218 struct vt_mode mode = { 0 };
219
220 if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
221 ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
Antonio Borneo39578632019-04-26 23:57:31 +0200222 weston_log("failed to restore kb mode: %s\n",
223 strerror(errno));
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700224
225 if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
Antonio Borneo39578632019-04-26 23:57:31 +0200226 weston_log("failed to set KD_TEXT mode on tty: %s\n",
227 strerror(errno));
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700228
229 /* We have to drop master before we switch the VT back in
230 * VT_AUTO, so we don't risk switching to a VT with another
231 * display server, that will then fail to set drm master. */
232 drmDropMaster(launcher->drm_fd);
233
234 mode.mode = VT_AUTO;
235 if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
236 weston_log("could not reset vt handling\n");
237}
238
239static int
240launcher_weston_launch_data(int fd, uint32_t mask, void *data)
241{
242 struct launcher_weston_launch *launcher = data;
Jonathan Marlerf153c492021-03-27 09:55:59 -0600243 int len, ret;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700244
245 if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
246 weston_log("launcher socket closed, exiting\n");
247 /* Normally the weston-launch will reset the tty, but
248 * in this case it died or something, so do it here so
249 * we don't end up with a stuck vt. */
250 launcher_weston_launch_restore(&launcher->base);
251 exit(-1);
252 }
253
Jonathan Marlerf153c492021-03-27 09:55:59 -0600254 if (launcher->deferred_deactivate) {
255 launcher->deferred_deactivate = 0;
256 handle_deactivate(launcher);
257 return 1;
258 }
259
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700260 do {
261 len = recv(launcher->fd, &ret, sizeof ret, 0);
262 } while (len < 0 && errno == EINTR);
263
264 switch (ret) {
265 case WESTON_LAUNCHER_ACTIVATE:
Robert Beckettc569bdc2019-07-10 16:40:12 +0100266 launcher->compositor->session_active = true;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700267 wl_signal_emit(&launcher->compositor->session_signal,
268 launcher->compositor);
269 break;
270 case WESTON_LAUNCHER_DEACTIVATE:
Jonathan Marlerf153c492021-03-27 09:55:59 -0600271 handle_deactivate(launcher);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700272 break;
273 default:
274 weston_log("unexpected event from weston-launch\n");
275 break;
276 }
277
278 return 1;
279}
280
281static int
282launcher_weston_launch_activate_vt(struct weston_launcher *launcher_base, int vt)
283{
284 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
285 return ioctl(launcher->tty, VT_ACTIVATE, vt);
286}
287
288static int
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500289launcher_weston_environment_get_fd(const char *env)
290{
291 char *e;
292 int fd, flags;
293
294 e = getenv(env);
Anurup Maa7de332020-12-03 07:55:04 +0530295 if (!e || !safe_strtoint(e, &fd)) {
296 weston_log("could not get launcher fd from env\n");
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500297 return -1;
Anurup Maa7de332020-12-03 07:55:04 +0530298 }
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500299
300 flags = fcntl(fd, F_GETFD);
Anurup Maa7de332020-12-03 07:55:04 +0530301 if (flags == -1) {
302 weston_log("could not get fd flags!, env: %s, error: %s\n",
303 env, strerror(errno));
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500304 return -1;
Anurup Maa7de332020-12-03 07:55:04 +0530305 }
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500306
307 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
308 unsetenv(env);
309
310 return fd;
311}
312
313
314static int
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700315launcher_weston_launch_connect(struct weston_launcher **out, struct weston_compositor *compositor,
316 int tty, const char *seat_id, bool sync_drm)
317{
318 struct launcher_weston_launch *launcher;
319 struct wl_event_loop *loop;
320
321 launcher = malloc(sizeof *launcher);
322 if (launcher == NULL)
323 return -ENOMEM;
324
325 launcher->base.iface = &launcher_weston_launch_iface;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700326 launcher->compositor = compositor;
327 launcher->drm_fd = -1;
Jonathan Marlerf153c492021-03-27 09:55:59 -0600328 launcher->deferred_deactivate = 0;
Guillaume Champagneb4bd12b2020-01-27 20:08:15 -0500329 launcher->fd = launcher_weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Leandro Ribeirod42fa302021-06-18 20:34:45 -0300330 if (launcher->fd == -1) {
331 free(launcher);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700332 return -1;
333 }
Leandro Ribeirod42fa302021-06-18 20:34:45 -0300334
335 launcher->tty = launcher_weston_environment_get_fd("WESTON_TTY_FD");
336 /* We don't get a chance to read out the original kb
337 * mode for the tty, so just hard code K_UNICODE here
338 * in case we have to clean if weston-launch dies. */
339 launcher->kb_mode = K_UNICODE;
340
341 loop = wl_display_get_event_loop(compositor->wl_display);
342 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
343 WL_EVENT_READABLE,
344 launcher_weston_launch_data,
345 launcher);
346 if (launcher->source == NULL) {
347 free(launcher);
348 weston_log("failed to get weston-launcher socket fd event source\n");
349 return -ENOMEM;
350 }
351
352 * (struct launcher_weston_launch **) out = launcher;
353
354 return 0;
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700355}
356
357static void
358launcher_weston_launch_destroy(struct weston_launcher *launcher_base)
359{
360 struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
361
362 if (launcher->fd != -1) {
363 close(launcher->fd);
364 wl_event_source_remove(launcher->source);
365 } else {
366 launcher_weston_launch_restore(&launcher->base);
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700367 }
368
369 if (launcher->tty >= 0)
370 close(launcher->tty);
371
372 free(launcher);
373}
374
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100375static int
376launcher_weston_launch_get_vt(struct weston_launcher *base)
377{
378 struct launcher_weston_launch *launcher = wl_container_of(base, launcher, base);
379 struct stat s;
Anurup Maa7de332020-12-03 07:55:04 +0530380 if (fstat(launcher->tty, &s) < 0) {
381 weston_log("could not fstat launcher tty: %s\n", strerror(errno));
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100382 return -1;
Anurup Maa7de332020-12-03 07:55:04 +0530383 }
Giulio Camuffoa32986e2016-12-05 14:50:36 +0100384
385 return minor(s.st_rdev);
386}
387
Emil Velikov8f7201e2017-02-10 20:14:22 +0000388const struct launcher_interface launcher_weston_launch_iface = {
Anurup Maa7de332020-12-03 07:55:04 +0530389 .name = "weston_launch",
Emil Velikov4d6eb172017-02-10 20:14:23 +0000390 .connect = launcher_weston_launch_connect,
391 .destroy = launcher_weston_launch_destroy,
392 .open = launcher_weston_launch_open,
393 .close = launcher_weston_launch_close,
394 .activate_vt = launcher_weston_launch_activate_vt,
Emil Velikov4d6eb172017-02-10 20:14:23 +0000395 .get_vt = launcher_weston_launch_get_vt,
Jasper St. Pierre72dea062015-09-23 10:46:47 -0700396};