weston-launch.c 18 KB
Newer Older
Benjamin Franzke's avatar
Benjamin Franzke committed
1
2
3
/*
 * Copyright © 2012 Benjamin Franzke
 *
4
5
6
7
8
9
10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
Benjamin Franzke's avatar
Benjamin Franzke committed
11
 *
12
13
14
15
16
17
18
19
20
21
22
23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
Benjamin Franzke's avatar
Benjamin Franzke committed
24
25
26
27
 */

#include "config.h"

28
#include <stdbool.h>
Benjamin Franzke's avatar
Benjamin Franzke committed
29
30
31
32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
33
#include <poll.h>
Benjamin Franzke's avatar
Benjamin Franzke committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <errno.h>

#include <getopt.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/signalfd.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>

#include <linux/vt.h>
#include <linux/major.h>
50
#include <linux/kd.h>
Benjamin Franzke's avatar
Benjamin Franzke committed
51
52
53
54
55
56
57
58
59
60
61

#include <pwd.h>
#include <grp.h>
#include <security/pam_appl.h>

#ifdef HAVE_SYSTEMD_LOGIN
#include <systemd/sd-login.h>
#endif

#include "weston-launch.h"

62
63
#define DRM_MAJOR 226

64
65
66
67
#ifndef KDSKBMUTE
#define KDSKBMUTE	0x4B51
#endif

68
69
70
71
#ifndef EVIOCREVOKE
#define EVIOCREVOKE _IOW('E', 0x91, int)
#endif

72
73
#define MAX_ARGV_SIZE 256

74
#ifdef BUILD_DRM_COMPOSITOR
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include <xf86drm.h>

#else

static inline int
drmDropMaster(int drm_fd)
{
	return 0;
}

static inline int
drmSetMaster(int drm_fd)
{
	return 0;
}

#endif

94
95
96
97
98
99
100
101
/* major()/minor() */
#ifdef MAJOR_IN_MKDEV
#    include <sys/mkdev.h>
#endif
#ifdef MAJOR_IN_SYSMACROS
#    include <sys/sysmacros.h>
#endif

Benjamin Franzke's avatar
Benjamin Franzke committed
102
103
104
105
106
107
struct weston_launch {
	struct pam_conv pc;
	pam_handle_t *ph;
	int tty;
	int ttynr;
	int sock[2];
108
	int drm_fd;
109
	int last_input_fd;
110
	int kb_mode;
Benjamin Franzke's avatar
Benjamin Franzke committed
111
112
113
114
115
116
	struct passwd *pw;

	int signalfd;

	pid_t child;
	int verbose;
117
	char *new_user;
Benjamin Franzke's avatar
Benjamin Franzke committed
118
119
};

120
121
union cmsg_data { unsigned char b[4]; int fd; };

Benjamin Franzke's avatar
Benjamin Franzke committed
122
static gid_t *
123
read_groups(int *ngroups)
Benjamin Franzke's avatar
Benjamin Franzke committed
124
125
126
{
	int n;
	gid_t *groups;
Murray Calavera's avatar
Murray Calavera committed
127

Benjamin Franzke's avatar
Benjamin Franzke committed
128
	n = getgroups(0, NULL);
129
130
131
132
133
134

	if (n < 0) {
		fprintf(stderr, "Unable to retrieve groups: %m\n");
		return NULL;
	}

Benjamin Franzke's avatar
Benjamin Franzke committed
135
136
137
138
139
	groups = malloc(n * sizeof(gid_t));
	if (!groups)
		return NULL;

	if (getgroups(n, groups) < 0) {
140
		fprintf(stderr, "Unable to retrieve groups: %m\n");
Benjamin Franzke's avatar
Benjamin Franzke committed
141
142
143
		free(groups);
		return NULL;
	}
144
145

	*ngroups = n;
Benjamin Franzke's avatar
Benjamin Franzke committed
146
147
148
	return groups;
}

149
static bool
Benjamin Franzke's avatar
Benjamin Franzke committed
150
151
152
153
weston_launch_allowed(struct weston_launch *wl)
{
	struct group *gr;
	gid_t *groups;
154
	int ngroups;
Benjamin Franzke's avatar
Benjamin Franzke committed
155
156
157
158
159
160
#ifdef HAVE_SYSTEMD_LOGIN
	char *session, *seat;
	int err;
#endif

	if (getuid() == 0)
161
		return true;
Benjamin Franzke's avatar
Benjamin Franzke committed
162
163
164

	gr = getgrnam("weston-launch");
	if (gr) {
165
166
167
168
		groups = read_groups(&ngroups);
		if (groups && ngroups > 0) {
			while (ngroups--) {
				if (groups[ngroups] == gr->gr_gid) {
Benjamin Franzke's avatar
Benjamin Franzke committed
169
					free(groups);
170
					return true;
Benjamin Franzke's avatar
Benjamin Franzke committed
171
172
173
174
175
176
177
178
179
180
181
182
183
				}
			}
			free(groups);
		}
	}

#ifdef HAVE_SYSTEMD_LOGIN
	err = sd_pid_get_session(getpid(), &session);
	if (err == 0 && session) {
		if (sd_session_is_active(session) &&
		    sd_session_get_seat(session, &seat) == 0) {
			free(seat);
			free(session);
184
			return true;
Benjamin Franzke's avatar
Benjamin Franzke committed
185
186
187
188
		}
		free(session);
	}
#endif
Murray Calavera's avatar
Murray Calavera committed
189

190
	return false;
Benjamin Franzke's avatar
Benjamin Franzke committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
}

static int
pam_conversation_fn(int msg_count,
		    const struct pam_message **messages,
		    struct pam_response **responses,
		    void *user_data)
{
	return PAM_SUCCESS;
}

static int
setup_pam(struct weston_launch *wl)
{
	int err;

	wl->pc.conv = pam_conversation_fn;
	wl->pc.appdata_ptr = wl;

	err = pam_start("login", wl->pw->pw_name, &wl->pc, &wl->ph);
211
212
213
214
215
216
	if (err != PAM_SUCCESS) {
		fprintf(stderr, "failed to start pam transaction: %d: %s\n",
			err, pam_strerror(wl->ph, err));
		return -1;
	}

Benjamin Franzke's avatar
Benjamin Franzke committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	err = pam_set_item(wl->ph, PAM_TTY, ttyname(wl->tty));
	if (err != PAM_SUCCESS) {
		fprintf(stderr, "failed to set PAM_TTY item: %d: %s\n",
			err, pam_strerror(wl->ph, err));
		return -1;
	}

	err = pam_open_session(wl->ph, 0);
	if (err != PAM_SUCCESS) {
		fprintf(stderr, "failed to open pam session: %d: %s\n",
			err, pam_strerror(wl->ph, err));
		return -1;
	}

	return 0;
}

static int
setup_launcher_socket(struct weston_launch *wl)
{
Randy Li's avatar
Randy Li committed
237
238
239
240
241
	if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, wl->sock) < 0) {
		fprintf(stderr, "weston: socketpair failed: %s\n",
			strerror(errno));
		return -1;
	}
Murray Calavera's avatar
Murray Calavera committed
242

Randy Li's avatar
Randy Li committed
243
244
245
246
247
	if (fcntl(wl->sock[0], F_SETFD, FD_CLOEXEC) < 0) {
		fprintf(stderr, "weston: fcntl failed: %s\n",
			strerror(errno));
		return -1;
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
248
249
250
251
252
253
254

	return 0;
}

static int
setup_signals(struct weston_launch *wl)
{
255
	int ret;
Benjamin Franzke's avatar
Benjamin Franzke committed
256
257
258
259
260
261
	sigset_t mask;
	struct sigaction sa;

	memset(&sa, 0, sizeof sa);
	sa.sa_handler = SIG_DFL;
	sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
262
263
	ret = sigaction(SIGCHLD, &sa, NULL);
	assert(ret == 0);
Benjamin Franzke's avatar
Benjamin Franzke committed
264

265
266
267
268
	sa.sa_handler = SIG_IGN;
	sa.sa_flags = 0;
	sigaction(SIGHUP, &sa, NULL);

269
270
	ret = sigemptyset(&mask);
	assert(ret == 0);
Benjamin Franzke's avatar
Benjamin Franzke committed
271
272
273
	sigaddset(&mask, SIGCHLD);
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGTERM);
274
275
	sigaddset(&mask, SIGUSR1);
	sigaddset(&mask, SIGUSR2);
276
277
	ret = sigprocmask(SIG_BLOCK, &mask, NULL);
	assert(ret == 0);
Benjamin Franzke's avatar
Benjamin Franzke committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

	wl->signalfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
	if (wl->signalfd < 0)
		return -errno;

	return 0;
}

static void
setenv_fd(const char *env, int fd)
{
	char buf[32];

	snprintf(buf, sizeof buf, "%d", fd);
	setenv(env, buf, 1);
}

static int
296
send_reply(struct weston_launch *wl, int reply)
Benjamin Franzke's avatar
Benjamin Franzke committed
297
{
298
	int len;
Benjamin Franzke's avatar
Benjamin Franzke committed
299
300

	do {
301
		len = send(wl->sock[0], &reply, sizeof reply, 0);
Benjamin Franzke's avatar
Benjamin Franzke committed
302
303
	} while (len < 0 && errno == EINTR);

304
	return len;
Benjamin Franzke's avatar
Benjamin Franzke committed
305
306
307
308
309
310
311
312
313
314
315
316
}

static int
handle_open(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
{
	int fd = -1, ret = -1;
	char control[CMSG_SPACE(sizeof(fd))];
	struct cmsghdr *cmsg;
	struct stat s;
	struct msghdr nmsg;
	struct iovec iov;
	struct weston_launcher_open *message;
317
	union cmsg_data *data;
Benjamin Franzke's avatar
Benjamin Franzke committed
318
319
320
321
322
323
324
325
326

	message = msg->msg_iov->iov_base;
	if ((size_t)len < sizeof(*message))
		goto err0;

	/* Ensure path is null-terminated */
	((char *) message)[len-1] = '\0';

	fd = open(message->path, message->flags);
327
328
329
	if (fd < 0) {
		fprintf(stderr, "Error opening device %s: %m\n",
			message->path);
Benjamin Franzke's avatar
Benjamin Franzke committed
330
		goto err0;
331
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
332

333
334
335
336
337
338
339
	if (fstat(fd, &s) < 0) {
		close(fd);
		fd = -1;
		fprintf(stderr, "Failed to stat %s\n", message->path);
		goto err0;
	}

340
341
	if (major(s.st_rdev) != INPUT_MAJOR &&
	    major(s.st_rdev) != DRM_MAJOR) {
Benjamin Franzke's avatar
Benjamin Franzke committed
342
343
		close(fd);
		fd = -1;
344
		fprintf(stderr, "Device %s is not an input or drm device\n",
345
			message->path);
Benjamin Franzke's avatar
Benjamin Franzke committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
		goto err0;
	}

err0:
	memset(&nmsg, 0, sizeof nmsg);
	nmsg.msg_iov = &iov;
	nmsg.msg_iovlen = 1;
	if (fd != -1) {
		nmsg.msg_control = control;
		nmsg.msg_controllen = sizeof control;
		cmsg = CMSG_FIRSTHDR(&nmsg);
		cmsg->cmsg_level = SOL_SOCKET;
		cmsg->cmsg_type = SCM_RIGHTS;
		cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
360
361
		data = (union cmsg_data *) CMSG_DATA(cmsg);
		data->fd = fd;
Benjamin Franzke's avatar
Benjamin Franzke committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
		nmsg.msg_controllen = cmsg->cmsg_len;
		ret = 0;
	}
	iov.iov_base = &ret;
	iov.iov_len = sizeof ret;

	if (wl->verbose)
		fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
			message->path, ret, fd);
	do {
		len = sendmsg(wl->sock[0], &nmsg, 0);
	} while (len < 0 && errno == EINTR);

	if (len < 0)
		return -1;

378
	if (fd != -1 && major(s.st_rdev) == DRM_MAJOR)
379
		wl->drm_fd = fd;
380
381
382
	if (fd != -1 && major(s.st_rdev) == INPUT_MAJOR &&
	    wl->last_input_fd < fd)
		wl->last_input_fd = fd;
383

Benjamin Franzke's avatar
Benjamin Franzke committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
	return 0;
}

static int
handle_socket_msg(struct weston_launch *wl)
{
	char control[CMSG_SPACE(sizeof(int))];
	char buf[BUFSIZ];
	struct msghdr msg;
	struct iovec iov;
	int ret = -1;
	ssize_t len;
	struct weston_launcher_message *message;

	memset(&msg, 0, sizeof(msg));
	iov.iov_base = buf;
	iov.iov_len  = sizeof buf;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = control;
	msg.msg_controllen = sizeof control;

	do {
		len = recvmsg(wl->sock[0], &msg, 0);
	} while (len < 0 && errno == EINTR);

	if (len < 1)
		return -1;

	message = (void *) buf;
	switch (message->opcode) {
	case WESTON_LAUNCHER_OPEN:
		ret = handle_open(wl, &msg, len);
		break;
	}

	return ret;
}

static void
quit(struct weston_launch *wl, int status)
{
426
	struct vt_mode mode = { 0 };
Benjamin Franzke's avatar
Benjamin Franzke committed
427
428
429
430
431
	int err;

	close(wl->signalfd);
	close(wl->sock[0]);

432
433
434
435
436
437
438
	if (wl->new_user) {
		err = pam_close_session(wl->ph, 0);
		if (err)
			fprintf(stderr, "pam_close_session failed: %d: %s\n",
				err, pam_strerror(wl->ph, err));
		pam_end(wl->ph, err);
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
439

440
441
	if (ioctl(wl->tty, KDSKBMUTE, 0) &&
	    ioctl(wl->tty, KDSKBMODE, wl->kb_mode))
442
443
444
445
446
		fprintf(stderr, "failed to restore keyboard mode: %m\n");

	if (ioctl(wl->tty, KDSETMODE, KD_TEXT))
		fprintf(stderr, "failed to set KD_TEXT mode on tty: %m\n");

447
448
449
450
451
	/* We have to drop master before we switch the VT back in
	 * VT_AUTO, so we don't risk switching to a VT with another
	 * display server, that will then fail to set drm master. */
	drmDropMaster(wl->drm_fd);

452
453
454
455
	mode.mode = VT_AUTO;
	if (ioctl(wl->tty, VT_SETMODE, &mode) < 0)
		fprintf(stderr, "could not reset vt handling\n");

Benjamin Franzke's avatar
Benjamin Franzke committed
456
457
458
	exit(status);
}

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
static void
close_input_fds(struct weston_launch *wl)
{
	struct stat s;
	int fd;

	for (fd = 3; fd <= wl->last_input_fd; fd++) {
		if (fstat(fd, &s) == 0 && major(s.st_rdev) == INPUT_MAJOR) {
			/* EVIOCREVOKE may fail if the kernel doesn't
			 * support it, but all we can do is ignore it. */
			ioctl(fd, EVIOCREVOKE, 0);
			close(fd);
		}
	}
}

Benjamin Franzke's avatar
Benjamin Franzke committed
475
476
477
478
static int
handle_signal(struct weston_launch *wl)
{
	struct signalfd_siginfo sig;
479
	int pid, status, ret;
Benjamin Franzke's avatar
Benjamin Franzke committed
480
481

	if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
Randy Li's avatar
Randy Li committed
482
483
		fprintf(stderr, "weston: reading signalfd failed: %s\n",
			strerror(errno));
Benjamin Franzke's avatar
Benjamin Franzke committed
484
485
486
487
488
489
490
491
		return -1;
	}

	switch (sig.ssi_signo) {
	case SIGCHLD:
		pid = waitpid(-1, &status, 0);
		if (pid == wl->child) {
			wl->child = 0;
492
493
494
495
496
497
498
499
500
501
502
503
504
			if (WIFEXITED(status))
				ret = WEXITSTATUS(status);
			else if (WIFSIGNALED(status))
				/*
				 * If weston dies because of signal N, we
				 * return 10+N. This is distinct from
				 * weston-launch dying because of a signal
				 * (128+N).
				 */
				ret = 10 + WTERMSIG(status);
			else
				ret = 0;
			quit(wl, ret);
Benjamin Franzke's avatar
Benjamin Franzke committed
505
506
507
508
509
		}
		break;
	case SIGTERM:
	case SIGINT:
		if (wl->child)
510
			kill(wl->child, sig.ssi_signo);
Benjamin Franzke's avatar
Benjamin Franzke committed
511
		break;
512
513
	case SIGUSR1:
		send_reply(wl, WESTON_LAUNCHER_DEACTIVATE);
514
		close_input_fds(wl);
515
516
517
518
519
520
521
522
		drmDropMaster(wl->drm_fd);
		ioctl(wl->tty, VT_RELDISP, 1);
		break;
	case SIGUSR2:
		ioctl(wl->tty, VT_RELDISP, VT_ACKACQ);
		drmSetMaster(wl->drm_fd);
		send_reply(wl, WESTON_LAUNCHER_ACTIVATE);
		break;
Benjamin Franzke's avatar
Benjamin Franzke committed
523
524
525
526
527
528
529
530
531
532
533
	default:
		return -1;
	}

	return 0;
}

static int
setup_tty(struct weston_launch *wl, const char *tty)
{
	struct stat buf;
534
	struct vt_mode mode = { 0 };
Benjamin Franzke's avatar
Benjamin Franzke committed
535
536
	char *t;

537
538
539
	if (!wl->new_user) {
		wl->tty = STDIN_FILENO;
	} else if (tty) {
Benjamin Franzke's avatar
Benjamin Franzke committed
540
541
542
543
544
545
546
547
548
		t = ttyname(STDIN_FILENO);
		if (t && strcmp(t, tty) == 0)
			wl->tty = STDIN_FILENO;
		else
			wl->tty = open(tty, O_RDWR | O_NOCTTY);
	} else {
		int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
		char filename[16];

Randy Li's avatar
Randy Li committed
549
550
551
552
553
		if (tty0 < 0) {
			fprintf(stderr, "weston: could not open tty0: %s\n",
				strerror(errno));
			return -1;
		}
Benjamin Franzke's avatar
Benjamin Franzke committed
554
555

		if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
Randy Li's avatar
Randy Li committed
556
557
558
559
560
		{
			fprintf(stderr, "weston: failed to find non-opened console: %s\n",
				strerror(errno));
			return -1;
		}
Benjamin Franzke's avatar
Benjamin Franzke committed
561
562
563
564
565
566

		snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
		wl->tty = open(filename, O_RDWR | O_NOCTTY);
		close(tty0);
	}

Randy Li's avatar
Randy Li committed
567
568
569
570
571
	if (wl->tty < 0) {
		fprintf(stderr, "weston: failed to open tty: %s\n",
			strerror(errno));
		return -1;
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
572

573
	if (fstat(wl->tty, &buf) == -1 ||
Randy Li's avatar
Randy Li committed
574
575
576
577
	    major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
		fprintf(stderr, "weston: weston-launch must be run from a virtual terminal\n");
		return -1;
	}
578

Benjamin Franzke's avatar
Benjamin Franzke committed
579
	if (tty) {
Randy Li's avatar
Randy Li committed
580
581
582
583
584
		if (fstat(wl->tty, &buf) < 0) {
			fprintf(stderr, "weston: stat %s failed: %s\n", tty,
				strerror(errno));
			return -1;
		}
Benjamin Franzke's avatar
Benjamin Franzke committed
585

Randy Li's avatar
Randy Li committed
586
587
588
589
590
		if (major(buf.st_rdev) != TTY_MAJOR) {
			fprintf(stderr,
				"weston: invalid tty device: %s\n", tty);
			return -1;
		}
Benjamin Franzke's avatar
Benjamin Franzke committed
591
592
593
594

		wl->ttynr = minor(buf.st_rdev);
	}

Randy Li's avatar
Randy Li committed
595
596
597
598
599
600
	if (ioctl(wl->tty, KDGKBMODE, &wl->kb_mode)) {
		fprintf(stderr,
			"weston: failed to get current keyboard mode: %s",
			strerror(errno));
		return -1;
	}
601

602
	if (ioctl(wl->tty, KDSKBMUTE, 1) &&
Randy Li's avatar
Randy Li committed
603
604
605
606
607
608
	    ioctl(wl->tty, KDSKBMODE, K_OFF)) {
		fprintf(stderr,
			"weston: failed to set K_OFF keyboard mode: %s\n",
			strerror(errno));
		return -1;
	}
609

Randy Li's avatar
Randy Li committed
610
611
612
613
614
615
	if (ioctl(wl->tty, KDSETMODE, KD_GRAPHICS)) {
		fprintf(stderr,
			"weston: failed to set KD_GRAPHICS mode on tty: %s\n",
			strerror(errno));
		return -1;
	}
616

617
618
619
	mode.mode = VT_PROCESS;
	mode.relsig = SIGUSR1;
	mode.acqsig = SIGUSR2;
Randy Li's avatar
Randy Li committed
620
621
622
623
624
625
	if (ioctl(wl->tty, VT_SETMODE, &mode) < 0) {
		fprintf(stderr,
			"weston: failed to take control of vt handling %s\n",
			strerror(errno));
		return -1;
	}
626

Benjamin Franzke's avatar
Benjamin Franzke committed
627
628
629
	return 0;
}

630
631
static int
setup_session(struct weston_launch *wl, char **child_argv)
632
633
{
	char **env;
634
	char *term;
635
636
637
	int i;

	if (wl->tty != STDIN_FILENO) {
Randy Li's avatar
Randy Li committed
638
639
640
641
642
643
644
645
646
647
		if (setsid() < 0) {
			fprintf(stderr, "weston: setsid failed %s\n",
				strerror(errno));
			exit(EXIT_FAILURE);
		}
		if (ioctl(wl->tty, TIOCSCTTY, 0) < 0) {
			fprintf(stderr, "TIOCSCTTY failed - tty is in use %s\n",
				strerror(errno));
			exit(EXIT_FAILURE);
		}
648
649
	}

650
651
	term = getenv("TERM");
	clearenv();
652
653
	if (term)
		setenv("TERM", term, 1);
654
655
656
657
658
	setenv("USER", wl->pw->pw_name, 1);
	setenv("LOGNAME", wl->pw->pw_name, 1);
	setenv("HOME", wl->pw->pw_dir, 1);
	setenv("SHELL", wl->pw->pw_shell, 1);

659
660
661
	env = pam_getenvlist(wl->ph);
	if (env) {
		for (i = 0; env[i]; ++i) {
662
			if (putenv(env[i]) != 0)
Randy Li's avatar
Randy Li committed
663
				fprintf(stderr, "putenv %s failed\n", env[i]);
664
665
666
		}
		free(env);
	}
667
668
669
670
671
672
673
674
675
676
677

	/*
	 * We open a new session, so it makes sense
	 * to run a new login shell
	 */
	child_argv[0] = "/bin/sh";
	child_argv[1] = "-l";
	child_argv[2] = "-c";
	child_argv[3] = BINDIR "/weston \"$@\"";
	child_argv[4] = "weston";
	return 5;
678
679
}

680
681
682
683
684
685
686
static void
drop_privileges(struct weston_launch *wl)
{
	if (setgid(wl->pw->pw_gid) < 0 ||
#ifdef HAVE_INITGROUPS
	    initgroups(wl->pw->pw_name, wl->pw->pw_gid) < 0 ||
#endif
Randy Li's avatar
Randy Li committed
687
688
689
690
691
	    setuid(wl->pw->pw_uid) < 0) {
		fprintf(stderr, "weston: dropping privileges failed %s\n",
			strerror(errno));
		exit(EXIT_FAILURE);
	}
692
693
}

694
695
696
697
static void
launch_compositor(struct weston_launch *wl, int argc, char *argv[])
{
	char *child_argv[MAX_ARGV_SIZE];
698
	sigset_t mask;
699
	int o, i;
700
701
702

	if (wl->verbose)
		printf("weston-launch: spawned weston with pid: %d\n", getpid());
703
704
705
706
707
708
709
710
711
	if (wl->new_user) {
		o = setup_session(wl, child_argv);
	} else {
		child_argv[0] = BINDIR "/weston";
		o = 1;
	}
	for (i = 0; i < argc; ++i)
		child_argv[o + i] = argv[i];
	child_argv[o + i] = NULL;
712

713
714
	if (geteuid() == 0)
		drop_privileges(wl);
715

716
	setenv_fd("WESTON_TTY_FD", wl->tty);
717
718
719
720
	setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]);

	unsetenv("DISPLAY");

721
722
723
724
725
726
727
	/* Do not give our signal mask to the new process. */
	sigemptyset(&mask);
	sigaddset(&mask, SIGTERM);
	sigaddset(&mask, SIGCHLD);
	sigaddset(&mask, SIGINT);
	sigprocmask(SIG_UNBLOCK, &mask, NULL);

728
729

	execv(child_argv[0], child_argv);
Randy Li's avatar
Randy Li committed
730
731
	fprintf(stderr, "weston: exec failed: %s\n", strerror(errno));
	exit(EXIT_FAILURE);
732
733
}

Benjamin Franzke's avatar
Benjamin Franzke committed
734
735
736
737
static void
help(const char *name)
{
	fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
738
739
740
741
	fprintf(stderr, "  -u, --user      Start session as specified username,\n"
			"                  e.g. -u joe, requires root.\n");
	fprintf(stderr, "  -t, --tty       Start session on alternative tty,\n"
			"                  e.g. -t /dev/tty4, requires -u option.\n");
Benjamin Franzke's avatar
Benjamin Franzke committed
742
743
744
745
746
747
748
749
750
	fprintf(stderr, "  -v, --verbose   Be verbose\n");
	fprintf(stderr, "  -h, --help      Display this help message\n");
}

int
main(int argc, char *argv[])
{
	struct weston_launch wl;
	int i, c;
751
	char *tty = NULL;
Benjamin Franzke's avatar
Benjamin Franzke committed
752
753
754
755
756
757
	struct option opts[] = {
		{ "user",    required_argument, NULL, 'u' },
		{ "tty",     required_argument, NULL, 't' },
		{ "verbose", no_argument,       NULL, 'v' },
		{ "help",    no_argument,       NULL, 'h' },
		{ 0,         0,                 NULL,  0  }
Murray Calavera's avatar
Murray Calavera committed
758
	};
Benjamin Franzke's avatar
Benjamin Franzke committed
759
760
761

	memset(&wl, 0, sizeof wl);

762
	while ((c = getopt_long(argc, argv, "u:t:vh", opts, &i)) != -1) {
Benjamin Franzke's avatar
Benjamin Franzke committed
763
764
		switch (c) {
		case 'u':
765
			wl.new_user = optarg;
Randy Li's avatar
Randy Li committed
766
767
768
769
			if (getuid() != 0) {
				fprintf(stderr, "weston: Permission denied. -u allowed for root only\n");
				exit(EXIT_FAILURE);
			}
Benjamin Franzke's avatar
Benjamin Franzke committed
770
771
772
773
774
775
776
777
			break;
		case 't':
			tty = optarg;
			break;
		case 'v':
			wl.verbose = 1;
			break;
		case 'h':
778
			help("weston-launch");
779
			exit(EXIT_FAILURE);
780
781
		default:
			exit(EXIT_FAILURE);
Benjamin Franzke's avatar
Benjamin Franzke committed
782
783
784
		}
	}

Randy Li's avatar
Randy Li committed
785
786
787
788
789
790
	if ((argc - optind) > (MAX_ARGV_SIZE - 6)) {
		fprintf(stderr,
			"weston: Too many arguments to pass to weston: %s\n",
			strerror(E2BIG));
		exit(EXIT_FAILURE);
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
791

Randy Li's avatar
Randy Li committed
792
793
794
795
	if (tty && !wl.new_user) {
		fprintf(stderr, "weston: -t/--tty option requires -u/--user option as well\n");
		exit(EXIT_FAILURE);
	}
796

797
798
	if (wl.new_user)
		wl.pw = getpwnam(wl.new_user);
Benjamin Franzke's avatar
Benjamin Franzke committed
799
800
	else
		wl.pw = getpwuid(getuid());
Randy Li's avatar
Randy Li committed
801
802
803
804
805
	if (wl.pw == NULL) {
		fprintf(stderr, "weston: failed to get username: %s\n",
			strerror(errno));
		exit(EXIT_FAILURE);
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
806

Randy Li's avatar
Randy Li committed
807
808
	if (!weston_launch_allowed(&wl)) {
		fprintf(stderr, "Permission denied. You should either:\n"
Benjamin Franzke's avatar
Benjamin Franzke committed
809
810
811
812
813
#ifdef HAVE_SYSTEMD_LOGIN
		      " - run from an active and local (systemd) session.\n"
#else
		      " - enable systemd session support for weston-launch.\n"
#endif
Randy Li's avatar
Randy Li committed
814
815
816
		      " - or add yourself to the 'weston-launch' group.\n");
		exit(EXIT_FAILURE);
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
817
818

	if (setup_tty(&wl, tty) < 0)
819
		exit(EXIT_FAILURE);
Benjamin Franzke's avatar
Benjamin Franzke committed
820

821
	if (wl.new_user && setup_pam(&wl) < 0)
822
		exit(EXIT_FAILURE);
Benjamin Franzke's avatar
Benjamin Franzke committed
823
824

	if (setup_launcher_socket(&wl) < 0)
825
		exit(EXIT_FAILURE);
Benjamin Franzke's avatar
Benjamin Franzke committed
826
827

	if (setup_signals(&wl) < 0)
828
		exit(EXIT_FAILURE);
Benjamin Franzke's avatar
Benjamin Franzke committed
829

830
	wl.child = fork();
Randy Li's avatar
Randy Li committed
831
832
833
834
	if (wl.child == -1) {
		fprintf(stderr, "weston: fork failed %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
835

836
837
	if (wl.child == 0)
		launch_compositor(&wl, argc - optind, argv + optind);
Benjamin Franzke's avatar
Benjamin Franzke committed
838

839
840
841
	close(wl.sock[1]);
	if (wl.tty != STDIN_FILENO)
		close(wl.tty);
Benjamin Franzke's avatar
Benjamin Franzke committed
842

843
	while (1) {
844
		struct pollfd fds[2];
845
		int n;
Benjamin Franzke's avatar
Benjamin Franzke committed
846

847
848
849
850
		fds[0].fd = wl.sock[0];
		fds[0].events = POLLIN;
		fds[1].fd = wl.signalfd;
		fds[1].events = POLLIN;
Benjamin Franzke's avatar
Benjamin Franzke committed
851

852
853
854
855
		n = poll(fds, 2, -1);
		if (n < 0)
			error(0, errno, "poll failed");
		if (fds[0].revents & POLLIN)
856
			handle_socket_msg(&wl);
857
		if (fds[1].revents)
858
			handle_signal(&wl);
Benjamin Franzke's avatar
Benjamin Franzke committed
859
860
861
862
	}

	return 0;
}