blob: ad774762ada6ea0fdcb7fe6517e20415060d4480 [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);
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100323 if (fd < 0) {
324 fprintf(stderr, "Error opening device %s: %m\n",
325 message->path);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100326 goto err0;
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100327 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100328
329 if (major(s.st_rdev) != INPUT_MAJOR) {
330 close(fd);
331 fd = -1;
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100332 fprintf(stderr, "Device %s is not an input device\n",
333 message->path);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100334 goto err0;
335 }
336
337err0:
338 memset(&nmsg, 0, sizeof nmsg);
339 nmsg.msg_iov = &iov;
340 nmsg.msg_iovlen = 1;
341 if (fd != -1) {
342 nmsg.msg_control = control;
343 nmsg.msg_controllen = sizeof control;
344 cmsg = CMSG_FIRSTHDR(&nmsg);
345 cmsg->cmsg_level = SOL_SOCKET;
346 cmsg->cmsg_type = SCM_RIGHTS;
347 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400348 data = (union cmsg_data *) CMSG_DATA(cmsg);
349 data->fd = fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100350 nmsg.msg_controllen = cmsg->cmsg_len;
351 ret = 0;
352 }
353 iov.iov_base = &ret;
354 iov.iov_len = sizeof ret;
355
356 if (wl->verbose)
357 fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
358 message->path, ret, fd);
359 do {
360 len = sendmsg(wl->sock[0], &nmsg, 0);
361 } while (len < 0 && errno == EINTR);
362
363 if (len < 0)
364 return -1;
365
366 return 0;
367}
368
369static int
370handle_socket_msg(struct weston_launch *wl)
371{
372 char control[CMSG_SPACE(sizeof(int))];
373 char buf[BUFSIZ];
374 struct msghdr msg;
375 struct iovec iov;
376 int ret = -1;
377 ssize_t len;
378 struct weston_launcher_message *message;
379
380 memset(&msg, 0, sizeof(msg));
381 iov.iov_base = buf;
382 iov.iov_len = sizeof buf;
383 msg.msg_iov = &iov;
384 msg.msg_iovlen = 1;
385 msg.msg_control = control;
386 msg.msg_controllen = sizeof control;
387
388 do {
389 len = recvmsg(wl->sock[0], &msg, 0);
390 } while (len < 0 && errno == EINTR);
391
392 if (len < 1)
393 return -1;
394
395 message = (void *) buf;
396 switch (message->opcode) {
397 case WESTON_LAUNCHER_OPEN:
398 ret = handle_open(wl, &msg, len);
399 break;
400 case WESTON_LAUNCHER_DRM_SET_MASTER:
401 ret = handle_setmaster(wl, &msg, len);
402 break;
403 }
404
405 return ret;
406}
407
408static void
409quit(struct weston_launch *wl, int status)
410{
411 int err;
412
413 close(wl->epollfd);
414 close(wl->signalfd);
415 close(wl->sock[0]);
416
417 err = pam_close_session(wl->ph, 0);
418 if (err)
419 fprintf(stderr, "pam_close_session failed: %d: %s\n",
420 err, pam_strerror(wl->ph, err));
421 pam_end(wl->ph, err);
422
423 exit(status);
424}
425
426static int
427handle_signal(struct weston_launch *wl)
428{
429 struct signalfd_siginfo sig;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100430 int pid, status, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100431
432 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
433 error(0, errno, "reading signalfd failed");
434 return -1;
435 }
436
437 switch (sig.ssi_signo) {
438 case SIGCHLD:
439 pid = waitpid(-1, &status, 0);
440 if (pid == wl->child) {
441 wl->child = 0;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100442 if (WIFEXITED(status))
443 ret = WEXITSTATUS(status);
444 else if (WIFSIGNALED(status))
445 /*
446 * If weston dies because of signal N, we
447 * return 10+N. This is distinct from
448 * weston-launch dying because of a signal
449 * (128+N).
450 */
451 ret = 10 + WTERMSIG(status);
452 else
453 ret = 0;
454 quit(wl, ret);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100455 }
456 break;
457 case SIGTERM:
458 if (wl->child)
459 kill(wl->child, SIGTERM);
460 quit(wl, 0);
461 break;
462 case SIGINT:
463 if (wl->child)
464 kill(wl->child, SIGTERM);
465 break;
466 default:
467 return -1;
468 }
469
470 return 0;
471}
472
473static int
474setup_tty(struct weston_launch *wl, const char *tty)
475{
476 struct stat buf;
477 char *t;
478
479 if (tty) {
480 t = ttyname(STDIN_FILENO);
481 if (t && strcmp(t, tty) == 0)
482 wl->tty = STDIN_FILENO;
483 else
484 wl->tty = open(tty, O_RDWR | O_NOCTTY);
485 } else {
486 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
487 char filename[16];
488
489 if (tty0 < 0)
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300490 error(1, errno, "could not open tty0");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100491
492 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
493 error(1, errno, "failed to find non-opened console");
494
495 snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
496 wl->tty = open(filename, O_RDWR | O_NOCTTY);
497 close(tty0);
498 }
499
500 if (wl->tty < 0)
501 error(1, errno, "failed to open tty");
502
503 if (tty) {
504 if (fstat(wl->tty, &buf) < 0)
505 error(1, errno, "stat %s failed", tty);
506
507 if (major(buf.st_rdev) != TTY_MAJOR)
508 error(1, 0, "invalid tty device: %s", tty);
509
510 wl->ttynr = minor(buf.st_rdev);
511 }
512
513 return 0;
514}
515
516static void
517help(const char *name)
518{
519 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
520 fprintf(stderr, " -u, --user Start session as specified username\n");
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300521 fprintf(stderr, " -t, --tty Start session on alternative tty\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100522 fprintf(stderr, " -v, --verbose Be verbose\n");
523 fprintf(stderr, " -s, --sleep Sleep specified amount of time before exec\n");
524 fprintf(stderr, " -h, --help Display this help message\n");
525}
526
527int
528main(int argc, char *argv[])
529{
530 struct weston_launch wl;
531 char **env;
532 int i, c;
Quentin Glidicff323092013-05-17 16:20:37 +0200533 char *child_argv[MAX_ARGV_SIZE];
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100534 char *tty = NULL, *new_user = NULL;
Quentin Glidicff323092013-05-17 16:20:37 +0200535 char *term;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100536 int sleep_fork = 0;
537 struct option opts[] = {
538 { "user", required_argument, NULL, 'u' },
539 { "tty", required_argument, NULL, 't' },
540 { "verbose", no_argument, NULL, 'v' },
541 { "sleep", optional_argument, NULL, 's' },
542 { "help", no_argument, NULL, 'h' },
543 { 0, 0, NULL, 0 }
544 };
545
546 memset(&wl, 0, sizeof wl);
547
548 while ((c = getopt_long(argc, argv, "u:t:s::vh", opts, &i)) != -1) {
549 switch (c) {
550 case 'u':
551 new_user = optarg;
552 if (getuid() != 0)
553 error(1, 0, "Permission denied. -u allowed for root only");
554 break;
555 case 't':
556 tty = optarg;
557 break;
558 case 'v':
559 wl.verbose = 1;
560 break;
561 case 's':
562 if (optarg)
563 sleep_fork = atoi(optarg);
564 else
565 sleep_fork = 10;
566 break;
567 case 'h':
Scott Moreaucc9acfc2013-01-21 23:40:59 -0700568 help("weston-launch");
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530569 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100570 }
571 }
572
Ander Conselvan de Oliveira9bdfc482013-05-22 22:55:33 +0300573 if ((argc - optind) > (MAX_ARGV_SIZE - 6))
Quentin Glidicff323092013-05-17 16:20:37 +0200574 error(1, E2BIG, "Too many arguments to pass to weston");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100575
576 if (new_user)
577 wl.pw = getpwnam(new_user);
578 else
579 wl.pw = getpwuid(getuid());
580 if (wl.pw == NULL)
581 error(1, errno, "failed to get username");
582
Quentin Glidicff323092013-05-17 16:20:37 +0200583 child_argv[0] = wl.pw->pw_shell;
584 child_argv[1] = "-l";
585 child_argv[2] = "-c";
586 child_argv[3] = BINDIR "/weston \"$@\"";
587 child_argv[4] = "weston";
588 for (i = 0; i < (argc - optind); ++i)
Ander Conselvan de Oliveira9bdfc482013-05-22 22:55:33 +0300589 child_argv[5 + i] = argv[optind + i];
590 child_argv[5 + i] = NULL;
Quentin Glidicff323092013-05-17 16:20:37 +0200591
592 term = getenv("TERM");
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500593 clearenv();
Quentin Glidicff323092013-05-17 16:20:37 +0200594 setenv("TERM", term, 1);
Kristian Høgsberg272d2432013-01-29 14:14:06 -0500595 setenv("USER", wl.pw->pw_name, 1);
596 setenv("LOGNAME", wl.pw->pw_name, 1);
597 setenv("HOME", wl.pw->pw_dir, 1);
598 setenv("SHELL", wl.pw->pw_shell, 1);
599
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100600 if (!weston_launch_allowed(&wl))
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300601 error(1, 0, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100602#ifdef HAVE_SYSTEMD_LOGIN
603 " - run from an active and local (systemd) session.\n"
604#else
605 " - enable systemd session support for weston-launch.\n"
606#endif
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300607 " - or add yourself to the 'weston-launch' group.");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100608
609 if (setup_tty(&wl, tty) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530610 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100611
612 if (setup_pam(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530613 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100614
615 wl.epollfd = epoll_create1(EPOLL_CLOEXEC);
616 if (wl.epollfd < 0)
617 error(1, errno, "epoll create failed");
618
619 if (setup_launcher_socket(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530620 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100621
622 if (setup_signals(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530623 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100624
625 switch ((wl.child = fork())) {
626 case -1:
627 error(1, errno, "fork failed");
628 break;
629 case 0:
630 if (wl.verbose)
631 printf("weston-launch: spawned weston with pid: %d\n", getpid());
632 if (wl.tty != STDIN_FILENO) {
633 if (setsid() < 0)
634 error(1, errno, "setsid failed");
635 if (ioctl(wl.tty, TIOCSCTTY, 0) < 0)
636 error(1, errno, "TIOCSCTTY failed - tty is in use");
637 }
638
639 if (setgid(wl.pw->pw_gid) < 0 ||
640 setuid(wl.pw->pw_uid) < 0)
641 error(1, errno, "dropping privilidges failed");
642
643 if (sleep_fork) {
644 if (wl.verbose)
645 printf("weston-launch: waiting %d seconds\n", sleep_fork);
646 sleep(sleep_fork);
647 }
648
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100649 env = pam_getenvlist(wl.ph);
650 if (env) {
651 for (i = 0; env[i]; ++i) {
652 if (putenv(env[i]) < 0)
653 error(0, 0, "putenv %s failed", env[i]);
654 }
655 free(env);
656 }
657
658 if (wl.tty != STDIN_FILENO)
659 setenv_fd("WESTON_TTY_FD", wl.tty);
660
661 setenv_fd("WESTON_LAUNCHER_SOCK", wl.sock[1]);
662
663 unsetenv("DISPLAY");
664
665 execv(child_argv[0], child_argv);
666 error(1, errno, "exec failed");
667 break;
668 default:
669 close(wl.sock[1]);
670 if (wl.tty != STDIN_FILENO)
671 close(wl.tty);
672
673 while (1) {
674 struct epoll_event ev;
675 int n;
676
677 n = epoll_wait(wl.epollfd, &ev, 1, -1);
678 if (n < 0)
679 error(0, errno, "epoll_wait failed");
680 if (n != 1)
681 continue;
682
683 if (ev.data.fd == wl.sock[0])
684 handle_socket_msg(&wl);
685 else if (ev.data.fd == wl.signalfd)
686 handle_signal(&wl);
687 }
688 break;
689 }
690
691 return 0;
692}