blob: cafa5ac7916690615a45f4bb6c88f814b5bfe603 [file] [log] [blame]
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001/*
2 * Copyright © 2012 Benjamin Franzke
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Benjamin Franzkebfeda132012-01-30 14:04:04 +010011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Benjamin Franzkebfeda132012-01-30 14:04:04 +010024 */
25
Benjamin Franzkebfeda132012-01-30 14:04:04 +010026#include "config.h"
27
Derek Foreman280e7dd2014-10-03 13:13:42 -050028#include <stdbool.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <assert.h>
Kristian Høgsbergf2807702013-07-23 11:43:03 -070033#include <poll.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010034#include <errno.h>
35
Benjamin Franzkebfeda132012-01-30 14:04:04 +010036#include <getopt.h>
37
38#include <sys/types.h>
39#include <sys/ioctl.h>
40#include <sys/stat.h>
41#include <sys/wait.h>
42#include <sys/socket.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010043#include <sys/signalfd.h>
44#include <signal.h>
45#include <unistd.h>
46#include <fcntl.h>
47
Benjamin Franzkebfeda132012-01-30 14:04:04 +010048#include <linux/vt.h>
49#include <linux/major.h>
Kristian Høgsberg81b49632013-09-17 22:43:22 -070050#include <linux/kd.h>
Benjamin Franzkebfeda132012-01-30 14:04:04 +010051
52#include <pwd.h>
53#include <grp.h>
leng.fang32af9fc2024-06-13 11:22:15 +080054#ifdef HAVE_PAM
Benjamin Franzkebfeda132012-01-30 14:04:04 +010055#include <security/pam_appl.h>
leng.fang32af9fc2024-06-13 11:22:15 +080056#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +010057
Benjamin Franzkebfeda132012-01-30 14:04:04 +010058#ifdef HAVE_SYSTEMD_LOGIN
59#include <systemd/sd-login.h>
60#endif
61
62#include "weston-launch.h"
63
Kristian Høgsberg1ff51092013-09-17 14:03:42 -070064#define DRM_MAJOR 226
65
Kristian Høgsberg3f495872013-09-18 23:00:17 -070066#ifndef KDSKBMUTE
67#define KDSKBMUTE 0x4B51
68#endif
69
Kristian Høgsberg0eee0a22013-10-25 13:34:58 -070070#ifndef EVIOCREVOKE
71#define EVIOCREVOKE _IOW('E', 0x91, int)
72#endif
73
Quentin Glidicff323092013-05-17 16:20:37 +020074#define MAX_ARGV_SIZE 256
75
Pekka Paalanen2667e9e2017-04-06 13:18:59 +030076#ifdef BUILD_DRM_COMPOSITOR
Kristian Høgsbergd2c9d8a2013-11-24 14:37:07 -080077
78#include <xf86drm.h>
79
80#else
81
82static inline int
83drmDropMaster(int drm_fd)
84{
85 return 0;
86}
87
88static inline int
89drmSetMaster(int drm_fd)
90{
91 return 0;
92}
93
94#endif
95
Sergei Trofimovich43c5a652017-05-31 22:17:50 +010096/* major()/minor() */
97#ifdef MAJOR_IN_MKDEV
98# include <sys/mkdev.h>
99#endif
100#ifdef MAJOR_IN_SYSMACROS
101# include <sys/sysmacros.h>
102#endif
103
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100104struct weston_launch {
leng.fang32af9fc2024-06-13 11:22:15 +0800105#ifdef HAVE_PAM
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100106 struct pam_conv pc;
107 pam_handle_t *ph;
leng.fang32af9fc2024-06-13 11:22:15 +0800108#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100109 int tty;
110 int ttynr;
111 int sock[2];
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700112 int drm_fd;
Kristian Høgsberg0eee0a22013-10-25 13:34:58 -0700113 int last_input_fd;
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700114 int kb_mode;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100115 struct passwd *pw;
116
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100117 int signalfd;
118
119 pid_t child;
120 int verbose;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700121 char *new_user;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100122};
123
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400124union cmsg_data { unsigned char b[4]; int fd; };
125
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100126static gid_t *
Peter Hutterer84bc4032018-06-11 09:57:04 +1000127read_groups(int *ngroups)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100128{
129 int n;
130 gid_t *groups;
Murray Calavera883ac022015-06-06 13:02:22 +0000131
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100132 n = getgroups(0, NULL);
Rob Bradford40be7b42012-12-05 18:47:11 +0000133
134 if (n < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200135 fprintf(stderr, "Unable to retrieve groups: %s\n",
136 strerror(errno));
Rob Bradford40be7b42012-12-05 18:47:11 +0000137 return NULL;
138 }
139
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100140 groups = malloc(n * sizeof(gid_t));
141 if (!groups)
142 return NULL;
143
144 if (getgroups(n, groups) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200145 fprintf(stderr, "Unable to retrieve groups: %s\n",
146 strerror(errno));
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100147 free(groups);
148 return NULL;
149 }
Peter Hutterer84bc4032018-06-11 09:57:04 +1000150
151 *ngroups = n;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100152 return groups;
153}
154
Derek Foreman280e7dd2014-10-03 13:13:42 -0500155static bool
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100156weston_launch_allowed(struct weston_launch *wl)
157{
158 struct group *gr;
159 gid_t *groups;
Peter Hutterer84bc4032018-06-11 09:57:04 +1000160 int ngroups;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100161#ifdef HAVE_SYSTEMD_LOGIN
162 char *session, *seat;
163 int err;
164#endif
165
166 if (getuid() == 0)
Derek Foreman280e7dd2014-10-03 13:13:42 -0500167 return true;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100168
169 gr = getgrnam("weston-launch");
170 if (gr) {
Peter Hutterer84bc4032018-06-11 09:57:04 +1000171 groups = read_groups(&ngroups);
172 if (groups && ngroups > 0) {
173 while (ngroups--) {
174 if (groups[ngroups] == gr->gr_gid) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100175 free(groups);
Derek Foreman280e7dd2014-10-03 13:13:42 -0500176 return true;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100177 }
178 }
179 free(groups);
180 }
181 }
182
183#ifdef HAVE_SYSTEMD_LOGIN
184 err = sd_pid_get_session(getpid(), &session);
185 if (err == 0 && session) {
186 if (sd_session_is_active(session) &&
187 sd_session_get_seat(session, &seat) == 0) {
188 free(seat);
189 free(session);
Derek Foreman280e7dd2014-10-03 13:13:42 -0500190 return true;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100191 }
192 free(session);
193 }
194#endif
Murray Calavera883ac022015-06-06 13:02:22 +0000195
Derek Foreman280e7dd2014-10-03 13:13:42 -0500196 return false;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100197}
198
leng.fang32af9fc2024-06-13 11:22:15 +0800199#ifdef HAVE_PAM
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100200static int
201pam_conversation_fn(int msg_count,
202 const struct pam_message **messages,
203 struct pam_response **responses,
204 void *user_data)
205{
206 return PAM_SUCCESS;
207}
208
209static int
210setup_pam(struct weston_launch *wl)
211{
212 int err;
213
214 wl->pc.conv = pam_conversation_fn;
215 wl->pc.appdata_ptr = wl;
216
217 err = pam_start("login", wl->pw->pw_name, &wl->pc, &wl->ph);
John Kåre Alsaker5b90d8f2012-10-12 12:25:05 +0200218 if (err != PAM_SUCCESS) {
219 fprintf(stderr, "failed to start pam transaction: %d: %s\n",
220 err, pam_strerror(wl->ph, err));
221 return -1;
222 }
223
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100224 err = pam_set_item(wl->ph, PAM_TTY, ttyname(wl->tty));
225 if (err != PAM_SUCCESS) {
226 fprintf(stderr, "failed to set PAM_TTY item: %d: %s\n",
227 err, pam_strerror(wl->ph, err));
228 return -1;
229 }
230
231 err = pam_open_session(wl->ph, 0);
232 if (err != PAM_SUCCESS) {
233 fprintf(stderr, "failed to open pam session: %d: %s\n",
234 err, pam_strerror(wl->ph, err));
235 return -1;
236 }
237
238 return 0;
239}
leng.fang32af9fc2024-06-13 11:22:15 +0800240#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100241
242static int
243setup_launcher_socket(struct weston_launch *wl)
244{
Randy Lia1450a82019-04-18 11:37:12 +0800245 if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, wl->sock) < 0) {
246 fprintf(stderr, "weston: socketpair failed: %s\n",
247 strerror(errno));
248 return -1;
249 }
Murray Calavera883ac022015-06-06 13:02:22 +0000250
Randy Lia1450a82019-04-18 11:37:12 +0800251 if (fcntl(wl->sock[0], F_SETFD, FD_CLOEXEC) < 0) {
252 fprintf(stderr, "weston: fcntl failed: %s\n",
253 strerror(errno));
254 return -1;
255 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100256
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100257 return 0;
258}
259
260static int
261setup_signals(struct weston_launch *wl)
262{
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100263 int ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100264 sigset_t mask;
265 struct sigaction sa;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100266
267 memset(&sa, 0, sizeof sa);
268 sa.sa_handler = SIG_DFL;
269 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100270 ret = sigaction(SIGCHLD, &sa, NULL);
271 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100272
Kristian Høgsberg18684d42013-07-22 11:59:18 -0700273 sa.sa_handler = SIG_IGN;
274 sa.sa_flags = 0;
275 sigaction(SIGHUP, &sa, NULL);
276
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100277 ret = sigemptyset(&mask);
278 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100279 sigaddset(&mask, SIGCHLD);
280 sigaddset(&mask, SIGINT);
281 sigaddset(&mask, SIGTERM);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700282 sigaddset(&mask, SIGUSR1);
283 sigaddset(&mask, SIGUSR2);
Philipp Brüschweilerff253122013-03-09 19:38:56 +0100284 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
285 assert(ret == 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100286
287 wl->signalfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
288 if (wl->signalfd < 0)
289 return -errno;
290
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100291 return 0;
292}
293
294static void
295setenv_fd(const char *env, int fd)
296{
297 char buf[32];
298
299 snprintf(buf, sizeof buf, "%d", fd);
300 setenv(env, buf, 1);
301}
302
303static int
Stefan Agner247392a2020-01-22 22:23:07 +0100304open_tty_by_number(int ttynr)
305{
306 int ret;
307 char filename[16];
308
309 ret = snprintf(filename, sizeof filename, "/dev/tty%d", ttynr);
310 if (ret < 0)
311 return -1;
312
313 return open(filename, O_RDWR | O_NOCTTY);
314}
315
316static int
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700317send_reply(struct weston_launch *wl, int reply)
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100318{
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700319 int len;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100320
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100321 do {
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700322 len = send(wl->sock[0], &reply, sizeof reply, 0);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100323 } while (len < 0 && errno == EINTR);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100324
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700325 return len;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100326}
327
328static int
329handle_open(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
330{
331 int fd = -1, ret = -1;
332 char control[CMSG_SPACE(sizeof(fd))];
333 struct cmsghdr *cmsg;
334 struct stat s;
335 struct msghdr nmsg;
336 struct iovec iov;
337 struct weston_launcher_open *message;
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400338 union cmsg_data *data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100339
340 message = msg->msg_iov->iov_base;
341 if ((size_t)len < sizeof(*message))
342 goto err0;
343
344 /* Ensure path is null-terminated */
345 ((char *) message)[len-1] = '\0';
346
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100347 fd = open(message->path, message->flags);
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100348 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200349 fprintf(stderr, "Error opening device %s: %s\n",
350 message->path, strerror(errno));
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100351 goto err0;
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100352 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100353
Kristian Høgsberg6a7c8492013-09-18 22:14:09 -0700354 if (fstat(fd, &s) < 0) {
355 close(fd);
356 fd = -1;
357 fprintf(stderr, "Failed to stat %s\n", message->path);
358 goto err0;
359 }
360
Kristian Høgsberg1ff51092013-09-17 14:03:42 -0700361 if (major(s.st_rdev) != INPUT_MAJOR &&
362 major(s.st_rdev) != DRM_MAJOR) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100363 close(fd);
364 fd = -1;
Kristian Høgsberg1ff51092013-09-17 14:03:42 -0700365 fprintf(stderr, "Device %s is not an input or drm device\n",
Rob Bradfordd33f2b02013-05-20 16:55:10 +0100366 message->path);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100367 goto err0;
368 }
369
370err0:
371 memset(&nmsg, 0, sizeof nmsg);
372 nmsg.msg_iov = &iov;
373 nmsg.msg_iovlen = 1;
374 if (fd != -1) {
375 nmsg.msg_control = control;
376 nmsg.msg_controllen = sizeof control;
377 cmsg = CMSG_FIRSTHDR(&nmsg);
378 cmsg->cmsg_level = SOL_SOCKET;
379 cmsg->cmsg_type = SCM_RIGHTS;
380 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
Kristian Høgsberg9e140912012-04-10 01:26:18 -0400381 data = (union cmsg_data *) CMSG_DATA(cmsg);
382 data->fd = fd;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100383 nmsg.msg_controllen = cmsg->cmsg_len;
384 ret = 0;
385 }
Jonathan Marlerf153c492021-03-27 09:55:59 -0600386 struct { int reply_id; int ret; } reply_iov_data = { WESTON_LAUNCHER_OPEN_REPLY, ret };
387 iov.iov_base = &reply_iov_data;
388 iov.iov_len = sizeof reply_iov_data;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100389
390 if (wl->verbose)
391 fprintf(stderr, "weston-launch: opened %s: ret: %d, fd: %d\n",
392 message->path, ret, fd);
393 do {
394 len = sendmsg(wl->sock[0], &nmsg, 0);
395 } while (len < 0 && errno == EINTR);
396
397 if (len < 0)
398 return -1;
399
Kristian Høgsbergaf393dc2013-10-09 11:25:14 -0700400 if (fd != -1 && major(s.st_rdev) == DRM_MAJOR)
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700401 wl->drm_fd = fd;
Kristian Høgsberg0eee0a22013-10-25 13:34:58 -0700402 if (fd != -1 && major(s.st_rdev) == INPUT_MAJOR &&
403 wl->last_input_fd < fd)
404 wl->last_input_fd = fd;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700405
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100406 return 0;
407}
408
Stefan Agnerda0cd682019-11-14 23:55:35 +0100409static void
410close_input_fds(struct weston_launch *wl)
411{
412 struct stat s;
413 int fd;
414
415 for (fd = 3; fd <= wl->last_input_fd; fd++) {
416 if (fstat(fd, &s) == 0 && major(s.st_rdev) == INPUT_MAJOR) {
417 /* EVIOCREVOKE may fail if the kernel doesn't
418 * support it, but all we can do is ignore it. */
419 ioctl(fd, EVIOCREVOKE, 0);
420 close(fd);
421 }
422 }
423}
424
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100425static int
426handle_socket_msg(struct weston_launch *wl)
427{
428 char control[CMSG_SPACE(sizeof(int))];
429 char buf[BUFSIZ];
430 struct msghdr msg;
431 struct iovec iov;
432 int ret = -1;
433 ssize_t len;
434 struct weston_launcher_message *message;
leng.fange9d800e2024-07-12 13:31:59 +0800435 int fd = -1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100436
437 memset(&msg, 0, sizeof(msg));
438 iov.iov_base = buf;
439 iov.iov_len = sizeof buf;
440 msg.msg_iov = &iov;
441 msg.msg_iovlen = 1;
442 msg.msg_control = control;
443 msg.msg_controllen = sizeof control;
444
445 do {
446 len = recvmsg(wl->sock[0], &msg, 0);
447 } while (len < 0 && errno == EINTR);
448
449 if (len < 1)
450 return -1;
451
452 message = (void *) buf;
453 switch (message->opcode) {
454 case WESTON_LAUNCHER_OPEN:
455 ret = handle_open(wl, &msg, len);
456 break;
Stefan Agnerda0cd682019-11-14 23:55:35 +0100457 case WESTON_LAUNCHER_DEACTIVATE_DONE:
limin.tianf8b1f8c2024-08-29 11:30:55 +0000458 //close_input_fds(wl);
leng.fang32af9fc2024-06-13 11:22:15 +0800459 //drmDropMaster(wl->drm_fd);
leng.fange9d800e2024-07-12 13:31:59 +0800460 fd = open_tty_by_number(wl->ttynr);
461 if (fd < 0) {
462 fprintf(stderr, "open fail: %d\n", errno);
463 break;
464 }
465 ioctl(fd, VT_RELDISP, 1);
466 close(fd);
Stefan Agnerda0cd682019-11-14 23:55:35 +0100467 break;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100468 }
469
470 return ret;
471}
472
473static void
474quit(struct weston_launch *wl, int status)
475{
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700476 struct vt_mode mode = { 0 };
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100477 int err;
Stefan Agnerbd1e39a2019-11-14 23:39:40 +0100478 int oldtty;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100479
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100480 close(wl->signalfd);
481 close(wl->sock[0]);
482
leng.fang32af9fc2024-06-13 11:22:15 +0800483#ifdef HAVE_PAM
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700484 if (wl->new_user) {
485 err = pam_close_session(wl->ph, 0);
486 if (err)
487 fprintf(stderr, "pam_close_session failed: %d: %s\n",
488 err, pam_strerror(wl->ph, err));
489 pam_end(wl->ph, err);
490 }
leng.fang32af9fc2024-06-13 11:22:15 +0800491#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100492
Stefan Agnerbd1e39a2019-11-14 23:39:40 +0100493 /*
494 * Get a fresh handle to the tty as the previous one is in
495 * hang-up state since weston (the controlling process for
496 * the tty) exit at this point. Reopen before closing the
497 * file descriptor to avoid a potential race condition.
498 *
499 * A similar fix exists in logind, see:
500 * https://github.com/systemd/systemd/pull/990
501 */
502 oldtty = wl->tty;
503 wl->tty = open_tty_by_number(wl->ttynr);
504 close(oldtty);
505
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700506 if (ioctl(wl->tty, KDSKBMUTE, 0) &&
507 ioctl(wl->tty, KDSKBMODE, wl->kb_mode))
Antonio Borneo39578632019-04-26 23:57:31 +0200508 fprintf(stderr, "failed to restore keyboard mode: %s\n",
509 strerror(errno));
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700510
511 if (ioctl(wl->tty, KDSETMODE, KD_TEXT))
Antonio Borneo39578632019-04-26 23:57:31 +0200512 fprintf(stderr, "failed to set KD_TEXT mode on tty: %s\n",
513 strerror(errno));
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700514
Kristian Høgsberga28ba552013-10-30 16:27:16 -0700515 /* We have to drop master before we switch the VT back in
516 * VT_AUTO, so we don't risk switching to a VT with another
517 * display server, that will then fail to set drm master. */
518 drmDropMaster(wl->drm_fd);
519
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700520 mode.mode = VT_AUTO;
521 if (ioctl(wl->tty, VT_SETMODE, &mode) < 0)
522 fprintf(stderr, "could not reset vt handling\n");
523
Stefan Agneraaa5b822019-11-14 22:07:27 +0100524 if (wl->tty != STDIN_FILENO)
525 close(wl->tty);
526
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100527 exit(status);
528}
529
530static int
531handle_signal(struct weston_launch *wl)
532{
533 struct signalfd_siginfo sig;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100534 int pid, status, ret;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100535
536 if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
Randy Lia1450a82019-04-18 11:37:12 +0800537 fprintf(stderr, "weston: reading signalfd failed: %s\n",
538 strerror(errno));
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100539 return -1;
540 }
541
542 switch (sig.ssi_signo) {
543 case SIGCHLD:
544 pid = waitpid(-1, &status, 0);
545 if (pid == wl->child) {
546 wl->child = 0;
Philipp Brüschweiler7a3ec742013-03-10 15:14:01 +0100547 if (WIFEXITED(status))
548 ret = WEXITSTATUS(status);
549 else if (WIFSIGNALED(status))
550 /*
551 * If weston dies because of signal N, we
552 * return 10+N. This is distinct from
553 * weston-launch dying because of a signal
554 * (128+N).
555 */
556 ret = 10 + WTERMSIG(status);
557 else
558 ret = 0;
559 quit(wl, ret);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100560 }
561 break;
562 case SIGTERM:
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100563 case SIGINT:
Stefan Agnera8b4dda2019-11-09 00:50:10 +0100564 if (!wl->child)
565 break;
566
567 if (wl->verbose)
568 fprintf(stderr, "weston-launch: sending %s to pid %d\n",
569 strsignal(sig.ssi_signo), wl->child);
570
571 kill(wl->child, sig.ssi_signo);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100572 break;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700573 case SIGUSR1:
574 send_reply(wl, WESTON_LAUNCHER_DEACTIVATE);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700575 break;
576 case SIGUSR2:
577 ioctl(wl->tty, VT_RELDISP, VT_ACKACQ);
leng.fang32af9fc2024-06-13 11:22:15 +0800578 //drmSetMaster(wl->drm_fd);
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700579 send_reply(wl, WESTON_LAUNCHER_ACTIVATE);
580 break;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100581 default:
582 return -1;
583 }
584
585 return 0;
586}
587
588static int
589setup_tty(struct weston_launch *wl, const char *tty)
590{
591 struct stat buf;
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700592 struct vt_mode mode = { 0 };
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100593 char *t;
594
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700595 if (!wl->new_user) {
596 wl->tty = STDIN_FILENO;
597 } else if (tty) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100598 t = ttyname(STDIN_FILENO);
599 if (t && strcmp(t, tty) == 0)
600 wl->tty = STDIN_FILENO;
601 else
602 wl->tty = open(tty, O_RDWR | O_NOCTTY);
603 } else {
604 int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100605
Randy Lia1450a82019-04-18 11:37:12 +0800606 if (tty0 < 0) {
607 fprintf(stderr, "weston: could not open tty0: %s\n",
608 strerror(errno));
609 return -1;
610 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100611
612 if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
Randy Lia1450a82019-04-18 11:37:12 +0800613 {
614 fprintf(stderr, "weston: failed to find non-opened console: %s\n",
615 strerror(errno));
616 return -1;
617 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100618
Stefan Agner247392a2020-01-22 22:23:07 +0100619 wl->tty = open_tty_by_number(wl->ttynr);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100620 close(tty0);
621 }
622
Randy Lia1450a82019-04-18 11:37:12 +0800623 if (wl->tty < 0) {
624 fprintf(stderr, "weston: failed to open tty: %s\n",
625 strerror(errno));
626 return -1;
627 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100628
Kristian Høgsberge05f2282013-10-02 13:06:02 -0700629 if (fstat(wl->tty, &buf) == -1 ||
Randy Lia1450a82019-04-18 11:37:12 +0800630 major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
631 fprintf(stderr, "weston: weston-launch must be run from a virtual terminal\n");
632 return -1;
633 }
Kristian Høgsberge05f2282013-10-02 13:06:02 -0700634
Stefan Agner77e3b052020-01-23 00:37:16 +0100635 if (!wl->new_user || tty) {
Randy Lia1450a82019-04-18 11:37:12 +0800636 if (fstat(wl->tty, &buf) < 0) {
637 fprintf(stderr, "weston: stat %s failed: %s\n", tty,
638 strerror(errno));
639 return -1;
640 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100641
Randy Lia1450a82019-04-18 11:37:12 +0800642 if (major(buf.st_rdev) != TTY_MAJOR) {
643 fprintf(stderr,
644 "weston: invalid tty device: %s\n", tty);
645 return -1;
646 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100647
648 wl->ttynr = minor(buf.st_rdev);
649 }
650
Stefan Agnerc6f818a2019-11-14 23:29:09 +0100651 if (ioctl(wl->tty, VT_ACTIVATE, wl->ttynr) < 0) {
652 fprintf(stderr,
653 "weston: failed to activate VT: %s\n",
654 strerror(errno));
655 return -1;
656 }
657
658 if (ioctl(wl->tty, VT_WAITACTIVE, wl->ttynr) < 0) {
659 fprintf(stderr,
660 "weston: failed to wait for VT to be active: %s\n",
661 strerror(errno));
662 return -1;
663 }
664
Randy Lia1450a82019-04-18 11:37:12 +0800665 if (ioctl(wl->tty, KDGKBMODE, &wl->kb_mode)) {
666 fprintf(stderr,
Stefan Agnercb24a7d2020-01-23 00:53:31 +0100667 "weston: failed to get current keyboard mode: %s\n",
Randy Lia1450a82019-04-18 11:37:12 +0800668 strerror(errno));
669 return -1;
670 }
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700671
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700672 if (ioctl(wl->tty, KDSKBMUTE, 1) &&
Randy Lia1450a82019-04-18 11:37:12 +0800673 ioctl(wl->tty, KDSKBMODE, K_OFF)) {
674 fprintf(stderr,
675 "weston: failed to set K_OFF keyboard mode: %s\n",
676 strerror(errno));
677 return -1;
678 }
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700679
Randy Lia1450a82019-04-18 11:37:12 +0800680 if (ioctl(wl->tty, KDSETMODE, KD_GRAPHICS)) {
681 fprintf(stderr,
682 "weston: failed to set KD_GRAPHICS mode on tty: %s\n",
683 strerror(errno));
684 return -1;
685 }
Kristian Høgsberg81b49632013-09-17 22:43:22 -0700686
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700687 mode.mode = VT_PROCESS;
688 mode.relsig = SIGUSR1;
689 mode.acqsig = SIGUSR2;
Randy Lia1450a82019-04-18 11:37:12 +0800690 if (ioctl(wl->tty, VT_SETMODE, &mode) < 0) {
691 fprintf(stderr,
692 "weston: failed to take control of vt handling %s\n",
693 strerror(errno));
694 return -1;
695 }
Kristian Høgsberg1eb482d2013-09-17 22:15:37 -0700696
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100697 return 0;
698}
699
Quentin Glidic056889a2016-05-29 13:39:26 +0200700static int
701setup_session(struct weston_launch *wl, char **child_argv)
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700702{
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700703 char **env;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700704 char *term;
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700705 int i;
706
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700707 if (wl->tty != STDIN_FILENO) {
Randy Lia1450a82019-04-18 11:37:12 +0800708 if (setsid() < 0) {
709 fprintf(stderr, "weston: setsid failed %s\n",
710 strerror(errno));
711 exit(EXIT_FAILURE);
712 }
713 if (ioctl(wl->tty, TIOCSCTTY, 0) < 0) {
714 fprintf(stderr, "TIOCSCTTY failed - tty is in use %s\n",
715 strerror(errno));
716 exit(EXIT_FAILURE);
717 }
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700718 }
719
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700720 term = getenv("TERM");
721 clearenv();
Rob Bradford7ac9f732013-08-09 11:30:38 +0100722 if (term)
723 setenv("TERM", term, 1);
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700724 setenv("USER", wl->pw->pw_name, 1);
725 setenv("LOGNAME", wl->pw->pw_name, 1);
726 setenv("HOME", wl->pw->pw_dir, 1);
727 setenv("SHELL", wl->pw->pw_shell, 1);
728
leng.fang32af9fc2024-06-13 11:22:15 +0800729#ifdef HAVE_PAM
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700730 env = pam_getenvlist(wl->ph);
731 if (env) {
732 for (i = 0; env[i]; ++i) {
Dawid Gajownikf3c83362015-08-09 12:50:40 -0300733 if (putenv(env[i]) != 0)
Randy Lia1450a82019-04-18 11:37:12 +0800734 fprintf(stderr, "putenv %s failed\n", env[i]);
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700735 }
736 free(env);
737 }
leng.fang32af9fc2024-06-13 11:22:15 +0800738#endif
Quentin Glidic056889a2016-05-29 13:39:26 +0200739
740 /*
741 * We open a new session, so it makes sense
742 * to run a new login shell
743 */
744 child_argv[0] = "/bin/sh";
745 child_argv[1] = "-l";
746 child_argv[2] = "-c";
Stefan Agner04eebc72019-11-09 00:51:29 +0100747 child_argv[3] = "exec " BINDIR "/weston \"$@\"";
Quentin Glidic056889a2016-05-29 13:39:26 +0200748 child_argv[4] = "weston";
749 return 5;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700750}
751
752static void
Peter Hutterer34be0602013-08-06 12:10:09 +1000753drop_privileges(struct weston_launch *wl)
754{
755 if (setgid(wl->pw->pw_gid) < 0 ||
756#ifdef HAVE_INITGROUPS
757 initgroups(wl->pw->pw_name, wl->pw->pw_gid) < 0 ||
758#endif
Randy Lia1450a82019-04-18 11:37:12 +0800759 setuid(wl->pw->pw_uid) < 0) {
760 fprintf(stderr, "weston: dropping privileges failed %s\n",
761 strerror(errno));
762 exit(EXIT_FAILURE);
763 }
Peter Hutterer34be0602013-08-06 12:10:09 +1000764}
765
766static void
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700767launch_compositor(struct weston_launch *wl, int argc, char *argv[])
768{
769 char *child_argv[MAX_ARGV_SIZE];
Eduardo Limad0357bb2013-07-30 10:43:41 -0700770 sigset_t mask;
Quentin Glidic056889a2016-05-29 13:39:26 +0200771 int o, i;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700772
773 if (wl->verbose)
774 printf("weston-launch: spawned weston with pid: %d\n", getpid());
Quentin Glidic056889a2016-05-29 13:39:26 +0200775 if (wl->new_user) {
776 o = setup_session(wl, child_argv);
777 } else {
778 child_argv[0] = BINDIR "/weston";
779 o = 1;
780 }
781 for (i = 0; i < argc; ++i)
782 child_argv[o + i] = argv[i];
783 child_argv[o + i] = NULL;
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700784
Tomeu Vizosod7865b22013-10-01 12:20:29 +0200785 if (geteuid() == 0)
786 drop_privileges(wl);
Peter Hutterer34be0602013-08-06 12:10:09 +1000787
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700788 setenv_fd("WESTON_TTY_FD", wl->tty);
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700789 setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]);
790
791 unsetenv("DISPLAY");
792
Kristian Høgsberg73c60ab2013-07-30 09:45:04 -0700793 /* Do not give our signal mask to the new process. */
794 sigemptyset(&mask);
795 sigaddset(&mask, SIGTERM);
796 sigaddset(&mask, SIGCHLD);
797 sigaddset(&mask, SIGINT);
798 sigprocmask(SIG_UNBLOCK, &mask, NULL);
799
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700800
801 execv(child_argv[0], child_argv);
Randy Lia1450a82019-04-18 11:37:12 +0800802 fprintf(stderr, "weston: exec failed: %s\n", strerror(errno));
803 exit(EXIT_FAILURE);
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700804}
805
806static void
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100807help(const char *name)
808{
809 fprintf(stderr, "Usage: %s [args...] [-- [weston args..]]\n", name);
leng.fang32af9fc2024-06-13 11:22:15 +0800810#ifdef HAVE_PAM
Pekka Paalanen1a2adfe2017-11-01 12:20:24 +0200811 fprintf(stderr, " -u, --user Start session as specified username,\n"
812 " e.g. -u joe, requires root.\n");
leng.fang32af9fc2024-06-13 11:22:15 +0800813#endif
Pekka Paalanen1a2adfe2017-11-01 12:20:24 +0200814 fprintf(stderr, " -t, --tty Start session on alternative tty,\n"
815 " e.g. -t /dev/tty4, requires -u option.\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100816 fprintf(stderr, " -v, --verbose Be verbose\n");
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100817 fprintf(stderr, " -h, --help Display this help message\n");
818}
819
820int
821main(int argc, char *argv[])
822{
823 struct weston_launch wl;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100824 int i, c;
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700825 char *tty = NULL;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100826 struct option opts[] = {
leng.fang32af9fc2024-06-13 11:22:15 +0800827#ifdef HAVE_PAM
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100828 { "user", required_argument, NULL, 'u' },
leng.fang32af9fc2024-06-13 11:22:15 +0800829#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100830 { "tty", required_argument, NULL, 't' },
831 { "verbose", no_argument, NULL, 'v' },
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100832 { "help", no_argument, NULL, 'h' },
833 { 0, 0, NULL, 0 }
Murray Calavera883ac022015-06-06 13:02:22 +0000834 };
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100835
836 memset(&wl, 0, sizeof wl);
837
Pekka Paalanen625f56f2017-11-01 13:20:03 +0200838 while ((c = getopt_long(argc, argv, "u:t:vh", opts, &i)) != -1) {
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100839 switch (c) {
840 case 'u':
leng.fang32af9fc2024-06-13 11:22:15 +0800841#ifdef HAVE_PAM
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700842 wl.new_user = optarg;
Randy Lia1450a82019-04-18 11:37:12 +0800843 if (getuid() != 0) {
844 fprintf(stderr, "weston: Permission denied. -u allowed for root only\n");
845 exit(EXIT_FAILURE);
846 }
leng.fang32af9fc2024-06-13 11:22:15 +0800847#else
848 fprintf(stderr, "weston: -u is unsupported in this weston-launch build\n");
849 exit(EXIT_FAILURE);
850#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100851 break;
852 case 't':
853 tty = optarg;
854 break;
855 case 'v':
856 wl.verbose = 1;
857 break;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100858 case 'h':
Scott Moreaucc9acfc2013-01-21 23:40:59 -0700859 help("weston-launch");
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530860 exit(EXIT_FAILURE);
Tom Hochsteine57056f2016-05-07 08:57:40 -0300861 default:
862 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100863 }
864 }
865
Randy Lia1450a82019-04-18 11:37:12 +0800866 if ((argc - optind) > (MAX_ARGV_SIZE - 6)) {
867 fprintf(stderr,
868 "weston: Too many arguments to pass to weston: %s\n",
869 strerror(E2BIG));
870 exit(EXIT_FAILURE);
871 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100872
Randy Lia1450a82019-04-18 11:37:12 +0800873 if (tty && !wl.new_user) {
874 fprintf(stderr, "weston: -t/--tty option requires -u/--user option as well\n");
875 exit(EXIT_FAILURE);
876 }
Pekka Paalanen5949e482017-11-01 12:12:17 +0200877
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700878 if (wl.new_user)
879 wl.pw = getpwnam(wl.new_user);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100880 else
881 wl.pw = getpwuid(getuid());
Randy Lia1450a82019-04-18 11:37:12 +0800882 if (wl.pw == NULL) {
883 fprintf(stderr, "weston: failed to get username: %s\n",
884 strerror(errno));
885 exit(EXIT_FAILURE);
886 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100887
Randy Lia1450a82019-04-18 11:37:12 +0800888 if (!weston_launch_allowed(&wl)) {
889 fprintf(stderr, "Permission denied. You should either:\n"
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100890#ifdef HAVE_SYSTEMD_LOGIN
891 " - run from an active and local (systemd) session.\n"
892#else
893 " - enable systemd session support for weston-launch.\n"
894#endif
Randy Lia1450a82019-04-18 11:37:12 +0800895 " - or add yourself to the 'weston-launch' group.\n");
896 exit(EXIT_FAILURE);
897 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100898
899 if (setup_tty(&wl, tty) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530900 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100901
leng.fang32af9fc2024-06-13 11:22:15 +0800902#ifdef HAVE_PAM
Kristian Høgsberg636156d2013-07-22 10:35:47 -0700903 if (wl.new_user && setup_pam(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530904 exit(EXIT_FAILURE);
leng.fang32af9fc2024-06-13 11:22:15 +0800905#endif
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100906
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100907 if (setup_launcher_socket(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530908 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100909
910 if (setup_signals(&wl) < 0)
Siddharth Heroord6be88b2013-03-12 02:36:52 +0530911 exit(EXIT_FAILURE);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100912
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700913 wl.child = fork();
Randy Lia1450a82019-04-18 11:37:12 +0800914 if (wl.child == -1) {
915 fprintf(stderr, "weston: fork failed %s\n", strerror(errno));
916 exit(EXIT_FAILURE);
917 }
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100918
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700919 if (wl.child == 0)
920 launch_compositor(&wl, argc - optind, argv + optind);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100921
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700922 close(wl.sock[1]);
Quentin Glidic735302e2013-06-19 15:27:11 +0200923
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700924 while (1) {
Kristian Høgsbergf2807702013-07-23 11:43:03 -0700925 struct pollfd fds[2];
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700926 int n;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100927
Kristian Høgsbergf2807702013-07-23 11:43:03 -0700928 fds[0].fd = wl.sock[0];
929 fds[0].events = POLLIN;
930 fds[1].fd = wl.signalfd;
931 fds[1].events = POLLIN;
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100932
Kristian Høgsbergf2807702013-07-23 11:43:03 -0700933 n = poll(fds, 2, -1);
Marius Vlad38c66cc2019-04-22 17:22:57 +0300934 if (n < 0) {
935 fprintf(stderr, "poll failed: %s\n", strerror(errno));
936 return -1;
937 }
Kristian Høgsbergf2807702013-07-23 11:43:03 -0700938 if (fds[0].revents & POLLIN)
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700939 handle_socket_msg(&wl);
Kristian Høgsbergf2807702013-07-23 11:43:03 -0700940 if (fds[1].revents)
Kristian Høgsbergca70f2f2013-07-19 21:25:20 -0700941 handle_signal(&wl);
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100942 }
943
944 return 0;
945}