Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:52091 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40648 invoked from network); 30 Apr 2011 12:04:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Apr 2011 12:04:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-ew0-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:49898] helo=mail-ew0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 09/54-10915-1CAFBBD4 for ; Sat, 30 Apr 2011 08:04:19 -0400 Received: by ewy2 with SMTP id 2so1379168ewy.29 for ; Sat, 30 Apr 2011 05:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=JMy7vtwyet7ceGqzrUr4y5S5LOilHqugctwFT2cyajk=; b=d9UZUiw0vkY50hJuK4TFchjQu1sTxMdCr8Ffdrfqy1ALbq0VtktXGd7BJPudc2Fnpd SZFqayzV33nEMquJeuo8YM0UBFeZ+u3ZxuOHcyXLPGxoH0pmylcuAGhfdMO32Tiii3mk vCDjmqchVDlYIRxftDv4H1LrZAdtHgkav6RJM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=KIi0h6Z8BztbcoGpET8ac5jkk2wkTAiPMkmHCo8Fxx8DxeD04OyDiVYBgm/QfYXGGu Pt9u1qDL+gZ0N62WvSIqgMW9skFbt32N+uCA8R5WeFWkJ1hxv+3JkQSTHcHw2gtRr8J8 ebbbu43rHHkLpXduMNe4fw8j47Y9/Es0rqTh4= MIME-Version: 1.0 Received: by 10.14.37.75 with SMTP id x51mr2526206eea.71.1304165052824; Sat, 30 Apr 2011 05:04:12 -0700 (PDT) Received: by 10.14.127.79 with HTTP; Sat, 30 Apr 2011 05:04:12 -0700 (PDT) Date: Sat, 30 Apr 2011 14:04:12 +0200 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=90e6ba615412b3bbae04a22196ba Subject: adding low level file handling stuff From: tyra3l@gmail.com (Ferenc Kovacs) --90e6ba615412b3bbae04a22196ba Content-Type: text/plain; charset=UTF-8 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 --90e6ba615412b3bbae04a22196ba--