From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-patches@cygwin.com
Cc: Takashi Yano <takashi.yano@nifty.ne.jp>,
Corinna Vinschen <corinna@vinschen.de>
Subject: [PATCH v2] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX.
Date: Wed, 21 Dec 2022 19:24:44 +0900 [thread overview]
Message-ID: <20221221102444.1915-1-takashi.yano@nifty.ne.jp> (raw)
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.
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
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 | 38 ++++++++++++++++++-------------
3 files changed, 29 insertions(+), 17 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..c0f3c4cbd 100644
--- a/winsup/cygwin/pinfo.cc
+++ b/winsup/cygwin/pinfo.cc
@@ -530,24 +530,30 @@ _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))
{
- ctty = tc.ntty;
- if (cygheap->ctty != fh->archetype)
+ if (tc.getsid ())
+ ; /* Do nothing if a session is already associated with the TTY. */
+ else
{
- debug_printf ("cygheap->ctty %p, archetype %p", cygheap->ctty, fh->archetype);
- if (!cygheap->ctty)
- syscall_printf ("ctty was NULL");
- else
- {
- syscall_printf ("ctty %p, usecount %d", cygheap->ctty,
- cygheap->ctty->archetype_usecount (0));
- cygheap->ctty->close ();
- }
- cygheap->ctty = (fhandler_termios *) fh->archetype;
- if (cygheap->ctty)
+ ctty = tc.ntty;
+ if (cygheap->ctty != fh->archetype)
{
- fh->archetype_usecount (1);
- /* guard ctty fh */
- report_tty_counts (cygheap->ctty, "ctty", "");
+ debug_printf ("cygheap->ctty %p, archetype %p",
+ cygheap->ctty, fh->archetype);
+ if (!cygheap->ctty)
+ syscall_printf ("ctty was NULL");
+ else
+ {
+ syscall_printf ("ctty %p, usecount %d", cygheap->ctty,
+ cygheap->ctty->archetype_usecount (0));
+ cygheap->ctty->close ();
+ }
+ cygheap->ctty = (fhandler_termios *) fh->archetype;
+ if (cygheap->ctty)
+ {
+ fh->archetype_usecount (1);
+ /* guard ctty fh */
+ report_tty_counts (cygheap->ctty, "ctty", "");
+ }
}
}
--
2.39.0
reply other threads:[~2022-12-21 10:25 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=20221221102444.1915-1-takashi.yano@nifty.ne.jp \
--to=takashi.yano@nifty.ne.jp \
--cc=corinna@vinschen.de \
--cc=cygwin-patches@cygwin.com \
/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).