Open FIFO non-blocking but have write() block(?)

mal content artifact.one at googlemail.com
Sat Jun 2 22:57:20 UTC 2007


Hello.

The wording of the question in the subject is terrible,
I know. What I'm trying to do is write a program that
creates two named pipes in the filesystem. Logging
date appears on one, to be read by any external program
and commands are read by the program from the other.

If data is written to the logging FIFO and there's no
external reader, the log info is discarded.

The problem I'm having is that I can create the 'log'
fifo with mkfifo but the open call fails because there's
no reader:

  if (mkfifo("control", 0600) == -1) die();
  fd1 = open("control", O_RDONLY | O_NONBLOCK);
  if (fd1 == -1) die();

  if (mkfifo("log", 0600) == -1) die();
  fd2 = open("log", O_WRONLY | O_NONBLOCK);
  if (fd2 == -1) die();

Obviously I can remove the O_NONBLOCK from the second
open call, but this causes it to block and I don't want
that.

The behaviour I'm actually looking for is that the first
open() call should succeed but the first write made to
the fifo (with no reader) should fail.

Is it possible to portably get this behaviour from FIFOs?
Or should I be looking at something else like unix domain
sockets?

thanks,
MC


More information about the freebsd-hackers mailing list