Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that TCGETS is indeed the way to go #76

Open
szakharchenko opened this issue Aug 22, 2022 · 1 comment
Open

Check that TCGETS is indeed the way to go #76

szakharchenko opened this issue Aug 22, 2022 · 1 comment

Comments

@szakharchenko
Copy link

Please check if TCGETS is appropriate here:

_, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)

See https://git.musl-libc.org/cgit/musl/patch/?id=2de85a985654d2c944931267645d9a0686242dfe for details.

@dolmen
Copy link
Contributor

dolmen commented Mar 15, 2023

Source: a patch in the musl libc.

HTML version: https://git.musl-libc.org/cgit/musl/commit/?id=2de85a985654d2c944931267645d9a0686242dfe

on most archs, the TCGETS ioctl command shares a value with SNDCTL_TMR_TIMEBASE, part of the OSS sound API which was apparently used with certain MIDI and timer devices. for file descriptors referring to such a device, TCGETS will not fail with ENOTTY as expected; it may produce a different error, or may succeed, and if it succeeds it changes the mode of the device. while it's unlikely that such devices are in use, this is in principle very harmful behavior for an operation which is supposed to do nothing but query whether the fd refers to a tty.

TIOCGWINSZ, used to query logical window size for a terminal, was chosen as an alternate ioctl to perform the isatty check. it does not share a value with any other ioctl commands, and it succeeds on any tty device.

Here is the current implementation of isatty in musl:

int isatty(int fd)
{
	struct winsize wsz;
	unsigned long r = syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz);
	if (r == 0) return 1;
	if (errno != EBADF) errno = ENOTTY;
	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants