All, I'm having some problems with a piece of code I wrote (attached below). I'm doing a system call which is executing a script and the script either returns 0 or 1 (running as a standalone script it works fine) but within a system call it's returning -1 (whether or not the script passed and/or failed). From what I read, -1 means the system called failed, but I know the script is being executing properly because I can see the output from the script. Thoughts ?
while (($new_sock, $c_addr) = $sock->accept()) { die "Can't fork: $!" unless defined ($pid = fork()); if ( $pid == 0 ) { # Child code $sock->close; my ($client_port, $c_ip) = sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($c_ip); my $client_host = gethostbyaddr($c_ip, AF_INET); print "CHILD CODE [cpid::$$]: Connection from: $client_host [$cl +ient_ipnum ", $new_sock->peerport, "] \n\n"; while (defined($buf = <$new_sock>) ) { if ($buf =~ /^EXECUTE:(.*)/ ) { print $1, "\n"; system("/msg/spgear/tools/bin/perl $1") || die "Could not +run program: $1 -- $!\n"; my $rtn = $?; print $new_sock "RETURN CODE: $rtn", "\n"; } else { print "INVALID SYNTAX\n"; print $new_sock "INVALID SYNTAX\n"; } } exit 0; } else { # Parent code $new_sock->close; print "\nPARENT CODE [ppid::$$ cpid:$pid]\n"; } }
** Update ** Actually, one thing I left out was I am using $SIG{CHLD} at the top of my code so all children get cleaned up properly. I believe this could be what is causing the bad return code ??? Also, I read the comments about using the regexp in the system call and, while I do understand your statement, once this code is integrated into the rest of my code, there is no way the client can pass in a "bad comment" (like you suggested). Without going into details, the client will only have a set up known "scripts" which will be executed. But thanks for your feedback, it is definately something I will keep in mind.

In reply to Wrong return code after a system call by TASdvlper

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.