You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Dmitry Karasik <dm...@karasik.eu.org> on 2007/09/25 16:06:58 UTC

[mp2] mod_perl closes apache's stdin and/or stdout

Hello,

In mixed environment of mod_perl and cgi, cgi scripts may choose to not use
mod_perl request interface, and read content of POST requests directly from
STDIN.  mod_perl during the initialization process closes file descriptor 0,
and frees it for further reuse in other processes.  Therefore, any cgi script
executing after mod_perl is initialized, and in the same process tree, will
have file descriptor 0 closed. The concrete example where this behavior leads
to impossibility of processing POST requests is reported earlier and is located
at

  http://marc.info/?l=apache-modperl&m=119062450730646&w=2

The proposed patch fixes the issue, however, not being a perl guts expert, I
would urge someone with a deeper knowledge of perl to look at it first. Chances
are that there exists a mechanism in perl that allows non-destructive
duplication and overloading of perl IO handles.

Cheers,
	Dmitry Karasik

--- modperl_io.c.orig	2007-09-25 15:36:02.000000000 +0200
+++ modperl_io.c	2007-09-25 15:35:51.000000000 +0200
@@ -129,6 +129,15 @@
             Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", TRUE));
         }
 
+        /* In mixed environment of mod_perl and cgi scripts, cgi scripts may read content of 
+	 * POST requests off STDIN. do_close() calls actual close(0), freeing the descriptor 0
+	 * for reuse, and creating havoc for anyone reading from file descriptor 0.
+	 * This hack changes the IO type to IoTYPE_STD, because do_close() does not call
+	 * underlying close() on IO handles of these types, but does free the associated
+	 * resources. */
+	if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
+           IoTYPE(io) = IoTYPE_STD;
+
         /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
          * have file descriptors, so STDIN must be closed before it can
          * be reopened */

2. Used Components and their Configuration:

*** mod_perl version 2.000003

*** using /usr/ports/www/mod_perl2/work/mod_perl-2.0.3/lib/Apache2/BuildConfig.pm

*** Makefile.PL options:
  MP_APR_LIB     => aprext
  MP_APXS        => /usr/local/sbin/apxs
  MP_COMPAT_1X   => 1
  MP_GENERATE_XS => 1
  MP_LIBNAME     => mod_perl
  MP_USE_DSO     => 1


*** The httpd binary was not found


*** (apr|apu)-config linking info

 -L/usr/local/lib -laprutil-1 -lexpat -liconv -L/usr/local/lib
 -L/usr/local/lib -lapr-1 -lcrypt  -lpthread 


*** /usr/local/bin/perl -V
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
  Platform:
    osname=freebsd, osvers=6.2-release, archname=i386-freebsd-64int
    uname='freebsd freebsd.org 6.2-release freebsd 6.2-release #0: sat oct 14 21:57:31 pdt 2006 kris@freebsd.org:usrsrcsysmagickernelpath i386 '
    config_args='-sde -Dprefix=/usr/local -Darchlib=/usr/local/lib/perl5/5.8.8/mach -Dprivlib=/usr/local/lib/perl5/5.8.8 -Dman3dir=/usr/local/lib/perl5/5.8.8/perl/man/man3 -Dman1dir=/usr/local/man/man1 -Dsitearch=/usr/local/lib/perl5/site_perl/5.8.8/mach -Dsitelib=/usr/local/lib/perl5/site_perl/5.8.8 -Dscriptdir=/usr/local/bin -Dsiteman3dir=/usr/local/lib/perl5/5.8.8/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Ui_malloc -Ui_iconv -Uinstallusrbinperl -Dcc=cc -Duseshrplib -Dccflags=-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" -Doptimize=-O2 -fno-strict-aliasing -pipe  -Ud_dosuid -Ui_gdbm -Dusethreads=n -Dusemymalloc=y -Duse64bitint'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=define use64bitall=undef uselongdouble=undef
    usemymalloc=y, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include',
    optimize='-O2 -fno-strict-aliasing -pipe ',
    cppflags='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include'
    ccversion='', gccversion='3.4.6 [FreeBSD] 20060305', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -Wl,-E -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib
    libs=-lm -lcrypt -lutil
    perllibs=-lm -lcrypt -lutil
    libc=, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='  -Wl,-R/usr/local/lib/perl5/5.8.8/mach/CORE'
    cccdlflags='-DPIC -fPIC', lddlflags='-shared  -L/usr/local/lib'


Characteristics of this binary (from libperl): 
  Compile-time options: MYMALLOC PERL_MALLOC_WRAP USE_64_BIT_INT
                        USE_LARGE_FILES USE_PERLIO
  Locally applied patches:
	defined-or
  Built under freebsd
  Compiled at Oct 15 2006 05:04:59
  %ENV:
    PERL_LWP_USE_HTTP_10="1"
  @INC:
    /usr/local/lib/perl5/5.8.8/BSDPAN
    /usr/local/lib/perl5/site_perl/5.8.8/mach
    /usr/local/lib/perl5/site_perl/5.8.8
    /usr/local/lib/perl5/site_perl
    /usr/local/lib/perl5/5.8.8/mach
    /usr/local/lib/perl5/5.8.8
    .

*** Packages of interest status:

Apache2            : -
Apache2::Request   : -
CGI                : 3.15
ExtUtils::MakeMaker: 6.30, undef
LWP                : 5.805
mod_perl           : -
mod_perl2          : 2.000003


This report was generated by t/REPORT on Tue Sep 25 13:39:09 2007 GMT.

-------------8<---------- End Bug Report --------------8<----------

Note: Complete the rest of the details and post this bug report to
modperl <at> perl.apache.org. To subscribe to the list send an empty
email to modperl-subscribe@perl.apache.org.




-- 
Sincerely,
	Dmitry Karasik


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Peter Valdemar Mørch <sw...@sneakemail.com>.
Torsten Foertsch torsten.foertsch-at-gmx.net |Lists| wrote:
> I think Dmitrys argument about file descriptors being a shared/global resource 
> and hence must not be closed is very strong. But on the other hand existing 
> tests must not be broken. (or otherwise clearly explained why the test is 
> buggy) And of course a test that demonstate the bug is necessary.

Thanks for your input.

I'll ask Dmitry if he'll provide relevant test case coverage so that the 
patch can be applied. It may be a little while, though, christmas and 
all. Just wanted to post that this is still breathing and that I now 
realize we have the ball.

Peter

-- 
Peter Valdemar Mørch
http://www.morch.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Torsten Foertsch <to...@gmx.net>.
On Tue 18 Dec 2007, Philippe M. Chiasson wrote:
> >> Dmitry's patch from  Sep 25, 2007
> >> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still
> >> not been applied to SVN. May I humbly ask that this patch is applied?
> >
> > I remember that Toersten and I put together a unit test for this but I
> > don't think there was any change in behavior with this patch, so I'm not
> > sure if anyone was able to demonstrate that it changed the behavior.
>
> It certainly breaks t/modperl/io_nested_with_closed_stds
> [error] [client 127.0.0.1] Filehandle STDOUT reopened as STDIN only for
> input at t/response/TestModperl/io_nested_with_closed_stds.pm line 49.\n
>
> > If you have the tuits and could track down that unit test, show that it
> > exercises the problem in question, we probably wouldn't need much else
> > to apply this patch.  I'd try to do this but am overextended on my tuits
> > right now.
>
> Yup, without a test case that correctly demonstrates the bug, and a patch
> that doesn't break existing tests, not very likely to get applied.

This patch is something I wanted to investigate more thoroughly but as ever 
there is no time left for interesting problems.

I think Dmitrys argument about file descriptors being a shared/global resource 
and hence must not be closed is very strong. But on the other hand existing 
tests must not be broken. (or otherwise clearly explained why the test is 
buggy) And of course a test that demonstate the bug is necessary.

Torsten

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Fred Moyer wrote:
> Peter Valdemar Mørch wrote:
>> Hi,
>>
>> Dmitry Karasik dmitry-at-karasik.eu.org wrote:
>>> mod_perl during the initialization process closes file descriptor 0,
>>> and frees it for further reuse in other processes.  Therefore, any cgi 
>>> script
>>> executing after mod_perl is initialized, and in the same process tree, 
>>> will
>>> have file descriptor 0 closed. The concrete example where this 
>>> behavior leads
>>> to impossibility of processing POST requests is reported earlier and 
>>> is located
>>> at
>>>
>>>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
>> Dmitry's patch from  Sep 25, 2007 
>> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
>> not been applied to SVN. May I humbly ask that this patch is applied?
> 
> I remember that Toersten and I put together a unit test for this but I 
> don't think there was any change in behavior with this patch, so I'm not 
> sure if anyone was able to demonstrate that it changed the behavior.

It certainly breaks t/modperl/io_nested_with_closed_stds
[error] [client 127.0.0.1] Filehandle STDOUT reopened as STDIN only for input at t/response/TestModperl/io_nested_with_closed_stds.pm line 49.\n

> If you have the tuits and could track down that unit test, show that it 
> exercises the problem in question, we probably wouldn't need much else 
> to apply this patch.  I'd try to do this but am overextended on my tuits 
> right now.

Yup, without a test case that correctly demonstrates the bug, and a patch
that doesn't break existing tests, not very likely to get applied.

-- 
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Fred Moyer wrote:
> Peter Valdemar Mørch wrote:
>> Hi,
>>
>> Dmitry Karasik dmitry-at-karasik.eu.org wrote:
>>> mod_perl during the initialization process closes file descriptor 0,
>>> and frees it for further reuse in other processes.  Therefore, any cgi 
>>> script
>>> executing after mod_perl is initialized, and in the same process tree, 
>>> will
>>> have file descriptor 0 closed. The concrete example where this 
>>> behavior leads
>>> to impossibility of processing POST requests is reported earlier and 
>>> is located
>>> at
>>>
>>>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
>> Dmitry's patch from  Sep 25, 2007 
>> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
>> not been applied to SVN. May I humbly ask that this patch is applied?
> 
> I remember that Toersten and I put together a unit test for this but I 
> don't think there was any change in behavior with this patch, so I'm not 
> sure if anyone was able to demonstrate that it changed the behavior.

It certainly breaks t/modperl/io_nested_with_closed_stds
[error] [client 127.0.0.1] Filehandle STDOUT reopened as STDIN only for input at t/response/TestModperl/io_nested_with_closed_stds.pm line 49.\n

> If you have the tuits and could track down that unit test, show that it 
> exercises the problem in question, we probably wouldn't need much else 
> to apply this patch.  I'd try to do this but am overextended on my tuits 
> right now.

Yup, without a test case that correctly demonstrates the bug, and a patch
that doesn't break existing tests, not very likely to get applied.

-- 
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Fred Moyer <fr...@redhotpenguin.com>.
Peter Valdemar Mørch wrote:
> Hi,
> 
> Dmitry Karasik dmitry-at-karasik.eu.org wrote:
>> mod_perl during the initialization process closes file descriptor 0,
>> and frees it for further reuse in other processes.  Therefore, any cgi 
>> script
>> executing after mod_perl is initialized, and in the same process tree, 
>> will
>> have file descriptor 0 closed. The concrete example where this 
>> behavior leads
>> to impossibility of processing POST requests is reported earlier and 
>> is located
>> at
>>
>>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
> 
> Dmitry's patch from  Sep 25, 2007 
> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
> not been applied to SVN. May I humbly ask that this patch is applied?

I remember that Toersten and I put together a unit test for this but I 
don't think there was any change in behavior with this patch, so I'm not 
sure if anyone was able to demonstrate that it changed the behavior.

If you have the tuits and could track down that unit test, show that it 
exercises the problem in question, we probably wouldn't need much else 
to apply this patch.  I'd try to do this but am overextended on my tuits 
right now.

> 
> On the dev list 
> http://marc.info/?l=apache-modperl-dev&m=119125026607247&w=2 it seems 
> that whether or not this really is a bug is drawn into question. I can 
> confirm that it, really, really is a bug!
> 
> I'm now getting emails from others experiencing the same problem, 
> querying for a solution. Dmitry's patch fixes our problem. I'm now 
> forwarding links to the patch to other users. Wouldn't it be easier to 
> have this or another patch applied or another that also fixes the 
> problem???
> 
> The patch didn't apply completely cleanly here against HEAD, but this 
> one does ( also at http://demo.capmon.dk/~pvm/modperl_io.patch ) :
> 
> ~/mod_perl-2.0/src/modules/perl> svn diff
> Index: modperl_io.c
> ===================================================================
> --- modperl_io.c        (revision 604795)
> +++ modperl_io.c        (working copy)
> @@ -116,6 +116,7 @@
>      /* if STDIN is open, dup it, to be restored at the end of response */
>      if (handle && SvTYPE(handle) == SVt_PVGV &&
>          IoTYPE(GvIO(handle)) != IoTYPE_CLOSED) {
> +       IO * io = GvIO(handle);
>          handle_save = gv_fetchpv(Perl_form(aTHX_
>                                             "Apache2::RequestIO::_GEN_%ld",
>                                             (long)PL_gensym++),
> @@ -128,6 +129,17 @@
>              Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", 
> TRUE));
>          }
> 
> +       /* In mixed environment of mod_perl and cgi scripts, cgi
> +        * scripts may read content of POST requests off
> +        * STDIN. do_close() calls actual close(0), freeing the
> +        * descriptor 0 for reuse, and creating havoc for anyone
> +        * reading from file descriptor 0.  This hack changes the IO
> +        * type to IoTYPE_STD, because do_close() does not call
> +        * underlying close() on IO handles of these types, but does
> +        * free the associated resources. */
> +        if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
> +           IoTYPE(io) = IoTYPE_STD;
> +
>          /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
>           * have file descriptors, so STDIN must be closed before it can
>           * be reopened */
> 
> Thanks,
> 
> Peter


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Peter Valdemar Mørch <sw...@sneakemail.com>.
Hi,

Dmitry Karasik dmitry-at-karasik.eu.org wrote:
> mod_perl during the initialization process closes file descriptor 0,
> and frees it for further reuse in other processes.  Therefore, any cgi script
> executing after mod_perl is initialized, and in the same process tree, will
> have file descriptor 0 closed. The concrete example where this behavior leads
> to impossibility of processing POST requests is reported earlier and is located
> at
> 
>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2

Dmitry's patch from  Sep 25, 2007 
http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
not been applied to SVN. May I humbly ask that this patch is applied?

On the dev list 
http://marc.info/?l=apache-modperl-dev&m=119125026607247&w=2 it seems 
that whether or not this really is a bug is drawn into question. I can 
confirm that it, really, really is a bug!

I'm now getting emails from others experiencing the same problem, 
querying for a solution. Dmitry's patch fixes our problem. I'm now 
forwarding links to the patch to other users. Wouldn't it be easier to 
have this or another patch applied or another that also fixes the problem???

The patch didn't apply completely cleanly here against HEAD, but this 
one does ( also at http://demo.capmon.dk/~pvm/modperl_io.patch ) :

~/mod_perl-2.0/src/modules/perl> svn diff
Index: modperl_io.c
===================================================================
--- modperl_io.c        (revision 604795)
+++ modperl_io.c        (working copy)
@@ -116,6 +116,7 @@
      /* if STDIN is open, dup it, to be restored at the end of response */
      if (handle && SvTYPE(handle) == SVt_PVGV &&
          IoTYPE(GvIO(handle)) != IoTYPE_CLOSED) {
+       IO * io = GvIO(handle);
          handle_save = gv_fetchpv(Perl_form(aTHX_
                                             "Apache2::RequestIO::_GEN_%ld",
                                             (long)PL_gensym++),
@@ -128,6 +129,17 @@
              Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", 
TRUE));
          }

+       /* In mixed environment of mod_perl and cgi scripts, cgi
+        * scripts may read content of POST requests off
+        * STDIN. do_close() calls actual close(0), freeing the
+        * descriptor 0 for reuse, and creating havoc for anyone
+        * reading from file descriptor 0.  This hack changes the IO
+        * type to IoTYPE_STD, because do_close() does not call
+        * underlying close() on IO handles of these types, but does
+        * free the associated resources. */
+        if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
+           IoTYPE(io) = IoTYPE_STD;
+
          /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
           * have file descriptors, so STDIN must be closed before it can
           * be reopened */

Thanks,

Peter
-- 
Peter Valdemar Mørch
http://www.morch.com

Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Peter Valdemar Mørch <sw...@sneakemail.com>.
Hi,

Dmitry Karasik dmitry-at-karasik.eu.org wrote:
> mod_perl during the initialization process closes file descriptor 0,
> and frees it for further reuse in other processes.  Therefore, any cgi script
> executing after mod_perl is initialized, and in the same process tree, will
> have file descriptor 0 closed. The concrete example where this behavior leads
> to impossibility of processing POST requests is reported earlier and is located
> at
> 
>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2

Dmitry's patch from  Sep 25, 2007 
http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
not been applied to SVN. May I humbly ask that this patch is applied?

On the dev list 
http://marc.info/?l=apache-modperl-dev&m=119125026607247&w=2 it seems 
that whether or not this really is a bug is drawn into question. I can 
confirm that it, really, really is a bug!

I'm now getting emails from others experiencing the same problem, 
querying for a solution. Dmitry's patch fixes our problem. I'm now 
forwarding links to the patch to other users. Wouldn't it be easier to 
have this or another patch applied or another that also fixes the problem???

The patch didn't apply completely cleanly here against HEAD, but this 
one does ( also at http://demo.capmon.dk/~pvm/modperl_io.patch ) :

~/mod_perl-2.0/src/modules/perl> svn diff
Index: modperl_io.c
===================================================================
--- modperl_io.c        (revision 604795)
+++ modperl_io.c        (working copy)
@@ -116,6 +116,7 @@
      /* if STDIN is open, dup it, to be restored at the end of response */
      if (handle && SvTYPE(handle) == SVt_PVGV &&
          IoTYPE(GvIO(handle)) != IoTYPE_CLOSED) {
+       IO * io = GvIO(handle);
          handle_save = gv_fetchpv(Perl_form(aTHX_
                                             "Apache2::RequestIO::_GEN_%ld",
                                             (long)PL_gensym++),
@@ -128,6 +129,17 @@
              Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", 
TRUE));
          }

+       /* In mixed environment of mod_perl and cgi scripts, cgi
+        * scripts may read content of POST requests off
+        * STDIN. do_close() calls actual close(0), freeing the
+        * descriptor 0 for reuse, and creating havoc for anyone
+        * reading from file descriptor 0.  This hack changes the IO
+        * type to IoTYPE_STD, because do_close() does not call
+        * underlying close() on IO handles of these types, but does
+        * free the associated resources. */
+        if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
+           IoTYPE(io) = IoTYPE_STD;
+
          /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
           * have file descriptors, so STDIN must be closed before it can
           * be reopened */

Thanks,

Peter
-- 
Peter Valdemar Mørch
http://www.morch.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Dmitry Karasik <dm...@karasik.eu.org>.
> I took a quick swing at putting one together but my mod_perl environment 
> is a bit screwed up right now.  Can you see if it runs on your setup? 

I don't know. I've run "perl ModPerl-Registry/t/cgi-bin/stdin.pl" and it 
(unsurprisingly) fails with a "Can't connect to localhost:8529", and
I can't find in the docs what is the correct test invocation. 

The problem might not be always observable in real live setup because
mod_perl and cgi can be in different process trees, but it will always
emerge when the very first request server by apache is handled using
mod_perl, and the second by cgi.

My test setup contains this request in a file:

	POST /cgi-bin/modperl.cgi HTTP/1.1
	Host: localhost
	Content-Type: application/x-www-form-urlencoded
	Content-Length: 13
	Keep-Alive: 300
	
	submit=Submit
	
	POST /cgi-bin/cgi.cgi HTTP/1.1
	Host: localhost
	Content-Type: application/x-www-form-urlencoded
	Content-Length: 13
	Keep-Alive: 0
	Connection: close
	
	submit=Submit

and it runs as

	httpd -X &
	nc localhost 80 < request
	killall httpd

and the error is always reproducible.  I don't know if the test suite can stop
and start apache at will, but if yes, the test must do that before executing
that stdin.t.

-- 
Sincerely,
	Dmitry Karasik


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Dmitry Karasik wrote:
>> What did I wrong?

Not running a perlio Perl ?

> Try the same input but with apache freshly started, it must be the very first
> apache request, then the error is fully reproducible.

------------------------------------------------------------------------
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Dmitry Karasik <dm...@karasik.eu.org>.
> What did I wrong?

Try the same input but with apache freshly started, it must be the very first
apache request, then the error is fully reproducible.

-- 
Sincerely,
	Dmitry Karasik


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Torsten Foertsch <to...@gmx.net>.
On Tuesday 25 September 2007 19:52, Fred Moyer wrote:
> I took a quick swing at putting one together but my mod_perl environment
> is a bit screwed up right now.  Can you see if it runs on your setup?

With a few tweaks the test runs in my environment (trunk+2.2.6-worker). But it 
succeeds in both cases.

I was really surprised to see this mail because my usual setup is a mix of 
mod_perl and mod_cgi/mod_cgid. The original post at 
http://marc.info/?l=apache-modperl&m=119062450730646&w=2 suggests that it may 
be some coincidence with keep-alive. So I even tried it via telnet:

$ telnet localhost 8529
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
POST /stdin_override/mod_perl HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 9

submit=1

HTTP/1.1 200 OK
Date: Tue, 25 Sep 2007 19:41:36 GMT
Server: Apache/2.2.6 (Unix) DAV/2 mod_ssl/2.2.3 OpenSSL/0.9.8d 
mod_perl/2.0.4-dev Perl/v5.8.8
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain

26
MOD_PERL: mod_perl/2.0.4-dev
submitted
0

POST /stdin_override/cgi HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 9

submit=1

HTTP/1.1 200 OK
Date: Tue, 25 Sep 2007 19:41:39 GMT
Server: Apache/2.2.6 (Unix) DAV/2 mod_ssl/2.2.3 OpenSSL/0.9.8d 
mod_perl/2.0.4-dev Perl/v5.8.8
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain

14
MOD_PERL:
submitted
0

Connection closed by foreign host.


What did I wrong?

Torsten

Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Fred Moyer <fr...@redhotpenguin.com>.
Dmitry Karasik wrote:
> Hello,
> 
> In mixed environment of mod_perl and cgi, cgi scripts may choose to not use
> mod_perl request interface, and read content of POST requests directly from
> STDIN.  mod_perl during the initialization process closes file descriptor 0,
> and frees it for further reuse in other processes.  Therefore, any cgi script
> executing after mod_perl is initialized, and in the same process tree, will
> have file descriptor 0 closed. The concrete example where this behavior leads
> to impossibility of processing POST requests is reported earlier and is located
> at
> 
>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
> 
> The proposed patch fixes the issue, however, not being a perl guts expert, I
> would urge someone with a deeper knowledge of perl to look at it first. Chances
> are that there exists a mechanism in perl that allows non-destructive
> duplication and overloading of perl IO handles.

I am not a perl guts expert either (gozer??), but looking through the 
perl source it looks like this idiom is used in a few places.  But we 
really need a reproducible test case to verify that this is indeed a 
problem.

I took a quick swing at putting one together but my mod_perl environment 
is a bit screwed up right now.  Can you see if it runs on your setup? 
And hopefully passes with your change, and fails without?  This test may 
not work at all :)  but if it does and this problem is reproducible then 
that improves our chances of getting the fix accepted in some form.

Index: ModPerl-Registry/t/conf/extra.conf.in
===================================================================
--- ModPerl-Registry/t/conf/extra.conf.in       (revision 543707)
+++ ModPerl-Registry/t/conf/extra.conf.in       (working copy)
@@ -182,6 +182,17 @@
      PerlResponseHandler ModPerl::Registry
  </Location>

+### test for mixed mod_perl/cgi environment ###
+<Location /stdin_override/mod_perl>
+    SetHandler perl-script
+    PerlResponseHandler ModPerl::Registry
+    PerlOptions +ParseHeaders
+</Location>
+
+<Location /stdin_override/cgi>
+    SetHandler cgi-script
+</Location>
+
  ### deflate tests ###
  <IfModule mod_alias.c>
      Alias /registry_bb_deflate/ @ServerRoot@/cgi-bin/

Index: ModPerl-Registry/t/cgi-bin/stdin.pl
===================================================================
--- ModPerl-Registry/t/cgi-bin/stdin.pl (revision 0)
+++ ModPerl-Registry/t/cgi-bin/stdin.pl (revision 0)
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use CGI qw/:standard/;
+
+print <<END;
+Content-type: text/html
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+   <body>
+     <form method="post"><input type="submit" name="submit" value="Submit">
+    </form>
+END
+
+if (param()) {
+    my $submit = param('submit');
+    if ($submit) {
+        print "<p>submitted</p>";
+    }
+}
+
+print <<END;
+    </body>
+</html>
+END
+

Index: ModPerl-Registry/t/stdin.t
===================================================================
--- ModPerl-Registry/t/stdin.t  (revision 0)
+++ ModPerl-Registry/t/stdin.t  (revision 0)
@@ -0,0 +1,24 @@
+use strict;
+use warnings FATAL => 'all';
+
+use Apache::Test;
+use Apache::TestUtil;
+use Apache::TestRequest qw(POST);
+
+plan tests => 2, need [qw(CGI)],
+    need_min_module_version CGI => 3.08;
+
+{
+    my $url = "/stdin_override/mod_perl";
+    my $res = POST $url;
+
+    ok t_cmp($res->content,
+             qr{submitted},
+             "form submission ok under mod_perl");
+
+    $url = "/stdin_override/cgi";
+    $res = POST $url;
+    ok t_cmp($res->content,
+             qr{submitted},
+             "stdin reset, form submission ok under cgi");
+}

> 
> Cheers,
> 	Dmitry Karasik
> 
> --- modperl_io.c.orig	2007-09-25 15:36:02.000000000 +0200
> +++ modperl_io.c	2007-09-25 15:35:51.000000000 +0200
> @@ -129,6 +129,15 @@
>              Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", TRUE));
>          }
>  
> +        /* In mixed environment of mod_perl and cgi scripts, cgi scripts may read content of 
> +	 * POST requests off STDIN. do_close() calls actual close(0), freeing the descriptor 0
> +	 * for reuse, and creating havoc for anyone reading from file descriptor 0.
> +	 * This hack changes the IO type to IoTYPE_STD, because do_close() does not call
> +	 * underlying close() on IO handles of these types, but does free the associated
> +	 * resources. */
> +	if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
> +           IoTYPE(io) = IoTYPE_STD;
> +
>          /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
>           * have file descriptors, so STDIN must be closed before it can
>           * be reopened */
> 
> 2. Used Components and their Configuration:
> 
> *** mod_perl version 2.000003
> 
> *** using /usr/ports/www/mod_perl2/work/mod_perl-2.0.3/lib/Apache2/BuildConfig.pm
> 
> *** Makefile.PL options:
>   MP_APR_LIB     => aprext
>   MP_APXS        => /usr/local/sbin/apxs
>   MP_COMPAT_1X   => 1
>   MP_GENERATE_XS => 1
>   MP_LIBNAME     => mod_perl
>   MP_USE_DSO     => 1
> 
> 
> *** The httpd binary was not found
> 
> 
> *** (apr|apu)-config linking info
> 
>  -L/usr/local/lib -laprutil-1 -lexpat -liconv -L/usr/local/lib
>  -L/usr/local/lib -lapr-1 -lcrypt  -lpthread 
> 
> 
> *** /usr/local/bin/perl -V
> Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
>   Platform:
>     osname=freebsd, osvers=6.2-release, archname=i386-freebsd-64int
>     uname='freebsd freebsd.org 6.2-release freebsd 6.2-release #0: sat oct 14 21:57:31 pdt 2006 kris@freebsd.org:usrsrcsysmagickernelpath i386 '
>     config_args='-sde -Dprefix=/usr/local -Darchlib=/usr/local/lib/perl5/5.8.8/mach -Dprivlib=/usr/local/lib/perl5/5.8.8 -Dman3dir=/usr/local/lib/perl5/5.8.8/perl/man/man3 -Dman1dir=/usr/local/man/man1 -Dsitearch=/usr/local/lib/perl5/site_perl/5.8.8/mach -Dsitelib=/usr/local/lib/perl5/site_perl/5.8.8 -Dscriptdir=/usr/local/bin -Dsiteman3dir=/usr/local/lib/perl5/5.8.8/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Ui_malloc -Ui_iconv -Uinstallusrbinperl -Dcc=cc -Duseshrplib -Dccflags=-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" -Doptimize=-O2 -fno-strict-aliasing -pipe  -Ud_dosuid -Ui_gdbm -Dusethreads=n -Dusemymalloc=y -Duse64bitint'
>     hint=recommended, useposix=true, d_sigaction=define
>     usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
>     useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
>     use64bitint=define use64bitall=undef uselongdouble=undef
>     usemymalloc=y, bincompat5005=undef
>   Compiler:
>     cc='cc', ccflags ='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include',
>     optimize='-O2 -fno-strict-aliasing -pipe ',
>     cppflags='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include'
>     ccversion='', gccversion='3.4.6 [FreeBSD] 20060305', gccosandvers=''
>     intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678
>     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
>     ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
>     alignbytes=4, prototype=define
>   Linker and Libraries:
>     ld='cc', ldflags =' -Wl,-E -L/usr/local/lib'
>     libpth=/usr/lib /usr/local/lib
>     libs=-lm -lcrypt -lutil
>     perllibs=-lm -lcrypt -lutil
>     libc=, so=so, useshrplib=true, libperl=libperl.so
>     gnulibc_version=''
>   Dynamic Linking:
>     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='  -Wl,-R/usr/local/lib/perl5/5.8.8/mach/CORE'
>     cccdlflags='-DPIC -fPIC', lddlflags='-shared  -L/usr/local/lib'
> 
> 
> Characteristics of this binary (from libperl): 
>   Compile-time options: MYMALLOC PERL_MALLOC_WRAP USE_64_BIT_INT
>                         USE_LARGE_FILES USE_PERLIO
>   Locally applied patches:
> 	defined-or
>   Built under freebsd
>   Compiled at Oct 15 2006 05:04:59
>   %ENV:
>     PERL_LWP_USE_HTTP_10="1"
>   @INC:
>     /usr/local/lib/perl5/5.8.8/BSDPAN
>     /usr/local/lib/perl5/site_perl/5.8.8/mach
>     /usr/local/lib/perl5/site_perl/5.8.8
>     /usr/local/lib/perl5/site_perl
>     /usr/local/lib/perl5/5.8.8/mach
>     /usr/local/lib/perl5/5.8.8
>     .
> 
> *** Packages of interest status:
> 
> Apache2            : -
> Apache2::Request   : -
> CGI                : 3.15
> ExtUtils::MakeMaker: 6.30, undef
> LWP                : 5.805
> mod_perl           : -
> mod_perl2          : 2.000003
> 
> 
> This report was generated by t/REPORT on Tue Sep 25 13:39:09 2007 GMT.
> 
> -------------8<---------- End Bug Report --------------8<----------
> 
> Note: Complete the rest of the details and post this bug report to
> modperl <at> perl.apache.org. To subscribe to the list send an empty
> email to modperl-subscribe@perl.apache.org.
> 
> 
> 
>