]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/spawn-ask-password-agent.c
9db8b6d1e74e2bf600428dacc2aab6d1c216d60f
[thirdparty/systemd.git] / src / shared / spawn-ask-password-agent.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2011 Lennart Poettering
4 ***/
5
6 #include <signal.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #include "log.h"
11 #include "process-util.h"
12 #include "spawn-ask-password-agent.h"
13 #include "util.h"
14
15 static pid_t agent_pid = 0;
16
17 int ask_password_agent_open(void) {
18 int r;
19
20 if (agent_pid > 0)
21 return 0;
22
23 /* We check STDIN here, not STDOUT, since this is about input,
24 * not output */
25 if (!isatty(STDIN_FILENO))
26 return 0;
27
28 if (!is_main_thread())
29 return -EPERM;
30
31 r = fork_agent("(sd-askpwagent)",
32 NULL, 0,
33 &agent_pid,
34 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
35 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
36 if (r < 0)
37 return log_error_errno(r, "Failed to fork TTY ask password agent: %m");
38
39 return 1;
40 }
41
42 void ask_password_agent_close(void) {
43
44 if (agent_pid <= 0)
45 return;
46
47 /* Inform agent that we are done */
48 (void) kill_and_sigcont(agent_pid, SIGTERM);
49 (void) wait_for_terminate(agent_pid, NULL);
50 agent_pid = 0;
51 }