public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Takashi Yano <tyan0@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: pinfo: Additional fix for CTTY behavior.
Date: Tue, 10 Jan 2023 13:15:52 +0000 (GMT)	[thread overview]
Message-ID: <20230110131552.33ABC3858430@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=8e77725f3183e91762b2cac3682c7a3f0f4483f9

commit 8e77725f3183e91762b2cac3682c7a3f0f4483f9
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Mon Dec 26 20:50:15 2022 +0900

    Cygwin: pinfo: Additional fix for CTTY behavior.
    
    The commit 25c4ad6ea52f did not fix the CTTY behavior enough. For
    example, in the following test case, TTY will be associated as
    a CTTY on the second open() call even though the TTY is already
    CTTY of another session. This patch fixes the issue.
    
      #include <unistd.h>
      #include <sys/fcntl.h>
    
      int main()
      {
        if (fork () == 0) {
          char *tty = ttyname(0);
          int fd;
          setsid();
          fd = open(tty, O_RDWR);
          close(fd);
          fd = open(tty, O_RDWR);
          usleep (60000000L);
        }
        return 0;
      }
    
    Fixes: 25c4ad6ea52f ("Cygwin: pinfo: Align CTTY behavior to the
    statement of POSIX.")
    Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
    Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>

Diff:
---
 winsup/cygwin/fhandler/termios.cc | 1 -
 winsup/cygwin/pinfo.cc            | 5 +++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/fhandler/termios.cc b/winsup/cygwin/fhandler/termios.cc
index f94e20ff6..5b92cdd31 100644
--- a/winsup/cygwin/fhandler/termios.cc
+++ b/winsup/cygwin/fhandler/termios.cc
@@ -736,7 +736,6 @@ fhandler_termios::ioctl (int cmd, void *varg)
       return -1;
     }
 
-  myself->ctty = -1;
   if (!myself->set_ctty (this, 0))
     {
       set_errno (EPERM);
diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
index ff10d9cf8..ece346037 100644
--- a/winsup/cygwin/pinfo.cc
+++ b/winsup/cygwin/pinfo.cc
@@ -530,7 +530,7 @@ _pinfo::set_ctty (fhandler_termios *fh, int flags)
   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 () != sid)
+      if (tc.getsid () && tc.getsid () != sid && ctty == -2)
 	; /* Do nothing if another session is associated with the TTY. */
       else
 	{
@@ -576,7 +576,8 @@ _pinfo::set_ctty (fhandler_termios *fh, int flags)
 	 an obvious bug surfaces. */
       if (sid == pid && !tc.getsid ())
 	tc.setsid (sid);
-      sid = tc.getsid ();
+      if (ctty > 0)
+	sid = tc.getsid ();
       /* See above */
       if ((!tc.getpgid () || being_debugged ()) && pgid == pid)
 	tc.setpgid (pgid);

                 reply	other threads:[~2023-01-10 13:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230110131552.33ABC3858430@sourceware.org \
    --to=tyan0@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).