blob: 89c3c5a2d62c0865e4c70255a5e0d57e70d88796 [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
Quentin Glidicff323092013-05-17 16:20:37 +020063#define MAX_ARGV_SIZE 256
64
Benjamin Franzkebfeda132012-01-30 14:04:04 +010065struct weston_launch {
66 struct pam_conv pc;
67 pam_handle_t *ph;
68 int tty;
69 int ttynr;
70 int sock[2];
71 struct passwd *pw;
72
73 int epollfd;
74 int signalfd;
75
76 pid_t child;
77 int verbose;
78};
79
Kristian Høgsberg9e140912012-04-10 01:26:18 -040080union cmsg_data { unsigned char b[4]; int fd; };
81
Benjamin Franzkebfeda132012-01-30 14:04:04 +010082static gid_t *
83read_groups(void)
84{
85 int n;
86 gid_t *groups;
87
88 n = getgroups(0, NULL);
Rob Bradford40be7b42012-12-05 18:47:11 +000089
90 if (n < 0) {
91 fprintf(stderr, "Unable to retrieve groups: %m\n");
92 return NULL;
93 }
94
Benjamin Franzkebfeda132012-01-30 14:04:04 +010095 groups = malloc(n * sizeof(gid_t));
96 if (!groups)
97 return NULL;
98
99 if (getgroups(n, groups) < 0) {
Rob Bradford40be7b42012-12-05 18:47:11 +0000100 fprintf(stderr, "Unable to retrieve groups: %m\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100101 free(groups);
102 return NULL;
103 }
104 return groups;
105}
106
107static int
108weston_launch_allowed(struct weston_launch *wl)
109{
110 struct group *gr;
111 gid_t *groups;
112 int i;
113#ifdef HAVE_SYSTEMD_LOGIN
114 char *session, *seat;
115 int err;
116#endif
117
118 if (getuid() == 0)
119 return 1;
120
121 gr = getgrnam("weston-launch");
122 if (gr) {
123 groups = read_groups();
124 if (groups) {
125 for (i = 0; groups[i]; ++i) {
126 if (groups[i] == gr->gr_gid) {
127 free(groups);
128 return 1;
129 }
130 }
131 free(groups);
132 }
133 }
134
135#ifdef HAVE_SYSTEMD_LOGIN
136 err = sd_pid_get_session(getpid(), &session);
137 if (err == 0 && session) {
138 if (sd_session_is_active(session) &&
139 sd_session_get_seat(session, &seat) == 0) {
140 free(seat);
141 free(session);
142 return 1;
143 }
144 free(session);
145 }
146#endif
147
148 return 0;
149}
150
151static int
152pam_conversation_fn(int msg_count,
153 const struct pam_message **messages,
154 struct pam_response **responses,
155 void *user_data)
156{
157 return PAM_SUCCESS;
158}
159
160static int
161setup_pam(struct weston_launch *wl)
162{
163 int err;
164
165 wl->pc.conv = pam_conversation_fn;
166 wl->pc.appdata_ptr = wl;
167
168 err = pam_start("login", wl->pw->pw_name, &wl->pc, &wl->ph);
John Kåre Alsaker5b90d8f2012-10-12 12:25:05 +0200169 if (err != PAM_SUCCESS) {
170 fprintf(stderr, "failed to start pam transaction: %d: %s\n",
171 err, pam_strerror(wl->ph, err));
172 return -1;
173 }
174
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100175 err = pam_set_item(wl->ph, PAM_TTY, ttyname(wl->tty));
176 if (err != PAM_SUCCESS) {
177 fprintf(stderr, "failed to set PAM_TTY item: %d: %s\n",
178 err, pam_strerror(wl->ph, err));
179 return -1;
180 }
181
182 err = pam_open_session(wl->ph, 0);
183 if (err != PAM_SUCCESS) {
184 fprintf(stderr, "failed to open pam session: %d: %s\n",
185 err, pam_strerror(wl->ph, err));
186 return -1;
187 }
188
189 return 0;
190}
191
192static int
193setup_launcher_socket(struct weston_launch *wl)
194{
195 struct epoll_event ev;
196
197 if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, wl->sock) < 0)
198 error(1, errno, "socketpair failed");
199
200 fcntl(wl->sock[0], F_SETFD, O_CLOEXEC);
201
202 memset(&ev, 0, sizeof ev);
203 ev.events = EPOLLIN;
204 ev.data.fd = wl->sock[0];
205 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
206 return -errno;
207
208 return 0;
209}
210
211static int
212setup_signals(struct weston_launch *wl)
213{
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100214 int ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100215 sigset_t mask;
216 struct sigaction sa;
217 struct epoll_event ev;
218
219 memset(&sa, 0, sizeof sa);
220 sa.sa_handler = SIG_DFL;
221 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100222 ret = sigaction(SIGCHLD, &sa, NULL);
223 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100224
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100225 ret = sigemptyset(&mask);
226 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100227 sigaddset(&mask, SIGCHLD);
228 sigaddset(&mask, SIGINT);
229 sigaddset(&mask, SIGTERM);
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100230 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
231 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100232
233 wl->signalfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
234 if (wl->signalfd < 0)
235 return -errno;
236
237 memset(&ev, 0, sizeof ev);
238 ev.events = EPOLLIN;
239 ev.data.fd = wl->signalfd;
240 if (epoll_ctl(wl->epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
241 return -errno;
242
243 return 0;
244}
245
246static void
247setenv_fd(const char *env, int fd)
248{
249 char buf[32];
250
251 snprintf(buf, sizeof buf, "%d", fd);
252 setenv(env, buf, 1);
253}
254
255static int
256handle_setmaster(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
257{
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400258 int ret = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100259 struct cmsghdr *cmsg;
260 struct weston_launcher_set_master *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400261 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100262
263 if (len != sizeof(*message)) {
264 error(0, 0, "missing value in setmaster request");
265 goto out;
266 }
267
268 message = msg->msg_iov->iov_base;
269
270 cmsg = CMSG_FIRSTHDR(msg);
271 if (!cmsg ||
272 cmsg->cmsg_level != SOL_SOCKET ||
273 cmsg->cmsg_type != SCM_RIGHTS) {
274 error(0, 0, "invalid control message");
275 goto out;
276 }
277
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400278 data = (union cmsg_data *) CMSG_DATA(cmsg);
279 if (data->fd == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100280 error(0, 0, "missing drm fd in socket request");
281 goto out;
282 }
283
284 if (message->set_master)
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400285 ret = drmSetMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100286 else
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400287 ret = drmDropMaster(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100288
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400289 close(data->fd);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100290out:
291 do {
292 len = send(wl->sock[0], &ret, sizeof ret, 0);
293 } while (len < 0 && errno == EINTR);
294 if (len < 0)
295 return -1;
296
297 return 0;
298}
299
300static int
301handle_open(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
302{
303 int fd = -1, ret = -1;
304 char control[CMSG_SPACE(sizeof(fd))];
305 struct cmsghdr *cmsg;
306 struct stat s;
307 struct msghdr nmsg;
308 struct iovec iov;
309 struct weston_launcher_open *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400310 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100311
312 message = msg->msg_iov->iov_base;
313 if ((size_t)len < sizeof(*message))
314 goto err0;
315
316 /* Ensure path is null-terminated */
317 ((char *) message)[len-1] = '\0';
318
319 if (stat(message->path, &s) < 0)
320 goto err0;
321
322 fd = open(message->path, message->flags);
323 if (fd < 0)
324 goto err0;
325
326 if (major(s.st_rdev) != INPUT_MAJOR) {
327 close(fd);
328 fd = -1;
329 goto err0;
330 }
331
332err0:
333 memset(&nmsg, 0, sizeof nmsg);
334 nmsg.msg_iov = &iov;
335 nmsg.msg_iovlen = 1;
336 if (fd != -1) {
337 nmsg.msg_control = control;
338 nmsg.msg_controllen = sizeof control;
339 cmsg = CMSG_FIRSTHDR(&nmsg);
340 cmsg->cmsg_level = SOL_SOCKET;
341 cmsg->cmsg_type = SCM_RIGHTS;
342 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400343 data = (union cmsg_data *) CMSG_DATA(cmsg);
344 data->fd = fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100345 nmsg.msg_controllen = cmsg->cmsg_len;
346 ret = 0;
347 }
348 iov.iov_base = &ret;
349 iov.iov_len = sizeof ret;
350
351 if (wl->verbose)
352 fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
353 message->path, ret, fd);
354 do {
355 len = sendmsg(wl->sock[0], &nmsg, 0);
356 } while (len < 0 && errno == EINTR);
357
358 if (len < 0)
359 return -1;
360
361 return 0;
362}
363
364static int
365handle_socket_msg(struct weston_launch *wl)
366{
367 char control[CMSG_SPACE(sizeof(int))];
368 char buf[BUFSIZ];
369 struct msghdr msg;
370 struct iovec iov;
371 int ret = -1;
372 ssize_t len;
373 struct weston_launcher_message *message;
374
375 memset(&msg, 0, sizeof(msg));
376 iov.iov_base = buf;
377 iov.iov_len = sizeof buf;
378 msg.msg_iov = &iov;
379 msg.msg_iovlen = 1;
380 msg.msg_control = control;
381 msg.msg_controllen = sizeof control;
382
383 do {
384 len = recvmsg(wl->sock[0], &msg, 0);
385 } while (len < 0 && errno == EINTR);
386
387 if (len < 1)
388 return -1;
389
390 message = (void *) buf;
391 switch (message->opcode) {
392 case WESTON_LAUNCHER_OPEN:
393 ret = handle_open(wl, &msg, len);
394 break;
395 case WESTON_LAUNCHER_DRM_SET_MASTER:
396 ret = handle_setmaster(wl, &msg, len);
397 break;
398 }
399
400 return ret;
401}
402
403static void
404quit(struct weston_launch *wl, int status)
405{
406 int err;
407
408 close(wl->epollfd);
409 close(wl->signalfd);
410 close(wl->sock[0]);
411
412 err = pam_close_session(wl->ph, 0);
413 if (err)
414 fprintf(stderr, "pam_close_session failed: %d: %s\n",
415 err, pam_strerror(wl->ph, err));
416 pam_end(wl->ph, err);
417
418 exit(status);
419}
420
421static int
422handle_signal(struct weston_launch *wl)
423{
424 struct signalfd_siginfo sig;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100425 int pid, status, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100426
427 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
428 error(0, errno, "reading signalfd failed");
429 return -1;
430 }
431
432 switch (sig.ssi_signo) {
433 case SIGCHLD:
434 pid = waitpid(-1, &status, 0);
435 if (pid == wl->child) {
436 wl->child = 0;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100437 if (WIFEXITED(status))
438 ret = WEXITSTATUS(status);
439 else if (WIFSIGNALED(status))
440 /*
441 * If weston dies because of signal N, we
442 * return 10+N. This is distinct from
443 * weston-launch dying because of a signal
444 * (128+N).
445 */
446 ret = 10 + WTERMSIG(status);
447 else
448 ret = 0;
449 quit(wl, ret);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100450 }
451 break;
452 case SIGTERM:
453 if (wl->child)
454 kill(wl->child, SIGTERM);
455 quit(wl, 0);
456 break;
457 case SIGINT:
458 if (wl->child)
459 kill(wl->child, SIGTERM);
460 break;
461 default:
462 return -1;
463 }
464
465 return 0;
466}
467
468static int
469setup_tty(struct weston_launch *wl, const char *tty)
470{
471 struct stat buf;
472 char *t;
473
474 if (tty) {
475 t = ttyname(STDIN_FILENO);
476 if (t && strcmp(t, tty) == 0)
477 wl->tty = STDIN_FILENO;
478 else
479 wl->tty = open(tty, O_RDWR | O_NOCTTY);
480 } else {
481 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
482 char filename[16];
483
484 if (tty0 < 0)
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300485 error(1, errno, "could not open tty0");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100486
487 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
488 error(1, errno, "failed to find non-opened console");
489
490 snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
491 wl->tty = open(filename, O_RDWR | O_NOCTTY);
492 close(tty0);
493 }
494
495 if (wl->tty < 0)
496 error(1, errno, "failed to open tty");
497
498 if (tty) {
499 if (fstat(wl->tty, &buf) < 0)
500 error(1, errno, "stat %s failed", tty);
501
502 if (major(buf.st_rdev) != TTY_MAJOR)
503 error(1, 0, "invalid tty device: %s", tty);
504
505 wl->ttynr = minor(buf.st_rdev);
506 }
507
508 return 0;
509}
510
511static void
512help(const char *name)
513{
514 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
515 fprintf(stderr, " -u, --user Start session as specified username\n");
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300516 fprintf(stderr, " -t, --tty Start session on alternative tty\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100517 fprintf(stderr, " -v, --verbose Be verbose\n");
518 fprintf(stderr, " -s, --sleep Sleep specified amount of time before exec\n");
519 fprintf(stderr, " -h, --help Display this help message\n");
520}
521
522int
523main(int argc, char *argv[])
524{
525 struct weston_launch wl;
526 char **env;
527 int i, c;
Quentin Glidicff323092013-05-17 16:20:37 +0200528 char *child_argv[MAX_ARGV_SIZE];
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100529 char *tty = NULL, *new_user = NULL;
Quentin Glidicff323092013-05-17 16:20:37 +0200530 char *term;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100531 int sleep_fork = 0;
532 struct option opts[] = {
533 { "user", required_argument, NULL, 'u' },
534 { "tty", required_argument, NULL, 't' },
535 { "verbose", no_argument, NULL, 'v' },
536 { "sleep", optional_argument, NULL, 's' },
537 { "help", no_argument, NULL, 'h' },
538 { 0, 0, NULL, 0 }
539 };
540
541 memset(&wl, 0, sizeof wl);
542
543 while ((c = getopt_long(argc, argv, "u:t:s::vh", opts, &i)) != -1) {
544 switch (c) {
545 case 'u':
546 new_user = optarg;
547 if (getuid() != 0)
548 error(1, 0, "Permission denied. -u allowed for root only");
549 break;
550 case 't':
551 tty = optarg;
552 break;
553 case 'v':
554 wl.verbose = 1;
555 break;
556 case 's':
557 if (optarg)
558 sleep_fork = atoi(optarg);
559 else
560 sleep_fork = 10;
561 break;
562 case 'h':
Scott Moreaucc9acfc2013-01-21 23:40:59 -0700563 help("weston-launch");
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530564 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100565 }
566 }
567
Quentin Glidicff323092013-05-17 16:20:37 +0200568 if ((argc - optind) > (MAX_ARGV_SIZE - 5))
569 error(1, E2BIG, "Too many arguments to pass to weston");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100570
571 if (new_user)
572 wl.pw = getpwnam(new_user);
573 else
574 wl.pw = getpwuid(getuid());
575 if (wl.pw == NULL)
576 error(1, errno, "failed to get username");
577
Quentin Glidicff323092013-05-17 16:20:37 +0200578 child_argv[0] = wl.pw->pw_shell;
579 child_argv[1] = "-l";
580 child_argv[2] = "-c";
581 child_argv[3] = BINDIR "/weston \"$@\"";
582 child_argv[4] = "weston";
583 for (i = 0; i < (argc - optind); ++i)
584 child_argv[5+i] = argv[optind+i];
585
586 term = getenv("TERM");
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500587 clearenv();
Quentin Glidicff323092013-05-17 16:20:37 +0200588 setenv("TERM", term, 1);
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500589 setenv("USER", wl.pw->pw_name, 1);
590 setenv("LOGNAME", wl.pw->pw_name, 1);
591 setenv("HOME", wl.pw->pw_dir, 1);
592 setenv("SHELL", wl.pw->pw_shell, 1);
593
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100594 if (!weston_launch_allowed(&wl))
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300595 error(1, 0, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100596#ifdef HAVE_SYSTEMD_LOGIN
597 " - run from an active and local (systemd) session.\n"
598#else
599 " - enable systemd session support for weston-launch.\n"
600#endif
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300601 " - or add yourself to the 'weston-launch' group.");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100602
603 if (setup_tty(&wl, tty) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530604 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100605
606 if (setup_pam(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530607 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100608
609 wl.epollfd = epoll_create1(EPOLL_CLOEXEC);
610 if (wl.epollfd < 0)
611 error(1, errno, "epoll create failed");
612
613 if (setup_launcher_socket(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530614 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100615
616 if (setup_signals(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530617 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100618
619 switch ((wl.child = fork())) {
620 case -1:
621 error(1, errno, "fork failed");
622 break;
623 case 0:
624 if (wl.verbose)
625 printf("weston-launch: spawned weston with pid: %d\n", getpid());
626 if (wl.tty != STDIN_FILENO) {
627 if (setsid() < 0)
628 error(1, errno, "setsid failed");
629 if (ioctl(wl.tty, TIOCSCTTY, 0) < 0)
630 error(1, errno, "TIOCSCTTY failed - tty is in use");
631 }
632
633 if (setgid(wl.pw->pw_gid) < 0 ||
634 setuid(wl.pw->pw_uid) < 0)
635 error(1, errno, "dropping privilidges failed");
636
637 if (sleep_fork) {
638 if (wl.verbose)
639 printf("weston-launch: waiting %d seconds\n", sleep_fork);
640 sleep(sleep_fork);
641 }
642
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100643 env = pam_getenvlist(wl.ph);
644 if (env) {
645 for (i = 0; env[i]; ++i) {
646 if (putenv(env[i]) < 0)
647 error(0, 0, "putenv %s failed", env[i]);
648 }
649 free(env);
650 }
651
652 if (wl.tty != STDIN_FILENO)
653 setenv_fd("WESTON_TTY_FD", wl.tty);
654
655 setenv_fd("WESTON_LAUNCHER_SOCK", wl.sock[1]);
656
657 unsetenv("DISPLAY");
658
659 execv(child_argv[0], child_argv);
660 error(1, errno, "exec failed");
661 break;
662 default:
663 close(wl.sock[1]);
664 if (wl.tty != STDIN_FILENO)
665 close(wl.tty);
666
667 while (1) {
668 struct epoll_event ev;
669 int n;
670
671 n = epoll_wait(wl.epollfd, &ev, 1, -1);
672 if (n < 0)
673 error(0, errno, "epoll_wait failed");
674 if (n != 1)
675 continue;
676
677 if (ev.data.fd == wl.sock[0])
678 handle_socket_msg(&wl);
679 else if (ev.data.fd == wl.signalfd)
680 handle_signal(&wl);
681 }
682 break;
683 }
684
685 return 0;
686}