From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id BBB983858D1E; Thu, 22 Dec 2022 13:33:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BBB983858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671716004; bh=UCk/qUnv1gVEchKpUgZB3EVHGBynUdkWVByeK0EYvuA=; h=From:To:Subject:Date:From; b=nHN7m0nlon6gAa3cjUBN+BzSoD6TSyhD6PuJv4+bUMcsR52fk1eCz9IKCRQUvqw++ eZG9cP5T2GxbGRW/cBQpJrX8nLZZO+jj8gaJtWvTOnDd5pzp7vV4rVIMUX/fCBFX8U u5uqI6ShvL4F0JEMO1b+3my3anbpGW9VoPiFR2U4= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pinfo: Align CTTY behavior to the statement of POSIX. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 5841b2f6a4208682264d03e3edfa0a09881932a6 X-Git-Newrev: 25c4ad6ea52fc5451193dc98fc5ab04b1f509302 Message-Id: <20221222133324.BBB983858D1E@sourceware.org> Date: Thu, 22 Dec 2022 13:33:24 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D25c4ad6ea52= fc5451193dc98fc5ab04b1f509302 commit 25c4ad6ea52fc5451193dc98fc5ab04b1f509302 Author: Takashi Yano Date: Tue Dec 20 20:26:36 2022 +0900 Cygwin: pinfo: Align CTTY behavior to the statement of POSIX. =20 POSIX states "A terminal may be the controlling terminal for at most one session." https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html =20 However, in cygwin, multiple sessions could be associated with the same TTY. This patch aligns CTTY behavior to the statement of POSIX. =20 Reviewed-by: Corinna Vinschen Signed-off-by: Takashi Yano Diff: --- 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/ter= mios.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) } =20 myself->ctty =3D -1; - myself->set_ctty (this, 0); + if (!myself->set_ctty (this, 0)) + { + set_errno (EPERM); + return -1; + } return 0; } =20 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 () =3D=3D pid) + cygheap->ctty->tc ()->setsid (0); /* Release CTTY ownership */ cygheap->ctty->close_with_arch (); cygheap->ctty =3D NULL; } diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index e086ab9a8..586a4204d 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 f= lags & O_NOCTTY %y", __ctty (), ctty, tc.ntty, flags & O_NOCTTY); if (fh && (ctty <=3D 0 || ctty =3D=3D tc.ntty) && !(flags & O_NOCTTY)) { - ctty =3D tc.ntty; - if (cygheap->ctty !=3D fh->archetype) + if (tc.getsid () && tc.getsid () !=3D sid) + ; /* Do nothing if another session is associated with the TTY. */ + else { - debug_printf ("cygheap->ctty %p, archetype %p", cygheap->ctty, fh->arch= etype); - 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 =3D (fhandler_termios *) fh->archetype; - if (cygheap->ctty) + ctty =3D tc.ntty; + if (cygheap->ctty !=3D 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 =3D (fhandler_termios *) fh->archetype; + if (cygheap->ctty) + { + fh->archetype_usecount (1); + /* guard ctty fh */ + report_tty_counts (cygheap->ctty, "ctty", ""); + } } }