public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX.
@ 2022-12-20 12:41 Takashi Yano
  2022-12-20 18:14 ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Takashi Yano @ 2022-12-20 12:41 UTC (permalink / raw)
  To: cygwin-patches; +Cc: Takashi Yano

POSIX states "A terminal may be the controlling terminal for at most
one session."
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html

However, in cygwin, multiple sessions could be associated with the
same TTY. This patch aligns CTTY behavior to the statement of POSIX.

Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
---
 winsup/cygwin/fhandler/termios.cc | 6 +++++-
 winsup/cygwin/mm/cygheap.cc       | 2 ++
 winsup/cygwin/pinfo.cc            | 4 +++-
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/termios.cc b/winsup/cygwin/fhandler/termios.cc
index fe4dfd13e..f94e20ff6 100644
--- a/winsup/cygwin/fhandler/termios.cc
+++ b/winsup/cygwin/fhandler/termios.cc
@@ -737,7 +737,11 @@ fhandler_termios::ioctl (int cmd, void *varg)
     }
 
   myself->ctty = -1;
-  myself->set_ctty (this, 0);
+  if (!myself->set_ctty (this, 0))
+    {
+      set_errno (EPERM);
+      return -1;
+    }
   return 0;
 }
 
diff --git a/winsup/cygwin/mm/cygheap.cc b/winsup/cygwin/mm/cygheap.cc
index a305570df..72861d8d7 100644
--- a/winsup/cygwin/mm/cygheap.cc
+++ b/winsup/cygwin/mm/cygheap.cc
@@ -127,6 +127,8 @@ void
 init_cygheap::close_ctty ()
 {
   debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
+  if (cygheap->ctty->tc ()->getsid () == pid)
+    cygheap->ctty->tc ()->setsid (0); /* Release CTTY ownership */
   cygheap->ctty->close_with_arch ();
   cygheap->ctty = NULL;
 }
diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
index e086ab9a8..749a4064c 100644
--- a/winsup/cygwin/pinfo.cc
+++ b/winsup/cygwin/pinfo.cc
@@ -528,7 +528,9 @@ _pinfo::set_ctty (fhandler_termios *fh, int flags)
 {
   tty_min& tc = *fh->tc ();
   debug_printf ("old %s, ctty device number %y, tc.ntty device number %y flags & O_NOCTTY %y", __ctty (), ctty, tc.ntty, flags & O_NOCTTY);
-  if (fh && (ctty <= 0 || ctty == tc.ntty) && !(flags & O_NOCTTY))
+  if (tc.getsid () && tc.getsid () != pid)
+    ; /* Do not attach if another session already attached to the CTTY. */
+  else if (fh && (ctty <= 0 || ctty == tc.ntty) && !(flags & O_NOCTTY))
     {
       ctty = tc.ntty;
       if (cygheap->ctty != fh->archetype)
-- 
2.39.0


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

* Re: [PATCH] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX.
  2022-12-20 12:41 [PATCH] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX Takashi Yano
@ 2022-12-20 18:14 ` Corinna Vinschen
  2022-12-21 10:23   ` Takashi Yano
  0 siblings, 1 reply; 3+ messages in thread
From: Corinna Vinschen @ 2022-12-20 18:14 UTC (permalink / raw)
  To: cygwin-patches

Hi Takashi,

On Dec 20 21:41, Takashi Yano wrote:
> POSIX states "A terminal may be the controlling terminal for at most
> one session."
> https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html
> 
> However, in cygwin, multiple sessions could be associated with the
> same TTY. This patch aligns CTTY behavior to the statement of POSIX.
> 
> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
> ---
>  winsup/cygwin/fhandler/termios.cc | 6 +++++-
>  winsup/cygwin/mm/cygheap.cc       | 2 ++
>  winsup/cygwin/pinfo.cc            | 4 +++-
>  3 files changed, 10 insertions(+), 2 deletions(-)

Do you want to handle this as bug (3.4) or extension (3.5)?

> diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
> index e086ab9a8..749a4064c 100644
> --- a/winsup/cygwin/pinfo.cc
> +++ b/winsup/cygwin/pinfo.cc
> @@ -528,7 +528,9 @@ _pinfo::set_ctty (fhandler_termios *fh, int flags)
>  {
>    tty_min& tc = *fh->tc ();
>    debug_printf ("old %s, ctty device number %y, tc.ntty device number %y flags & O_NOCTTY %y", __ctty (), ctty, tc.ntty, flags & O_NOCTTY);
> -  if (fh && (ctty <= 0 || ctty == tc.ntty) && !(flags & O_NOCTTY))
> +  if (tc.getsid () && tc.getsid () != pid)
> +    ; /* Do not attach if another session already attached to the CTTY. */

I'm sure I'm missing something, but I'm a bit puzzled about the

  tc.getsid () != pid

test here.  If that's not the case, this process is the process group
leader and already has a controlling tty, isn't it?


Thanks,
Corinna

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

* Re: [PATCH] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX.
  2022-12-20 18:14 ` Corinna Vinschen
@ 2022-12-21 10:23   ` Takashi Yano
  0 siblings, 0 replies; 3+ messages in thread
From: Takashi Yano @ 2022-12-21 10:23 UTC (permalink / raw)
  To: cygwin-patches

On Tue, 20 Dec 2022 19:14:35 +0100
Corinna Vinschen wrote:
> Hi Takashi,
> 
> On Dec 20 21:41, Takashi Yano wrote:
> > POSIX states "A terminal may be the controlling terminal for at most
> > one session."
> > https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html
> > 
> > However, in cygwin, multiple sessions could be associated with the
> > same TTY. This patch aligns CTTY behavior to the statement of POSIX.
> > 
> > Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
> > ---
> >  winsup/cygwin/fhandler/termios.cc | 6 +++++-
> >  winsup/cygwin/mm/cygheap.cc       | 2 ++
> >  winsup/cygwin/pinfo.cc            | 4 +++-
> >  3 files changed, 10 insertions(+), 2 deletions(-)
> 
> Do you want to handle this as bug (3.4) or extension (3.5)?

I think this is bug fix (3.4).

> > diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
> > index e086ab9a8..749a4064c 100644
> > --- a/winsup/cygwin/pinfo.cc
> > +++ b/winsup/cygwin/pinfo.cc
> > @@ -528,7 +528,9 @@ _pinfo::set_ctty (fhandler_termios *fh, int flags)
> >  {
> >    tty_min& tc = *fh->tc ();
> >    debug_printf ("old %s, ctty device number %y, tc.ntty device number %y flags & O_NOCTTY %y", __ctty (), ctty, tc.ntty, flags & O_NOCTTY);
> > -  if (fh && (ctty <= 0 || ctty == tc.ntty) && !(flags & O_NOCTTY))
> > +  if (tc.getsid () && tc.getsid () != pid)
> > +    ; /* Do not attach if another session already attached to the CTTY. */
> 
> I'm sure I'm missing something, but I'm a bit puzzled about the
> 
>   tc.getsid () != pid
> 
> test here.  If that's not the case, this process is the process group
> leader and already has a controlling tty, isn't it?

You are right. But, it seems that set_cttt() procedure other
than setting pinfo::ctty and cygpeap->ctty is necessary even
in that case. So, I revised the patch. Please see v2 patch.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

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

end of thread, other threads:[~2022-12-21 10:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-20 12:41 [PATCH] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX Takashi Yano
2022-12-20 18:14 ` Corinna Vinschen
2022-12-21 10:23   ` Takashi Yano

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 read-only IMAP folder(s) and NNTP newsgroup(s).