Detect deletion of FIFO file with blocking open
I have a C file that can receive messages from a bash script through a
named pipe (FIFO). echo "abc" > /tmp/fifo will both open and close the
writing end of the pipe. I therefore open the file repeatedly from C, and
the blocking nature of open means that fopen only returns when something
has been written to the pipe. The code looks something like this:
while (1) {
if (stat(fifo_path, &fifo_st) != 0) {
perror("FIFO does not exist, exiting\n");
exit(0)
}
fifo = fopen(fifo_path, "r");
read = fgets(buf, 1024 * sizeof(char), fifo);
My issue is that the bash script may delete the file to indicate that it
is done communicating, in which case I have to do some cleanup. However,
fopen does not return when the file is deleted (which is expected
behaviour I suppose), and so I have no way of telling that the FIFO file
has been deleted.
I suppose I could have a separate thread that watches the file to detect
when it is deleted and does the cleanup, but that seems like
overengineering, and I'd like to know if there's a better way of doing it?
EDIT: The problem is discussed here, but it just ends with "it means your
process will hang forever" and no suggested workaround...
No comments:
Post a Comment