File: | build-scan/../src/tty-ask-password-agent/tty-ask-password-agent.c |
Warning: | line 372, column 33 Value stored to 'tty_fd' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | /*** |
3 | Copyright © 2015 Werner Fink |
4 | ***/ |
5 | |
6 | #include <errno(*__errno_location ()).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_Bool arg_plymouth = false0; |
52 | static bool_Bool arg_console = false0; |
53 | static const char *arg_device = NULL((void*)0); |
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{ .un.sun_family = 1, .un.sun_path = "\0/org/freedesktop/plymouthd" , }; |
63 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1, notify = -1; |
64 | _cleanup_free___attribute__((cleanup(freep))) char *packet = NULL((void*)0); |
65 | ssize_t k; |
66 | int r, n; |
67 | struct pollfd pollfd[2] = {}; |
68 | char buffer[LINE_MAX2048]; |
69 | size_t p = 0; |
70 | enum { |
71 | POLL_SOCKET, |
72 | POLL_INOTIFY |
73 | }; |
74 | |
75 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 75, __PRETTY_FUNCTION__); } while (0); |
76 | |
77 | if (flag_file) { |
78 | notify = inotify_init1(IN_CLOEXECIN_CLOEXEC|IN_NONBLOCKIN_NONBLOCK); |
79 | if (notify < 0) |
80 | return -errno(*__errno_location ()); |
81 | |
82 | r = inotify_add_watch(notify, flag_file, IN_ATTRIB0x00000004); /* for the link count */ |
83 | if (r < 0) |
84 | return -errno(*__errno_location ()); |
85 | } |
86 | |
87 | fd = socket(AF_UNIX1, SOCK_STREAMSOCK_STREAM|SOCK_CLOEXECSOCK_CLOEXEC|SOCK_NONBLOCKSOCK_NONBLOCK, 0); |
88 | if (fd < 0) |
89 | return -errno(*__errno_location ()); |
90 | |
91 | r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)({ const struct sockaddr_un *_sa = &(sa.un); do { if ((__builtin_expect (!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm( LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 91, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof(struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1 + strnlen (_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen(_sa ->sun_path, sizeof(_sa->sun_path))); })); |
92 | if (r < 0) |
93 | return -errno(*__errno_location ()); |
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((void*)0); |
100 | if (!packet) |
101 | return -ENOMEM12; |
102 | |
103 | r = loop_write(fd, packet, n + 1, true1); |
104 | if (r < 0) |
105 | return r; |
106 | |
107 | pollfd[POLL_SOCKET].fd = fd; |
108 | pollfd[POLL_SOCKET].events = POLLIN0x001; |
109 | pollfd[POLL_INOTIFY].fd = notify; |
110 | pollfd[POLL_INOTIFY].events = POLLIN0x001; |
111 | |
112 | for (;;) { |
113 | int sleep_for = -1, j; |
114 | |
115 | if (until > 0) { |
116 | usec_t y; |
117 | |
118 | y = now(CLOCK_MONOTONIC1); |
119 | |
120 | if (y > until) { |
121 | r = -ETIME62; |
122 | goto finish; |
123 | } |
124 | |
125 | sleep_for = (int) ((until - y) / USEC_PER_MSEC((usec_t) 1000ULL)); |
126 | } |
127 | |
128 | if (flag_file && access(flag_file, F_OK0) < 0) { |
129 | r = -errno(*__errno_location ()); |
130 | goto finish; |
131 | } |
132 | |
133 | j = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for); |
134 | if (j < 0) { |
135 | if (errno(*__errno_location ()) == EINTR4) |
136 | continue; |
137 | |
138 | r = -errno(*__errno_location ()); |
139 | goto finish; |
140 | } else if (j == 0) { |
141 | r = -ETIME62; |
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)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){4, 11})/sizeof(int)]; switch((*__errno_location ())) { case 4: case 11: _found = 1; break; default: break; } _found; })) |
154 | continue; |
155 | |
156 | r = -errno(*__errno_location ()); |
157 | goto finish; |
158 | } else if (k == 0) { |
159 | r = -EIO5; |
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 = -ENOMEM12; |
178 | goto finish; |
179 | } |
180 | |
181 | r = loop_write(fd, packet, n+1, true1); |
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 = -ENOENT2; |
192 | goto finish; |
193 | |
194 | } else if (IN_SET(buffer[0], 2, 9)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){2, 9})/sizeof(int)]; switch(buffer[0]) { case 2: case 9: _found = 1; break; default: break; } _found; })) { |
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)__uint32_identity (size); |
204 | if (size + 5 > sizeof(buffer)) { |
205 | r = -EIO5; |
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 = -ENOMEM12; |
215 | goto finish; |
216 | } |
217 | |
218 | *ret = l; |
219 | break; |
220 | |
221 | } else { |
222 | /* Unknown packet */ |
223 | r = -EIO5; |
224 | goto finish; |
225 | } |
226 | } |
227 | |
228 | r = 0; |
229 | |
230 | finish: |
231 | explicit_bzero(buffer, sizeof(buffer)); |
232 | return r; |
233 | } |
234 | |
235 | static int send_passwords(const char *socket_name, char **passwords) { |
236 | _cleanup_free___attribute__((cleanup(freep))) char *packet = NULL((void*)0); |
237 | _cleanup_close___attribute__((cleanup(closep))) int socket_fd = -1; |
238 | union sockaddr_union sa = { .un.sun_family = AF_UNIX1 }; |
239 | size_t packet_length = 1; |
240 | char **p, *d; |
241 | ssize_t n; |
242 | int r; |
243 | |
244 | assert(socket_name)do { if ((__builtin_expect(!!(!(socket_name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("socket_name"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 244, __PRETTY_FUNCTION__); } while (0); |
245 | |
246 | STRV_FOREACH(p, passwords)for ((p) = (passwords); (p) && *(p); (p)++) |
247 | packet_length += strlen(*p) + 1; |
248 | |
249 | packet = new(char, packet_length)((char*) malloc_multiply(sizeof(char), (packet_length))); |
250 | if (!packet) |
251 | return -ENOMEM12; |
252 | |
253 | packet[0] = '+'; |
254 | |
255 | d = packet + 1; |
256 | STRV_FOREACH(p, passwords)for ((p) = (passwords); (p) && *(p); (p)++) |
257 | d = stpcpy(d, *p) + 1; |
258 | |
259 | socket_fd = socket(AF_UNIX1, SOCK_DGRAMSOCK_DGRAM|SOCK_CLOEXECSOCK_CLOEXEC, 0); |
260 | if (socket_fd < 0) { |
261 | r = log_debug_errno(errno, "socket(): %m")({ int _level = ((7)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 261, __func__, "socket(): %m") : -abs(_e); }); |
262 | goto finish; |
263 | } |
264 | |
265 | strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path)); |
266 | |
267 | n = sendto(socket_fd, packet, packet_length, MSG_NOSIGNALMSG_NOSIGNAL, &sa.sa, SOCKADDR_UN_LEN(sa.un)({ const struct sockaddr_un *_sa = &(sa.un); do { if ((__builtin_expect (!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm( LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 267, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof( struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen (_sa->sun_path, sizeof(_sa->sun_path))); })); |
268 | if (n < 0) { |
269 | r = log_debug_errno(errno, "sendto(): %m")({ int _level = ((7)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 269, __func__, "sendto(): %m") : -abs(_e); }); |
270 | goto finish; |
271 | } |
272 | |
273 | r = (int) n; |
274 | |
275 | finish: |
276 | explicit_bzero(packet, packet_length); |
277 | return r; |
278 | } |
279 | |
280 | static int parse_password(const char *filename, char **wall) { |
281 | _cleanup_free___attribute__((cleanup(freep))) char *socket_name = NULL((void*)0), *message = NULL((void*)0); |
282 | bool_Bool accept_cached = false0, echo = false0; |
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, ¬_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)do { if ((__builtin_expect(!!(!(filename)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("filename"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 298, __PRETTY_FUNCTION__); } while (0); |
299 | |
300 | r = config_parse(NULL((void*)0), filename, NULL((void*)0), |
301 | NULL((void*)0), |
302 | config_item_table_lookup, items, |
303 | CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN, NULL((void*)0)); |
304 | if (r < 0) |
305 | return r; |
306 | |
307 | if (!socket_name) { |
308 | log_error("Invalid password file %s", filename)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 308 , __func__, "Invalid password file %s", filename) : -abs(_e); }); |
309 | return -EBADMSG74; |
310 | } |
311 | |
312 | if (not_after > 0 && now(CLOCK_MONOTONIC1) > not_after) |
313 | return 0; |
314 | |
315 | if (pid > 0 && !pid_is_alive(pid)) |
316 | return 0; |
317 | |
318 | if (arg_action == ACTION_LIST) |
319 | printf("'%s' (PID %u)\n", message, pid); |
320 | |
321 | else if (arg_action == ACTION_WALL) { |
322 | char *_wall; |
323 | |
324 | if (asprintf(&_wall, |
325 | "%s%sPassword entry required for \'%s\' (PID %u).\r\n" |
326 | "Please enter password with the systemd-tty-ask-password-agent tool!", |
327 | strempty(*wall), |
328 | *wall ? "\r\n\r\n" : "", |
329 | message, |
330 | pid) < 0) |
331 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 331, __func__); |
332 | |
333 | free(*wall); |
334 | *wall = _wall; |
335 | |
336 | } else { |
337 | _cleanup_strv_free_erase___attribute__((cleanup(strv_free_erasep))) char **passwords = NULL((void*)0); |
338 | |
339 | assert(IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended[20 - sizeof((int[]){ACTION_QUERY, ACTION_WATCH})/sizeof(int)]; switch (arg_action) { case ACTION_QUERY: case ACTION_WATCH: _found = 1; break; default: break; } _found; }))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH)" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 339 , __PRETTY_FUNCTION__); } while (0); |
340 | |
341 | if (access(socket_name, W_OK2) < 0) { |
342 | if (arg_action == ACTION_QUERY) |
343 | log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 343 , __func__, "Not querying '%s' (PID %u), lacking privileges." , message, pid) : -abs(_e); }); |
344 | |
345 | return 0; |
346 | } |
347 | |
348 | if (arg_plymouth) |
349 | r = ask_password_plymouth(message, not_after, accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0, filename, &passwords); |
350 | else { |
351 | char *password = NULL((void*)0); |
352 | int tty_fd = -1; |
353 | |
354 | if (arg_console) { |
355 | const char *con = arg_device ?: "/dev/console"; |
356 | |
357 | tty_fd = acquire_terminal(con, ACQUIRE_TERMINAL_WAIT, USEC_INFINITY((usec_t) -1)); |
358 | if (tty_fd < 0) |
359 | return log_error_errno(tty_fd, "Failed to acquire %s: %m", con)({ int _level = ((3)), _e = ((tty_fd)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 359 , __func__, "Failed to acquire %s: %m", con) : -abs(_e); }); |
360 | |
361 | r = reset_terminal_fd(tty_fd, true1); |
362 | if (r < 0) |
363 | log_warning_errno(r, "Failed to reset terminal, ignoring: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 363 , __func__, "Failed to reset terminal, ignoring: %m") : -abs( _e); }); |
364 | } |
365 | |
366 | r = ask_password_tty(tty_fd, message, NULL((void*)0), not_after, |
367 | (echo ? ASK_PASSWORD_ECHO : 0) | |
368 | (arg_console ? ASK_PASSWORD_CONSOLE_COLOR : 0), |
369 | filename, &password); |
370 | |
371 | if (arg_console) { |
372 | tty_fd = safe_close(tty_fd); |
Value stored to 'tty_fd' is never read | |
373 | release_terminal(); |
374 | } |
375 | |
376 | if (r >= 0) |
377 | r = strv_push(&passwords, password); |
378 | |
379 | if (r < 0) |
380 | string_free_erase(password); |
381 | } |
382 | |
383 | /* If the query went away, that's OK */ |
384 | if (IN_SET(r, -ETIME, -ENOENT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){-62, -2})/sizeof(int)]; switch(r) { case -62: case -2: _found = 1; break; default: break; } _found; } )) |
385 | return 0; |
386 | |
387 | if (r < 0) |
388 | return log_error_errno(r, "Failed to query password: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 388 , __func__, "Failed to query password: %m") : -abs(_e); }); |
389 | |
390 | r = send_passwords(socket_name, passwords); |
391 | if (r < 0) |
392 | return log_error_errno(r, "Failed to send: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 392 , __func__, "Failed to send: %m") : -abs(_e); }); |
393 | } |
394 | |
395 | return 0; |
396 | } |
397 | |
398 | static int wall_tty_block(void) { |
399 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
400 | dev_t devnr; |
401 | int fd, r; |
402 | |
403 | r = get_ctty_devnr(0, &devnr); |
404 | if (r == -ENXIO6) /* We have no controlling tty */ |
405 | return -ENOTTY25; |
406 | if (r < 0) |
407 | return log_error_errno(r, "Failed to get controlling TTY: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 407 , __func__, "Failed to get controlling TTY: %m") : -abs(_e); } ); |
408 | |
409 | if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(devnr)gnu_dev_major (devnr), minor(devnr)gnu_dev_minor (devnr)) < 0) |
410 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 410, __func__); |
411 | |
412 | (void) mkdir_parents_label(p, 0700); |
413 | (void) mkfifo(p, 0600); |
414 | |
415 | fd = open(p, O_RDONLY00|O_CLOEXEC02000000|O_NONBLOCK04000|O_NOCTTY0400); |
416 | if (fd < 0) |
417 | return log_debug_errno(errno, "Failed to open %s: %m", p)({ int _level = ((7)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 417, __func__, "Failed to open %s: %m", p) : -abs(_e); }); |
418 | |
419 | return fd; |
420 | } |
421 | |
422 | static bool_Bool wall_tty_match(const char *path, void *userdata) { |
423 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
424 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; |
425 | struct stat st; |
426 | |
427 | if (!path_is_absolute(path)) |
428 | path = strjoina("/dev/", path)({ const char *_appendees_[] = { "/dev/", path }; char *_d_, * _p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_ )/sizeof((_appendees_)[0]), ((void)0))) && _appendees_ [_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca (_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(_appendees_), typeof(& *(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0] ), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy (_p_, _appendees_[_i_]); *_p_ = 0; _d_; }); |
429 | |
430 | if (lstat(path, &st) < 0) { |
431 | log_debug_errno(errno, "Failed to stat %s: %m", path)({ int _level = ((7)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 431, __func__, "Failed to stat %s: %m", path) : -abs(_e); } ); |
432 | return true1; |
433 | } |
434 | |
435 | if (!S_ISCHR(st.st_mode)((((st.st_mode)) & 0170000) == (0020000))) { |
436 | log_debug("%s is not a character device.", path)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 436 , __func__, "%s is not a character device.", path) : -abs(_e) ; }); |
437 | return true1; |
438 | } |
439 | |
440 | /* We use named pipes to ensure that wall messages suggesting |
441 | * password entry are not printed over password prompts |
442 | * already shown. We use the fact here that opening a pipe in |
443 | * non-blocking mode for write-only will succeed only if |
444 | * there's some writer behind it. Using pipes has the |
445 | * advantage that the block will automatically go away if the |
446 | * process dies. */ |
447 | |
448 | if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(st.st_rdev)gnu_dev_major (st.st_rdev), minor(st.st_rdev)gnu_dev_minor (st.st_rdev)) < 0) { |
449 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 449, __func__); |
450 | return true1; |
451 | } |
452 | |
453 | fd = open(p, O_WRONLY01|O_CLOEXEC02000000|O_NONBLOCK04000|O_NOCTTY0400); |
454 | if (fd < 0) { |
455 | log_debug_errno(errno, "Failed to open the wall pipe: %m")({ int _level = ((7)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 455, __func__, "Failed to open the wall pipe: %m") : -abs(_e ); }); |
456 | return 1; |
457 | } |
458 | |
459 | /* What, we managed to open the pipe? Then this tty is filtered. */ |
460 | return 0; |
461 | } |
462 | |
463 | static int show_passwords(void) { |
464 | _cleanup_closedir___attribute__((cleanup(closedirp))) DIR *d; |
465 | struct dirent *de; |
466 | int r = 0; |
467 | |
468 | d = opendir("/run/systemd/ask-password"); |
469 | if (!d) { |
470 | if (errno(*__errno_location ()) == ENOENT2) |
471 | return 0; |
472 | |
473 | return log_error_errno(errno, "Failed to open /run/systemd/ask-password: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 473, __func__, "Failed to open /run/systemd/ask-password: %m" ) : -abs(_e); }); |
474 | } |
475 | |
476 | FOREACH_DIRENT_ALL(de, d, return log_error_errno(errno, "Failed to read directory: %m"))for ((*__errno_location ()) = 0, de = readdir(d);; (*__errno_location ()) = 0, de = readdir(d)) if (!de) { if ((*__errno_location ( )) > 0) { return ({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm (_realm) >= ((_level) & 0x07)) ? log_internal_realm((( _realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 476, __func__, "Failed to read directory: %m") : -abs(_e); } ); } break; } else { |
477 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0), *wall = NULL((void*)0); |
478 | int q; |
479 | |
480 | /* We only support /dev on tmpfs, hence we can rely on |
481 | * d_type to be reliable */ |
482 | |
483 | if (de->d_type != DT_REGDT_REG) |
484 | continue; |
485 | |
486 | if (hidden_or_backup_file(de->d_name)) |
487 | continue; |
488 | |
489 | if (!startswith(de->d_name, "ask.")) |
490 | continue; |
491 | |
492 | p = strappend("/run/systemd/ask-password/", de->d_name); |
493 | if (!p) |
494 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 494, __func__); |
495 | |
496 | q = parse_password(p, &wall); |
497 | if (q < 0 && r == 0) |
498 | r = q; |
499 | |
500 | if (wall) |
501 | (void) utmp_wall(wall, NULL((void*)0), NULL((void*)0), wall_tty_match, NULL((void*)0)); |
502 | } |
503 | |
504 | return r; |
505 | } |
506 | |
507 | static int watch_passwords(void) { |
508 | enum { |
509 | FD_INOTIFY, |
510 | FD_SIGNAL, |
511 | _FD_MAX |
512 | }; |
513 | |
514 | _cleanup_close___attribute__((cleanup(closep))) int notify = -1, signal_fd = -1, tty_block_fd = -1; |
515 | struct pollfd pollfd[_FD_MAX] = {}; |
516 | sigset_t mask; |
517 | int r; |
518 | |
519 | tty_block_fd = wall_tty_block(); |
520 | |
521 | (void) mkdir_p_label("/run/systemd/ask-password", 0755); |
522 | |
523 | notify = inotify_init1(IN_CLOEXECIN_CLOEXEC); |
524 | if (notify < 0) |
525 | return log_error_errno(errno, "Failed to allocate directory watch: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 525, __func__, "Failed to allocate directory watch: %m") : - abs(_e); }); |
526 | |
527 | if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE0x00000008|IN_MOVED_TO0x00000080) < 0) |
528 | return log_error_errno(errno, "Failed to add /run/systemd/ask-password to directory watch: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 528, __func__, "Failed to add /run/systemd/ask-password to directory watch: %m" ) : -abs(_e); }); |
529 | |
530 | assert_se(sigemptyset(&mask) >= 0)do { if ((__builtin_expect(!!(!(sigemptyset(&mask) >= 0 )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("sigemptyset(&mask) >= 0" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 530 , __PRETTY_FUNCTION__); } while (0); |
531 | assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0)do { if ((__builtin_expect(!!(!(sigset_add_many(&mask, 2, 15, -1) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0") , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 531 , __PRETTY_FUNCTION__); } while (0); |
532 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) >= 0)do { if ((__builtin_expect(!!(!(sigprocmask(2, &mask, ((void *)0)) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("sigprocmask(SIG_SETMASK, &mask, NULL) >= 0"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 532, __PRETTY_FUNCTION__); } while (0); |
533 | |
534 | signal_fd = signalfd(-1, &mask, SFD_NONBLOCKSFD_NONBLOCK|SFD_CLOEXECSFD_CLOEXEC); |
535 | if (signal_fd < 0) |
536 | return log_error_errno(errno, "Failed to allocate signal file descriptor: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 536, __func__, "Failed to allocate signal file descriptor: %m" ) : -abs(_e); }); |
537 | |
538 | pollfd[FD_INOTIFY].fd = notify; |
539 | pollfd[FD_INOTIFY].events = POLLIN0x001; |
540 | pollfd[FD_SIGNAL].fd = signal_fd; |
541 | pollfd[FD_SIGNAL].events = POLLIN0x001; |
542 | |
543 | for (;;) { |
544 | r = show_passwords(); |
545 | if (r < 0) |
546 | log_error_errno(r, "Failed to show password: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 546 , __func__, "Failed to show password: %m") : -abs(_e); }); |
547 | |
548 | if (poll(pollfd, _FD_MAX, -1) < 0) { |
549 | if (errno(*__errno_location ()) == EINTR4) |
550 | continue; |
551 | |
552 | return -errno(*__errno_location ()); |
553 | } |
554 | |
555 | if (pollfd[FD_INOTIFY].revents != 0) |
556 | (void) flush_fd(notify); |
557 | |
558 | if (pollfd[FD_SIGNAL].revents != 0) |
559 | break; |
560 | } |
561 | |
562 | return 0; |
563 | } |
564 | |
565 | static void help(void) { |
566 | printf("%s [OPTIONS...]\n\n" |
567 | "Process system password requests.\n\n" |
568 | " -h --help Show this help\n" |
569 | " --version Show package version\n" |
570 | " --list Show pending password requests\n" |
571 | " --query Process pending password requests\n" |
572 | " --watch Continuously process password requests\n" |
573 | " --wall Continuously forward password requests to wall\n" |
574 | " --plymouth Ask question with Plymouth instead of on TTY\n" |
575 | " --console Ask question on /dev/console instead of current TTY\n", |
576 | program_invocation_short_name); |
577 | } |
578 | |
579 | static int parse_argv(int argc, char *argv[]) { |
580 | |
581 | enum { |
582 | ARG_LIST = 0x100, |
583 | ARG_QUERY, |
584 | ARG_WATCH, |
585 | ARG_WALL, |
586 | ARG_PLYMOUTH, |
587 | ARG_CONSOLE, |
588 | ARG_VERSION |
589 | }; |
590 | |
591 | static const struct option options[] = { |
592 | { "help", no_argument0, NULL((void*)0), 'h' }, |
593 | { "version", no_argument0, NULL((void*)0), ARG_VERSION }, |
594 | { "list", no_argument0, NULL((void*)0), ARG_LIST }, |
595 | { "query", no_argument0, NULL((void*)0), ARG_QUERY }, |
596 | { "watch", no_argument0, NULL((void*)0), ARG_WATCH }, |
597 | { "wall", no_argument0, NULL((void*)0), ARG_WALL }, |
598 | { "plymouth", no_argument0, NULL((void*)0), ARG_PLYMOUTH }, |
599 | { "console", optional_argument2, NULL((void*)0), ARG_CONSOLE }, |
600 | {} |
601 | }; |
602 | |
603 | int c; |
604 | |
605 | assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 605, __PRETTY_FUNCTION__); } while (0); |
606 | assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("argv"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 606, __PRETTY_FUNCTION__); } while (0); |
607 | |
608 | while ((c = getopt_long(argc, argv, "h", options, NULL((void*)0))) >= 0) |
609 | |
610 | switch (c) { |
611 | |
612 | case 'h': |
613 | help(); |
614 | return 0; |
615 | |
616 | case ARG_VERSION: |
617 | return version(); |
618 | |
619 | case ARG_LIST: |
620 | arg_action = ACTION_LIST; |
621 | break; |
622 | |
623 | case ARG_QUERY: |
624 | arg_action = ACTION_QUERY; |
625 | break; |
626 | |
627 | case ARG_WATCH: |
628 | arg_action = ACTION_WATCH; |
629 | break; |
630 | |
631 | case ARG_WALL: |
632 | arg_action = ACTION_WALL; |
633 | break; |
634 | |
635 | case ARG_PLYMOUTH: |
636 | arg_plymouth = true1; |
637 | break; |
638 | |
639 | case ARG_CONSOLE: |
640 | arg_console = true1; |
641 | if (optarg) { |
642 | |
643 | if (isempty(optarg)) { |
644 | log_error("Empty console device path is not allowed.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 644 , __func__, "Empty console device path is not allowed.") : -abs (_e); }); |
645 | return -EINVAL22; |
646 | } |
647 | |
648 | arg_device = optarg; |
649 | } |
650 | break; |
651 | |
652 | case '?': |
653 | return -EINVAL22; |
654 | |
655 | default: |
656 | assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, ( "Unhandled option"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 656, __PRETTY_FUNCTION__); } while (0); |
657 | } |
658 | |
659 | if (optind != argc) { |
660 | log_error("%s takes no arguments.", program_invocation_short_name)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 660 , __func__, "%s takes no arguments.", program_invocation_short_name ) : -abs(_e); }); |
661 | return -EINVAL22; |
662 | } |
663 | |
664 | if (arg_plymouth || arg_console) { |
665 | |
666 | if (!IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){ACTION_QUERY, ACTION_WATCH})/sizeof(int) ]; switch(arg_action) { case ACTION_QUERY: case ACTION_WATCH: _found = 1; break; default: break; } _found; })) { |
667 | log_error("Options --query and --watch conflict.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 667 , __func__, "Options --query and --watch conflict.") : -abs(_e ); }); |
668 | return -EINVAL22; |
669 | } |
670 | |
671 | if (arg_plymouth && arg_console) { |
672 | log_error("Options --plymouth and --console conflict.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 672 , __func__, "Options --plymouth and --console conflict.") : - abs(_e); }); |
673 | return -EINVAL22; |
674 | } |
675 | } |
676 | |
677 | return 1; |
678 | } |
679 | |
680 | /* |
681 | * To be able to ask on all terminal devices of /dev/console |
682 | * the devices are collected. If more than one device is found, |
683 | * then on each of the terminals a inquiring task is forked. |
684 | * Every task has its own session and its own controlling terminal. |
685 | * If one of the tasks does handle a password, the remaining tasks |
686 | * will be terminated. |
687 | */ |
688 | static int ask_on_this_console(const char *tty, pid_t *ret_pid, int argc, char *argv[]) { |
689 | struct sigaction sig = { |
690 | .sa_handler__sigaction_handler.sa_handler = nop_signal_handler, |
691 | .sa_flags = SA_NOCLDSTOP1 | SA_RESTART0x10000000, |
692 | }; |
693 | pid_t pid; |
694 | int r; |
695 | |
696 | assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD, -1) >= 0)do { if ((__builtin_expect(!!(!(sigprocmask_many(1, ((void*)0 ), 1, 17, -1) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD, -1) >= 0" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 696 , __PRETTY_FUNCTION__); } while (0); |
697 | |
698 | assert_se(sigemptyset(&sig.sa_mask) >= 0)do { if ((__builtin_expect(!!(!(sigemptyset(&sig.sa_mask) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("sigemptyset(&sig.sa_mask) >= 0" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 698 , __PRETTY_FUNCTION__); } while (0); |
699 | assert_se(sigaction(SIGCHLD, &sig, NULL) >= 0)do { if ((__builtin_expect(!!(!(sigaction(17, &sig, ((void *)0)) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("sigaction(SIGCHLD, &sig, NULL) >= 0"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 699, __PRETTY_FUNCTION__); } while (0); |
700 | |
701 | sig.sa_handler__sigaction_handler.sa_handler = SIG_DFL((__sighandler_t) 0); |
702 | assert_se(sigaction(SIGHUP, &sig, NULL) >= 0)do { if ((__builtin_expect(!!(!(sigaction(1, &sig, ((void *)0)) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("sigaction(SIGHUP, &sig, NULL) >= 0"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 702, __PRETTY_FUNCTION__); } while (0); |
703 | |
704 | r = safe_fork("(sd-passwd)", FORK_RESET_SIGNALS|FORK_LOG, &pid); |
705 | if (r < 0) |
706 | return r; |
707 | if (r == 0) { |
708 | int ac; |
709 | |
710 | assert_se(prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0)do { if ((__builtin_expect(!!(!(prctl(1, 1) >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 710 , __PRETTY_FUNCTION__); } while (0); |
711 | |
712 | for (ac = 0; ac < argc; ac++) { |
713 | if (streq(argv[ac], "--console")(strcmp((argv[ac]),("--console")) == 0)) { |
714 | argv[ac] = strjoina("--console=", tty)({ const char *_appendees_[] = { "--console=", tty }; char *_d_ , *_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_ )/sizeof((_appendees_)[0]), ((void)0))) && _appendees_ [_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca (_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(_appendees_), typeof(& *(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0] ), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy (_p_, _appendees_[_i_]); *_p_ = 0; _d_; }); |
715 | break; |
716 | } |
717 | } |
718 | |
719 | assert(ac < argc)do { if ((__builtin_expect(!!(!(ac < argc)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ac < argc"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 719, __PRETTY_FUNCTION__); } while (0); |
720 | |
721 | execv(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH"/usr/bin/systemd-tty-ask-password-agent", argv); |
722 | _exit(EXIT_FAILURE1); |
723 | } |
724 | |
725 | *ret_pid = pid; |
726 | return 0; |
727 | } |
728 | |
729 | static void terminate_agents(Set *pids) { |
730 | struct timespec ts; |
731 | siginfo_t status = {}; |
732 | sigset_t set; |
733 | Iterator i; |
734 | void *p; |
735 | int r, signum; |
736 | |
737 | /* |
738 | * Request termination of the remaining processes as those |
739 | * are not required anymore. |
740 | */ |
741 | SET_FOREACH(p, pids, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); set_iterate((pids), &(i), (void **)&(p)); ) |
742 | (void) kill(PTR_TO_PID(p), SIGTERM15); |
743 | |
744 | /* |
745 | * Collect the processes which have go away. |
746 | */ |
747 | assert_se(sigemptyset(&set) >= 0)do { if ((__builtin_expect(!!(!(sigemptyset(&set) >= 0 )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("sigemptyset(&set) >= 0" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 747 , __PRETTY_FUNCTION__); } while (0); |
748 | assert_se(sigaddset(&set, SIGCHLD) >= 0)do { if ((__builtin_expect(!!(!(sigaddset(&set, 17) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("sigaddset(&set, SIGCHLD) >= 0" ), "../src/tty-ask-password-agent/tty-ask-password-agent.c", 748 , __PRETTY_FUNCTION__); } while (0); |
749 | timespec_store(&ts, 50 * USEC_PER_MSEC((usec_t) 1000ULL)); |
750 | |
751 | while (!set_isempty(pids)) { |
752 | |
753 | zero(status)(({ size_t _l_ = (sizeof(status)); void *_x_ = (&(status) ); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); })); |
754 | r = waitid(P_ALL, 0, &status, WEXITED4|WNOHANG1); |
755 | if (r < 0 && errno(*__errno_location ()) == EINTR4) |
756 | continue; |
757 | |
758 | if (r == 0 && status.si_pid_sifields._kill.si_pid > 0) { |
759 | set_remove(pids, PID_TO_PTR(status.si_pid_sifields._kill.si_pid)); |
760 | continue; |
761 | } |
762 | |
763 | signum = sigtimedwait(&set, NULL((void*)0), &ts); |
764 | if (signum < 0) { |
765 | if (errno(*__errno_location ()) != EAGAIN11) |
766 | log_error_errno(errno, "sigtimedwait() failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 766, __func__, "sigtimedwait() failed: %m") : -abs(_e); }); |
767 | break; |
768 | } |
769 | assert(signum == SIGCHLD)do { if ((__builtin_expect(!!(!(signum == 17)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("signum == SIGCHLD"), "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 769, __PRETTY_FUNCTION__); } while (0); |
770 | } |
771 | |
772 | /* |
773 | * Kill hanging processes. |
774 | */ |
775 | SET_FOREACH(p, pids, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); set_iterate((pids), &(i), (void **)&(p)); ) { |
776 | log_warning("Failed to terminate child %d, killing it", PTR_TO_PID(p))({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 776 , __func__, "Failed to terminate child %d, killing it", PTR_TO_PID (p)) : -abs(_e); }); |
777 | (void) kill(PTR_TO_PID(p), SIGKILL9); |
778 | } |
779 | } |
780 | |
781 | static int ask_on_consoles(int argc, char *argv[]) { |
782 | _cleanup_set_free___attribute__((cleanup(set_freep))) Set *pids = NULL((void*)0); |
783 | _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **consoles = NULL((void*)0); |
784 | siginfo_t status = {}; |
785 | char **tty; |
786 | pid_t pid; |
787 | int r; |
788 | |
789 | r = get_kernel_consoles(&consoles); |
790 | if (r < 0) |
791 | return log_error_errno(r, "Failed to determine devices of /dev/console: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 791 , __func__, "Failed to determine devices of /dev/console: %m" ) : -abs(_e); }); |
792 | |
793 | pids = set_new(NULL)internal_set_new(((void*)0) ); |
794 | if (!pids) |
795 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 795, __func__); |
796 | |
797 | /* Start an agent on each console. */ |
798 | STRV_FOREACH(tty, consoles)for ((tty) = (consoles); (tty) && *(tty); (tty)++) { |
799 | r = ask_on_this_console(*tty, &pid, argc, argv); |
800 | if (r < 0) |
801 | return r; |
802 | |
803 | if (set_put(pids, PID_TO_PTR(pid)) < 0) |
804 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 804, __func__); |
805 | } |
806 | |
807 | /* Wait for an agent to exit. */ |
808 | for (;;) { |
809 | zero(status)(({ size_t _l_ = (sizeof(status)); void *_x_ = (&(status) ); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); })); |
810 | |
811 | if (waitid(P_ALL, 0, &status, WEXITED4) < 0) { |
812 | if (errno(*__errno_location ()) == EINTR4) |
813 | continue; |
814 | |
815 | return log_error_errno(errno, "waitid() failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/tty-ask-password-agent/tty-ask-password-agent.c" , 815, __func__, "waitid() failed: %m") : -abs(_e); }); |
816 | } |
817 | |
818 | set_remove(pids, PID_TO_PTR(status.si_pid_sifields._kill.si_pid)); |
819 | break; |
820 | } |
821 | |
822 | if (!is_clean_exit(status.si_code, status.si_status_sifields._sigchld.si_status, EXIT_CLEAN_DAEMON, NULL((void*)0))) |
823 | log_error("Password agent failed with: %d", status.si_status)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/tty-ask-password-agent/tty-ask-password-agent.c", 823 , __func__, "Password agent failed with: %d", status._sifields ._sigchld.si_status) : -abs(_e); }); |
824 | |
825 | terminate_agents(pids); |
826 | return 0; |
827 | } |
828 | |
829 | int main(int argc, char *argv[]) { |
830 | int r; |
831 | |
832 | log_set_target(LOG_TARGET_AUTO); |
833 | log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD); |
834 | log_open(); |
835 | |
836 | umask(0022); |
837 | |
838 | r = parse_argv(argc, argv); |
839 | if (r <= 0) |
840 | goto finish; |
841 | |
842 | if (arg_console && !arg_device) |
843 | /* |
844 | * Spawn for each console device a separate process. |
845 | */ |
846 | r = ask_on_consoles(argc, argv); |
847 | else { |
848 | |
849 | if (arg_device) { |
850 | /* |
851 | * Later on, a controlling terminal will be acquired, |
852 | * therefore the current process has to become a session |
853 | * leader and should not have a controlling terminal already. |
854 | */ |
855 | (void) setsid(); |
856 | (void) release_terminal(); |
857 | } |
858 | |
859 | if (IN_SET(arg_action, ACTION_WATCH, ACTION_WALL)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){ACTION_WATCH, ACTION_WALL})/sizeof(int)] ; switch(arg_action) { case ACTION_WATCH: case ACTION_WALL: _found = 1; break; default: break; } _found; })) |
860 | r = watch_passwords(); |
861 | else |
862 | r = show_passwords(); |
863 | } |
864 | |
865 | finish: |
866 | return r < 0 ? EXIT_FAILURE1 : EXIT_SUCCESS0; |
867 | } |