blob: ea13c4f7961f713e9b76b5788513804a372210df [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
Benjamin Franzkebfeda132012-01-30 14:04:04 +010023#include "config.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <assert.h>
29#include <errno.h>
30
31#include <error.h>
32#include <getopt.h>
33
34#include <sys/types.h>
35#include <sys/ioctl.h>
36#include <sys/stat.h>
37#include <sys/wait.h>
38#include <sys/socket.h>
39#include <sys/epoll.h>
40#include <sys/signalfd.h>
41#include <signal.h>
42#include <unistd.h>
43#include <fcntl.h>
44
45#include <termios.h>
46#include <linux/vt.h>
47#include <linux/major.h>
48
49#include <pwd.h>
50#include <grp.h>
51#include <security/pam_appl.h>
52
53#include <xf86drm.h>
54
55#ifdef HAVE_SYSTEMD_LOGIN
56#include <systemd/sd-login.h>
57#endif
58
59#include "weston-launch.h"
60
Quentin Glidicff323092013-05-17 16:20:37 +020061#define MAX_ARGV_SIZE 256
62
Benjamin Franzkebfeda132012-01-30 14:04:04 +010063struct 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;
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -070076 int sleep_fork;
Benjamin Franzkebfeda132012-01-30 14:04:04 +010077};
78
Kristian Høgsberg9e140912012-04-10 01:26:18 -040079union cmsg_data { unsigned char b[4]; int fd; };
80
Benjamin Franzkebfeda132012-01-30 14:04:04 +010081static gid_t *
82read_groups(void)
83{
84 int n;
85 gid_t *groups;
86
87 n = getgroups(0, NULL);
Rob Bradford40be7b42012-12-05 18:47:11 +000088
89 if (n < 0) {
90 fprintf(stderr, "Unable to retrieve groups: %m\n");
91 return NULL;
92 }
93
Benjamin Franzkebfeda132012-01-30 14:04:04 +010094 groups = malloc(n * sizeof(gid_t));
95 if (!groups)
96 return NULL;
97
98 if (getgroups(n, groups) < 0) {
Rob Bradford40be7b42012-12-05 18:47:11 +000099 fprintf(stderr, "Unable to retrieve groups: %m\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100100 free(groups);
101 return NULL;
102 }
103 return groups;
104}
105
106static int
107weston_launch_allowed(struct weston_launch *wl)
108{
109 struct group *gr;
110 gid_t *groups;
111 int i;
112#ifdef HAVE_SYSTEMD_LOGIN
113 char *session, *seat;
114 int err;
115#endif
116
117 if (getuid() == 0)
118 return 1;
119
120 gr = getgrnam("weston-launch");
121 if (gr) {
122 groups = read_groups();
123 if (groups) {
124 for (i = 0; groups[i]; ++i) {
125 if (groups[i] == gr->gr_gid) {
126 free(groups);
127 return 1;
128 }
129 }
130 free(groups);
131 }
132 }
133
134#ifdef HAVE_SYSTEMD_LOGIN
135 err = sd_pid_get_session(getpid(), &session);
136 if (err == 0 && session) {
137 if (sd_session_is_active(session) &&
138 sd_session_get_seat(session, &seat) == 0) {
139 free(seat);
140 free(session);
141 return 1;
142 }
143 free(session);
144 }
145#endif
146
147 return 0;
148}
149
150static int
151pam_conversation_fn(int msg_count,
152 const struct pam_message **messages,
153 struct pam_response **responses,
154 void *user_data)
155{
156 return PAM_SUCCESS;
157}
158
159static int
160setup_pam(struct weston_launch *wl)
161{
162 int err;
163
164 wl->pc.conv = pam_conversation_fn;
165 wl->pc.appdata_ptr = wl;
166
167 err = pam_start("login", wl->pw->pw_name, &wl->pc, &wl->ph);
John Kåre Alsaker5b90d8f2012-10-12 12:25:05 +0200168 if (err != PAM_SUCCESS) {
169 fprintf(stderr, "failed to start pam transaction: %d: %s\n",
170 err, pam_strerror(wl->ph, err));
171 return -1;
172 }
173
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100174 err = pam_set_item(wl->ph, PAM_TTY, ttyname(wl->tty));
175 if (err != PAM_SUCCESS) {
176 fprintf(stderr, "failed to set PAM_TTY item: %d: %s\n",
177 err, pam_strerror(wl->ph, err));
178 return -1;
179 }
180
181 err = pam_open_session(wl->ph, 0);
182 if (err != PAM_SUCCESS) {
183 fprintf(stderr, "failed to open pam session: %d: %s\n",
184 err, pam_strerror(wl->ph, err));
185 return -1;
186 }
187
188 return 0;
189}
190
191static int
192setup_launcher_socket(struct weston_launch *wl)
193{
194 struct epoll_event ev;
195
196 if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, wl->sock) < 0)
197 error(1, errno, "socketpair failed");
198
199 fcntl(wl->sock[0], F_SETFD, O_CLOEXEC);
200
201 memset(&ev, 0, sizeof ev);
202 ev.events = EPOLLIN;
203 ev.data.fd = wl->sock[0];
204 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
205 return -errno;
206
207 return 0;
208}
209
210static int
211setup_signals(struct weston_launch *wl)
212{
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100213 int ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100214 sigset_t mask;
215 struct sigaction sa;
216 struct epoll_event ev;
217
218 memset(&sa, 0, sizeof sa);
219 sa.sa_handler = SIG_DFL;
220 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100221 ret = sigaction(SIGCHLD, &sa, NULL);
222 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100223
Kristian Høgsberg18684d42013-07-22 11:59:18 -0700224 sa.sa_handler = SIG_IGN;
225 sa.sa_flags = 0;
226 sigaction(SIGHUP, &sa, NULL);
227
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100228 ret = sigemptyset(&mask);
229 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100230 sigaddset(&mask, SIGCHLD);
231 sigaddset(&mask, SIGINT);
232 sigaddset(&mask, SIGTERM);
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100233 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
234 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100235
236 wl->signalfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
237 if (wl->signalfd < 0)
238 return -errno;
239
240 memset(&ev, 0, sizeof ev);
241 ev.events = EPOLLIN;
242 ev.data.fd = wl->signalfd;
243 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
244 return -errno;
245
246 return 0;
247}
248
249static void
250setenv_fd(const char *env, int fd)
251{
252 char buf[32];
253
254 snprintf(buf, sizeof buf, "%d", fd);
255 setenv(env, buf, 1);
256}
257
258static int
259handle_setmaster(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
260{
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400261 int ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100262 struct cmsghdr *cmsg;
263 struct weston_launcher_set_master *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400264 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100265
266 if (len != sizeof(*message)) {
267 error(0, 0, "missing value in setmaster request");
268 goto out;
269 }
270
271 message = msg->msg_iov->iov_base;
272
273 cmsg = CMSG_FIRSTHDR(msg);
274 if (!cmsg ||
275 cmsg->cmsg_level != SOL_SOCKET ||
276 cmsg->cmsg_type != SCM_RIGHTS) {
277 error(0, 0, "invalid control message");
278 goto out;
279 }
280
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400281 data = (union cmsg_data *) CMSG_DATA(cmsg);
282 if (data->fd == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100283 error(0, 0, "missing drm fd in socket request");
284 goto out;
285 }
286
287 if (message->set_master)
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400288 ret = drmSetMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100289 else
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400290 ret = drmDropMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100291
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400292 close(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100293out:
294 do {
295 len = send(wl->sock[0], &ret, sizeof ret, 0);
296 } while (len < 0 && errno == EINTR);
297 if (len < 0)
298 return -1;
299
300 return 0;
301}
302
303static int
304handle_open(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
305{
306 int fd = -1, ret = -1;
307 char control[CMSG_SPACE(sizeof(fd))];
308 struct cmsghdr *cmsg;
309 struct stat s;
310 struct msghdr nmsg;
311 struct iovec iov;
312 struct weston_launcher_open *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400313 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100314
315 message = msg->msg_iov->iov_base;
316 if ((size_t)len < sizeof(*message))
317 goto err0;
318
319 /* Ensure path is null-terminated */
320 ((char *) message)[len-1] = '\0';
321
322 if (stat(message->path, &s) < 0)
323 goto err0;
324
325 fd = open(message->path, message->flags);
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100326 if (fd < 0) {
327 fprintf(stderr, "Error opening device %s: %m\n",
328 message->path);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100329 goto err0;
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100330 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100331
332 if (major(s.st_rdev) != INPUT_MAJOR) {
333 close(fd);
334 fd = -1;
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100335 fprintf(stderr, "Device %s is not an input device\n",
336 message->path);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100337 goto err0;
338 }
339
340err0:
341 memset(&nmsg, 0, sizeof nmsg);
342 nmsg.msg_iov = &iov;
343 nmsg.msg_iovlen = 1;
344 if (fd != -1) {
345 nmsg.msg_control = control;
346 nmsg.msg_controllen = sizeof control;
347 cmsg = CMSG_FIRSTHDR(&nmsg);
348 cmsg->cmsg_level = SOL_SOCKET;
349 cmsg->cmsg_type = SCM_RIGHTS;
350 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400351 data = (union cmsg_data *) CMSG_DATA(cmsg);
352 data->fd = fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100353 nmsg.msg_controllen = cmsg->cmsg_len;
354 ret = 0;
355 }
356 iov.iov_base = &ret;
357 iov.iov_len = sizeof ret;
358
359 if (wl->verbose)
360 fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
361 message->path, ret, fd);
362 do {
363 len = sendmsg(wl->sock[0], &nmsg, 0);
364 } while (len < 0 && errno == EINTR);
365
366 if (len < 0)
367 return -1;
368
369 return 0;
370}
371
372static int
373handle_socket_msg(struct weston_launch *wl)
374{
375 char control[CMSG_SPACE(sizeof(int))];
376 char buf[BUFSIZ];
377 struct msghdr msg;
378 struct iovec iov;
379 int ret = -1;
380 ssize_t len;
381 struct weston_launcher_message *message;
382
383 memset(&msg, 0, sizeof(msg));
384 iov.iov_base = buf;
385 iov.iov_len = sizeof buf;
386 msg.msg_iov = &iov;
387 msg.msg_iovlen = 1;
388 msg.msg_control = control;
389 msg.msg_controllen = sizeof control;
390
391 do {
392 len = recvmsg(wl->sock[0], &msg, 0);
393 } while (len < 0 && errno == EINTR);
394
395 if (len < 1)
396 return -1;
397
398 message = (void *) buf;
399 switch (message->opcode) {
400 case WESTON_LAUNCHER_OPEN:
401 ret = handle_open(wl, &msg, len);
402 break;
403 case WESTON_LAUNCHER_DRM_SET_MASTER:
404 ret = handle_setmaster(wl, &msg, len);
405 break;
406 }
407
408 return ret;
409}
410
411static void
412quit(struct weston_launch *wl, int status)
413{
414 int err;
415
416 close(wl->epollfd);
417 close(wl->signalfd);
418 close(wl->sock[0]);
419
420 err = pam_close_session(wl->ph, 0);
421 if (err)
422 fprintf(stderr, "pam_close_session failed: %d: %s\n",
423 err, pam_strerror(wl->ph, err));
424 pam_end(wl->ph, err);
425
426 exit(status);
427}
428
429static int
430handle_signal(struct weston_launch *wl)
431{
432 struct signalfd_siginfo sig;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100433 int pid, status, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100434
435 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
436 error(0, errno, "reading signalfd failed");
437 return -1;
438 }
439
440 switch (sig.ssi_signo) {
441 case SIGCHLD:
442 pid = waitpid(-1, &status, 0);
443 if (pid == wl->child) {
444 wl->child = 0;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100445 if (WIFEXITED(status))
446 ret = WEXITSTATUS(status);
447 else if (WIFSIGNALED(status))
448 /*
449 * If weston dies because of signal N, we
450 * return 10+N. This is distinct from
451 * weston-launch dying because of a signal
452 * (128+N).
453 */
454 ret = 10 + WTERMSIG(status);
455 else
456 ret = 0;
457 quit(wl, ret);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100458 }
459 break;
460 case SIGTERM:
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100461 case SIGINT:
462 if (wl->child)
Kristian Høgsberg1a81abb2013-06-17 15:23:20 -0400463 kill(wl->child, sig.ssi_signo);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100464 break;
465 default:
466 return -1;
467 }
468
469 return 0;
470}
471
472static int
473setup_tty(struct weston_launch *wl, const char *tty)
474{
475 struct stat buf;
476 char *t;
477
478 if (tty) {
479 t = ttyname(STDIN_FILENO);
480 if (t && strcmp(t, tty) == 0)
481 wl->tty = STDIN_FILENO;
482 else
483 wl->tty = open(tty, O_RDWR | O_NOCTTY);
484 } else {
485 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
486 char filename[16];
487
488 if (tty0 < 0)
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300489 error(1, errno, "could not open tty0");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100490
491 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
492 error(1, errno, "failed to find non-opened console");
493
494 snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
495 wl->tty = open(filename, O_RDWR | O_NOCTTY);
496 close(tty0);
497 }
498
499 if (wl->tty < 0)
500 error(1, errno, "failed to open tty");
501
502 if (tty) {
503 if (fstat(wl->tty, &buf) < 0)
504 error(1, errno, "stat %s failed", tty);
505
506 if (major(buf.st_rdev) != TTY_MAJOR)
507 error(1, 0, "invalid tty device: %s", tty);
508
509 wl->ttynr = minor(buf.st_rdev);
510 }
511
512 return 0;
513}
514
515static void
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700516launch_compositor(struct weston_launch *wl, int argc, char *argv[])
517{
518 char *child_argv[MAX_ARGV_SIZE];
519 char **env;
520 int i;
521
522 if (wl->verbose)
523 printf("weston-launch: spawned weston with pid: %d\n", getpid());
524 if (wl->tty != STDIN_FILENO) {
525 if (setsid() < 0)
526 error(1, errno, "setsid failed");
527 if (ioctl(wl->tty, TIOCSCTTY, 0) < 0)
528 error(1, errno, "TIOCSCTTY failed - tty is in use");
529 }
530
531 if (setgid(wl->pw->pw_gid) < 0 ||
532#ifdef HAVE_INITGROUPS
533 initgroups(wl->pw->pw_name, wl->pw->pw_gid) < 0 ||
534#endif
535 setuid(wl->pw->pw_uid) < 0)
536 error(1, errno, "dropping privileges failed");
537
538 if (wl->sleep_fork) {
539 if (wl->verbose)
540 printf("weston-launch: waiting %d seconds\n",
541 wl->sleep_fork);
542 sleep(wl->sleep_fork);
543 }
544
545 env = pam_getenvlist(wl->ph);
546 if (env) {
547 for (i = 0; env[i]; ++i) {
548 if (putenv(env[i]) < 0)
549 error(0, 0, "putenv %s failed", env[i]);
550 }
551 free(env);
552 }
553
554 if (wl->tty != STDIN_FILENO)
555 setenv_fd("WESTON_TTY_FD", wl->tty);
556
557 setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]);
558
559 unsetenv("DISPLAY");
560
561 child_argv[0] = wl->pw->pw_shell;
562 child_argv[1] = "-l";
563 child_argv[2] = "-c";
564 child_argv[3] = BINDIR "/weston \"$@\"";
565 child_argv[4] = "weston";
566 for (i = 0; i < argc; ++i)
567 child_argv[5 + i] = argv[i];
568 child_argv[5 + i] = NULL;
569
570 execv(child_argv[0], child_argv);
571 error(1, errno, "exec failed");
572}
573
574static void
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100575help(const char *name)
576{
577 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
578 fprintf(stderr, " -u, --user Start session as specified username\n");
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300579 fprintf(stderr, " -t, --tty Start session on alternative tty\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100580 fprintf(stderr, " -v, --verbose Be verbose\n");
581 fprintf(stderr, " -s, --sleep Sleep specified amount of time before exec\n");
582 fprintf(stderr, " -h, --help Display this help message\n");
583}
584
585int
586main(int argc, char *argv[])
587{
588 struct weston_launch wl;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100589 int i, c;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100590 char *tty = NULL, *new_user = NULL;
Quentin Glidicff323092013-05-17 16:20:37 +0200591 char *term;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100592 struct option opts[] = {
593 { "user", required_argument, NULL, 'u' },
594 { "tty", required_argument, NULL, 't' },
595 { "verbose", no_argument, NULL, 'v' },
596 { "sleep", optional_argument, NULL, 's' },
597 { "help", no_argument, NULL, 'h' },
598 { 0, 0, NULL, 0 }
599 };
600
601 memset(&wl, 0, sizeof wl);
602
603 while ((c = getopt_long(argc, argv, "u:t:s::vh", opts, &i)) != -1) {
604 switch (c) {
605 case 'u':
606 new_user = optarg;
607 if (getuid() != 0)
608 error(1, 0, "Permission denied. -u allowed for root only");
609 break;
610 case 't':
611 tty = optarg;
612 break;
613 case 'v':
614 wl.verbose = 1;
615 break;
616 case 's':
617 if (optarg)
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700618 wl.sleep_fork = atoi(optarg);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100619 else
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700620 wl.sleep_fork = 10;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100621 break;
622 case 'h':
Scott Moreaucc9acfc2013-01-21 23:40:59 -0700623 help("weston-launch");
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530624 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100625 }
626 }
627
Ander Conselvan de Oliveira9bdfc482013-05-22 22:55:33 +0300628 if ((argc - optind) > (MAX_ARGV_SIZE - 6))
Quentin Glidicff323092013-05-17 16:20:37 +0200629 error(1, E2BIG, "Too many arguments to pass to weston");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100630
631 if (new_user)
632 wl.pw = getpwnam(new_user);
633 else
634 wl.pw = getpwuid(getuid());
635 if (wl.pw == NULL)
636 error(1, errno, "failed to get username");
637
Quentin Glidicff323092013-05-17 16:20:37 +0200638 term = getenv("TERM");
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500639 clearenv();
Quentin Glidicff323092013-05-17 16:20:37 +0200640 setenv("TERM", term, 1);
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500641 setenv("USER", wl.pw->pw_name, 1);
642 setenv("LOGNAME", wl.pw->pw_name, 1);
643 setenv("HOME", wl.pw->pw_dir, 1);
644 setenv("SHELL", wl.pw->pw_shell, 1);
645
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100646 if (!weston_launch_allowed(&wl))
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300647 error(1, 0, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100648#ifdef HAVE_SYSTEMD_LOGIN
649 " - run from an active and local (systemd) session.\n"
650#else
651 " - enable systemd session support for weston-launch.\n"
652#endif
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300653 " - or add yourself to the 'weston-launch' group.");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100654
655 if (setup_tty(&wl, tty) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530656 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100657
658 if (setup_pam(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530659 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100660
661 wl.epollfd = epoll_create1(EPOLL_CLOEXEC);
662 if (wl.epollfd < 0)
663 error(1, errno, "epoll create failed");
664
665 if (setup_launcher_socket(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530666 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100667
668 if (setup_signals(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530669 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100670
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700671 wl.child = fork();
672 if (wl.child == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100673 error(1, errno, "fork failed");
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700674 exit(EXIT_FAILURE);
675 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100676
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700677 if (wl.child == 0)
678 launch_compositor(&wl, argc - optind, argv + optind);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100679
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700680 close(wl.sock[1]);
681 if (wl.tty != STDIN_FILENO)
682 close(wl.tty);
Quentin Glidic735302e2013-06-19 15:27:11 +0200683
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700684 while (1) {
685 struct epoll_event ev;
686 int n;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100687
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700688 n = epoll_wait(wl.epollfd, &ev, 1, -1);
689 if (n < 0)
690 error(0, errno, "epoll_wait failed");
691 if (n != 1)
692 continue;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100693
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700694 if (ev.data.fd == wl.sock[0])
695 handle_socket_msg(&wl);
696 else if (ev.data.fd == wl.signalfd)
697 handle_signal(&wl);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100698 }
699
700 return 0;
701}