1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
26 #include <sys/prctl.h>
32 #include "spawn-agent.h"
34 static pid_t agent_pid = 0;
36 void agent_open(void) {
42 /* We check STDIN here, not STDOUT, since this is about input,
44 if (!isatty(STDIN_FILENO))
47 parent_pid = getpid();
49 /* Spawns a temporary TTY agent, making sure it goes away when
54 log_error("Failed to fork agent: %m");
62 bool stdout_is_tty, stderr_is_tty;
64 /* Make sure the agent goes away when the parent dies */
65 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
68 /* Check whether our parent died before we were able
69 * to set the death signal */
70 if (getppid() != parent_pid)
73 /* Don't leak fds to the agent */
74 close_all_fds(NULL, 0);
76 stdout_is_tty = isatty(STDOUT_FILENO);
77 stderr_is_tty = isatty(STDERR_FILENO);
79 if (!stdout_is_tty || !stderr_is_tty) {
80 /* Detach from stdout/stderr. and reopen
81 * /dev/tty for them. This is important to
82 * ensure that when systemctl is started via
83 * popen() or a similar call that expects to
84 * read EOF we actually do generate EOF and
85 * not delay this indefinitely by because we
86 * keep an unused copy of stdin around. */
87 fd = open("/dev/tty", O_WRONLY);
89 log_error("Failed to open /dev/tty: %m");
94 dup2(fd, STDOUT_FILENO);
97 dup2(fd, STDERR_FILENO);
103 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
105 log_error("Unable to execute agent: %m");
110 void agent_close(void) {
115 /* Inform agent that we are done */
116 kill(agent_pid, SIGTERM);
117 kill(agent_pid, SIGCONT);
118 wait_for_terminate(agent_pid, NULL);