systemctl: minor optimizations

This commit is contained in:
Lennart Poettering 2011-02-13 17:18:45 +01:00
parent 060ed82ec2
commit 7fc01d3319

View file

@ -161,6 +161,7 @@ static void spawn_ask_password_agent(void) {
}; };
int fd; int fd;
bool stdout_is_tty, stderr_is_tty;
/* Make sure the agent goes away when the parent dies */ /* Make sure the agent goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
@ -174,7 +175,10 @@ static void spawn_ask_password_agent(void) {
/* Don't leak fds to the agent */ /* Don't leak fds to the agent */
close_all_fds(NULL, 0); close_all_fds(NULL, 0);
if (!isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO)) { stdout_is_tty = isatty(STDOUT_FILENO);
stderr_is_tty = isatty(STDERR_FILENO);
if (!stdout_is_tty || !stderr_is_tty) {
/* Detach from stdout/stderr. and reopen /* Detach from stdout/stderr. and reopen
* /dev/tty for them. This is important to * /dev/tty for them. This is important to
* ensure that when systemctl is started via * ensure that when systemctl is started via
@ -187,15 +191,11 @@ static void spawn_ask_password_agent(void) {
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
if (!isatty(STDOUT_FILENO)) { if (!stdout_is_tty)
close(STDOUT_FILENO);
dup2(fd, STDOUT_FILENO); dup2(fd, STDOUT_FILENO);
}
if (!isatty(STDERR_FILENO)) { if (!stderr_is_tty)
close(STDERR_FILENO);
dup2(fd, STDERR_FILENO); dup2(fd, STDERR_FILENO);
}
if (fd > 2) if (fd > 2)
close(fd); close(fd);