Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Perl system() call ever die?

Can a system() call can ever die in Perl 5?

(in other words, in order to 100% crash-proof a program that does a system() call, does it need to be wrapped into an eval block, or is that wholly totally unnecessary?)


I haven't found a single mention to that possibility in perldoc system, but didn't quite find the precise "this call never dies" either.

NOTE: the question is about basic CORE Perl here, no autodie or any other custom module that would have similar effect. Also, assume no ALRM signal was set, or any other custom signal handler for that matter.

I'm assuming that all versions of Perl 5.* behave the same, but if not, an answer pertaining to 5.8 would be appreciated.

like image 917
DVK Avatar asked Nov 05 '12 15:11

DVK


People also ask

What does System command do in Perl?

Perl System () Function: What It Does Perl's system() function executes a system shell command. Here the parent process forks a child process, and then waits for the child process to terminate. The command will either succeed or fail returning a value for each situation.

What does system return in Perl?

Perl's System Command Here are some things you should know about the system command: It returns 0 if the command succeeds and 1 if it fails. These return values are the inverse of most commands, which typically return 0 when they fail and 1 when they succeed.

How do I get the output system command in Perl?

The easiest way is to use the `` feature in Perl. This will execute what is inside and return what was printed to stdout: my $pid = 5892; my $var = `top -H -p $pid -n 1 | grep myprocess | wc -l`; print "not = $var\n"; This should do it.


1 Answers

Unless my interpretation of the source is incorrect, this looks like a possibility:

Source: Perl 5.16.2 (checked 5.8.8 too), file: pp_sys.c, line: 4224 within the PP(pp_system) code block:

if (n != sizeof(int))
  DIE(aTHX_ "panic: kid popen errno read, n=%u", n);

DIE is Perl_die(pTHX_ const* pat, ...) declared in util.c

According to the documentation, "panic: kid popen errno read" means "forked child returned an incomprehensible message about its errno".

Explanation of panic messages in Perl:

The convention is that when the interpreter dies with an internal error, the message starts "panic: ". Historically, many panic messages had been terse fixed strings, which means that the out-of-range values that triggered the panic are lost. Now we try to report these values, as such panics may not be repeatable, and the original error message may be the only diagnostic we get when we try to find the cause.

like image 189
j.w.r Avatar answered Sep 23 '22 23:09

j.w.r