Skip to contentSkip to conversation

mesg: do not print 'ttyname failed' message unless --verbose requested

PATCH In review Sami Kerola 2 participants 4 messages Aug 1, 2018 – Aug 3, 2018 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mesg: do not print 'ttyname failed' message unless --verbose requested

Lots of people are confused why mesg(1) is priting this message. Usual cause seems to be an uninteractive shell trying to turn running 'mesg n' from a /root/.profile where command invocation is by default on debian based systems. This might be rare case when failing silently is better.

Reference: https://www.google.com/search?q=mesg+ttyname+failed

---
 term-utils/mesg.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
term-utils/mesg.c +5 -2
index 8714ad1aa..7c4e390bb 100644
@@ -121,8 +121,11 @@ int main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if ((tty = ttyname(STDERR_FILENO)) == NULL)
-		err(MESG_EXIT_FAILURE, _("ttyname failed"));
+	if ((tty = ttyname(STDERR_FILENO)) == NULL) {
+		if (verbose == TRUE)
+			err(MESG_EXIT_FAILURE, _("ttyname failed"));
+		exit(MESG_EXIT_FAILURE);
+	}
 	if ((fd = open(tty, O_RDONLY)) < 0)
 		err(MESG_EXIT_FAILURE, _("cannot open %s"), tty);
 	if (fstat(fd, &sb))
Signature · 1 line
2.18.0
maintainer
#1 lore raw
Quoted 16 lines · Sami Kerola

Lots of people are confused why mesg(1) is priting this message. Usual cause seems to be an uninteractive shell trying to turn running 'mesg n' from a /root/.profile where command invocation is by default on debian based systems. This might be rare case when failing silently is better.

Reference: https://www.google.com/search?q=mesg+ttyname+failed

---
 term-utils/mesg.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
term-utils/mesg.c +0 -0
index 8714ad1aa..7c4e390bb 100644

term-utils/mesg.c int main(int argc, char *argv[]) · line 128

Quoted 10 lines
term-utils/mesg.c +5 -2
@@ -121,8 +121,11 @@ int main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if ((tty = ttyname(STDERR_FILENO)) == NULL)
-		err(MESG_EXIT_FAILURE, _("ttyname failed"));
+	if ((tty = ttyname(STDERR_FILENO)) == NULL) {
+		if (verbose == TRUE)
+			err(MESG_EXIT_FAILURE, _("ttyname failed"));
+		exit(MESG_EXIT_FAILURE);
+	}

What about:

if (isatty(STDERR_FILENO)) {
    if (verbose)
        warnx(_("no tty"));
    exit(MESG_EXIT_FAILURE);
}

tty = ttyname(STDERR_FILENO);
if (!tty)
... the current code ...

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw
Quoted 36 lines · Karel Zak
Quoted 16 lines · Sami Kerola

Lots of people are confused why mesg(1) is priting this message. Usual cause seems to be an uninteractive shell trying to turn running 'mesg n' from a /root/.profile where command invocation is by default on debian based systems. This might be rare case when failing silently is better.

Reference: https://www.google.com/search?q=mesg+ttyname+failed

---
 term-utils/mesg.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
term-utils/mesg.c +0 -0
index 8714ad1aa..7c4e390bb 100644

term-utils/mesg.c int main(int argc, char *argv[]) · line 128

Quoted 10 lines
term-utils/mesg.c +5 -2
@@ -121,8 +121,11 @@ int main(int argc, char *argv[])
      argc -= optind;
      argv += optind;
 
-     if ((tty = ttyname(STDERR_FILENO)) == NULL)
-             err(MESG_EXIT_FAILURE, _("ttyname failed"));
+     if ((tty = ttyname(STDERR_FILENO)) == NULL) {
+             if (verbose == TRUE)
+                     err(MESG_EXIT_FAILURE, _("ttyname failed"));
+             exit(MESG_EXIT_FAILURE);
+     }

What about:

if (isatty(STDERR_FILENO)) {
    if (verbose)
        warnx(_("no tty"));
    exit(MESG_EXIT_FAILURE);
}
Quoted 3 lines

tty = ttyname(STDERR_FILENO);
if (!tty)
... the current code ...

Hi Karel

That's probably better. I pushed that version to my remote branch 'mesg' and made pull request:

https://github.com/karelzak/util-linux/pull/675

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
maintainer
#3 lore raw
Quoted 37 lines · Sami Kerola
Quoted 36 lines · Karel Zak
Quoted 16 lines · Sami Kerola

Lots of people are confused why mesg(1) is priting this message. Usual cause seems to be an uninteractive shell trying to turn running 'mesg n' from a /root/.profile where command invocation is by default on debian based systems. This might be rare case when failing silently is better.

Reference: https://www.google.com/search?q=mesg+ttyname+failed

---
 term-utils/mesg.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
term-utils/mesg.c +0 -0
index 8714ad1aa..7c4e390bb 100644

term-utils/mesg.c int main(int argc, char *argv[]) · line 128

Quoted 10 lines
term-utils/mesg.c +5 -2
@@ -121,8 +121,11 @@ int main(int argc, char *argv[])
      argc -= optind;
      argv += optind;
 
-     if ((tty = ttyname(STDERR_FILENO)) == NULL)
-             err(MESG_EXIT_FAILURE, _("ttyname failed"));
+     if ((tty = ttyname(STDERR_FILENO)) == NULL) {
+             if (verbose == TRUE)
+                     err(MESG_EXIT_FAILURE, _("ttyname failed"));
+             exit(MESG_EXIT_FAILURE);
+     }

What about:

if (isatty(STDERR_FILENO)) {
    if (verbose)
        warnx(_("no tty"));
    exit(MESG_EXIT_FAILURE);
}
Quoted 8 lines
Quoted 3 lines

tty = ttyname(STDERR_FILENO);
if (!tty)
... the current code ...

Hi Karel

That's probably better. I pushed that version to my remote branch 'mesg' and made pull request:

It's mistake to copy & past without testing. Should be

if (!isatty(STDERR_FILENO))

Fixed. I have also added a note to the man page as hide a warning is little bit controversial.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com