Hi. recently I found a nice blogpost about how to properly daemonize a php daemon: http://andytson.com/blog/2010/05/daemonising-a-php-cli-script-on-a-posix-system/ I've noticed in this article, that you can replace/redirect the STDIN/STDOUT/STDERR from inside of your script, you have to close them, and open in the correct order. It works (at least on linux), because the opened files will have the 1,2,3 FDs, because the OS assign the lowest available FD, which will happen to be the required ones.
I would love the idea to replace the STDIN/STDOUT/STDERR (and maybe any open streams/fps) from my script, but in this form I see many possible pitfalls. - if the STDIN/STDOUT/STDERR isn't the lowest available FDs, then you get wrong FD-s, and you have no sane way to tell it(you can't get the FD for a fp/stream from php). - if the OS doesn't follows the Unix/Posix spec: "The *open*() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process." then you get wrong FD-s, and you have no sane way to tell it. - if something opens an FD between the fclose and fopen call(callback for register_tick_function and pcntl_signal can happen between those calls), then you get wrong FD-s, and you have no sane way to tell it. - if you close a fp which has higher FD than the lowest available, then you get wrong FD-s, and you have no sane way to tell it. So to correctly address this, one would need a way to get the FD for a given fp and to be able to explicitly set the FD for an fp. The required C methods for this would be the fileno and dup2. I've started to look around, that maybe there are existing functions/extensions to make this work. I've found that with 5.3.6 one can open arbitrary FDs thanks to Gustavo: http://bugs.php.net/bug.php?id=53465 this is the equivalent of fdopen, which was requested by Dennis Hotson http://marc.info/?t=126854891300001&r=1&w=2 and he did implement that https://github.com/dhotson/fdopen-php (+1 for adding fdopen also, the stream based solution isn't obvious and easy to spot) But this doesn't help this problem. I checked and found that there is a pecl module, which provides some low level file handling: http://pecl.php.net/package/dio it doesn't provide fileno, and dup2, and doesn't support file pointers/streams, but I started wondering, why was this removed from the core, and moved to pecl. so I think that those methods needs to be added, but I'm not sure where would be appropriate to have methods like dup, dup2, finfo, fcntl (the dio_fcntl is far from complete). it could be added to the posix methods, or to the dio pecl extension, or a third extension, so I'm a little bit confused. I would like to know, that why do these methods lacks (just nobody needed them, or maybe it was a decision to keep out such low level methods from the core)? and if someone would make those, what should be the best place to add. btw: I've did POC implementation of fileno: https://gist.github.com/947595, but I don't have enough experience in C and php internals, so while I would like to help move things forward, but would need assistance with it. What do you think? Tyrael