system() return value affected by stderr redirect? 
Author Message
 system() return value affected by stderr redirect?

Quote:

> The ordering of the 2-byte value returned by system() when used to
> execute an external command that coredumps appears to be affected
> by stderr redirection.

> Without redirection, the return value from my test runs is 0x008b .

> Without redirection, the return value appears to be reversed: 0x8b00 .


>    "testval ...",
>    "testval ... 2>foo.stderr2"

It appears to have something to do with the fact that the command with
redirection gets processed by the shell in a way that the other one
doesn't.  By using the form of system which takes a vector instead
of a string (per the system() docs), you bypass this processing
(whatever it is), and the observed wierdness goes away.  I.e. try


     [ "testval", "..." ],
     [ "testval", "...", "2>foo.stderr2" ],


--
John Porter

Ann wenno haddum billizac...



Sun, 27 Jul 2003 22:27:05 GMT  
 system() return value affected by stderr redirect?
[A complimentary Cc of this posting was sent to Mark Cavanaugh


Quote:
> Dear clpmod'ers,

> The ordering of the 2-byte value returned by system() when used to
> execute an external command that coredumps appears to be affected
> by stderr redirection.

It is affected by the fact whether Perl executes the command directly,
or via shell.

Quote:
> Without redirection, the return value from my test runs is 0x008b .
> Without redirection, the return value appears to be reversed: 0x8b00 .

  ^^^^^^^
          probably WITH

With redirection, it is the shell who gets 0x008b from your program.
Since the shell did not core dump, it cannot return the same exit
code.  ;-) So it returns some *other* exit code which "indicates" a
failure of the executed program.

The rules for such an "indication" are set by convention. IIRC, it is
the highest bit of the exit code is set.

Hope this helps,
Ilya



Mon, 28 Jul 2003 03:44:26 GMT  
 system() return value affected by stderr redirect?

Quote:

> The ordering of the 2-byte value returned by system() when used to
> execute an external command that coredumps appears to be affected
> by stderr redirection.

> Platform: SunOS lagrange 5.6 Generic_105181-22 sun4u sparc SUNW,Ultra-1
> Perl:     This is perl, version 5.004_03

I see the same behaviour using perl 5.005_03 on Linux 2.2.16

Quote:
> Without redirection, the return value from my test runs is 0x008b .
> The eighth-bit of the lower byte can be checked via a bitwise AND
> with value 128, and given 0x008b, the result is true.

Without redirection, the command is run by perl and when it dumps
core, it returns a two-byte value as documented.

Quote:
> Without redirection, the return value appears to be reversed: 0x8b00 .
> So a check for the coredump bit yields false.

With redirection, Perl hands the command to the shell. The shell does
the redirect and then runs the command.  The value 0x008b is returned
to now the shell.  The shell reacts by doing an exit(0x008b), which,
unfortunately, means the value 0x8b00 gets returned to Perl.

Quote:
> I can't supply you with an app that coredumps,

I can:

        #!/usr/bin/perl -w

        kill SEGV, $$

A SEGV signal is a SEGV signal; it doesn't matter whether it comes
from a kill() or an actual segmentation fault.

[ Lots of code deleted ]

Quote:
>     $rc = system ( $_ );
>     $core_dump = $rc & 128;

Perhaps you could replace this by:

        $rc = system ( "$_ 1>&1" );
        $core_dump = $rc & 0x8000;

The extra redirect has no effect in the shell, but it forces Perl to always
hand off the command to the shell.  That way, you'll get the wrong value
consistently.  Not great, but at least you know which bit to look at.
You'll get false positives, though,  if the command exit()s with a value
greater than 127.

Hope this helps,

-- HansM



Mon, 28 Jul 2003 06:52:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Access 97/2000 and connection problem

2. STDERR redirect to system log?

3. Redirecting stdout/stderr from a call to system()

4. problem redirecting stderr during a system call

5. redirecting stdout and stderr within system()

6. redirecting stdout and stderr within system()

7. Redirect returns text after system process

8. Q: Returning value from system()

9. testing return value of system()

10. Finding Arc-cos -or- returning value from system call

11. Q: Returning value from system()

12. System call return value 256 - Bad file descriptor

 

 
Powered by phpBB® Forum Software