util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* agetty problem after upgrading v2.19.1 -> v2.20.1
@ 2011-12-03 17:16 Andrew Walrond
  2011-12-04 12:05 ` Andrew Walrond
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Walrond @ 2011-12-03 17:16 UTC (permalink / raw)
  To: util-linux


After upgrading from v2.19.1 to v2.20.1 I can't login from the terminal anymore and I see lots of these in the syslog:

Dec 03 16:57:11 [agetty] /dev/ttyS0: cannot get controlling tty: Operation not permitted

So having a quick look at the git logs I see this commit

commit 3aa6b68f7e19fa3e1c2bba75bee921a98b7b46af
Author: Werner Fink <werner@suse.de>
Date:   Mon May 9 15:52:36 2011 +0200

    agetty: proper session on the terminal line
    
    Ensure a proper session on the terminal line, that is do a
    vhangup() and become the controlling terminal.  After this
    determine if the terminal line a virtual console by using
    the ioctl TIOCMGET to get the status modem bits of a serial
    line which is a invalid argument on a virtual console.
    
    Signed-off-by: Werner Fink <werner@suse.de>

and these relevant changes:

-           /* Open the tty as standard input. */
+         if (((tid = tcgetsid(fd)) < 0) || (pid != tid)) {
+                 if (ioctl (fd, TIOCSCTTY, 1) == -1)
+                         log_err("/dev/%s: cannot get controlling tty: %m", tty);
+         }
+
+         if (op->flags & F_HANGUP) {
+                 /*
+                  * vhangup() will replace all open file descriptors in the kernel
+                  * that point to our controlling tty by a dummy that will deny
+                  * further reading/writing to our device. It will also reset the
+                  * tty to sane defaults, so we don't have to modify the tty device
+                  * for sane settings. We also get a SIGHUP/SIGCONT.
+                  */
+                 if (vhangup())
+                         log_err("/dev/%s: vhangup() failed: %m", tty);
+                 (void)ioctl(fd, TIOCNOTTY);
+         }
+
+         (void) close(fd);
                close(STDIN_FILENO);
                errno = 0;
 
                debug("open(2)\n");
-           if (open(tty, O_RDWR | O_NONBLOCK, 0) != 0)
-                   log_err(_("/dev/%s: cannot open as standard input: %m"),
-                         tty);
+         if (open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0) != 0)
+                 log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
+         if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) {
+                 if (ioctl (STDIN_FILENO, TIOCSCTTY, 1) == -1)
+                         log_err("/dev/%s: cannot get controlling tty: %m", tty);
+         }
+

which all looks pretty valid to me, but is obviously the source of my problems.
So, before I do a brain dump and reload the knotty TTY knowledge back in to my brain to investigate, does anything obvious occur to anyone?

TIA

Andrew Walrond

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: agetty problem after upgrading v2.19.1 -> v2.20.1
  2011-12-03 17:16 agetty problem after upgrading v2.19.1 -> v2.20.1 Andrew Walrond
@ 2011-12-04 12:05 ` Andrew Walrond
  2011-12-08 12:04   ` Karel Zak
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Walrond @ 2011-12-04 12:05 UTC (permalink / raw)
  To: util-linux

After reading the following from Andries Brouwer's excellent "The Linux Kernel",
     http://www.win.tue.nl/~aeb/linux/lk/lk-10.html#ss10.3

>
> How does one get a controlling terminal? Nobody knows, this is a great mystery.
>
> The System V approach is that the first tty opened by the process becomes its controlling tty.
>
> The BSD approach is that one has to explicitly call
>
>    ioctl(fd, TIOCSCTTY, ...);
>
> to get a controlling tty.
>
> Linux tries to be compatible with both, as always, and this results in a very obscure complex of conditions. Roughly:
>
> The TIOCSCTTY ioctl will give us a controlling tty, provided that (i) the current process is a session leader, and (ii) it does not yet have a controlling tty, and (iii) maybe the tty should not already control some other session; if it does it is an error if we aren't root, or we steal the tty if we are all-powerful.
>
> Opening some terminal will give us a controlling tty, provided that (i) the current process is a session leader, and (ii) it does not yet have a controlling tty, and (iii) the tty does not already control some other session, and (iv) the open did not have the O_NOCTTY flag, and (v) the tty is not the foreground VT, and (vi) the tty is not the console, and (vii) maybe the tty should not be master or slave pty." 
>

it seems obvious that agetty is always going to fail unless it has already been made a session leader by the caller. Indeed, modifying init to call setsid() before exec'ing agetty makes everything work again.
What's not so obvious to me is whether agetty should always expect this, or become a session leader of it's own accord.

Andrew Walrond

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: agetty problem after upgrading v2.19.1 -> v2.20.1
  2011-12-04 12:05 ` Andrew Walrond
@ 2011-12-08 12:04   ` Karel Zak
  2011-12-08 14:52     ` Andrew Walrond
  0 siblings, 1 reply; 4+ messages in thread
From: Karel Zak @ 2011-12-08 12:04 UTC (permalink / raw)
  To: Andrew Walrond; +Cc: util-linux

On Sun, Dec 04, 2011 at 12:05:49PM +0000, Andrew Walrond wrote:
> After reading the following from Andries Brouwer's excellent "The Linux Kernel",
>      http://www.win.tue.nl/~aeb/linux/lk/lk-10.html#ss10.3
> 
> >
> > How does one get a controlling terminal? Nobody knows, this is a great mystery.
> >
> > The System V approach is that the first tty opened by the process becomes its controlling tty.
> >
> > The BSD approach is that one has to explicitly call
> >
> >    ioctl(fd, TIOCSCTTY, ...);
> >
> > to get a controlling tty.
> >
> > Linux tries to be compatible with both, as always, and this results in a very obscure complex of conditions. Roughly:
> >
> > The TIOCSCTTY ioctl will give us a controlling tty, provided that (i) the current process is a session leader, and (ii) it does not yet have a controlling tty, and (iii) maybe the tty should not already control some other session; if it does it is an error if we aren't root, or we steal the tty if we are all-powerful.
> >
> > Opening some terminal will give us a controlling tty, provided that (i) the current process is a session leader, and (ii) it does not yet have a controlling tty, and (iii) the tty does not already control some other session, and (iv) the open did not have the O_NOCTTY flag, and (v) the tty is not the foreground VT, and (vi) the tty is not the console, and (vii) maybe the tty should not be master or slave pty." 
> >
> 
> it seems obvious that agetty is always going to fail unless it has
> already been made a session leader by the caller. Indeed, modifying
> init to call setsid() before exec'ing agetty makes everything work

 BTW, what initd are using?

> again.  What's not so obvious to me is whether agetty should always
> expect this, or become a session leader of it's own accord.

 I think this (session initialization) belongs to login(1) rather than
 to getty -- so it's should be optional and not strictly required.
 Note that agetty before v2.20 was without TIOCSCTTY at all.

 I have committed the patch below. Try it.

 Thanks for your report!

    Karel



>From 1593b134ebf596ae7a2b1e73f2dcc8c4e7febddd Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 8 Dec 2011 11:39:05 +0100
Subject: [PATCH] agetty: don't use log_err() for non-fatal errors

The TIOCSCTTY ioctl requires that caller is session leader -- so it
depends on initd (or we have to add setsid() to aggety). It seems that the
traditional way is to setup tty in agetty and session in login(1).

It means that all session related things (TIOCSCTTY, vhangup, ...) in the
command agetty should be optional. (Note that vhangup() is called when
--hangup is explicitly specified on command line, so log_err() makes
sense there.)

Reported-by: Andrew Walrond <andrew@walrond.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 term-utils/agetty.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/term-utils/agetty.c b/term-utils/agetty.c
index 079a737..3500a8e 100644
--- a/term-utils/agetty.c
+++ b/term-utils/agetty.c
@@ -925,7 +925,7 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
 
 		if (((tid = tcgetsid(fd)) < 0) || (pid != tid)) {
 			if (ioctl(fd, TIOCSCTTY, 1) == -1)
-				log_err("/dev/%s: cannot get controlling tty: %m", tty);
+				log_warn("/dev/%s: cannot get controlling tty: %m", tty);
 		}
 
 		if (op->flags & F_HANGUP) {
@@ -950,7 +950,7 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
 			log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
 		if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) {
 			if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1)
-				log_err("/dev/%s: cannot get controlling tty: %m", tty);
+				log_warn("/dev/%s: cannot get controlling tty: %m", tty);
 		}
 
 	} else {
-- 
1.7.6.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: agetty problem after upgrading v2.19.1 -> v2.20.1
  2011-12-08 12:04   ` Karel Zak
@ 2011-12-08 14:52     ` Andrew Walrond
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Walrond @ 2011-12-08 14:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux

On Thu, Dec 08, 2011 at 01:04:38PM +0100, Karel Zak wrote:
> 
>  BTW, what initd are using?
> 

A custom job of my own. But I have systemd compiled up and ready to play with when I get time ;)


>  I have committed the patch below. Try it.
> 

Will do, although it looks trivially correct so I'm sure it'll work :)

>  Thanks for your report!
> 

Welcome. Keep up the sterling work!

Andrew

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-12-08 14:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-03 17:16 agetty problem after upgrading v2.19.1 -> v2.20.1 Andrew Walrond
2011-12-04 12:05 ` Andrew Walrond
2011-12-08 12:04   ` Karel Zak
2011-12-08 14:52     ` Andrew Walrond

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).