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