]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mesg: avoid 'ttyname failed: Success' message
authorKarel Zak <kzak@redhat.com>
Mon, 27 May 2019 11:07:12 +0000 (13:07 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 27 May 2019 11:07:12 +0000 (13:07 +0200)
The ttyname(3) can fail to access /dev/ path, and that will cause function
to fail without setting errno value with result of rather confusing error
message.  Lets start setting stdin permission via /proc when this happens as
a go-around, with hope kernel following symlink does not fail.  Ok, noted,
that hopes of symlink follow working are pretty slim.

Based on patch from Sami Kerola <kerolasa@iki.fi>.

Reference: https://github.com/lxc/lxd/issues/1724
Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/Makemodule.am
term-utils/mesg.1
term-utils/mesg.c

index da05a722c70c3088ba46e3569fed29a5f498c7cb..d0550f54a7ddeaa923de239c6a278d02724147da 100644 (file)
@@ -54,6 +54,7 @@ endif
 
 if BUILD_MESG
 usrbin_exec_PROGRAMS += mesg
+mesg_LDADD = $(LDADD) libcommon.la
 dist_man_MANS += term-utils/mesg.1
 mesg_SOURCES = term-utils/mesg.c
 endif
index 15c834dd71e49a409d58627307c757e7ef03015a..18e7c2451361189299c141d4d396da4c598e2cd8 100644 (file)
@@ -58,8 +58,8 @@ should be executed in your login scripts.
 .PP
 The
 .B mesg
-utility silently exits with error status 2 if the current standard error output does
-not refer to the terminal.  In this case execute
+utility silently exits with error status 2 if not executed on terminal.  In this
+case execute
 .B mesg
 is pointless.  The command line option \fB\-\-verbose\fR forces
 mesg to print a warning in this situation.  This behaviour has been introduced
index 57d53379f2d7ad785a92ff9090c9cd3823cb28db..56fb70094668879ff328b1984e85f3f64d6889aa 100644 (file)
@@ -59,6 +59,8 @@
 #include "nls.h"
 #include "c.h"
 #include "rpmatch.h"
+#include "ttyutils.h"
+#include "pathnames.h"
 
 /* exit codes */
 
@@ -90,6 +92,7 @@ int main(int argc, char *argv[])
 {
        struct stat sb;
        char *tty;
+       char ttybuf[sizeof(_PATH_PROC_FDDIR) + sizeof(stringify_value(INT_MAX))];
        int ch, fd, verbose = FALSE, ret;
 
        static const struct option longopts[] = {
@@ -121,13 +124,21 @@ int main(int argc, char *argv[])
        argc -= optind;
        argv += optind;
 
-       if (!isatty(STDERR_FILENO)) {
+       fd = get_terminal_stdfd();
+       if (fd < 0) {
                if (verbose)
                        warnx(_("no tty"));
                exit(MESG_EXIT_FAILURE);
        }
-       if ((tty = ttyname(STDERR_FILENO)) == NULL)
-               err(MESG_EXIT_FAILURE, _("ttyname failed"));
+
+       tty = ttyname(fd);
+       if (!tty) {
+               snprintf(ttybuf, sizeof(ttybuf), "%s/%d", _PATH_PROC_FDDIR, fd);
+               tty = ttybuf;
+               if (verbose)
+                       warnx(_("ttyname() failed, attempting to go around using: %s"), tty);
+       }
+
        if ((fd = open(tty, O_RDONLY)) < 0)
                err(MESG_EXIT_FAILURE, _("cannot open %s"), tty);
        if (fstat(fd, &sb))