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