develooper Front page | perl.perl5.porters | Postings from April 2002

Bug in Perl_my_popen?

From:
Green, Paul
Date:
April 29, 2002 14:33
Subject:
Bug in Perl_my_popen?
Message ID:
A2A34F15EE916148BC4C4748223E67A4014E255F@EXNA4.stratus.com
This annotated code fragment comes from util.c in bleadperl 16147.  I was
gazing at it today trying to understand a problem I'm having when fork()
fails.  I'm wondering why the code opens a pipe but upon detecting an error,
closes only 1/2 of the pipe?   The local array named "p" will be lost upon
return, so this looks like a file-descriptor leak to me.

Also, when the fork fails, why not always Perl_croak ("can't fork")?  Why
conditionally display a message?

My inexperience inside perl is vast, so I'm hoping someone can glance at
this and see if these are issues or not.

Thanks much,
PG

PerlIO *
Perl_my_popen(pTHX_ char *cmd, char *mode)
{
    int p[2];
    register I32 This, that;
    register Pid_t pid;
    SV *sv;
    I32 doexec = strNE(cmd,"-");
    I32 did_pipes = 0;
    int pp[2];

    PERL_FLUSHALL_FOR_CHILD;
#ifdef OS2
    if (doexec) {
     return my_syspopen(aTHX_ cmd,mode);
    }
#endif
    This = (*mode == 'w');
    that = !This;
    if (doexec && PL_tainting) {
     taint_env();
     taint_proper("Insecure %s%s", "EXEC");
    }
    if (PerlProc_pipe(p) < 0)		/* OPEN FIRST PIPE "p" */
     return Nullfp;
    if (doexec && PerlProc_pipe(pp) >= 0)	/* OPEN SECOND PIPE "pp" */
     did_pipes = 1;
    while ((pid = PerlProc_fork()) < 0) {
     if (errno != EAGAIN) {			/* ERROR but not EAGAIN */
         PerlLIO_close(p[This]);		/* CLOSE only p[This]. Why?
*/
         if (did_pipes) {
          PerlLIO_close(pp[0]);		/* CLOSE all of "pp". */
          PerlLIO_close(pp[1]);
         }
         if (!doexec)			/* Why this test? */
          Perl_croak(aTHX_ "Can't fork");
         return Nullfp;			/* UNCONDITIONALLY RETURN */
     }
     sleep(5);	/* GOT EAGAIN, WAIT AWHILE, TRY AGAIN */
    }
....

Thanks
PG
--
Paul Green, Senior Technical Consultant, Stratus Technologies.
Voice: +1 978-461-7557; FAX: +1 978-461-3610; Video on request.





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About