• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Benjamin Franzke
3  * Copyright © 2013 Intel Corporation
4  *
5  * 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:
12  *
13  * 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.
25  */
26 
27 #include "config.h"
28 
29 #include <stdint.h>
30 #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 
47 #include <libweston/libweston.h>
48 #include "weston-launch.h"
49 #include "launcher-impl.h"
50 #include "shared/string-helpers.h"
51 
52 #define DRM_MAJOR 226
53 
54 #ifndef KDSKBMUTE
55 #define KDSKBMUTE	0x4B51
56 #endif
57 
58 #ifdef BUILD_DRM_COMPOSITOR
59 
60 #include <xf86drm.h>
61 
62 #else
63 
64 static inline int
drmDropMaster(int drm_fd)65 drmDropMaster(int drm_fd)
66 {
67 	return 0;
68 }
69 
70 static inline int
drmSetMaster(int drm_fd)71 drmSetMaster(int drm_fd)
72 {
73 	return 0;
74 }
75 
76 #endif
77 
78 /* 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
85 
86 union cmsg_data { unsigned char b[4]; int fd; };
87 
88 struct 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;
96 };
97 
98 static ssize_t
launcher_weston_launch_send(int sockfd,void * buf,size_t buflen)99 launcher_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 
110 static int
launcher_weston_launch_open(struct weston_launcher * launcher_base,const char * path,int flags)111 launcher_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 
133 	launcher_weston_launch_send(launcher->fd, message, n);
134 	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;
143 
144 	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 
169 static void
launcher_weston_launch_close(struct weston_launcher * launcher_base,int fd)170 launcher_weston_launch_close(struct weston_launcher *launcher_base, int fd)
171 {
172 	close(fd);
173 }
174 
175 static void
launcher_weston_launch_restore(struct weston_launcher * launcher_base)176 launcher_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))
183 		weston_log("failed to restore kb mode: %s\n",
184 			   strerror(errno));
185 
186 	if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
187 		weston_log("failed to set KD_TEXT mode on tty: %s\n",
188 			   strerror(errno));
189 
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 
200 static int
launcher_weston_launch_data(int fd,uint32_t mask,void * data)201 launcher_weston_launch_data(int fd, uint32_t mask, void *data)
202 {
203 	struct launcher_weston_launch *launcher = data;
204 	int len, ret, reply;
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:
221 		launcher->compositor->session_active = true;
222 		wl_signal_emit(&launcher->compositor->session_signal,
223 			       launcher->compositor);
224 		break;
225 	case WESTON_LAUNCHER_DEACTIVATE:
226 		launcher->compositor->session_active = false;
227 		wl_signal_emit(&launcher->compositor->session_signal,
228 			       launcher->compositor);
229 
230 		reply = WESTON_LAUNCHER_DEACTIVATE_DONE;
231 		launcher_weston_launch_send(launcher->fd, &reply, sizeof reply);
232 
233 		break;
234 	default:
235 		weston_log("unexpected event from weston-launch\n");
236 		break;
237 	}
238 
239 	return 1;
240 }
241 
242 static int
launcher_weston_launch_activate_vt(struct weston_launcher * launcher_base,int vt)243 launcher_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 
249 static int
launcher_weston_environment_get_fd(const char * env)250 launcher_weston_environment_get_fd(const char *env)
251 {
252 	char *e;
253 	int fd, flags;
254 
255 	e = getenv(env);
256 	if (!e || !safe_strtoint(e, &fd))
257 		return -1;
258 
259 	flags = fcntl(fd, F_GETFD);
260 	if (flags == -1)
261 		return -1;
262 
263 	fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
264 	unsetenv(env);
265 
266 	return fd;
267 }
268 
269 
270 static int
launcher_weston_launch_connect(struct weston_launcher ** out,struct weston_compositor * compositor,int tty,const char * seat_id,bool sync_drm)271 launcher_weston_launch_connect(struct weston_launcher **out, struct weston_compositor *compositor,
272 			       int tty, const char *seat_id, bool sync_drm)
273 {
274 	struct launcher_weston_launch *launcher;
275 	struct wl_event_loop *loop;
276 
277 	launcher = malloc(sizeof *launcher);
278 	if (launcher == NULL)
279 		return -ENOMEM;
280 
281 	launcher->base.iface = &launcher_weston_launch_iface;
282 	* (struct launcher_weston_launch **) out = launcher;
283 	launcher->compositor = compositor;
284 	launcher->drm_fd = -1;
285 	launcher->fd = launcher_weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
286 	if (launcher->fd != -1) {
287 		launcher->tty = launcher_weston_environment_get_fd("WESTON_TTY_FD");
288 		/* We don't get a chance to read out the original kb
289 		 * mode for the tty, so just hard code K_UNICODE here
290 		 * in case we have to clean if weston-launch dies. */
291 		launcher->kb_mode = K_UNICODE;
292 
293 		loop = wl_display_get_event_loop(compositor->wl_display);
294 		launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
295 							WL_EVENT_READABLE,
296 							launcher_weston_launch_data,
297 							launcher);
298 		if (launcher->source == NULL) {
299 			free(launcher);
300 			return -ENOMEM;
301 		}
302 
303 		return 0;
304 	} else {
305 		return -1;
306 	}
307 }
308 
309 static void
launcher_weston_launch_destroy(struct weston_launcher * launcher_base)310 launcher_weston_launch_destroy(struct weston_launcher *launcher_base)
311 {
312 	struct launcher_weston_launch *launcher = wl_container_of(launcher_base, launcher, base);
313 
314 	if (launcher->fd != -1) {
315 		close(launcher->fd);
316 		wl_event_source_remove(launcher->source);
317 	} else {
318 		launcher_weston_launch_restore(&launcher->base);
319 	}
320 
321 	if (launcher->tty >= 0)
322 		close(launcher->tty);
323 
324 	free(launcher);
325 }
326 
327 static int
launcher_weston_launch_get_vt(struct weston_launcher * base)328 launcher_weston_launch_get_vt(struct weston_launcher *base)
329 {
330 	struct launcher_weston_launch *launcher = wl_container_of(base, launcher, base);
331 	struct stat s;
332 	if (fstat(launcher->tty, &s) < 0)
333 		return -1;
334 
335 	return minor(s.st_rdev);
336 }
337 
338 const struct launcher_interface launcher_weston_launch_iface = {
339 	.connect = launcher_weston_launch_connect,
340 	.destroy = launcher_weston_launch_destroy,
341 	.open = launcher_weston_launch_open,
342 	.close = launcher_weston_launch_close,
343 	.activate_vt = launcher_weston_launch_activate_vt,
344 	.get_vt = launcher_weston_launch_get_vt,
345 };
346