CLI: constants STDIN, STDOUT & STDERR do not point to df 0, 1 & 2

php.internals

Bernard Fouché

22 years ago
Hello. I'm trying to run a php script from xinetd (Fedora Linux). I need to close stdin, stdout & stderr since I want to close the socket established with the caller process (I have a long processing to do locally, I need to release a remote resource & closing the connection is the only way I can do it). I've tried : fclose(STDIN); fclose(STDOUT); fclose(STDERR); It does not work, the socket is still up & running. When I run strace(1), I see that I'm closing fds 4, 5 & 6 (fd 3 is the fd of the script being read) and not fd 0, 1 & 2. It seems that CLI PHP calls dup(2) to get duplicates of fd 0, 1 & 2 and so these 3 fds are totally unreachable from the script itself. I'm not used to php source code, but I think that the problem comes from getting constants STDIN, STDOUT & STDERR thru filter code generation used by "php://stdin" etc. and no provision is done for the first calls: a dup(2) is automatically used. So constants STDIN, STDOUT & STDERR are not pointing to the correct fd's, but dup's. Can this be fixed or should I consider using another solution? (coding my script in C ;-( ) Regards, Bernard

Wez Furlong

22 years ago
The dup() is there "just-in-case" something breaks during PHP shutdown. I don't remember now exactly what the potential problem(s) might have been, so perhaps the dup is overly cautious. So, I don't have a problem eliminating it, unless someone points out a good reason to keep it :-) --Wez. ----- Original Message ----- From: "Bernard Fouché" <bernard.fouche@kuantic.com> To: <internals@lists.php.net> Sent: Friday, April 02, 2004 10:24 AM Subject: [PHP-DEV] CLI: constants STDIN, STDOUT & STDERR do not point to df 0, 1 & 2
> Hello. > > I'm trying to run a php script from xinetd (Fedora Linux). I need to close > stdin, stdout & stderr since I want to close > the socket established with the caller process (I have a long processing
to
> do locally, I need to release a remote > resource & closing the connection is the only way I can do it). > > I've tried : > > fclose(STDIN); fclose(STDOUT); fclose(STDERR); > > It does not work, the socket is still up & running. > > When I run strace(1), I see that I'm closing fds 4, 5 & 6 (fd 3 is the fd
of
> the script being read) and not > fd 0, 1 & 2. > > It seems that CLI PHP calls dup(2) to get duplicates of fd 0, 1 & 2 and so > these 3 fds are totally > unreachable from the script itself. I'm not used to php source code, but I > think that the problem > comes from getting constants STDIN, STDOUT & STDERR thru filter code > generation used > by "php://stdin" etc. and no provision is done for the first calls: a
dup(2)

Bernard Fouché

22 years ago
Well we're now 3 days later and no one pointed out a good reason :-) ----- Original Message ----- From: "Wez Furlong" <wez@thebrainroom.com> To: "Bernard Fouché" <bernard.fouche@kuantic.com>; <internals@lists.php.net> Sent: Friday, April 02, 2004 12:06 PM Subject: Re: [PHP-DEV] CLI: constants STDIN, STDOUT & STDERR do not point to df 0, 1 & 2
> The dup() is there "just-in-case" something breaks during PHP shutdown. > I don't remember now exactly what the potential problem(s) might have > been, so perhaps the dup is overly cautious. > > So, I don't have a problem eliminating it, unless someone points out > a good reason to keep it :-) > > --Wez. > > ----- Original Message ----- > From: "Bernard Fouché" <bernard.fouche@kuantic.com> > To: <internals@lists.php.net> > Sent: Friday, April 02, 2004 10:24 AM > Subject: [PHP-DEV] CLI: constants STDIN, STDOUT & STDERR do not point to
df
> 0, 1 & 2 > > > > Hello. > > > > I'm trying to run a php script from xinetd (Fedora Linux). I need to
close
> > stdin, stdout & stderr since I want to close > > the socket established with the caller process (I have a long processing > to > > do locally, I need to release a remote > > resource & closing the connection is the only way I can do it). > > > > I've tried : > > > > fclose(STDIN); fclose(STDOUT); fclose(STDERR); > > > > It does not work, the socket is still up & running. > > > > When I run strace(1), I see that I'm closing fds 4, 5 & 6 (fd 3 is the
fd
> of > > the script being read) and not > > fd 0, 1 & 2. > > > > It seems that CLI PHP calls dup(2) to get duplicates of fd 0, 1 & 2 and
so
> > these 3 fds are totally > > unreachable from the script itself. I'm not used to php source code, but
I
> > think that the problem > > comes from getting constants STDIN, STDOUT & STDERR thru filter code > > generation used > > by "php://stdin" etc. and no provision is done for the first calls: a > dup(2) > > is automatically used. > > > > So constants STDIN, STDOUT & STDERR are not pointing to the correct
fd's,
> > but dup's. > > > > Can this be fixed or should I consider using another solution? (coding
my

Wez Furlong

22 years ago
Would you mind opening a bug report for this on bugs.php.net? This serves two purposes: it gives me an excuse to commit to the tree (which is in bug-fix only mode), and also provides a handy way for me to remember which PHP things I'm going to be sorting out in the next couple of days. Thanks, --Wez. ----- Original Message ----- From: "Bernard Fouché" <bernard.fouche@kuantic.com> To: "Wez Furlong" <wez@thebrainroom.com>; <internals@lists.php.net> Sent: Monday, April 05, 2004 8:15 AM Subject: Re: [PHP-DEV] CLI: constants STDIN, STDOUT & STDERR do not point to df 0, 1 & 2
> Well we're now 3 days later and no one pointed out a good reason :-) > > ----- Original Message ----- > From: "Wez Furlong" <wez@thebrainroom.com> > To: "Bernard Fouché" <bernard.fouche@kuantic.com>;
<internals@lists.php.net>
> Sent: Friday, April 02, 2004 12:06 PM > Subject: Re: [PHP-DEV] CLI: constants STDIN, STDOUT & STDERR do not point
to
> df 0, 1 & 2 > > > > The dup() is there "just-in-case" something breaks during PHP shutdown. > > I don't remember now exactly what the potential problem(s) might have > > been, so perhaps the dup is overly cautious. > > > > So, I don't have a problem eliminating it, unless someone points out > > a good reason to keep it :-) > > > > --Wez. > > > > ----- Original Message ----- > > From: "Bernard Fouché" <bernard.fouche@kuantic.com> > > To: <internals@lists.php.net> > > Sent: Friday, April 02, 2004 10:24 AM > > Subject: [PHP-DEV] CLI: constants STDIN, STDOUT & STDERR do not point to > df > > 0, 1 & 2 > > > > > > > Hello. > > > > > > I'm trying to run a php script from xinetd (Fedora Linux). I need to > close > > > stdin, stdout & stderr since I want to close > > > the socket established with the caller process (I have a long
processing
> > to > > > do locally, I need to release a remote > > > resource & closing the connection is the only way I can do it). > > > > > > I've tried : > > > > > > fclose(STDIN); fclose(STDOUT); fclose(STDERR); > > > > > > It does not work, the socket is still up & running. > > > > > > When I run strace(1), I see that I'm closing fds 4, 5 & 6 (fd 3 is the > fd > > of > > > the script being read) and not > > > fd 0, 1 & 2. > > > > > > It seems that CLI PHP calls dup(2) to get duplicates of fd 0, 1 & 2
and
> so > > > these 3 fds are totally > > > unreachable from the script itself. I'm not used to php source code,
but