1

Say I have a named pipe:

mypipe="foobar"
mkfifo $mypipe

... later on say I want to write to it

echo "foo" > $mypipe

if nobody is listening, I am pretty certain this echo call just hangs. Is there a way to determine if anyone is reading from the pipe before I make the echo call?

  • 1
    You seem to be setting yourself up for a race condition. What is the problem with the call just hanging until someone is ready to read the data? – Asad Saeeduddin Mar 04 '18 at 09:58
  • I agree re: the race condition, but for my use case, if someone is not ready to read then, they will never be ready, basically, so I will just exit/return before attempting to write –  Mar 04 '18 at 10:15
  • If the reader dies between the `fuser` call and the `echo` call, you still havo the same problem. Checking with `fuser` so you can avoid timeouts for `echo` most of the time is a valid optimization, but you still have to cope with the possibility that `echo` will hang. – tripleee Mar 04 '18 at 10:20

1 Answers1

1

The fuser utility tells you who holds an open handle, but in the general case, it requires root privileges.

Quoting the manual page,

fuser may only be able to gather partial information unless run with privileges. As a consequence, files opened by processes belonging to other users may not be listed and executables may be classified as mapped only.

Installing fuser SUID root will avoid problems associated with partial information, but may be undesirable for security and privacy reasons.

tripleee
  • 175,061
  • 34
  • 275
  • 318