Updated this description: I'm looking at a program in which a connection to MySQL is established via the DBI module. Then a wget command is issued that takes a long time. During that time, the database connection times out, and must send a signal to the parent. This causes $? and $! to be set. But in addition to those, the system('wget') call returns -1. I know the wget succeeds, because the file is completely downloaded. But since system() returns -1, it looks like the wget failed!

Update2: I think Tye figured this out while we were chatting in the CB: system calls waitpid, and waitpid is interrupted by the signal. So my child wget command keeps on executing the background, while the perl script resumes execution.

I wrote a sample program that shows the same problem, but I think that it might be causing confusion. I don't want the child to receive the signal, nor am I using this alarm as a timeout to make sure the system call doesn't hang. My use of alarm w/ system here may be wrong, as mr_mischief pointed out to me. I'm leaving it here though, because I *think* it demonstrates the same problem.

#!/usr/bin/env perl use strict; use warnings; $SIG{ALRM} = sub { print( "Alarm triggered, making system call...\n" ); unlink('/doesnt/exist'); # this will definitely fail }; alarm( 2 ); my $retval = system('sleep 4'); if( $retval == -1 ) { print( "system() retval = $retval; " . '$?' . " = $?; " . '$!' . " + = $!\n" ); } elsif( $retval == 0 ) { print( "system() returned 0\n" ); } else { print( "system() return $retval\n" ); }
And so here is a test run:
test$ ./test.pl Alarm triggered, making system call... system() retval = -1; $? = -1; $! = No such file or directory
I searched the archives, and I found an old node that may be related.

I tested this on two different systems, with two different perl version. Some system details:
System 1: $ perl -v This is perl, v5.8.8 built for i386-linux-thread-multi libc=/lib/libc-2.5.so $ uname -a Linux hostname.removed 2.6.18-8.1.1.el5 #1 SMP Mon Feb 26 20:38:02 EST + 2007 i686 i686 i386 GNU/Linux System 2: test$ perl -v This is perl, v5.8.6 built for i686-linux-64int libc=/lib/libc-2.3.2.so test$ uname -a Linux hostname.removed 2.6.9-55.0.6.ELsmp #1 SMP Tue Sep 4 21:36:00 ED +T 2007 i686 unknown
I haven't been able to find any documentation about this behavior. Is this intended? If so, how can I trust system's return value?

In reply to System call + signals = bad return code? by swkronenfeld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.