]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/tty-ask-password-agent/tty-ask-password-agent.c
c147493bf884ab19d5df24bf2b1285b470596f1b
[thirdparty/systemd.git] / src / tty-ask-password-agent / tty-ask-password-agent.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2015 Werner Fink
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <getopt.h>
9 #include <poll.h>
10 #include <signal.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <string.h>
14 #include <sys/inotify.h>
15 #include <sys/prctl.h>
16 #include <sys/signalfd.h>
17 #include <sys/socket.h>
18 #include <sys/wait.h>
19 #include <sys/un.h>
20 #include <unistd.h>
21
22 #include "alloc-util.h"
23 #include "ask-password-api.h"
24 #include "conf-parser.h"
25 #include "def.h"
26 #include "dirent-util.h"
27 #include "exit-status.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "hashmap.h"
31 #include "io-util.h"
32 #include "macro.h"
33 #include "main-func.h"
34 #include "mkdir.h"
35 #include "path-util.h"
36 #include "pretty-print.h"
37 #include "process-util.h"
38 #include "signal-util.h"
39 #include "socket-util.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "terminal-util.h"
43 #include "util.h"
44 #include "utmp-wtmp.h"
45
46 static enum {
47 ACTION_LIST,
48 ACTION_QUERY,
49 ACTION_WATCH,
50 ACTION_WALL
51 } arg_action = ACTION_QUERY;
52
53 static bool arg_plymouth = false;
54 static bool arg_console = false;
55 static const char *arg_device = NULL;
56
57 static int ask_password_plymouth(
58 const char *message,
59 usec_t until,
60 AskPasswordFlags flags,
61 const char *flag_file,
62 char ***ret) {
63
64 static const union sockaddr_union sa = PLYMOUTH_SOCKET;
65 _cleanup_close_ int fd = -1, notify = -1;
66 _cleanup_free_ char *packet = NULL;
67 ssize_t k;
68 int r, n;
69 struct pollfd pollfd[2] = {};
70 char buffer[LINE_MAX];
71 size_t p = 0;
72 enum {
73 POLL_SOCKET,
74 POLL_INOTIFY
75 };
76
77 assert(ret);
78
79 if (flag_file) {
80 notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
81 if (notify < 0)
82 return -errno;
83
84 r = inotify_add_watch(notify, flag_file, IN_ATTRIB); /* for the link count */
85 if (r < 0)
86 return -errno;
87 }
88
89 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
90 if (fd < 0)
91 return -errno;
92
93 r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
94 if (r < 0)
95 return -errno;
96
97 if (flags & ASK_PASSWORD_ACCEPT_CACHED) {
98 packet = strdup("c");
99 n = 1;
100 } else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0)
101 packet = NULL;
102 if (!packet)
103 return -ENOMEM;
104
105 r = loop_write(fd, packet, n + 1, true);
106 if (r < 0)
107 return r;
108
109 pollfd[POLL_SOCKET].fd = fd;
110 pollfd[POLL_SOCKET].events = POLLIN;
111 pollfd[POLL_INOTIFY].fd = notify;
112 pollfd[POLL_INOTIFY].events = POLLIN;
113
114 for (;;) {
115 int sleep_for = -1, j;
116
117 if (until > 0) {
118 usec_t y;
119
120 y = now(CLOCK_MONOTONIC);
121
122 if (y > until) {
123 r = -ETIME;
124 goto finish;
125 }
126
127 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
128 }
129
130 if (flag_file && access(flag_file, F_OK) < 0) {
131 r = -errno;
132 goto finish;
133 }
134
135 j = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for);
136 if (j < 0) {
137 if (errno == EINTR)
138 continue;
139
140 r = -errno;
141 goto finish;
142 } else if (j == 0) {
143 r = -ETIME;
144 goto finish;
145 }
146
147 if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0)
148 (void) flush_fd(notify);
149
150 if (pollfd[POLL_SOCKET].revents == 0)
151 continue;
152
153 k = read(fd, buffer + p, sizeof(buffer) - p);
154 if (k < 0) {
155 if (IN_SET(errno, EINTR, EAGAIN))
156 continue;
157
158 r = -errno;
159 goto finish;
160 } else if (k == 0) {
161 r = -EIO;
162 goto finish;
163 }
164
165 p += k;
166
167 if (p < 1)
168 continue;
169
170 if (buffer[0] == 5) {
171
172 if (flags & ASK_PASSWORD_ACCEPT_CACHED) {
173 /* Hmm, first try with cached
174 * passwords failed, so let's retry
175 * with a normal password request */
176 packet = mfree(packet);
177
178 if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
179 r = -ENOMEM;
180 goto finish;
181 }
182
183 r = loop_write(fd, packet, n+1, true);
184 if (r < 0)
185 goto finish;
186
187 flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
188 p = 0;
189 continue;
190 }
191
192 /* No password, because UI not shown */
193 r = -ENOENT;
194 goto finish;
195
196 } else if (IN_SET(buffer[0], 2, 9)) {
197 uint32_t size;
198 char **l;
199
200 /* One or more answers */
201 if (p < 5)
202 continue;
203
204 memcpy(&size, buffer+1, sizeof(size));
205 size = le32toh(size);
206 if (size + 5 > sizeof(buffer)) {
207 r = -EIO;
208 goto finish;
209 }
210
211 if (p-5 < size)
212 continue;
213
214 l = strv_parse_nulstr(buffer + 5, size);
215 if (!l) {
216 r = -ENOMEM;
217 goto finish;
218 }
219
220 *ret = l;
221 break;
222
223 } else {
224 /* Unknown packet */
225 r = -EIO;
226 goto finish;
227 }
228 }
229
230 r = 0;
231
232 finish:
233 explicit_bzero_safe(buffer, sizeof(buffer));
234 return r;
235 }
236
237 static int send_passwords(const char *socket_name, char **passwords) {
238 _cleanup_free_ char *packet = NULL;
239 _cleanup_close_ int socket_fd = -1;
240 union sockaddr_union sa = {};
241 size_t packet_length = 1;
242 char **p, *d;
243 ssize_t n;
244 int r, salen;
245
246 assert(socket_name);
247
248 salen = sockaddr_un_set_path(&sa.un, socket_name);
249 if (salen < 0)
250 return salen;
251
252 STRV_FOREACH(p, passwords)
253 packet_length += strlen(*p) + 1;
254
255 packet = new(char, packet_length);
256 if (!packet)
257 return -ENOMEM;
258
259 packet[0] = '+';
260
261 d = packet + 1;
262 STRV_FOREACH(p, passwords)
263 d = stpcpy(d, *p) + 1;
264
265 socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
266 if (socket_fd < 0) {
267 r = log_debug_errno(errno, "socket(): %m");
268 goto finish;
269 }
270
271 n = sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, salen);
272 if (n < 0) {
273 r = log_debug_errno(errno, "sendto(): %m");
274 goto finish;
275 }
276
277 r = (int) n;
278
279 finish:
280 explicit_bzero_safe(packet, packet_length);
281 return r;
282 }
283
284 static int parse_password(const char *filename, char **wall) {
285 _cleanup_free_ char *socket_name = NULL, *message = NULL;
286 bool accept_cached = false, echo = false;
287 uint64_t not_after = 0;
288 unsigned pid = 0;
289
290 const ConfigTableItem items[] = {
291 { "Ask", "Socket", config_parse_string, 0, &socket_name },
292 { "Ask", "NotAfter", config_parse_uint64, 0, &not_after },
293 { "Ask", "Message", config_parse_string, 0, &message },
294 { "Ask", "PID", config_parse_unsigned, 0, &pid },
295 { "Ask", "AcceptCached", config_parse_bool, 0, &accept_cached },
296 { "Ask", "Echo", config_parse_bool, 0, &echo },
297 {}
298 };
299
300 int r;
301
302 assert(filename);
303
304 r = config_parse(NULL, filename, NULL,
305 NULL,
306 config_item_table_lookup, items,
307 CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN, NULL);
308 if (r < 0)
309 return r;
310
311 if (!socket_name) {
312 log_error("Invalid password file %s", filename);
313 return -EBADMSG;
314 }
315
316 if (not_after > 0 && now(CLOCK_MONOTONIC) > not_after)
317 return 0;
318
319 if (pid > 0 && !pid_is_alive(pid))
320 return 0;
321
322 if (arg_action == ACTION_LIST)
323 printf("'%s' (PID %u)\n", message, pid);
324
325 else if (arg_action == ACTION_WALL) {
326 char *_wall;
327
328 if (asprintf(&_wall,
329 "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
330 "Please enter password with the systemd-tty-ask-password-agent tool:",
331 strempty(*wall),
332 *wall ? "\r\n\r\n" : "",
333 message,
334 pid) < 0)
335 return log_oom();
336
337 free(*wall);
338 *wall = _wall;
339
340 } else {
341 _cleanup_strv_free_erase_ char **passwords = NULL;
342
343 assert(IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH));
344
345 if (access(socket_name, W_OK) < 0) {
346 if (arg_action == ACTION_QUERY)
347 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
348
349 return 0;
350 }
351
352 if (arg_plymouth)
353 r = ask_password_plymouth(message, not_after, accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0, filename, &passwords);
354 else {
355 int tty_fd = -1;
356
357 if (arg_console) {
358 const char *con = arg_device ?: "/dev/console";
359
360 tty_fd = acquire_terminal(con, ACQUIRE_TERMINAL_WAIT, USEC_INFINITY);
361 if (tty_fd < 0)
362 return log_error_errno(tty_fd, "Failed to acquire %s: %m", con);
363
364 r = reset_terminal_fd(tty_fd, true);
365 if (r < 0)
366 log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
367 }
368
369 r = ask_password_tty(tty_fd, message, NULL, not_after,
370 (echo ? ASK_PASSWORD_ECHO : 0) |
371 (arg_console ? ASK_PASSWORD_CONSOLE_COLOR : 0),
372 filename, &passwords);
373
374 if (arg_console) {
375 tty_fd = safe_close(tty_fd);
376 release_terminal();
377 }
378 }
379
380 /* If the query went away, that's OK */
381 if (IN_SET(r, -ETIME, -ENOENT))
382 return 0;
383
384 if (r < 0)
385 return log_error_errno(r, "Failed to query password: %m");
386
387 r = send_passwords(socket_name, passwords);
388 if (r < 0)
389 return log_error_errno(r, "Failed to send: %m");
390 }
391
392 return 0;
393 }
394
395 static int wall_tty_block(void) {
396 _cleanup_free_ char *p = NULL;
397 dev_t devnr;
398 int fd, r;
399
400 r = get_ctty_devnr(0, &devnr);
401 if (r == -ENXIO) /* We have no controlling tty */
402 return -ENOTTY;
403 if (r < 0)
404 return log_error_errno(r, "Failed to get controlling TTY: %m");
405
406 if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(devnr), minor(devnr)) < 0)
407 return log_oom();
408
409 (void) mkdir_parents_label(p, 0700);
410 (void) mkfifo(p, 0600);
411
412 fd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
413 if (fd < 0)
414 return log_debug_errno(errno, "Failed to open %s: %m", p);
415
416 return fd;
417 }
418
419 static bool wall_tty_match(const char *path, void *userdata) {
420 _cleanup_free_ char *p = NULL;
421 _cleanup_close_ int fd = -1;
422 struct stat st;
423
424 if (!path_is_absolute(path))
425 path = strjoina("/dev/", path);
426
427 if (lstat(path, &st) < 0) {
428 log_debug_errno(errno, "Failed to stat %s: %m", path);
429 return true;
430 }
431
432 if (!S_ISCHR(st.st_mode)) {
433 log_debug("%s is not a character device.", path);
434 return true;
435 }
436
437 /* We use named pipes to ensure that wall messages suggesting
438 * password entry are not printed over password prompts
439 * already shown. We use the fact here that opening a pipe in
440 * non-blocking mode for write-only will succeed only if
441 * there's some writer behind it. Using pipes has the
442 * advantage that the block will automatically go away if the
443 * process dies. */
444
445 if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0) {
446 log_oom();
447 return true;
448 }
449
450 fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
451 if (fd < 0) {
452 log_debug_errno(errno, "Failed to open the wall pipe: %m");
453 return 1;
454 }
455
456 /* What, we managed to open the pipe? Then this tty is filtered. */
457 return 0;
458 }
459
460 static int show_passwords(void) {
461 _cleanup_closedir_ DIR *d;
462 struct dirent *de;
463 int r = 0;
464
465 d = opendir("/run/systemd/ask-password");
466 if (!d) {
467 if (errno == ENOENT)
468 return 0;
469
470 return log_error_errno(errno, "Failed to open /run/systemd/ask-password: %m");
471 }
472
473 FOREACH_DIRENT_ALL(de, d, return log_error_errno(errno, "Failed to read directory: %m")) {
474 _cleanup_free_ char *p = NULL, *wall = NULL;
475 int q;
476
477 /* We only support /dev on tmpfs, hence we can rely on
478 * d_type to be reliable */
479
480 if (de->d_type != DT_REG)
481 continue;
482
483 if (hidden_or_backup_file(de->d_name))
484 continue;
485
486 if (!startswith(de->d_name, "ask."))
487 continue;
488
489 p = strappend("/run/systemd/ask-password/", de->d_name);
490 if (!p)
491 return log_oom();
492
493 q = parse_password(p, &wall);
494 if (q < 0 && r == 0)
495 r = q;
496
497 if (wall)
498 (void) utmp_wall(wall, NULL, NULL, wall_tty_match, NULL);
499 }
500
501 return r;
502 }
503
504 static int watch_passwords(void) {
505 enum {
506 FD_INOTIFY,
507 FD_SIGNAL,
508 _FD_MAX
509 };
510
511 _cleanup_close_ int notify = -1, signal_fd = -1, tty_block_fd = -1;
512 struct pollfd pollfd[_FD_MAX] = {};
513 sigset_t mask;
514 int r;
515
516 tty_block_fd = wall_tty_block();
517
518 (void) mkdir_p_label("/run/systemd/ask-password", 0755);
519
520 notify = inotify_init1(IN_CLOEXEC);
521 if (notify < 0)
522 return log_error_errno(errno, "Failed to allocate directory watch: %m");
523
524 if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
525 if (errno == ENOSPC)
526 return log_error_errno(errno, "Failed to add /run/systemd/ask-password to directory watch: inotify watch limit reached");
527 else
528 return log_error_errno(errno, "Failed to add /run/systemd/ask-password to directory watch: %m");
529 }
530
531 assert_se(sigemptyset(&mask) >= 0);
532 assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0);
533 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) >= 0);
534
535 signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
536 if (signal_fd < 0)
537 return log_error_errno(errno, "Failed to allocate signal file descriptor: %m");
538
539 pollfd[FD_INOTIFY].fd = notify;
540 pollfd[FD_INOTIFY].events = POLLIN;
541 pollfd[FD_SIGNAL].fd = signal_fd;
542 pollfd[FD_SIGNAL].events = POLLIN;
543
544 for (;;) {
545 r = show_passwords();
546 if (r < 0)
547 log_error_errno(r, "Failed to show password: %m");
548
549 if (poll(pollfd, _FD_MAX, -1) < 0) {
550 if (errno == EINTR)
551 continue;
552
553 return -errno;
554 }
555
556 if (pollfd[FD_INOTIFY].revents != 0)
557 (void) flush_fd(notify);
558
559 if (pollfd[FD_SIGNAL].revents != 0)
560 break;
561 }
562
563 return 0;
564 }
565
566 static int help(void) {
567 _cleanup_free_ char *link = NULL;
568 int r;
569
570 r = terminal_urlify_man("systemd-tty-ask-password-agent", "1", &link);
571 if (r < 0)
572 return log_oom();
573
574 printf("%s [OPTIONS...]\n\n"
575 "Process system password requests.\n\n"
576 " -h --help Show this help\n"
577 " --version Show package version\n"
578 " --list Show pending password requests\n"
579 " --query Process pending password requests\n"
580 " --watch Continuously process password requests\n"
581 " --wall Continuously forward password requests to wall\n"
582 " --plymouth Ask question with Plymouth instead of on TTY\n"
583 " --console Ask question on /dev/console instead of current TTY\n"
584 "\nSee the %s for details.\n"
585 , program_invocation_short_name
586 , link
587 );
588
589 return 0;
590 }
591
592 static int parse_argv(int argc, char *argv[]) {
593
594 enum {
595 ARG_LIST = 0x100,
596 ARG_QUERY,
597 ARG_WATCH,
598 ARG_WALL,
599 ARG_PLYMOUTH,
600 ARG_CONSOLE,
601 ARG_VERSION
602 };
603
604 static const struct option options[] = {
605 { "help", no_argument, NULL, 'h' },
606 { "version", no_argument, NULL, ARG_VERSION },
607 { "list", no_argument, NULL, ARG_LIST },
608 { "query", no_argument, NULL, ARG_QUERY },
609 { "watch", no_argument, NULL, ARG_WATCH },
610 { "wall", no_argument, NULL, ARG_WALL },
611 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
612 { "console", optional_argument, NULL, ARG_CONSOLE },
613 {}
614 };
615
616 int c;
617
618 assert(argc >= 0);
619 assert(argv);
620
621 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
622
623 switch (c) {
624
625 case 'h':
626 return help();
627
628 case ARG_VERSION:
629 return version();
630
631 case ARG_LIST:
632 arg_action = ACTION_LIST;
633 break;
634
635 case ARG_QUERY:
636 arg_action = ACTION_QUERY;
637 break;
638
639 case ARG_WATCH:
640 arg_action = ACTION_WATCH;
641 break;
642
643 case ARG_WALL:
644 arg_action = ACTION_WALL;
645 break;
646
647 case ARG_PLYMOUTH:
648 arg_plymouth = true;
649 break;
650
651 case ARG_CONSOLE:
652 arg_console = true;
653 if (optarg) {
654
655 if (isempty(optarg)) {
656 log_error("Empty console device path is not allowed.");
657 return -EINVAL;
658 }
659
660 arg_device = optarg;
661 }
662 break;
663
664 case '?':
665 return -EINVAL;
666
667 default:
668 assert_not_reached("Unhandled option");
669 }
670
671 if (optind != argc) {
672 log_error("%s takes no arguments.", program_invocation_short_name);
673 return -EINVAL;
674 }
675
676 if (arg_plymouth || arg_console) {
677
678 if (!IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH)) {
679 log_error("Options --query and --watch conflict.");
680 return -EINVAL;
681 }
682
683 if (arg_plymouth && arg_console) {
684 log_error("Options --plymouth and --console conflict.");
685 return -EINVAL;
686 }
687 }
688
689 return 1;
690 }
691
692 /*
693 * To be able to ask on all terminal devices of /dev/console
694 * the devices are collected. If more than one device is found,
695 * then on each of the terminals a inquiring task is forked.
696 * Every task has its own session and its own controlling terminal.
697 * If one of the tasks does handle a password, the remaining tasks
698 * will be terminated.
699 */
700 static int ask_on_this_console(const char *tty, pid_t *ret_pid, int argc, char *argv[]) {
701 struct sigaction sig = {
702 .sa_handler = nop_signal_handler,
703 .sa_flags = SA_NOCLDSTOP | SA_RESTART,
704 };
705 pid_t pid;
706 int r;
707
708 assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD, -1) >= 0);
709
710 assert_se(sigemptyset(&sig.sa_mask) >= 0);
711 assert_se(sigaction(SIGCHLD, &sig, NULL) >= 0);
712
713 sig.sa_handler = SIG_DFL;
714 assert_se(sigaction(SIGHUP, &sig, NULL) >= 0);
715
716 r = safe_fork("(sd-passwd)", FORK_RESET_SIGNALS|FORK_LOG, &pid);
717 if (r < 0)
718 return r;
719 if (r == 0) {
720 int ac;
721
722 assert_se(prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0);
723
724 for (ac = 0; ac < argc; ac++) {
725 if (streq(argv[ac], "--console")) {
726 argv[ac] = strjoina("--console=", tty);
727 break;
728 }
729 }
730
731 assert(ac < argc);
732
733 execv(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, argv);
734 _exit(EXIT_FAILURE);
735 }
736
737 *ret_pid = pid;
738 return 0;
739 }
740
741 static void terminate_agents(Set *pids) {
742 struct timespec ts;
743 siginfo_t status = {};
744 sigset_t set;
745 Iterator i;
746 void *p;
747 int r, signum;
748
749 /*
750 * Request termination of the remaining processes as those
751 * are not required anymore.
752 */
753 SET_FOREACH(p, pids, i)
754 (void) kill(PTR_TO_PID(p), SIGTERM);
755
756 /*
757 * Collect the processes which have go away.
758 */
759 assert_se(sigemptyset(&set) >= 0);
760 assert_se(sigaddset(&set, SIGCHLD) >= 0);
761 timespec_store(&ts, 50 * USEC_PER_MSEC);
762
763 while (!set_isempty(pids)) {
764
765 zero(status);
766 r = waitid(P_ALL, 0, &status, WEXITED|WNOHANG);
767 if (r < 0 && errno == EINTR)
768 continue;
769
770 if (r == 0 && status.si_pid > 0) {
771 set_remove(pids, PID_TO_PTR(status.si_pid));
772 continue;
773 }
774
775 signum = sigtimedwait(&set, NULL, &ts);
776 if (signum < 0) {
777 if (errno != EAGAIN)
778 log_error_errno(errno, "sigtimedwait() failed: %m");
779 break;
780 }
781 assert(signum == SIGCHLD);
782 }
783
784 /*
785 * Kill hanging processes.
786 */
787 SET_FOREACH(p, pids, i) {
788 log_warning("Failed to terminate child %d, killing it", PTR_TO_PID(p));
789 (void) kill(PTR_TO_PID(p), SIGKILL);
790 }
791 }
792
793 static int ask_on_consoles(int argc, char *argv[]) {
794 _cleanup_set_free_ Set *pids = NULL;
795 _cleanup_strv_free_ char **consoles = NULL;
796 siginfo_t status = {};
797 char **tty;
798 pid_t pid;
799 int r;
800
801 r = get_kernel_consoles(&consoles);
802 if (r < 0)
803 return log_error_errno(r, "Failed to determine devices of /dev/console: %m");
804
805 pids = set_new(NULL);
806 if (!pids)
807 return log_oom();
808
809 /* Start an agent on each console. */
810 STRV_FOREACH(tty, consoles) {
811 r = ask_on_this_console(*tty, &pid, argc, argv);
812 if (r < 0)
813 return r;
814
815 if (set_put(pids, PID_TO_PTR(pid)) < 0)
816 return log_oom();
817 }
818
819 /* Wait for an agent to exit. */
820 for (;;) {
821 zero(status);
822
823 if (waitid(P_ALL, 0, &status, WEXITED) < 0) {
824 if (errno == EINTR)
825 continue;
826
827 return log_error_errno(errno, "waitid() failed: %m");
828 }
829
830 set_remove(pids, PID_TO_PTR(status.si_pid));
831 break;
832 }
833
834 if (!is_clean_exit(status.si_code, status.si_status, EXIT_CLEAN_DAEMON, NULL))
835 log_error("Password agent failed with: %d", status.si_status);
836
837 terminate_agents(pids);
838 return 0;
839 }
840
841 static int run(int argc, char *argv[]) {
842 int r;
843
844 log_setup_service();
845
846 umask(0022);
847
848 r = parse_argv(argc, argv);
849 if (r <= 0)
850 return r;
851
852 if (arg_console && !arg_device)
853 /*
854 * Spawn a separate process for each console device.
855 */
856 return ask_on_consoles(argc, argv);
857
858 if (arg_device) {
859 /*
860 * Later on, a controlling terminal will be acquired,
861 * therefore the current process has to become a session
862 * leader and should not have a controlling terminal already.
863 */
864 (void) setsid();
865 (void) release_terminal();
866 }
867
868 if (IN_SET(arg_action, ACTION_WATCH, ACTION_WALL))
869 return watch_passwords();
870 else
871 return show_passwords();
872 }
873
874 DEFINE_MAIN_FUNCTION(run);