chiark / gitweb /
exec: add high-level controls for blkio cgroup attributes
[elogind.git] / src / spawn-agent.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <sys/prctl.h>
27 #include <signal.h>
28 #include <fcntl.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "spawn-agent.h"
33
34 static pid_t agent_pid = 0;
35
36 void agent_open(void) {
37         pid_t parent_pid;
38
39         if (agent_pid > 0)
40                 return;
41
42         /* We check STDIN here, not STDOUT, since this is about input,
43          * not output */
44         if (!isatty(STDIN_FILENO))
45                 return;
46
47         parent_pid = getpid();
48
49         /* Spawns a temporary TTY agent, making sure it goes away when
50          * we go away */
51
52         agent_pid = fork();
53         if (agent_pid < 0) {
54                 log_error("Failed to fork agent: %m");
55                 return;
56         }
57
58         if (agent_pid == 0) {
59                 /* In the child */
60
61                 int fd;
62                 bool stdout_is_tty, stderr_is_tty;
63
64                 /* Make sure the agent goes away when the parent dies */
65                 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
66                         _exit(EXIT_FAILURE);
67
68                 /* Check whether our parent died before we were able
69                  * to set the death signal */
70                 if (getppid() != parent_pid)
71                         _exit(EXIT_SUCCESS);
72
73                 /* Don't leak fds to the agent */
74                 close_all_fds(NULL, 0);
75
76                 stdout_is_tty = isatty(STDOUT_FILENO);
77                 stderr_is_tty = isatty(STDERR_FILENO);
78
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);
88                         if (fd < 0) {
89                                 log_error("Failed to open /dev/tty: %m");
90                                 _exit(EXIT_FAILURE);
91                         }
92
93                         if (!stdout_is_tty)
94                                 dup2(fd, STDOUT_FILENO);
95
96                         if (!stderr_is_tty)
97                                 dup2(fd, STDERR_FILENO);
98
99                         if (fd > 2)
100                                 close(fd);
101                 }
102
103                 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
104
105                 log_error("Unable to execute agent: %m");
106                 _exit(EXIT_FAILURE);
107         }
108 }
109
110 void agent_close(void) {
111
112         if (agent_pid <= 0)
113                 return;
114
115         /* Inform agent that we are done */
116         kill(agent_pid, SIGTERM);
117         kill(agent_pid, SIGCONT);
118         wait_for_terminate(agent_pid, NULL);
119         agent_pid = 0;
120 }