blob: 98f01113adb88b321da1cbed9ee9c576d8878d85 [file] [log] [blame]
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001/*
2 * Copyright © 2012 Benjamin Franzke
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#define _GNU_SOURCE
24
25#include "config.h"
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <assert.h>
31#include <errno.h>
32
33#include <error.h>
34#include <getopt.h>
35
36#include <sys/types.h>
37#include <sys/ioctl.h>
38#include <sys/stat.h>
39#include <sys/wait.h>
40#include <sys/socket.h>
41#include <sys/epoll.h>
42#include <sys/signalfd.h>
43#include <signal.h>
44#include <unistd.h>
45#include <fcntl.h>
46
47#include <termios.h>
48#include <linux/vt.h>
49#include <linux/major.h>
50
51#include <pwd.h>
52#include <grp.h>
53#include <security/pam_appl.h>
54
55#include <xf86drm.h>
56
57#ifdef HAVE_SYSTEMD_LOGIN
58#include <systemd/sd-login.h>
59#endif
60
61#include "weston-launch.h"
62
63struct weston_launch {
64 struct pam_conv pc;
65 pam_handle_t *ph;
66 int tty;
67 int ttynr;
68 int sock[2];
69 struct passwd *pw;
70
71 int epollfd;
72 int signalfd;
73
74 pid_t child;
75 int verbose;
76};
77
Kristian Høgsberg9e140912012-04-10 01:26:18 -040078union cmsg_data { unsigned char b[4]; int fd; };
79
Benjamin Franzkebfeda132012-01-30 14:04:04 +010080static gid_t *
81read_groups(void)
82{
83 int n;
84 gid_t *groups;
85
86 n = getgroups(0, NULL);
Rob Bradford40be7b42012-12-05 18:47:11 +000087
88 if (n < 0) {
89 fprintf(stderr, "Unable to retrieve groups: %m\n");
90 return NULL;
91 }
92
Benjamin Franzkebfeda132012-01-30 14:04:04 +010093 groups = malloc(n * sizeof(gid_t));
94 if (!groups)
95 return NULL;
96
97 if (getgroups(n, groups) < 0) {
Rob Bradford40be7b42012-12-05 18:47:11 +000098 fprintf(stderr, "Unable to retrieve groups: %m\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +010099 free(groups);
100 return NULL;
101 }
102 return groups;
103}
104
105static int
106weston_launch_allowed(struct weston_launch *wl)
107{
108 struct group *gr;
109 gid_t *groups;
110 int i;
111#ifdef HAVE_SYSTEMD_LOGIN
112 char *session, *seat;
113 int err;
114#endif
115
116 if (getuid() == 0)
117 return 1;
118
119 gr = getgrnam("weston-launch");
120 if (gr) {
121 groups = read_groups();
122 if (groups) {
123 for (i = 0; groups[i]; ++i) {
124 if (groups[i] == gr->gr_gid) {
125 free(groups);
126 return 1;
127 }
128 }
129 free(groups);
130 }
131 }
132
133#ifdef HAVE_SYSTEMD_LOGIN
134 err = sd_pid_get_session(getpid(), &session);
135 if (err == 0 && session) {
136 if (sd_session_is_active(session) &&
137 sd_session_get_seat(session, &seat) == 0) {
138 free(seat);
139 free(session);
140 return 1;
141 }
142 free(session);
143 }
144#endif
145
146 return 0;
147}
148
149static int
150pam_conversation_fn(int msg_count,
151 const struct pam_message **messages,
152 struct pam_response **responses,
153 void *user_data)
154{
155 return PAM_SUCCESS;
156}
157
158static int
159setup_pam(struct weston_launch *wl)
160{
161 int err;
162
163 wl->pc.conv = pam_conversation_fn;
164 wl->pc.appdata_ptr = wl;
165
166 err = pam_start("login", wl->pw->pw_name, &wl->pc, &wl->ph);
John Kåre Alsaker5b90d8f2012-10-12 12:25:05 +0200167 if (err != PAM_SUCCESS) {
168 fprintf(stderr, "failed to start pam transaction: %d: %s\n",
169 err, pam_strerror(wl->ph, err));
170 return -1;
171 }
172
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100173 err = pam_set_item(wl->ph, PAM_TTY, ttyname(wl->tty));
174 if (err != PAM_SUCCESS) {
175 fprintf(stderr, "failed to set PAM_TTY item: %d: %s\n",
176 err, pam_strerror(wl->ph, err));
177 return -1;
178 }
179
180 err = pam_open_session(wl->ph, 0);
181 if (err != PAM_SUCCESS) {
182 fprintf(stderr, "failed to open pam session: %d: %s\n",
183 err, pam_strerror(wl->ph, err));
184 return -1;
185 }
186
187 return 0;
188}
189
190static int
191setup_launcher_socket(struct weston_launch *wl)
192{
193 struct epoll_event ev;
194
195 if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, wl->sock) < 0)
196 error(1, errno, "socketpair failed");
197
198 fcntl(wl->sock[0], F_SETFD, O_CLOEXEC);
199
200 memset(&ev, 0, sizeof ev);
201 ev.events = EPOLLIN;
202 ev.data.fd = wl->sock[0];
203 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
204 return -errno;
205
206 return 0;
207}
208
209static int
210setup_signals(struct weston_launch *wl)
211{
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100212 int ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100213 sigset_t mask;
214 struct sigaction sa;
215 struct epoll_event ev;
216
217 memset(&sa, 0, sizeof sa);
218 sa.sa_handler = SIG_DFL;
219 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100220 ret = sigaction(SIGCHLD, &sa, NULL);
221 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100222
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100223 ret = sigemptyset(&mask);
224 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100225 sigaddset(&mask, SIGCHLD);
226 sigaddset(&mask, SIGINT);
227 sigaddset(&mask, SIGTERM);
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100228 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
229 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100230
231 wl->signalfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
232 if (wl->signalfd < 0)
233 return -errno;
234
235 memset(&ev, 0, sizeof ev);
236 ev.events = EPOLLIN;
237 ev.data.fd = wl->signalfd;
238 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
239 return -errno;
240
241 return 0;
242}
243
244static void
245setenv_fd(const char *env, int fd)
246{
247 char buf[32];
248
249 snprintf(buf, sizeof buf, "%d", fd);
250 setenv(env, buf, 1);
251}
252
253static int
254handle_setmaster(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
255{
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400256 int ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100257 struct cmsghdr *cmsg;
258 struct weston_launcher_set_master *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400259 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100260
261 if (len != sizeof(*message)) {
262 error(0, 0, "missing value in setmaster request");
263 goto out;
264 }
265
266 message = msg->msg_iov->iov_base;
267
268 cmsg = CMSG_FIRSTHDR(msg);
269 if (!cmsg ||
270 cmsg->cmsg_level != SOL_SOCKET ||
271 cmsg->cmsg_type != SCM_RIGHTS) {
272 error(0, 0, "invalid control message");
273 goto out;
274 }
275
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400276 data = (union cmsg_data *) CMSG_DATA(cmsg);
277 if (data->fd == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100278 error(0, 0, "missing drm fd in socket request");
279 goto out;
280 }
281
282 if (message->set_master)
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400283 ret = drmSetMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100284 else
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400285 ret = drmDropMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100286
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400287 close(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100288out:
289 do {
290 len = send(wl->sock[0], &ret, sizeof ret, 0);
291 } while (len < 0 && errno == EINTR);
292 if (len < 0)
293 return -1;
294
295 return 0;
296}
297
298static int
299handle_open(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
300{
301 int fd = -1, ret = -1;
302 char control[CMSG_SPACE(sizeof(fd))];
303 struct cmsghdr *cmsg;
304 struct stat s;
305 struct msghdr nmsg;
306 struct iovec iov;
307 struct weston_launcher_open *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400308 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100309
310 message = msg->msg_iov->iov_base;
311 if ((size_t)len < sizeof(*message))
312 goto err0;
313
314 /* Ensure path is null-terminated */
315 ((char *) message)[len-1] = '\0';
316
317 if (stat(message->path, &s) < 0)
318 goto err0;
319
320 fd = open(message->path, message->flags);
321 if (fd < 0)
322 goto err0;
323
324 if (major(s.st_rdev) != INPUT_MAJOR) {
325 close(fd);
326 fd = -1;
327 goto err0;
328 }
329
330err0:
331 memset(&nmsg, 0, sizeof nmsg);
332 nmsg.msg_iov = &iov;
333 nmsg.msg_iovlen = 1;
334 if (fd != -1) {
335 nmsg.msg_control = control;
336 nmsg.msg_controllen = sizeof control;
337 cmsg = CMSG_FIRSTHDR(&nmsg);
338 cmsg->cmsg_level = SOL_SOCKET;
339 cmsg->cmsg_type = SCM_RIGHTS;
340 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400341 data = (union cmsg_data *) CMSG_DATA(cmsg);
342 data->fd = fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100343 nmsg.msg_controllen = cmsg->cmsg_len;
344 ret = 0;
345 }
346 iov.iov_base = &ret;
347 iov.iov_len = sizeof ret;
348
349 if (wl->verbose)
350 fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
351 message->path, ret, fd);
352 do {
353 len = sendmsg(wl->sock[0], &nmsg, 0);
354 } while (len < 0 && errno == EINTR);
355
356 if (len < 0)
357 return -1;
358
359 return 0;
360}
361
362static int
363handle_socket_msg(struct weston_launch *wl)
364{
365 char control[CMSG_SPACE(sizeof(int))];
366 char buf[BUFSIZ];
367 struct msghdr msg;
368 struct iovec iov;
369 int ret = -1;
370 ssize_t len;
371 struct weston_launcher_message *message;
372
373 memset(&msg, 0, sizeof(msg));
374 iov.iov_base = buf;
375 iov.iov_len = sizeof buf;
376 msg.msg_iov = &iov;
377 msg.msg_iovlen = 1;
378 msg.msg_control = control;
379 msg.msg_controllen = sizeof control;
380
381 do {
382 len = recvmsg(wl->sock[0], &msg, 0);
383 } while (len < 0 && errno == EINTR);
384
385 if (len < 1)
386 return -1;
387
388 message = (void *) buf;
389 switch (message->opcode) {
390 case WESTON_LAUNCHER_OPEN:
391 ret = handle_open(wl, &msg, len);
392 break;
393 case WESTON_LAUNCHER_DRM_SET_MASTER:
394 ret = handle_setmaster(wl, &msg, len);
395 break;
396 }
397
398 return ret;
399}
400
401static void
402quit(struct weston_launch *wl, int status)
403{
404 int err;
405
406 close(wl->epollfd);
407 close(wl->signalfd);
408 close(wl->sock[0]);
409
410 err = pam_close_session(wl->ph, 0);
411 if (err)
412 fprintf(stderr, "pam_close_session failed: %d: %s\n",
413 err, pam_strerror(wl->ph, err));
414 pam_end(wl->ph, err);
415
416 exit(status);
417}
418
419static int
420handle_signal(struct weston_launch *wl)
421{
422 struct signalfd_siginfo sig;
423 int pid, status;
424
425 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
426 error(0, errno, "reading signalfd failed");
427 return -1;
428 }
429
430 switch (sig.ssi_signo) {
431 case SIGCHLD:
432 pid = waitpid(-1, &status, 0);
433 if (pid == wl->child) {
434 wl->child = 0;
435 quit(wl, WIFEXITED(status) ? WEXITSTATUS(status) : 0);
436 }
437 break;
438 case SIGTERM:
439 if (wl->child)
440 kill(wl->child, SIGTERM);
441 quit(wl, 0);
442 break;
443 case SIGINT:
444 if (wl->child)
445 kill(wl->child, SIGTERM);
446 break;
447 default:
448 return -1;
449 }
450
451 return 0;
452}
453
454static int
455setup_tty(struct weston_launch *wl, const char *tty)
456{
457 struct stat buf;
458 char *t;
459
460 if (tty) {
461 t = ttyname(STDIN_FILENO);
462 if (t && strcmp(t, tty) == 0)
463 wl->tty = STDIN_FILENO;
464 else
465 wl->tty = open(tty, O_RDWR | O_NOCTTY);
466 } else {
467 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
468 char filename[16];
469
470 if (tty0 < 0)
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300471 error(1, errno, "could not open tty0");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100472
473 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
474 error(1, errno, "failed to find non-opened console");
475
476 snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
477 wl->tty = open(filename, O_RDWR | O_NOCTTY);
478 close(tty0);
479 }
480
481 if (wl->tty < 0)
482 error(1, errno, "failed to open tty");
483
484 if (tty) {
485 if (fstat(wl->tty, &buf) < 0)
486 error(1, errno, "stat %s failed", tty);
487
488 if (major(buf.st_rdev) != TTY_MAJOR)
489 error(1, 0, "invalid tty device: %s", tty);
490
491 wl->ttynr = minor(buf.st_rdev);
492 }
493
494 return 0;
495}
496
497static void
498help(const char *name)
499{
500 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
501 fprintf(stderr, " -u, --user Start session as specified username\n");
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300502 fprintf(stderr, " -t, --tty Start session on alternative tty\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100503 fprintf(stderr, " -v, --verbose Be verbose\n");
504 fprintf(stderr, " -s, --sleep Sleep specified amount of time before exec\n");
505 fprintf(stderr, " -h, --help Display this help message\n");
506}
507
508int
509main(int argc, char *argv[])
510{
511 struct weston_launch wl;
512 char **env;
513 int i, c;
514 char **child_argv;
515 char *tty = NULL, *new_user = NULL;
516 int sleep_fork = 0;
517 struct option opts[] = {
518 { "user", required_argument, NULL, 'u' },
519 { "tty", required_argument, NULL, 't' },
520 { "verbose", no_argument, NULL, 'v' },
521 { "sleep", optional_argument, NULL, 's' },
522 { "help", no_argument, NULL, 'h' },
523 { 0, 0, NULL, 0 }
524 };
525
526 memset(&wl, 0, sizeof wl);
527
528 while ((c = getopt_long(argc, argv, "u:t:s::vh", opts, &i)) != -1) {
529 switch (c) {
530 case 'u':
531 new_user = optarg;
532 if (getuid() != 0)
533 error(1, 0, "Permission denied. -u allowed for root only");
534 break;
535 case 't':
536 tty = optarg;
537 break;
538 case 'v':
539 wl.verbose = 1;
540 break;
541 case 's':
542 if (optarg)
543 sleep_fork = atoi(optarg);
544 else
545 sleep_fork = 10;
546 break;
547 case 'h':
Scott Moreaucc9acfc2013-01-21 23:40:59 -0700548 help("weston-launch");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100549 exit(1);
550 }
551 }
552
553 child_argv = &argv[optind-1];
554 child_argv[0] = BINDIR "/weston";
555
556 if (new_user)
557 wl.pw = getpwnam(new_user);
558 else
559 wl.pw = getpwuid(getuid());
560 if (wl.pw == NULL)
561 error(1, errno, "failed to get username");
562
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500563 clearenv();
564 setenv("USER", wl.pw->pw_name, 1);
565 setenv("LOGNAME", wl.pw->pw_name, 1);
566 setenv("HOME", wl.pw->pw_dir, 1);
567 setenv("SHELL", wl.pw->pw_shell, 1);
568
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100569 if (!weston_launch_allowed(&wl))
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300570 error(1, 0, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100571#ifdef HAVE_SYSTEMD_LOGIN
572 " - run from an active and local (systemd) session.\n"
573#else
574 " - enable systemd session support for weston-launch.\n"
575#endif
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300576 " - or add yourself to the 'weston-launch' group.");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100577
578 if (setup_tty(&wl, tty) < 0)
579 return 1;
580
581 if (setup_pam(&wl) < 0)
582 return 1;
583
584 wl.epollfd = epoll_create1(EPOLL_CLOEXEC);
585 if (wl.epollfd < 0)
586 error(1, errno, "epoll create failed");
587
588 if (setup_launcher_socket(&wl) < 0)
589 return 1;
590
591 if (setup_signals(&wl) < 0)
592 return 1;
593
594 switch ((wl.child = fork())) {
595 case -1:
596 error(1, errno, "fork failed");
597 break;
598 case 0:
599 if (wl.verbose)
600 printf("weston-launch: spawned weston with pid: %d\n", getpid());
601 if (wl.tty != STDIN_FILENO) {
602 if (setsid() < 0)
603 error(1, errno, "setsid failed");
604 if (ioctl(wl.tty, TIOCSCTTY, 0) < 0)
605 error(1, errno, "TIOCSCTTY failed - tty is in use");
606 }
607
608 if (setgid(wl.pw->pw_gid) < 0 ||
609 setuid(wl.pw->pw_uid) < 0)
610 error(1, errno, "dropping privilidges failed");
611
612 if (sleep_fork) {
613 if (wl.verbose)
614 printf("weston-launch: waiting %d seconds\n", sleep_fork);
615 sleep(sleep_fork);
616 }
617
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100618 env = pam_getenvlist(wl.ph);
619 if (env) {
620 for (i = 0; env[i]; ++i) {
621 if (putenv(env[i]) < 0)
622 error(0, 0, "putenv %s failed", env[i]);
623 }
624 free(env);
625 }
626
627 if (wl.tty != STDIN_FILENO)
628 setenv_fd("WESTON_TTY_FD", wl.tty);
629
630 setenv_fd("WESTON_LAUNCHER_SOCK", wl.sock[1]);
631
632 unsetenv("DISPLAY");
633
634 execv(child_argv[0], child_argv);
635 error(1, errno, "exec failed");
636 break;
637 default:
638 close(wl.sock[1]);
639 if (wl.tty != STDIN_FILENO)
640 close(wl.tty);
641
642 while (1) {
643 struct epoll_event ev;
644 int n;
645
646 n = epoll_wait(wl.epollfd, &ev, 1, -1);
647 if (n < 0)
648 error(0, errno, "epoll_wait failed");
649 if (n != 1)
650 continue;
651
652 if (ev.data.fd == wl.sock[0])
653 handle_socket_msg(&wl);
654 else if (ev.data.fd == wl.signalfd)
655 handle_signal(&wl);
656 }
657 break;
658 }
659
660 return 0;
661}