]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/spawn-ask-password-agent.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / shared / spawn-ask-password-agent.c
CommitLineData
6bb92a16
LP
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
5430f7f2
LP
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
6bb92a16
LP
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
5430f7f2 14 Lesser General Public License for more details.
6bb92a16 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
6bb92a16
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
00843602 20#include <signal.h>
6bb92a16
LP
21#include <stdlib.h>
22#include <unistd.h>
6bb92a16
LP
23
24#include "log.h"
0b452006 25#include "process-util.h"
6bb92a16 26#include "spawn-ask-password-agent.h"
cf0fbc49 27#include "util.h"
6bb92a16
LP
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
9bdc770c
LP
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);
6bb92a16 46 if (r < 0)
00843602 47 return log_error_errno(r, "Failed to fork TTY ask password agent: %m");
6bb92a16 48
00843602 49 return 1;
6bb92a16
LP
50}
51
52void ask_password_agent_close(void) {
53
54 if (agent_pid <= 0)
55 return;
56
57 /* Inform agent that we are done */
00843602
LP
58 (void) kill(agent_pid, SIGTERM);
59 (void) kill(agent_pid, SIGCONT);
c73d180d 60 (void) wait_for_terminate(agent_pid, NULL);
6bb92a16
LP
61 agent_pid = 0;
62}