0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Schiavini
  • 2,869
  • 2
  • 23
  • 49
  • ough, I suggest using more than one file (eg timestamp named ones) containing the whole set of commands you wish to be done (created by PHP) and your C++ code watches the directory for any file. if folder contains files, open them for read (one by one), parse/execute and afterwards get rid of the file completely (rm/del). Your approach looks error prone alone from the point of access by processes. Anyway, a Database could also work for this. Edit: PHP application? did I read that right... what stopped you from writing a c++ application :-P? – Najzero Feb 01 '13 at 10:17
  • PHP web application :P And this website is used by a single user actually. Your approach sounds good as well actually – Schiavini Feb 01 '13 at 10:41
  • well, that mutlible file one was just a suggestion since we did quite a few interfaces between erp systems that way. For you linux fifo could work, but I believe you should rather have a look here: http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/ – Najzero Feb 01 '13 at 11:13

0 Answers0