ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
Lennart Poettering 1e8ec7
diff --git a/src/systemctl.c b/src/systemctl.c
Lennart Poettering 1e8ec7
index 8cdc01a..c09b31d 100644
Lennart Poettering 1e8ec7
--- a/src/systemctl.c
Lennart Poettering 1e8ec7
+++ b/src/systemctl.c
Lennart Poettering 1e8ec7
@@ -141,6 +141,9 @@ static void spawn_ask_password_agent(void) {
Lennart Poettering 1e8ec7
         if (!arg_ask_password)
Lennart Poettering 1e8ec7
                 return;
Lennart Poettering 1e8ec7
Lennart Poettering 1e8ec7
+        if (arg_user)
Lennart Poettering 1e8ec7
+                return;
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
         parent = getpid();
Lennart Poettering 1e8ec7
Lennart Poettering 1e8ec7
         /* Spawns a temporary TTY agent, making sure it goes away when
Lennart Poettering 1e8ec7
@@ -151,13 +154,15 @@ static void spawn_ask_password_agent(void) {
Lennart Poettering 1e8ec7
Lennart Poettering 1e8ec7
         if (child == 0) {
Lennart Poettering 1e8ec7
                 /* In the child */
Lennart Poettering 1e8ec7
-
Lennart Poettering 1e8ec7
                 const char * const args[] = {
Lennart Poettering 1e8ec7
                         SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
Lennart Poettering 1e8ec7
                         "--watch",
Lennart Poettering 1e8ec7
                         NULL
Lennart Poettering 1e8ec7
                 };
Lennart Poettering 1e8ec7
Lennart Poettering 1e8ec7
+                int fd;
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
+                /* Make sure the agent goes away when the parent dies */
Lennart Poettering 1e8ec7
                 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
Lennart Poettering 1e8ec7
                         _exit(EXIT_FAILURE);
Lennart Poettering 1e8ec7
Lennart Poettering 1e8ec7
@@ -166,6 +171,31 @@ static void spawn_ask_password_agent(void) {
Lennart Poettering 1e8ec7
                 if (getppid() != parent)
Lennart Poettering 1e8ec7
                         _exit(EXIT_SUCCESS);
Lennart Poettering 1e8ec7
Lennart Poettering 1e8ec7
+                /* Don't leak fds to the agent */
Lennart Poettering 1e8ec7
+                close_all_fds(NULL, 0);
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
+                /* Detach from stdin/stdout/stderr. and reopen
Lennart Poettering 1e8ec7
+                 * /dev/tty for them. This is important to ensure that
Lennart Poettering 1e8ec7
+                 * when systemctl is started via popen() or a similar
Lennart Poettering 1e8ec7
+                 * call that expects to read EOF we actually do
Lennart Poettering 1e8ec7
+                 * generate EOF and not delay this indefinitely by
Lennart Poettering 1e8ec7
+                 * because we keep an unused copy of stdin around. */
Lennart Poettering 1e8ec7
+                if ((fd = open("/dev/tty", O_RDWR)) < 0) {
Lennart Poettering 1e8ec7
+                        log_error("Failed to open /dev/tty: %m");
Lennart Poettering 1e8ec7
+                        _exit(EXIT_FAILURE);
Lennart Poettering 1e8ec7
+                }
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
+                close(STDIN_FILENO);
Lennart Poettering 1e8ec7
+                close(STDOUT_FILENO);
Lennart Poettering 1e8ec7
+                close(STDERR_FILENO);
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
+                dup2(fd, STDIN_FILENO);
Lennart Poettering 1e8ec7
+                dup2(fd, STDOUT_FILENO);
Lennart Poettering 1e8ec7
+                dup2(fd, STDERR_FILENO);
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
+                if (fd > 2)
Lennart Poettering 1e8ec7
+                        close(fd);
Lennart Poettering 1e8ec7
+
Lennart Poettering 1e8ec7
                 execv(args[0], (char **) args);
Lennart Poettering 1e8ec7
                 _exit(EXIT_FAILURE);
Lennart Poettering 1e8ec7
         }