blob: b438e50e08bd1d6ef357fc5c357523afada7ca17 [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øgsberg636156d2013-07-22 10:35:47 -070076 char *new_user;
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
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700420 if (wl->new_user) {
421 err = pam_close_session(wl->ph, 0);
422 if (err)
423 fprintf(stderr, "pam_close_session failed: %d: %s\n",
424 err, pam_strerror(wl->ph, err));
425 pam_end(wl->ph, err);
426 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100427
428 exit(status);
429}
430
431static int
432handle_signal(struct weston_launch *wl)
433{
434 struct signalfd_siginfo sig;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100435 int pid, status, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100436
437 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
438 error(0, errno, "reading signalfd failed");
439 return -1;
440 }
441
442 switch (sig.ssi_signo) {
443 case SIGCHLD:
444 pid = waitpid(-1, &status, 0);
445 if (pid == wl->child) {
446 wl->child = 0;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100447 if (WIFEXITED(status))
448 ret = WEXITSTATUS(status);
449 else if (WIFSIGNALED(status))
450 /*
451 * If weston dies because of signal N, we
452 * return 10+N. This is distinct from
453 * weston-launch dying because of a signal
454 * (128+N).
455 */
456 ret = 10 + WTERMSIG(status);
457 else
458 ret = 0;
459 quit(wl, ret);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100460 }
461 break;
462 case SIGTERM:
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100463 case SIGINT:
464 if (wl->child)
Kristian Høgsberg1a81abb2013-06-17 15:23:20 -0400465 kill(wl->child, sig.ssi_signo);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100466 break;
467 default:
468 return -1;
469 }
470
471 return 0;
472}
473
474static int
475setup_tty(struct weston_launch *wl, const char *tty)
476{
477 struct stat buf;
478 char *t;
479
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700480 if (!wl->new_user) {
481 wl->tty = STDIN_FILENO;
482 } else if (tty) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100483 t = ttyname(STDIN_FILENO);
484 if (t && strcmp(t, tty) == 0)
485 wl->tty = STDIN_FILENO;
486 else
487 wl->tty = open(tty, O_RDWR | O_NOCTTY);
488 } else {
489 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
490 char filename[16];
491
492 if (tty0 < 0)
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300493 error(1, errno, "could not open tty0");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100494
495 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
496 error(1, errno, "failed to find non-opened console");
497
498 snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
499 wl->tty = open(filename, O_RDWR | O_NOCTTY);
500 close(tty0);
501 }
502
503 if (wl->tty < 0)
504 error(1, errno, "failed to open tty");
505
506 if (tty) {
507 if (fstat(wl->tty, &buf) < 0)
508 error(1, errno, "stat %s failed", tty);
509
510 if (major(buf.st_rdev) != TTY_MAJOR)
511 error(1, 0, "invalid tty device: %s", tty);
512
513 wl->ttynr = minor(buf.st_rdev);
514 }
515
516 return 0;
517}
518
519static void
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700520setup_session(struct weston_launch *wl)
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700521{
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700522 char **env;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700523 char *term;
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700524 int i;
525
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700526 if (wl->tty != STDIN_FILENO) {
527 if (setsid() < 0)
528 error(1, errno, "setsid failed");
529 if (ioctl(wl->tty, TIOCSCTTY, 0) < 0)
530 error(1, errno, "TIOCSCTTY failed - tty is in use");
531 }
532
533 if (setgid(wl->pw->pw_gid) < 0 ||
534#ifdef HAVE_INITGROUPS
535 initgroups(wl->pw->pw_name, wl->pw->pw_gid) < 0 ||
536#endif
537 setuid(wl->pw->pw_uid) < 0)
538 error(1, errno, "dropping privileges failed");
539
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700540 term = getenv("TERM");
541 clearenv();
542 setenv("TERM", term, 1);
543 setenv("USER", wl->pw->pw_name, 1);
544 setenv("LOGNAME", wl->pw->pw_name, 1);
545 setenv("HOME", wl->pw->pw_dir, 1);
546 setenv("SHELL", wl->pw->pw_shell, 1);
547
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700548 env = pam_getenvlist(wl->ph);
549 if (env) {
550 for (i = 0; env[i]; ++i) {
551 if (putenv(env[i]) < 0)
552 error(0, 0, "putenv %s failed", env[i]);
553 }
554 free(env);
555 }
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700556}
557
558static void
559launch_compositor(struct weston_launch *wl, int argc, char *argv[])
560{
561 char *child_argv[MAX_ARGV_SIZE];
562 int i;
563
564 if (wl->verbose)
565 printf("weston-launch: spawned weston with pid: %d\n", getpid());
566 if (wl->new_user)
567 setup_session(wl);
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700568
569 if (wl->tty != STDIN_FILENO)
570 setenv_fd("WESTON_TTY_FD", wl->tty);
571
572 setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]);
573
574 unsetenv("DISPLAY");
575
576 child_argv[0] = wl->pw->pw_shell;
577 child_argv[1] = "-l";
578 child_argv[2] = "-c";
579 child_argv[3] = BINDIR "/weston \"$@\"";
580 child_argv[4] = "weston";
581 for (i = 0; i < argc; ++i)
582 child_argv[5 + i] = argv[i];
583 child_argv[5 + i] = NULL;
584
585 execv(child_argv[0], child_argv);
586 error(1, errno, "exec failed");
587}
588
589static void
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100590help(const char *name)
591{
592 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
593 fprintf(stderr, " -u, --user Start session as specified username\n");
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300594 fprintf(stderr, " -t, --tty Start session on alternative tty\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100595 fprintf(stderr, " -v, --verbose Be verbose\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100596 fprintf(stderr, " -h, --help Display this help message\n");
597}
598
599int
600main(int argc, char *argv[])
601{
602 struct weston_launch wl;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100603 int i, c;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700604 char *tty = NULL;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100605 struct option opts[] = {
606 { "user", required_argument, NULL, 'u' },
607 { "tty", required_argument, NULL, 't' },
608 { "verbose", no_argument, NULL, 'v' },
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100609 { "help", no_argument, NULL, 'h' },
610 { 0, 0, NULL, 0 }
611 };
612
613 memset(&wl, 0, sizeof wl);
614
Kristian Høgsbergab499942013-07-19 21:26:24 -0700615 while ((c = getopt_long(argc, argv, "u:t::vh", opts, &i)) != -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100616 switch (c) {
617 case 'u':
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700618 wl.new_user = optarg;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100619 if (getuid() != 0)
620 error(1, 0, "Permission denied. -u allowed for root only");
621 break;
622 case 't':
623 tty = optarg;
624 break;
625 case 'v':
626 wl.verbose = 1;
627 break;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100628 case 'h':
Scott Moreaucc9acfc2013-01-21 23:40:59 -0700629 help("weston-launch");
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530630 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100631 }
632 }
633
Ander Conselvan de Oliveira9bdfc482013-05-22 22:55:33 +0300634 if ((argc - optind) > (MAX_ARGV_SIZE - 6))
Quentin Glidicff323092013-05-17 16:20:37 +0200635 error(1, E2BIG, "Too many arguments to pass to weston");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100636
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700637 if (wl.new_user)
638 wl.pw = getpwnam(wl.new_user);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100639 else
640 wl.pw = getpwuid(getuid());
641 if (wl.pw == NULL)
642 error(1, errno, "failed to get username");
643
644 if (!weston_launch_allowed(&wl))
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300645 error(1, 0, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100646#ifdef HAVE_SYSTEMD_LOGIN
647 " - run from an active and local (systemd) session.\n"
648#else
649 " - enable systemd session support for weston-launch.\n"
650#endif
Tiago Vignatti314db6e2012-04-17 20:10:11 +0300651 " - or add yourself to the 'weston-launch' group.");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100652
653 if (setup_tty(&wl, tty) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530654 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100655
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700656 if (wl.new_user && setup_pam(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530657 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100658
659 wl.epollfd = epoll_create1(EPOLL_CLOEXEC);
660 if (wl.epollfd < 0)
661 error(1, errno, "epoll create failed");
662
663 if (setup_launcher_socket(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530664 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100665
666 if (setup_signals(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530667 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100668
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700669 wl.child = fork();
670 if (wl.child == -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100671 error(1, errno, "fork failed");
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700672 exit(EXIT_FAILURE);
673 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100674
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700675 if (wl.child == 0)
676 launch_compositor(&wl, argc - optind, argv + optind);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100677
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700678 close(wl.sock[1]);
679 if (wl.tty != STDIN_FILENO)
680 close(wl.tty);
Quentin Glidic735302e2013-06-19 15:27:11 +0200681
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700682 while (1) {
683 struct epoll_event ev;
684 int n;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100685
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700686 n = epoll_wait(wl.epollfd, &ev, 1, -1);
687 if (n < 0)
688 error(0, errno, "epoll_wait failed");
689 if (n != 1)
690 continue;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100691
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700692 if (ev.data.fd == wl.sock[0])
693 handle_socket_msg(&wl);
694 else if (ev.data.fd == wl.signalfd)
695 handle_signal(&wl);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100696 }
697
698 return 0;
699}