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