Front page | perl.perl5.porters |
Postings from September 2003
Re: [perl #23779] $? and negative exit codes
Thread Previous
|
Thread Next
From:
Tels
Date:
September 16, 2003 09:51
Subject:
Re: [perl #23779] $? and negative exit codes
Message ID:
200309161846.28023@bloodgate.com
-----BEGIN PGP SIGNED MESSAGE-----
Moin,
On Tuesday 16 September 2003 01:20, Dave Mitchell (via RT) wrote:
> On Mon, Sep 15, 2003 at 09:40:52PM +0200, Tels wrote:
> > > But exit values are unsigned 8-bit integers (at least on the UNIX
> > > systems I'm familiar with). ie exit(-1) is the same as exit(255)
> > I couldn't find this info for my system (reading the entire bash
> > manpage). Where would that be documented and where could I find this
> > documentation?
>
> man 2 exit
> man 2 wait
It is man 3 exit on my system (2 exit is something else...)
And it says nothing about positive or negative, but from the tone it is clear
that both are possible:
SYNOPSIS
#include <stdlib.h>
void exit(int status);
and:
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more
portable (to non-Unix environments) than that of 0 and
some nonzero value like 1 or -1. In particular, VMS uses a
different convention.
And "2 wait" doesn't either tell about 8 bits, or positive or negative:
Instead it says:
pid_t wait(int *status);
This is SuSE 8.2 Linux. I do not know how exit() and are coded internally. So
I guess, systems just differ and it could well be possible that some returns
a negative value... (whether it should or not) :o)
> (We're talking OS-level stuff here, not shell level.).
> These basically say that you can exit() with any value you like (+ve or
> -ve), but the OS will then take the lower 8-bits of this value and stick
> them in some of the bits of the status word returned by wait(2), along
> with other stuff like signal numbers. This status value from wait(2) is
> what system() eventually passes back to you.
>
> > In any event, watch these examples close:
> >
> > te@null:~> perl -le 'system "perl exit.pl -1"; $exit_code = int($? /
> > 256); print "$exit_code $?"'
> > 255 65280
> >
> > te@null:~> perl -le 'system "exit.pl -1"; $exit_code = int($? / 256);
> > print "$exit_code $?"'
> > 0 -1
> >
> > There is a difference between "exit.pl -1" and "perl exit.pl -1". Shell
> > vs. Perl? Which one is right? Both?
>
> The second is most likely failing to execute exit.pl (because it isn't in
> your path, or is not executable. Try adding a print to exit.pl to
> confirm.) In neither case is the shell getting involved. (No
> metacharacters.)
Ok, but this is _the_ point. The "exit status" (or what you call it) from
system "somecommand" returned in $? is _negative_ and the documented method
of extracting it returns garbage. (instead of -1).
> In the second example, the following section from perldoc -f system
> applies:
>
> Return value of -1 indicates a failure to start the program (inspect
> $! for the reason).
> So the full test should be something like
>
> if ($? == -1) {
> print "failed to execute: $!\n";
> }
> elsif ($? & 128) {
> print "executed but coredumped\n";
> }
> elsif ($? & 127) {
> printf "executed but died with signal %d\n", $? & 127;
> }
> else {
> printf "executed and exited with value %d\n", $? >> 8;
Yeah, but this is _not_ what the doc says. It says that
$exit_code = $? >> 8;
is sufficient. Which it isn't. That was my complaint all along!
> > Also, watch this again (in slow motion :o):
> >
> > te@null:~/perl/File-Spec> perl -le 'system "exit.pl -1"; $exit_code = $?
> > >> 8; print "$exit_code $?"'
> > 16777215 -1
> >
> > If $? is ever negative (for whatever reason,
>
> Only if it's -1, in which case it doesn't hold a meaningful value; $! does
> instead.
But the documentation doesn't tell you that this "-1" state is possible. Hence
my bugreport. :)
> > this is outside of your program's
> > scope and control!), the code snipped in the doc in Perl will leave you
> > with a wrong value for $exit_code (according to your info, it should be
> > either 255 or -1, depending).
Best wishes,
Tels
- --
Signed on Tue Sep 16 18:38:19 2003 with key 0x93B84C15.
Visit my photo gallery at http://bloodgate.com/photos/
PGP key on http://bloodgate.com/tels.asc or per email.
"Call me Justin, Justin Case."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)
Comment: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.
iQEVAwUBP2c+Y3cLPEOTuEwVAQHw8gf9HlCB68O3nMI6f0KEOXFYrzsgYQ5HlJML
qspddDhTZJKWFQ5eUKiJq1st/OAOXpxdYoEk1Bnct4nJMUR226q/HtFVjhaDOYUL
Ast/7XEAUQkYNBGMMwLCCRfkr82Y3QO2+jgihS6rd34E5R6ofRLobrw0LHmvF/jD
s3pR9UKjEhxv7iSUVp85ecw5/C6Ep5QQPdKrsW+TWEcVZcvDY4dV/6KhNdQPnk7M
FT4CLMAGwjcWRnzWnotW+xn8w4tnN8KG6HP7VRdf5ARH/JJTwsUTfdSyOwbBXidH
jipNt+euMvv1JIeCdHU2aassiqjrvus/38Zm4kWaH9hUtPiwDFGAIQ==
=6Zu8
-----END PGP SIGNATURE-----
Thread Previous
|
Thread Next