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