I am trying to realize a very simple communication between a PHP website and a C++ program. The solution chosen was to make use of Linux fifo's.
This works well for the first command, but when we try to reopen the file, an error is returned. See the code below for more information about the error messages.
C++ application:
string path = "/tmp/cmd.fifo";
__mode_t priv = 0666;
int fifo;
mkfifo(path.c_str(), priv); // returns 0, but privileges are not applied
chmod(path.c_str(), priv); // returns 0 as well, and now privileges are working
fifo = open(path, O_RDONLY); // blocks the thread
PHP:
$fifoPath = "/tmp/cmd.fifo";
$fifo = fopen($fifoPath, 'w');
fwrite($fifo, "COMMAND");
fclose($fifo);
C++ application:
// thread is unblocked. fifo==16
char in[20];
ssize_t r = read(fifo, in, sizeof(in)); // r == 7, in == "COMMAND"
// process the command. And read again:
ssize_t r = read(fifo, in, sizeof(in)); // r = 0, so EOF
// Let's reopen it so we can wait for the next command.
// tried using close() here, no success though.
open(path.c_str(), O_RDONLY); // returns -1! strerror(errno) == "No such file or directory"
mkfifo(path.c_str(), priv); // returns -1! strerror(errno) == "File exists"
What could be the problem on the code above? The messages "No such file or directory" and "File exists" are quite confronting.
BTW: I am open for another kind of solution for this communication, for example a C++ solution or something using the boost libraries.