mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-10-28 20:25:25 +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:
parent
088dcd8e41
commit
daa557208d
@ -558,7 +558,7 @@ static int prompt_root_password(void) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
_cleanup_string_free_erase_ char *a = NULL, *b = NULL;
|
_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)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to query root password: %m");
|
return log_error_errno(r, "Failed to query root password: %m");
|
||||||
|
|
||||||
@ -567,7 +567,7 @@ static int prompt_root_password(void) {
|
|||||||
break;
|
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)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to query root password: %m");
|
return log_error_errno(r, "Failed to query root password: %m");
|
||||||
|
|
||||||
|
@ -202,6 +202,7 @@ static void backspace_chars(int ttyfd, size_t p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ask_password_tty(
|
int ask_password_tty(
|
||||||
|
int ttyfd,
|
||||||
const char *message,
|
const char *message,
|
||||||
const char *keyname,
|
const char *keyname,
|
||||||
usec_t until,
|
usec_t until,
|
||||||
@ -215,7 +216,7 @@ int ask_password_tty(
|
|||||||
_POLL_MAX,
|
_POLL_MAX,
|
||||||
};
|
};
|
||||||
|
|
||||||
_cleanup_close_ int ttyfd = -1, notify = -1;
|
_cleanup_close_ int cttyfd = -1, notify = -1;
|
||||||
struct termios old_termios, new_termios;
|
struct termios old_termios, new_termios;
|
||||||
char passphrase[LINE_MAX + 1] = {}, *x;
|
char passphrase[LINE_MAX + 1] = {}, *x;
|
||||||
struct pollfd pollfd[_POLL_MAX];
|
struct pollfd pollfd[_POLL_MAX];
|
||||||
@ -241,9 +242,11 @@ int ask_password_tty(
|
|||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC);
|
/* If the caller didn't specify a TTY, then use the controlling tty, if we can. */
|
||||||
if (ttyfd >= 0) {
|
if (ttyfd < 0)
|
||||||
|
ttyfd = cttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC);
|
||||||
|
|
||||||
|
if (ttyfd >= 0) {
|
||||||
if (tcgetattr(ttyfd, &old_termios) < 0)
|
if (tcgetattr(ttyfd, &old_termios) < 0)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
@ -715,7 +718,7 @@ int ask_password_auto(
|
|||||||
if (!(flags & ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) {
|
if (!(flags & ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) {
|
||||||
char *s = NULL, **l = NULL;
|
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)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ typedef enum AskPasswordFlags {
|
|||||||
ASK_PASSWORD_NO_AGENT = 32,
|
ASK_PASSWORD_NO_AGENT = 32,
|
||||||
} AskPasswordFlags;
|
} 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_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_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);
|
int ask_password_auto(const char *message, const char *icon, const char *id, const char *keyname, usec_t until, AskPasswordFlags flag, char ***ret);
|
||||||
|
@ -26,7 +26,7 @@ static void ask_password(void) {
|
|||||||
int r;
|
int r;
|
||||||
_cleanup_free_ char *ret;
|
_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);
|
assert(r >= 0);
|
||||||
|
|
||||||
log_info("Got %s", ret);
|
log_info("Got %s", ret);
|
||||||
|
@ -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) {
|
if (arg_console) {
|
||||||
tty_fd = safe_close(tty_fd);
|
tty_fd = safe_close(tty_fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user