]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/spawn-ask-password-agent.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / shared / spawn-ask-password-agent.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6bb92a16 2/***
6bb92a16 3 Copyright 2011 Lennart Poettering
6bb92a16
LP
4***/
5
00843602 6#include <signal.h>
6bb92a16
LP
7#include <stdlib.h>
8#include <unistd.h>
6bb92a16
LP
9
10#include "log.h"
0b452006 11#include "process-util.h"
6bb92a16 12#include "spawn-ask-password-agent.h"
cf0fbc49 13#include "util.h"
6bb92a16
LP
14
15static pid_t agent_pid = 0;
16
17int 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
85afeae8
LP
28 if (!is_main_thread())
29 return -EPERM;
30
78752f2e 31 r = fork_agent("(sd-askpwagent)",
9bdc770c 32 NULL, 0,
78752f2e 33 &agent_pid,
9bdc770c
LP
34 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
35 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
6bb92a16 36 if (r < 0)
00843602 37 return log_error_errno(r, "Failed to fork TTY ask password agent: %m");
6bb92a16 38
00843602 39 return 1;
6bb92a16
LP
40}
41
42void ask_password_agent_close(void) {
43
44 if (agent_pid <= 0)
45 return;
46
47 /* Inform agent that we are done */
0adc28ce 48 (void) kill_and_sigcont(agent_pid, SIGTERM);
c73d180d 49 (void) wait_for_terminate(agent_pid, NULL);
6bb92a16
LP
50 agent_pid = 0;
51}