FIFOs and (apparent) time travel

Most, if not all, Unix-like implementations include a feature called a fifo or named pipe. Created with the mkfifo(1) command, fifos are persistent pipes that appear as nodes in the file system and to which processes can attach. As in normal pipes anything that gets sent to its stdin appears in it stdout.

However, because they are persistent and can be created win advance they allow for some pretty strange-looking command chains.

For instance this one, if you have a previously created fifo in the filesystem (using mkfifo my_fifo)

$ cat my_fifo | nc -k -l 80 -v | cat > my_fifo

you have a network echo server using netcat. The weirdness comes from the fact that apparently, given the way the command is written, you read from the fifo pipe before you write to it.

What is happening is that what netcat sends to stdout is what it is getting from the network as it listens on port 80. This gets sent to the FIFO which is then read from and the data sent to netcat’s stdin which in turn sends it out to the network. This works because of the FIFO’s persistence which allows you to attach to it before you ever write to it.