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