]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/spawn-ask-password-agent.c
machined: return recognizable error when we try to register the same machine name...
[thirdparty/systemd.git] / src / shared / spawn-ask-password-agent.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "exec-util.h"
8 #include "log.h"
9 #include "process-util.h"
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
25 if (!is_main_thread())
26 return -EPERM;
27
28 r = fork_agent("(sd-askpwagent)",
29 NULL, 0,
30 &agent_pid,
31 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
32 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
33 if (r < 0)
34 return log_error_errno(r, "Failed to fork TTY ask password agent: %m");
35
36 return 1;
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 */
45 sigterm_wait(TAKE_PID(agent_pid));
46 }
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 }