]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tty-ask-password-agent: don't open terminal multiple times
authorLennart Poettering <lennart@poettering.net>
Tue, 13 Feb 2018 23:10:00 +0000 (00:10 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 13 Feb 2018 23:11:16 +0000 (00:11 +0100)
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.

src/firstboot/firstboot.c
src/shared/ask-password-api.c
src/shared/ask-password-api.h
src/test/test-ask-password-api.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index 262e520d5637acac47e0d7f3689ae03e33650085..effa092ec9df3b2ebb2bd2c5524b38971008d157 100644 (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");
 
index 8664513a89a53eee3a2849faabf3a94a91cd740c..c37920b7b9a1b0a7d26e30bc17a33432aeabff12 100644 (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;
 
index f3ca6743a97e385e3d0e6c5ba9f7bf7569ecd89f..41d45c62cebc6ac746d4af575b1f3403f5a93820 100644 (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);
index da44465821fde0477af01336267c3e81fbeaf82b..562a774531fc6e2eb682f4d56d7160f36f6fdb40 100644 (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);
index ed9adab0676be6b153c8e3e923ce9ce0a8e4e9fa..be7889202ceed6d9f73f5af63f04397a6c5d19cf 100644 (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);