Multiseat support for drm/wayland backends
[platform/upstream/weston.git] / src / launcher-util.c
1 /*
2  * Copyright © 2012 Benjamin Franzke
3  * Copyright © 2013 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <errno.h>
31 #include <signal.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/uio.h>
36 #include <sys/ioctl.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <linux/vt.h>
40 #include <linux/kd.h>
41 #include <linux/major.h>
42
43 #include "compositor.h"
44 #include "launcher-util.h"
45 #include "logind-util.h"
46 #include "weston-launch.h"
47
48 #define DRM_MAJOR 226
49
50 #ifndef KDSKBMUTE
51 #define KDSKBMUTE       0x4B51
52 #endif
53
54 #ifdef HAVE_LIBDRM
55
56 #include <xf86drm.h>
57
58 static inline int
59 is_drm_master(int drm_fd)
60 {
61         drm_magic_t magic;
62
63         return drmGetMagic(drm_fd, &magic) == 0 &&
64                 drmAuthMagic(drm_fd, magic) == 0;
65 }
66
67 #else
68
69 static inline int
70 drmDropMaster(int drm_fd)
71 {
72         return 0;
73 }
74
75 static inline int
76 drmSetMaster(int drm_fd)
77 {
78         return 0;
79 }
80
81 static inline int
82 is_drm_master(int drm_fd)
83 {
84         return 0;
85 }
86
87 #endif
88
89
90 union cmsg_data { unsigned char b[4]; int fd; };
91
92 struct weston_launcher {
93         struct weston_compositor *compositor;
94         struct weston_logind *logind;
95         struct wl_event_loop *loop;
96         int fd;
97         struct wl_event_source *source;
98
99         int kb_mode, tty, drm_fd;
100         struct wl_event_source *vt_source;
101 };
102
103 int
104 weston_launcher_open(struct weston_launcher *launcher,
105                      const char *path, int flags)
106 {
107         int n, fd, ret = -1;
108         struct msghdr msg;
109         struct cmsghdr *cmsg;
110         struct iovec iov;
111         union cmsg_data *data;
112         char control[CMSG_SPACE(sizeof data->fd)];
113         ssize_t len;
114         struct weston_launcher_open *message;
115         struct stat s;
116
117         if (launcher->logind)
118                 return weston_logind_open(launcher->logind, path, flags);
119
120         if (launcher->fd == -1) {
121                 fd = open(path, flags | O_CLOEXEC);
122                 if (fd == -1)
123                         return -1;
124
125                 if (fstat(fd, &s) == -1) {
126                         close(fd);
127                         return -1;
128                 }
129
130                 if (major(s.st_rdev) == DRM_MAJOR) {
131                         launcher->drm_fd = fd;
132                         if (!is_drm_master(fd)) {
133                                 weston_log("drm fd not master\n");
134                                 close(fd);
135                                 return -1;
136                         }
137                 }
138
139                 return fd;
140         }
141
142         n = sizeof(*message) + strlen(path) + 1;
143         message = malloc(n);
144         if (!message)
145                 return -1;
146
147         message->header.opcode = WESTON_LAUNCHER_OPEN;
148         message->flags = flags;
149         strcpy(message->path, path);
150
151         do {
152                 len = send(launcher->fd, message, n, 0);
153         } while (len < 0 && errno == EINTR);
154         free(message);
155
156         memset(&msg, 0, sizeof msg);
157         iov.iov_base = &ret;
158         iov.iov_len = sizeof ret;
159         msg.msg_iov = &iov;
160         msg.msg_iovlen = 1;
161         msg.msg_control = control;
162         msg.msg_controllen = sizeof control;
163         
164         do {
165                 len = recvmsg(launcher->fd, &msg, MSG_CMSG_CLOEXEC);
166         } while (len < 0 && errno == EINTR);
167
168         if (len != sizeof ret ||
169             ret < 0)
170                 return -1;
171
172         cmsg = CMSG_FIRSTHDR(&msg);
173         if (!cmsg ||
174             cmsg->cmsg_level != SOL_SOCKET ||
175             cmsg->cmsg_type != SCM_RIGHTS) {
176                 fprintf(stderr, "invalid control message\n");
177                 return -1;
178         }
179
180         data = (union cmsg_data *) CMSG_DATA(cmsg);
181         if (data->fd == -1) {
182                 fprintf(stderr, "missing drm fd in socket request\n");
183                 return -1;
184         }
185
186         return data->fd;
187 }
188
189 void
190 weston_launcher_close(struct weston_launcher *launcher, int fd)
191 {
192         if (launcher->logind)
193                 return weston_logind_close(launcher->logind, fd);
194
195         close(fd);
196 }
197
198 void
199 weston_launcher_restore(struct weston_launcher *launcher)
200 {
201         struct vt_mode mode = { 0 };
202
203         if (launcher->logind)
204                 return weston_logind_restore(launcher->logind);
205
206         if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
207             ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
208                 weston_log("failed to restore kb mode: %m\n");
209
210         if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
211                 weston_log("failed to set KD_TEXT mode on tty: %m\n");
212
213         /* We have to drop master before we switch the VT back in
214          * VT_AUTO, so we don't risk switching to a VT with another
215          * display server, that will then fail to set drm master. */
216         drmDropMaster(launcher->drm_fd);
217
218         mode.mode = VT_AUTO;
219         if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
220                 weston_log("could not reset vt handling\n");
221 }
222
223 static int
224 weston_launcher_data(int fd, uint32_t mask, void *data)
225 {
226         struct weston_launcher *launcher = data;
227         int len, ret;
228
229         if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
230                 weston_log("launcher socket closed, exiting\n");
231                 /* Normally the weston-launch will reset the tty, but
232                  * in this case it died or something, so do it here so
233                  * we don't end up with a stuck vt. */
234                 weston_launcher_restore(launcher);
235                 exit(-1);
236         }
237
238         do {
239                 len = recv(launcher->fd, &ret, sizeof ret, 0);
240         } while (len < 0 && errno == EINTR);
241
242         switch (ret) {
243         case WESTON_LAUNCHER_ACTIVATE:
244                 launcher->compositor->session_active = 1;
245                 wl_signal_emit(&launcher->compositor->session_signal,
246                                launcher->compositor);
247                 break;
248         case WESTON_LAUNCHER_DEACTIVATE:
249                 launcher->compositor->session_active = 0;
250                 wl_signal_emit(&launcher->compositor->session_signal,
251                                launcher->compositor);
252                 break;
253         default:
254                 weston_log("unexpected event from weston-launch\n");
255                 break;
256         }
257
258         return 1;
259 }
260
261 static int
262 vt_handler(int signal_number, void *data)
263 {
264         struct weston_launcher *launcher = data;
265         struct weston_compositor *compositor = launcher->compositor;
266
267         if (compositor->session_active) {
268                 compositor->session_active = 0;
269                 wl_signal_emit(&compositor->session_signal, compositor);
270                 drmDropMaster(launcher->drm_fd);
271                 ioctl(launcher->tty, VT_RELDISP, 1);
272         } else {
273                 ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
274                 drmSetMaster(launcher->drm_fd);
275                 compositor->session_active = 1;
276                 wl_signal_emit(&compositor->session_signal, compositor);
277         }
278
279         return 1;
280 }
281
282 static int
283 setup_tty(struct weston_launcher *launcher, int tty)
284 {
285         struct wl_event_loop *loop;
286         struct vt_mode mode = { 0 };
287         struct stat buf;
288         char tty_device[32] ="<stdin>";
289         int ret, kd_mode;
290
291         if (tty == 0) {
292                 launcher->tty = dup(tty);
293                 if (launcher->tty == -1) {
294                         weston_log("couldn't dup stdin: %m\n");
295                         return -1;
296                 }
297         } else {
298                 snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty);
299                 launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC);
300                 if (launcher->tty == -1) {
301                         weston_log("couldn't open tty %s: %m\n", tty_device);
302                         return -1;
303                 }
304         }
305
306         if (fstat(launcher->tty, &buf) == -1 ||
307             major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
308                 weston_log("%s not a vt\n", tty_device);
309                 weston_log("if running weston from ssh, "
310                            "use --tty to specify a tty\n");
311                 goto err_close;
312         }
313
314         ret = ioctl(launcher->tty, KDGETMODE, &kd_mode);
315         if (ret) {
316                 weston_log("failed to get VT mode: %m\n");
317                 return -1;
318         }
319         if (kd_mode != KD_TEXT) {
320                 weston_log("%s is already in graphics mode, "
321                            "is another display server running?\n", tty_device);
322                 goto err_close;
323         }
324
325         ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
326         ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
327
328         if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
329                 weston_log("failed to read keyboard mode: %m\n");
330                 goto err_close;
331         }
332
333         if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
334             ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
335                 weston_log("failed to set K_OFF keyboard mode: %m\n");
336                 goto err_close;
337         }
338
339         ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
340         if (ret) {
341                 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
342                 goto err_close;
343         }
344
345         mode.mode = VT_PROCESS;
346         mode.relsig = SIGUSR1;
347         mode.acqsig = SIGUSR1;
348         if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
349                 weston_log("failed to take control of vt handling\n");
350                 goto err_close;
351         }
352
353         loop = wl_display_get_event_loop(launcher->compositor->wl_display);
354         launcher->vt_source =
355                 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
356         if (!launcher->vt_source)
357                 goto err_close;
358
359         return 0;
360
361  err_close:
362         close(launcher->tty);
363         return -1;
364 }
365
366 int
367 weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
368 {
369         if (launcher->logind)
370                 return weston_logind_activate_vt(launcher->logind, vt);
371
372         return ioctl(launcher->tty, VT_ACTIVATE, vt);
373 }
374
375 struct weston_launcher *
376 weston_launcher_connect(struct weston_compositor *compositor, int tty,
377                         const char *seat_id)
378 {
379         struct weston_launcher *launcher;
380         struct wl_event_loop *loop;
381         int r;
382
383         launcher = malloc(sizeof *launcher);
384         if (launcher == NULL)
385                 return NULL;
386
387         launcher->logind = NULL;
388         launcher->compositor = compositor;
389         launcher->drm_fd = -1;
390         launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
391         if (launcher->fd != -1) {
392                 launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
393                 /* We don't get a chance to read out the original kb
394                  * mode for the tty, so just hard code K_UNICODE here
395                  * in case we have to clean if weston-launch dies. */
396                 launcher->kb_mode = K_UNICODE;
397
398                 loop = wl_display_get_event_loop(compositor->wl_display);
399                 launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
400                                                         WL_EVENT_READABLE,
401                                                         weston_launcher_data,
402                                                         launcher);
403                 if (launcher->source == NULL) {
404                         free(launcher);
405                         return NULL;
406                 }
407         } else {
408                 r = weston_logind_connect(&launcher->logind, compositor,
409                                           seat_id, tty);
410                 if (r < 0) {
411                         launcher->logind = NULL;
412 #ifdef ENABLE_SYS_UID
413                         if (geteuid() <= 499) { /* 499 = SYS_UID_MAX in login.defs, but it should be parsed */
414 #else
415                         if (geteuid() == 0) {
416 #endif
417                                 if (setup_tty(launcher, tty) == -1) {
418                                         free(launcher);
419                                         return NULL;
420                                 }
421                         } else {
422                                 free(launcher);
423                                 return NULL;
424                         }
425                 }
426         }
427
428         return launcher;
429 }
430
431 void
432 weston_launcher_destroy(struct weston_launcher *launcher)
433 {
434         if (launcher->logind) {
435                 weston_logind_destroy(launcher->logind);
436         } else if (launcher->fd != -1) {
437                 close(launcher->fd);
438                 if (launcher->source) wl_event_source_remove(launcher->source);
439         } else {
440                 weston_launcher_restore(launcher);
441                 if (launcher->vt_source) wl_event_source_remove(launcher->vt_source);
442         }
443
444         if (launcher->tty >= 0)
445                 close(launcher->tty);
446
447         free(launcher);
448 }