Subject: Re: bug#6330: Feature request: mktemp creates named pipes

From: Bob Proulx <[email-masked]>
Date: Wed, 2 Jun 2010 10:13:27 -0600

Sebastien Andre wrote:
> When needing a temporary named pipe in shell scripts, I've often been
> writing the following function:
> 
> mktempfifo() {
>     local path=$(mktemp -t)
>     rm "$path"
>     mkfifo -m 0600 "$path"
>     echo "$path"
> }

Ew...  That isn't safe.  There is a time gap between when you remove
the temporary file and create the pipe.  That isn't good.

> I was wondering if anybody would be interested in having an option -p --pipe
> (or -f --fifo since -p is deprecated)
> to create temporary named pipes?
> 
> Example:
>    $ file $(mktemp -tp)
>    /tmp/tmp.24665457: fifo (named pipe)

The traditional way to deal with the entire range of issues such as
this (creating files with different suffixes, whatever) is to have
mktemp create a directory and then create your special files within
the directory.  It is fully safe that way.  Because the directory is
uniquely named the file within can have a fixed name.  No race
condition exists.

This is off of the top of my head, untested, but you might try
something like this example.  This is a do-nothing but with enough to
hopefully show you the technique.

  #!/bin/sh
  unset tmpdir
  trap 'cd / ; rm -rf "$tmpdir"' EXIT
  tmpdir=$(mktemp -d) || exit 1
  tmppipe="$tmpdir/pipe"
  mknod "$tmppipe" p
  ls -log "$tmppipe"
  exit 0

Bob