]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: try to find tty in get_terminal_name()
authorSami Kerola <kerolasa@iki.fi>
Sat, 14 May 2016 18:50:41 +0000 (19:50 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 3 Jul 2016 22:35:10 +0000 (23:35 +0100)
Try all standard terminal input/output file descriptors when finding tty
name in get_germinal_name().  This should make all invocations of the
function as robust as they can get.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/ttyutils.h
lib/ttyutils.c
login-utils/login.c
login-utils/su-common.c
term-utils/write.c

index 200e9a565eb4c0b272406c4522e48067e424307d..7278d3615d9acf3162e908d04d71b4e42dbf9c3d 100644 (file)
@@ -51,7 +51,7 @@ struct chardata {
        } while (0)
 
 extern int get_terminal_width(int default_width);
-extern int get_terminal_name(int fd, const char **path, const char **name,
+extern int get_terminal_name(const char **path, const char **name,
                             const char **number);
 
 #define UL_TTY_KEEPCFLAGS      (1 << 1)
index 4e62c20d67f8edea1fead01afd1c8ed6ee30a99f..06dd800f7d1d83fb9e1fa1ad66d9f6bc592eac8e 100644 (file)
@@ -5,6 +5,7 @@
  * Written by Karel Zak <kzak@redhat.com>
  */
 #include <ctype.h>
+#include <unistd.h>
 
 #include "c.h"
 #include "ttyutils.h"
@@ -42,13 +43,14 @@ int get_terminal_width(int default_width)
        return width > 0 ? width : default_width;
 }
 
-int get_terminal_name(int fd,
-                     const char **path,
+int get_terminal_name(const char **path,
                      const char **name,
                      const char **number)
 {
        const char *tty;
        const char *p;
+       int fd;
+
 
        if (name)
                *name = NULL;
@@ -57,6 +59,15 @@ int get_terminal_name(int fd,
        if (number)
                *number = NULL;
 
+       if (isatty(STDIN_FILENO))
+               fd = STDIN_FILENO;
+       else if (isatty(STDOUT_FILENO))
+               fd = STDOUT_FILENO;
+       else if (isatty(STDERR_FILENO))
+               fd = STDERR_FILENO;
+       else
+               return -1;
+
        tty = ttyname(fd);
        if (!tty)
                return -1;
index 4d71f542425b34f7274265dff7ae27c446e17c85..7501f6d696edb152c9b6700a1ad9d43d5926d039 100644 (file)
@@ -356,7 +356,7 @@ static void init_tty(struct login_context *cxt)
 
        cxt->tty_mode = (mode_t) getlogindefs_num("TTYPERM", TTY_MODE);
 
-       get_terminal_name(0, &cxt->tty_path, &cxt->tty_name, &cxt->tty_number);
+       get_terminal_name(&cxt->tty_path, &cxt->tty_name, &cxt->tty_number);
 
        /*
         * In case login is suid it was possible to use a hardlink as stdin
index 78a734c67ee6af228573ec1247b9bfb9b51e18e0..1776b6b795aed915ca4960c00aa62b536826df6f 100644 (file)
@@ -165,7 +165,7 @@ log_syslog(struct passwd const *pw, bool successful)
       old_user = pwd ? pwd->pw_name : "";
     }
 
-  if (get_terminal_name(STDERR_FILENO, NULL, &tty, NULL) != 0 || !tty)
+  if (get_terminal_name(NULL, &tty, NULL) != 0 || !tty)
     tty = "none";
 
   openlog (program_invocation_short_name, 0 , LOG_AUTH);
@@ -192,7 +192,7 @@ static void log_btmp(struct passwd const *pw)
                pw && pw->pw_name ? pw->pw_name : "(unknown)",
                sizeof(ut.ut_user));
 
-       get_terminal_name(STDERR_FILENO, NULL, &tty_name, &tty_num);
+       get_terminal_name(NULL, &tty_name, &tty_num);
        if (tty_num)
                xstrncpy(ut.ut_id, tty_num, sizeof(ut.ut_id));
        if (tty_name)
index be91218cfcdc9e3f9c607864b83c05bab573de7c..6bd393441e0e6142ebfe624458543124450cdb79 100644 (file)
@@ -296,7 +296,7 @@ static void do_write(const struct write_control *ctl)
 
 int main(int argc, char **argv)
 {
-       int tty_writeable = 0, src_fd, c;
+       int tty_writeable = 0, c;
        struct write_control ctl = { 0 };
 
        static const struct option longopts[] = {
@@ -321,18 +321,8 @@ int main(int argc, char **argv)
                        usage(stderr);
                }
 
-       /* check that sender has write enabled */
-       if (isatty(STDIN_FILENO))
-               src_fd = STDIN_FILENO;
-       else if (isatty(STDOUT_FILENO))
-               src_fd = STDOUT_FILENO;
-       else if (isatty(STDERR_FILENO))
-               src_fd = STDERR_FILENO;
-       else
-               src_fd = -1;
-
-       if (src_fd != -1 &&
-           get_terminal_name(src_fd, &ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
+       if (get_terminal_name(&ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
+               /* check that sender has write enabled */
                if (check_tty(ctl.src_tty_path, &tty_writeable, NULL, 1))
                        exit(EXIT_FAILURE);
                if (!tty_writeable)