1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

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.
This commit is contained in:
Lennart Poettering 2018-02-14 00:10:00 +01:00
parent 088dcd8e41
commit daa557208d
5 changed files with 12 additions and 9 deletions

View File

@ -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");

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);