Perl's open() pukes when trying to open a read-type pipe 
Author Message
 Perl's open() pukes when trying to open a read-type pipe

I've narrowed down a pernicious bug in a local Perl script to the one
exhibited in the following snippet:

#!/usr/um/bin/perl

open(ABC, "/bin/date |") || die "$!";
while (<ABC>) { print; }

This script works on some local machines, but not on others.  On the ones
it doesn't work on, I get this error message:

Bad file number at ./pp line 3.

More testing showed that any statement of this type:

open(ABC, "command args |");

failed with the same "Bad file number" message.  (I also seem to recall
seeing something similar to a "File/directory does not exist" message while
tinkering around earlier, but I cannot duplicate that message now.)

Opening pipes for writing works fine.

I recompiled Perl with DEBUGGING defined and tried again.  This time the
program aborted with a segmentation fault.  When I ran the script with
-D65535 added to the first line of the script, I got this:

<large trace deleted>

EXECUTING...

    =>  
(./pp:0)        block entry
Entering block 0, type BLOCK
    =>  
(./pp:0)        next statement
    =>  
(./pp:3)        glob value(main::ABC)
    =>  GV()  
(./pp:3)        constant item(PV("/bin/date |"))
    =>  GV()  PV("/bin/date |")  
(./pp:3)        open
Pad sv 2

...followed by the segmentation fault.

I recompiled Perl again, telling it not to use Perl's malloc(), but the new
executable was just as faulty as the original.

Might anyone know the reason for this untoward behavior?

--



Tue, 06 Oct 1998 03:00:00 GMT  
 Perl's open() pukes when trying to open a read-type pipe
: I've narrowed down a pernicious bug in a local Perl script to the one
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: exhibited in the following snippet:
                   ^^^^^^^^^^^^^^^^^

Wonderful, clever, effective!!! Oh that more posters to c.l.p.m were
so courteous (and self-helpful).

: #!/usr/um/bin/perl

: open(ABC, "/bin/date |") || die "$!";

Perhaps what's throwing you off is the fact that you may not get
a filehandle NOR the 'die' message.

from the perl FAQ:

5.16) Why doesn't open return an error when a pipe open fails?

    These statements:

        open(TOPIPE, "|bogus_command") || die ...
        open(FROMPIPE, "bogus_command|") || die ...

    will not fail just for lack of the bogus_command.  They'll only
    fail if the fork to run them fails, which is seldom the problem.

    If you're writing to the TOPIPE, you'll get a SIGPIPE if the child
    exits prematurely or doesn't run.  If you are reading from the
    FROMPIPE, you need to check the close() to see what happened.

    If you want an answer sooner than pipe buffering might otherwise
    afford you, you can do something like this:

        $kid = open (PIPE, "bogus_command |");   # XXX: check defined($kid)
        (kill 0, $kid) || die "bogus_command failed";

    This works fine if bogus_command doesn't have shell metas in it, but
    if it does, the shell may well not have exited before the kill 0.  You
    could always introduce a delay:

        $kid = open (PIPE, "bogus_command </dev/null |");
        sleep 1;
        (kill 0, $kid) || die "bogus_command failed";

    but this is sometimes undesirable, and in any event does not guarantee
    correct behavior.  But it seems slightly better than nothing.

    Similar tricks can be played with writable pipes if you don't wish to
    catch the SIGPIPE.

--
  Tad McClellan,      Logistics Specialist (IETMs and SGML guy)

  Interesting trivia: If you took all the sand in North Africa and spread
     it out... it would cover the Sahara desert.



Tue, 06 Oct 1998 03:00:00 GMT  
 Perl's open() pukes when trying to open a read-type pipe

Quote:

>More testing showed that any statement of this type:
>open(ABC, "command args |");
>failed with the same "Bad file number" message.  (I also seem to recall
>seeing something similar to a "File/directory does not exist" message while
>tinkering around earlier, but I cannot duplicate that message now.)

I tried recompiled Perl after instructing it not to use vfork, and this
problem went away.  Thanks to all who offered help through e-mail!

--



Wed, 07 Oct 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. PERLFUNC: open - open a file, pipe, or descriptor

2. PERLFUNC: open - open a file, pipe, or descriptor

3. Permission Denied Error trying to open dir for reading in Win2k

4. open on pipe and 'wait'

5. open pipe with read and write?

6. repost : Read return value from a sql script using piped open

7. read return value from a sql script using piped open

8. open() bug reading from a pipe?

9. Opening and Reading Named Pipes

10. Untainting and Safe Pipe Opens for reading

11. open won't open files in subdirectories?

12. Piping in stderr when creating a pipe with the open call

 

 
Powered by phpBB® Forum Software