Unnecessary ioctl(n, TCGETS) Calls On Socket Handles

Viewed 210

Why does Perl5 implicitly seem to run unnecessary ioctl() tty checks against various file handles?

From what I understand, this is a long-standing feature of Perl5 and nobody seems to consider it for change. When I say change, I mean 'implement a change to Perl5 to facilitate the enablement or disablement of such ioctl() tty checks' on file handles which you KNOW are NOT tty handles.

I have a TCP/IP socket program and after accept() returns on a new socket, the following strace shows the ioctl()s being called on the new handle. In this context, the ioctl() calls are a waste of CPU and load. Of course, if accept() and ioctl() is called only once, it means nothing much. However, over time, with multiple processes, multiple ioctl() calls...it all adds up.

Is there an existing way to explicitly disable such superfluous ioctl() tty checks on file handles and sockets etc?

If it cannot be disabled, does anyone think such a contextual ability to disable/enable checks is worthy?

Here is the strace excerpt showing a client socket initial connection:

18:21:59.818126 select(8, [6], NULL, NULL, {tv_sec=10, tv_usec=0}) = 1 (in [6], left {tv_sec=9, tv_usec=999997})
18:21:59.818321 accept4(6, {sa_family=AF_INET, sin_port=htons(56124), sin_addr=inet_addr("127.0.0.1")}, [4096->16], SOCK_CLOEXEC) = 7
18:21:59.818419 fcntl(7, F_SETFD, FD_CLOEXEC) = 0
18:21:59.818453 ioctl(7, TCGETS, 0x7ffff5befcd0) = -1 ENOTTY (Inappropriate ioctl for device)
1 Answers

So you think there should be a way of telling Perl to check if the handle is a socket to skip executing code that doesn't pertain to sockets.

You shouldn't have to tell Perl to do that; it should be automatic. And it is automatic. The call to ioctl is that check.

I think the call in question is from this sub which uses isatty to check what kind of buffering should be used. If so, the call to ioctl is being used to check if the handle is a socket or not to skip executing code that doesn't pertain to sockets.

Do you have propose there's a better way of checking if a file handle is a socket?

Related