From: Lennart Poettering Date: Tue, 13 Feb 2018 23:10:00 +0000 (+0100) Subject: tty-ask-password-agent: don't open terminal multiple times X-Git-Tag: v238~100^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=daa557208de1d77d4aa4f8f8fb162b10d796678f;p=thirdparty%2Fsystemd.git tty-ask-password-agent: don't open terminal multiple times We already have the terminal open, hence pass the fd we got to ask_password_tty(), so that it doesn't have to reopen it a second time. This is mostly an optimization, but it has the nice benefit of making us independent from RLIMIT_NOFILE issues and so on, as we don't need to allocate another fd needlessly. --- diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 262e520d563..effa092ec9d 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -558,7 +558,7 @@ static int prompt_root_password(void) { for (;;) { _cleanup_string_free_erase_ char *a = NULL, *b = NULL; - r = ask_password_tty(msg1, NULL, 0, 0, NULL, &a); + r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a); if (r < 0) return log_error_errno(r, "Failed to query root password: %m"); @@ -567,7 +567,7 @@ static int prompt_root_password(void) { break; } - r = ask_password_tty(msg2, NULL, 0, 0, NULL, &b); + r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b); if (r < 0) return log_error_errno(r, "Failed to query root password: %m"); diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 8664513a89a..c37920b7b9a 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -202,6 +202,7 @@ static void backspace_chars(int ttyfd, size_t p) { } int ask_password_tty( + int ttyfd, const char *message, const char *keyname, usec_t until, @@ -215,7 +216,7 @@ int ask_password_tty( _POLL_MAX, }; - _cleanup_close_ int ttyfd = -1, notify = -1; + _cleanup_close_ int cttyfd = -1, notify = -1; struct termios old_termios, new_termios; char passphrase[LINE_MAX + 1] = {}, *x; struct pollfd pollfd[_POLL_MAX]; @@ -241,9 +242,11 @@ int ask_password_tty( return -errno; } - ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC); - if (ttyfd >= 0) { + /* If the caller didn't specify a TTY, then use the controlling tty, if we can. */ + if (ttyfd < 0) + ttyfd = cttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC); + if (ttyfd >= 0) { if (tcgetattr(ttyfd, &old_termios) < 0) return -errno; @@ -715,7 +718,7 @@ int ask_password_auto( if (!(flags & ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) { char *s = NULL, **l = NULL; - r = ask_password_tty(message, keyname, until, flags, NULL, &s); + r = ask_password_tty(-1, message, keyname, until, flags, NULL, &s); if (r < 0) return r; diff --git a/src/shared/ask-password-api.h b/src/shared/ask-password-api.h index f3ca6743a97..41d45c62ceb 100644 --- a/src/shared/ask-password-api.h +++ b/src/shared/ask-password-api.h @@ -33,7 +33,7 @@ typedef enum AskPasswordFlags { ASK_PASSWORD_NO_AGENT = 32, } AskPasswordFlags; -int ask_password_tty(const char *message, const char *keyname, usec_t until, AskPasswordFlags flags, const char *flag_file, char **ret); +int ask_password_tty(int tty_fd, const char *message, const char *keyname, usec_t until, AskPasswordFlags flags, const char *flag_file, char **ret); int ask_password_agent(const char *message, const char *icon, const char *id, const char *keyname, usec_t until, AskPasswordFlags flag, char ***ret); int ask_password_keyring(const char *keyname, AskPasswordFlags flags, char ***ret); int ask_password_auto(const char *message, const char *icon, const char *id, const char *keyname, usec_t until, AskPasswordFlags flag, char ***ret); diff --git a/src/test/test-ask-password-api.c b/src/test/test-ask-password-api.c index da44465821f..562a774531f 100644 --- a/src/test/test-ask-password-api.c +++ b/src/test/test-ask-password-api.c @@ -26,7 +26,7 @@ static void ask_password(void) { int r; _cleanup_free_ char *ret; - r = ask_password_tty("hello?", "da key", 0, 0, NULL, &ret); + r = ask_password_tty(-1, "hello?", "da key", 0, 0, NULL, &ret); assert(r >= 0); log_info("Got %s", ret); diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index ed9adab0676..be7889202ce 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -380,7 +380,7 @@ static int parse_password(const char *filename, char **wall) { } - r = ask_password_tty(message, NULL, not_after, echo ? ASK_PASSWORD_ECHO : 0, filename, &password); + r = ask_password_tty(tty_fd, message, NULL, not_after, echo ? ASK_PASSWORD_ECHO : 0, filename, &password); if (arg_console) { tty_fd = safe_close(tty_fd);