Bug 251868

Summary: Add tcgetwinsize() and tcsetwinsize() to termios.h
Product: Base System Reporter: SOUMENDRA GANGULY <0.gangzta>
Component: binAssignee: Konstantin Belousov <kib>
Status: Closed FIXED    
Severity: Affects Only Me CC: 0.gangzta, cem, emaste, fuz, kib, markj, ota
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
lib/libc/gen/termios.c.diff
none
/include/termios.h.diff
none
test program
none
/include/termios.h.diff
none
/include/termios.h.diff
none
lib/libc/gen/Symbol.map none

Description SOUMENDRA GANGULY 2020-12-15 14:24:11 UTC
Created attachment 220579 [details]
lib/libc/gen/termios.c.diff

Patches for adding tcgetwinsize() and tcsetwinsize() functions that are now available in NetBSD and are expected to be a part of POSIX.1 issue 8. These functions get/set tty winsize respectively. Please see https://www.austingroupbugs.net/view.php?id=1151#c3856.

If these patches are accepted, then please add a comment to remind me to supply my preferred email address. The Bugzilla account email address is unsuitable for a commit. Thank you.
Comment 1 SOUMENDRA GANGULY 2020-12-15 14:25:51 UTC
Created attachment 220580 [details]
/include/termios.h.diff
Comment 2 SOUMENDRA GANGULY 2020-12-15 14:43:39 UTC
Created attachment 220581 [details]
test program
Comment 3 Konstantin Belousov freebsd_committer freebsd_triage 2020-12-15 23:09:02 UTC
Your test program cannot link with your patch.  termios.h changes, esp. the move of headers, are wrong.
Comment 4 SOUMENDRA GANGULY 2020-12-16 03:57:56 UTC
Created attachment 220607 [details]
/include/termios.h.diff
Comment 5 SOUMENDRA GANGULY 2020-12-16 04:07:39 UTC
Sir, thank you for notifying me about this. I have now restored the order in termios.h. Additionally, I have #include-d <sys/ioctl.h>; we need it for struct winsize. According to the POSIX proposal, the tcgetwinsize() and tcsetwinsize() declarations need to be in termios.h.

I left termios.c.diff and the test program unchanged. Please let me know if there are any more problems.
Comment 6 Konstantin Belousov freebsd_committer freebsd_triage 2020-12-16 04:24:29 UTC
sys/ioctl.h must not be included into posix header, it contaminates user namespace.
You test program still cannot link.

Use phabricator to submit patches, https://reviews.freebsd.org
Comment 7 SOUMENDRA GANGULY 2020-12-16 11:36:15 UTC
Sir, I have created a phabricator account. Can you please approve it? The email address is the same as that of this Bugzilla account.

I have made an observation. Both the NetBSD and the OpenBSD versions of termios.h only have sys/ttydefaults.h in the last block after the #endif /* !_TERMIOS_H_ */:

https://github.com/NetBSD/src/blob/trunk/sys/sys/termios.h
https://github.com/openbsd/src/blob/master/sys/sys/termios.h

glibc does something similar: https://github.com/bminor/glibc/blob/master/termios/termios.h although their sys/ttydefaults.h is being included in the protected block.

FreeBSD's termios.h was changed from https://svnweb.freebsd.org/base/head/include/termios.h?revision=191882&view=markup to https://svnweb.freebsd.org/base/head/include/termios.h?revision=199898&view=markup when ttycom.h suddenly got moved to the last block; there is no explanation in the SVN "commit message" as to why that was done. I suspect that it was a mistake.

If you think that my claim is correct, then I will replace the #include <sys/ioctl.h> in my diff with a #include <sys/ttycom.h> and remove the #include <sys/ttycom.h> from the final block; sys/ttycom.h contains the definition of struct winsize needed for tcgetwinsize() and tcsetwinsize(). Basically, then it will look like the NetBSD header: https://github.com/NetBSD/src/blob/trunk/sys/sys/termios.h

Thank you for your time.
Comment 8 SOUMENDRA GANGULY 2020-12-16 11:42:54 UTC
Created attachment 220616 [details]
/include/termios.h.diff
Comment 9 SOUMENDRA GANGULY 2020-12-16 11:45:20 UTC
I updated termios.h.diff to demonstrate what I was proposing in my last comment. There is no need to test it; I have not tested the patches on phabricator yet. Thank you.
Comment 10 SOUMENDRA GANGULY 2020-12-17 04:37:48 UTC
Created attachment 220654 [details]
lib/libc/gen/Symbol.map
Comment 11 SOUMENDRA GANGULY 2020-12-17 04:39:23 UTC
Added the Symbol.map file. It should now link after rebuilding libc. I will now also submit this patch to phabricator.
Comment 12 SOUMENDRA GANGULY 2020-12-17 05:16:54 UTC
https://reviews.freebsd.org/D27650
Comment 13 SOUMENDRA GANGULY 2020-12-17 11:41:36 UTC
The patches here are now outdated. New patches are on phabricator.
Comment 14 commit-hook freebsd_committer freebsd_triage 2020-12-25 18:47:41 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7d7fad7bd969fb464f64d34932234060cee112af

commit 7d7fad7bd969fb464f64d34932234060cee112af
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2020-12-24 23:05:31 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2020-12-25 18:43:09 +0000

    Add tcgetwinsize(3) and tcsetwinsize(3) to termios

    These functions get/set tty winsize respectively, and are trivial wrappers
    around corresponding termio ioctls.

    The functions are expected to be a part of POSIX.1 issue 8:
    https://www.austingroupbugs.net/view.php?id=1151#c3856.
    They are currently available in NetBSD and in musl libc.

    PR:     251868
    Submitted by:   Soumendra Ganguly <soumendraganguly@gmail.com>
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D27650

 include/termios.h        |  6 ++++++
 lib/libc/gen/Symbol.map  |  2 ++
 lib/libc/gen/termios.c   | 14 ++++++++++++++
 sys/sys/_winsize.h (new) | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 sys/sys/ttycom.h         | 12 +-----------
 5 files changed, 72 insertions(+), 11 deletions(-)
Comment 15 SOUMENDRA GANGULY 2020-12-26 14:55:35 UTC
In https://reviews.freebsd.org/D27650, I had written: "OTOH, while it's not a big deal anymore, I wonder why some of the other platforms such as NetBSD have chosen to not put the winsize materials under _NETBSD_SOURCE."

Here lies the answer to the question: https://mail-index.netbsd.org/tech-userlevel/2017/10/21/msg010899.html

Basically, Christos Zoulas cleverly avoided the namespace contamination by letting sys/ttycom.h only make visible struct winsize when _NETBSD_SOURCE is not defined: https://github.com/NetBSD/src/blob/trunk/sys/sys/ttycom.h

My OCD is now satisfied.
Comment 16 SOUMENDRA GANGULY 2020-12-28 01:54:33 UTC
I have created a new differential revision for the manpage: https://reviews.freebsd.org/D27787
Comment 17 Robert Clausecker freebsd_committer freebsd_triage 2021-04-08 11:28:37 UTC
Note that for full implementation of POSIX issue 1151, the sys/signal.h header will need to be changed to expose SIGWINCH even when _BSD_VISIBLE is not defined.  While probably too late for 13.0, this change is needed at some point.
Comment 18 Konstantin Belousov freebsd_committer freebsd_triage 2021-04-08 21:25:29 UTC
(In reply to Robert Clausecker from comment #17)
To implement this change, we need a released POSIX version.