Linux – are there generally available programs that use file descriptors more than 2

linuxposix

Standard file descriptors <=2 are opened by default. A program can write to or read from, a file descriptor more than 2, without using the open system call to obtain such a descriptor. The program can then advertise in its manual, what file descriptor it is using and how, and a POSIX shell can open a file and assign that file descriptor, with the exec built-in, and then call the program which will use that file.

I guess a good reason for doing that, would be if the program wants to have more than one output or input file, and does not want to specify them as command line arguments (if there was just one file, you could just redirect a standard file descriptor).

I have never seen a generally available program which would advertise such a thing in its manual. Does this happen in practice? Has anybody heard of such a thing?

Yes, I do want to stay within POSIX world – so no bash-only built-ins. I just want to know if there is such a program, not a shell built-in.

Best Answer

When you use process substitution with <(...) or >(...), bash will open a pipe to the other program on an arbitrary high file descriptor (I think it used to count up from 10, but now it counts down from 63) and pass the name as /dev/fd/N on the command line of the first program. This isn't POSIX, but other shells also support it (it's a ksh88 feature).

That's not exactly a feature of the program you're running though, it just sees /dev/fd/N and tries to open it like a regular file.

The Autoconf manual mentions some historic notes:

A few ancient systems reserved some file descriptors. By convention, file descriptor 3 was opened to /dev/tty when you logged into Eighth Edition (1985) through Tenth Edition Unix (1989). File descriptor 4 had a special use on the Stardent/Kubota Titan (circa 1990), though we don't now remember what it was. Both these systems are obsolete, so it's now safe to treat file descriptors 3 and 4 like any other file descriptors.

Also while I did a google search for this I found a program called runit that uses file descriptors 4 and 5 for some purpose related to log rotation.

Related Question