Home > Net >  Error opening named pipe in c using O_WRONLY flag
Error opening named pipe in c using O_WRONLY flag

Time:02-18

I am trying to opened a named pipe with the following properties/permissions:

prw------- 1 myUser    0 FEB 17 20:08 fifoName

I need the open to be non-blocking, so the program doesnt hang. I am also required to open the pipe in write-only mode. however, I have been having some issues...

The following works: int fd = open("fifoName", O_RDONLY | O_NONBLOCK);

The following works: int fd = open("fifoName", O_RDWR | O_NONBLOCK);

The following DOES NOT work: int fd = open("fifoName", O_WRONLY | O_NONBLOCK);, fd gets set to -1.

I need to be able to open the pipe with the O_WRONLY flag set. I am able to open the pipe and write to it with the O_RDWR flag set. So I do not understand why it would not also work for the O_WRONLY flag.

Your help is greatly appreciated!!!!

CodePudding user response:

From the Linux fifo(7) manpage:

A process can open a FIFO in nonblocking mode. In this case, opening for read-only succeeds even if no one has opened on the write side yet and opening for write-only fails with ENXIO (no such device or address) unless the other end has already been opened.

So when you say

int fd = open("fifoName", O_WRONLY | O_NONBLOCK);

is returning -1, it seems like a safe bet the other end isn't open and errno is being set to ENXIO (You can use perror() or strerror() to get a nice human readable error when open() and anything else that sets errno fails.)

If you can't use O_RDWR to open it, you might attempt to open the FIFO in non-blocking mode, and on this error, do something else for a while and try again later, and repeat until it succeeds.

  • Related