9

I know /dev/tcp/<host>/<port> is a magic path handled specifically by some shells in redirections. But according to man bash:

If the operating system on which bash is running provides these special files, bash will use them; otherwise it will emulate them internally [...]

On the Linux box I am using right now, the /dev/tcp special file is not present, so the Bash will emulate it. But, is there actually a Unix-like system that does provide the /dev/tcp special file with the same semantic as the one supported by the Bash?

  • It would require an emulated directory. Making emulated directories is possible only with specific filesystems, for example procfs or sysfs can do that. /dev is a tmpfs on current linuxes, and it was on the root partition in older ones. This is why it is impossible currently. It could be made possible easily with a sysfs or procfs extension (both fs are very flexible and similar (actually probably no one knows why they aren't the same)), no one does it today, but it could be made in around 100 lines of code. – peterh Jan 14 at 11:58
  • But is there any other OS that does it already? At some point, I thought Plan 9 did it but I was wrong. – Sylvain Leroux Jan 14 at 12:42
  • 1
    I don't know, probably not (as far I know, it is a bash invention). I voted your question up, because the answer is interesting also to me. As far I know, no popular x86-capable unix OS can do this. – peterh Jan 14 at 12:44
8

I think the Bash documentation is somewhat misleading on this topic. Looking at the code, even going back to version 2.04 where network redirections were introduced, /dev/tcp and /dev/udp works as follows:

  • at build time, the configure script checks to see whether various networking features are supported; if so, if network redirections are enabled (which is the case by default), internal networking code is built in;
  • at run time, if the internal networking code is built in, /dev/tcp and /dev/udp (in the right format) are handled internally; otherwise, a warning is produced (“/dev/(tcp|udp)/host/port not supported without networking”) and Bash attempts to open the given path on the system;
  • if network redirections are configured out, nothing special is done.

What this boils down to is:

  • if network redirection is enabled:
    • if networking is supported on the target platform, /dev/tcp and /dev/udp will always be handled internally;
    • otherwise, Bash will warn and try to open the file “blindly”; if the system somehow supports /dev/(tcp|udp)/host/port, that will be used, otherwise in all likelihood the redirection will fail;
  • if network redirection is disabled, no special handling is performed; as above, if the system somehow supports /dev/(tcp|udp)/host/port, that will be used, otherwise in all likelihood the redirection will fail.

/dev/tcp does exist on some systems, but as far as I’m aware none support the same abstraction as Bash. On Solaris, /dev/tcp is used with the ndd tool to query and change network configuration. In XTI (see also the Open Group if you’re a member), the t_open function can be used with /dev/tcp to open a TCP connection, but it doesn’t use path-based setup, there’s a separate data structure to specify the target host and port (and other parameters).

  • The feature itself comes from ksh93. /dev/tcp on SysV systems would be a handle on the STREAMS tcp module. – Stéphane Chazelas Jan 14 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.