I am trying to write some data to a device node in php..It works fine, however I am having trouble handling errors.
What I want to do is, if the device fails..then prompt the user and write the data to a file instead.
At the moment if my fopen fails because say the device node doesn't exist, or is busy, or because my script doesn't have the right privileges then it can be trapped with something like
if (!$handle = fopen($lpdev, 'w')) { echo "Cannot write to ($lpdev) Using dump file"; $handle = fopen('/tmp/dump.txt'); }
However if the device node is free but the device on the end of it isn't responding..it just sits there waiting.
I have also tried
if ($handle = fopen($lpdev, 'w') === FALSE){ echo "Cannot write to ($lpdev) using dump file"; $handle = fopen('/tmp/dump.txt'); }
or trying to trap it with "or die"
None of those seem to work either, if there is nothing responding on $lpdev (/dev/lp0) the fopen will just sit there trying to write to it (seemingly for ever)
So I am guessing what I need is some sort of timeout on the fopen after which it exits and runs the alternative code....but part of me is thinking that fopen is really meant for files not devices and files are either there or not and writable or not..hence my trouble.
Am I using a square peg to fill a round hole..or is there a way of wrapping up the fopen to get it to do what I want.
Help as always is appreciated
On Sat, 2006-04-01 at 19:46 +0100, Wayne Stallwood wrote:
I am trying to write some data to a device node in php..It works fine, however I am having trouble handling errors.
That's no unusual when you use the buffered I/O !
For devices I much prefer to use open/read/write where you are closer to the device and can get more reliable error indications.
You also probably want to try and open it in non-blocking mode so that the call to open can't block.
Peter
On Sat, 2006-04-01 at 20:18 +0100, Peter Onion wrote:
For devices I much prefer to use open/read/write where you are closer to the device and can get more reliable error indications.
You also probably want to try and open it in non-blocking mode so that the call to open can't block.
Do you mean dio_open dio_write etc ?
I looked at those but they seem to be getting deprecated (No longer available on Unix platforms from PHP5 and only available as an external function on Windows from php5.10) which made me think they weren't the best choice.....I may reconsider if there are no other options.
I was thinking of something like using stream_select in a loop to check the device...but I don't think I can do that until I have a file pointer..which I can't get until I do the fopen which hangs if the device is not responding....hmmm
How about this ? Use system () to create a fifo and link the output to /dev/lp0...I think if I do it with system I can get it to not block...return from system with a true or false...if false then set the fifo to the dump file....then my fwrites etc go to the fifo rather than the dev node...or is that going to far into kludge city :-)
On Sun, 2006-04-02 at 10:01 +0100, Wayne Stallwood wrote:
On Sat, 2006-04-01 at 20:18 +0100, Peter Onion wrote:
For devices I much prefer to use open/read/write where you are closer to the device and can get more reliable error indications.
You also probably want to try and open it in non-blocking mode so that the call to open can't block.
Do you mean dio_open dio_write etc ?
Never heard of them...... I assume PHP provides wrappers for the real open/read/write system calls ?
Peter