]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
6bb92a16 | 2 | |
00843602 | 3 | #include <signal.h> |
6bb92a16 LP |
4 | #include <stdlib.h> |
5 | #include <unistd.h> | |
6bb92a16 | 6 | |
3e24e8cd | 7 | #include "exec-util.h" |
6bb92a16 | 8 | #include "log.h" |
0b452006 | 9 | #include "process-util.h" |
6bb92a16 LP |
10 | #include "spawn-ask-password-agent.h" |
11 | ||
12 | static pid_t agent_pid = 0; | |
13 | ||
14 | int ask_password_agent_open(void) { | |
15 | int r; | |
16 | ||
17 | if (agent_pid > 0) | |
18 | return 0; | |
19 | ||
20 | /* We check STDIN here, not STDOUT, since this is about input, | |
21 | * not output */ | |
22 | if (!isatty(STDIN_FILENO)) | |
23 | return 0; | |
24 | ||
85afeae8 LP |
25 | if (!is_main_thread()) |
26 | return -EPERM; | |
27 | ||
78752f2e | 28 | r = fork_agent("(sd-askpwagent)", |
9bdc770c | 29 | NULL, 0, |
78752f2e | 30 | &agent_pid, |
9bdc770c LP |
31 | SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, |
32 | SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL); | |
6bb92a16 | 33 | if (r < 0) |
00843602 | 34 | return log_error_errno(r, "Failed to fork TTY ask password agent: %m"); |
6bb92a16 | 35 | |
00843602 | 36 | return 1; |
6bb92a16 LP |
37 | } |
38 | ||
39 | void ask_password_agent_close(void) { | |
40 | ||
41 | if (agent_pid <= 0) | |
42 | return; | |
43 | ||
44 | /* Inform agent that we are done */ | |
8f03de53 | 45 | sigterm_wait(TAKE_PID(agent_pid)); |
6bb92a16 | 46 | } |
c59e2ec6 LP |
47 | |
48 | int ask_password_agent_open_if_enabled(BusTransport transport, bool ask_password) { | |
49 | ||
50 | /* Open the ask password agent as a child process if necessary */ | |
51 | ||
52 | if (transport != BUS_TRANSPORT_LOCAL) | |
53 | return 0; | |
54 | ||
55 | if (!ask_password) | |
56 | return 0; | |
57 | ||
58 | return ask_password_agent_open(); | |
59 | } |