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

What could cause print to fail before calling write(2)?

Thread Next
From:
Maxwell Carey
Date:
April 19, 2016 19:27
Subject:
What could cause print to fail before calling write(2)?
Message ID:
CADLEpY5d5-RbH=6s0Q-S9FXrmyFC3pYtairMH7MBg9tU35Vs2A@mail.gmail.com
Interesting question on Stack Overflow:
http://stackoverflow.com/q/36667502/176646

The OP is able to print to a file but not to STDOUT or STDERR. strace shows
that write is only called when printing to the file.

perl -e'print "foo" or die $!' exit code is 9, so print is reporting EBADF.
Looking at the 5.14.2 source, I see two places that could be setting this
in pp_print:

    if (!io) {
        if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv)))
            && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
            goto had_magic;
        report_evil_fh(gv);
        SETERRNO(EBADF,RMS_IFI);
        goto just_say_no;
    }
    else if (!(fp = IoOFP(io))) {
        if (IoIFP(io))
            report_wrongway_fh(gv, '<');
        else
            report_evil_fh(gv);
        SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
        goto just_say_no;
    }

But I can't figure out what would trigger either of these conditions. I
tried closing STDOUT before running perl and strace shows

    write(1, "foo", 3)                      = -1 EBADF (Bad file descriptor)

so it's obviously getting past that point even if STDOUT is closed. Any
ideas what could cause this?

Thread Next


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