]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/spawn-ask-password-agent.c
Merge pull request #12603 from ssahani/ndisc-blacklist
[thirdparty/systemd.git] / src / shared / spawn-ask-password-agent.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6bb92a16 2
00843602 3#include <signal.h>
6bb92a16
LP
4#include <stdlib.h>
5#include <unistd.h>
6bb92a16
LP
6
7#include "log.h"
0b452006 8#include "process-util.h"
6bb92a16 9#include "spawn-ask-password-agent.h"
cf0fbc49 10#include "util.h"
6bb92a16
LP
11
12static pid_t agent_pid = 0;
13
14int 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
39void ask_password_agent_close(void) {
40
41 if (agent_pid <= 0)
42 return;
43
44 /* Inform agent that we are done */
0adc28ce 45 (void) kill_and_sigcont(agent_pid, SIGTERM);
c73d180d 46 (void) wait_for_terminate(agent_pid, NULL);
6bb92a16
LP
47 agent_pid = 0;
48}