]> git.proxmox.com Git - systemd.git/blame - src/shared/spawn-ask-password-agent.c
Imported Upstream version 229
[systemd.git] / src / shared / spawn-ask-password-agent.c
CommitLineData
663996b3
MS
1/***
2 This file is part of systemd.
3
4 Copyright 2011 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
6300502b 20#include <signal.h>
663996b3
MS
21#include <stdlib.h>
22#include <unistd.h>
663996b3
MS
23
24#include "log.h"
e3bff60a 25#include "process-util.h"
663996b3 26#include "spawn-ask-password-agent.h"
db2df898 27#include "util.h"
663996b3
MS
28
29static pid_t agent_pid = 0;
30
31int ask_password_agent_open(void) {
32 int r;
33
34 if (agent_pid > 0)
35 return 0;
36
37 /* We check STDIN here, not STDOUT, since this is about input,
38 * not output */
39 if (!isatty(STDIN_FILENO))
40 return 0;
41
42 r = fork_agent(&agent_pid,
43 NULL, 0,
44 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
45 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
46 if (r < 0)
6300502b 47 return log_error_errno(r, "Failed to fork TTY ask password agent: %m");
663996b3 48
6300502b 49 return 1;
663996b3
MS
50}
51
52void ask_password_agent_close(void) {
53
54 if (agent_pid <= 0)
55 return;
56
57 /* Inform agent that we are done */
6300502b
MP
58 (void) kill(agent_pid, SIGTERM);
59 (void) kill(agent_pid, SIGCONT);
f47781d8 60 (void) wait_for_terminate(agent_pid, NULL);
663996b3
MS
61 agent_pid = 0;
62}