perl: handle die before the framework - perl

I am working with a perl framework which monitor $SIG{DIE} itself, my code was executed by the framework, so my exception handle code cannot be executed because the framework is first one to detected the exception then terminate the script.
frame.pm
sub execute
{
$SIG{__DIE__} = \&_handleDie;
eval{ #execute myscript.pl sub main
$rv = &$pFunct(#args);}
if ($#){ processException($#)}
print "myscript.pl success executed"
}
myscript.pl
use frame;
frame->execute( \&main );
sub main
{
%codes that redirect STDOUT to a file%
#if below API cmd no exception, hide it's output,
#otherwise output the API cmd STDERR msg
%codes called API of another module%
try
{
die("sth wrong");
}catch{
%codes restore STDOUT to terminal%
print "error msg, but this line will not be executed, how to get it be execute?"
}
}
The script first redirect STDOUT to a file for dumy some no use output.
When I want to implement is if exception happen(die line), the script can restore STDOUT to terminal then print error to terminal. Now it was handled by frame and print to STDOUT but not STDERR, so I need to handle restore STDOUT before frame print it to STDOUT.
with ruakh's solution, myscript.pl has passed SIG of frame, now catched by frame line if ($#){ processException($#)}, that is when execute myscript->die(), the program come to frame->if ($#){ processException($#)}, but not myscript->catch
=====================
I finally found this works for me:
myscript.pl
frame->execute( \&main );
sub main
{
open my $stdOri, ">&STDOUT";
my $tmpFile = "/tmp/.output.txt.$$";
open STDOUT, ">$tmpFile";
#overwrite frame provided exception handling.
local $SIG{__DIE__}=sub{close STDOUT; open STDOUT, ">&", $stdOri;};
#cause a exception,
#this exception will be processed by 'local $SIG{__DIE__}' block which restore STDOUT
#then frame->eval catch this exception, and print it in the terminal.
my $c=5/0;
}
thanks for ruakh's inspire.

Assuming that you don't want to modify the framework, you can locally override the signal-handler:
use frame;
frame->execute( \&main );
sub main
{
try
{
local $SIG{__DIE__}; # remove signal-handler
die("sth wrong");
}catch{
print STDERR "error msg";
die $#; # pass control back to framework's signal handler
}
}
Disclaimer: tested with an eval-block, rather than with try/catch, since I don't have TryCatch installed. My understanding is that TryCatch depends on eval, and not on $SIG{__DIE__}, but I could be wrong about that.

The framework's $SIG{__DIE__} handler is wrong wrong wrong. It shouldn't be eating exceptions inside of an eval. It should do die #_ if $^S as suggested by perldoc -f die.

Related

How come my fork/exec and close is losing the error code from the child

I have a piece of perl code that is roughly like this
my $pid = open(PIPE, '-|');
die "Unable to fork: $!\n" if !defined $pid;
if ($pid == 0)
{
open STDERR, '>&STDOUT' or die "Can't redirect STDERR to STDOUT\n";
exec(#cmd);
die "Unexpected exec failure: $!\n";
}
my #lines = (<PIPE>);
close PIPE;
if ($? != 0) { do stuff; }
However, for reasons I can't understand, possibly related to the actual program being called not existing, sometimes this fails to pick up the error from the child in $?
#lines contains the "Unexpected exec failure: File or directory does not exist" as expected (and some other output from a $SIG{__DIE__} handler), but $? is set to 0. I'm working round this for now by also checking the return from close which is fortunately set to 1. But where did my error code go?
I don't know. It shouldn't happen. You could use the following instead:
use IPC::Open qw( open3 );
my $pid = open3(local *PIPE, '>&STDOUT', undef, #cmd);
while (<PIPE>) {
...
}
waitpid($pid, 0);
Bonus: An error that occur while launching the child (e.g. an error from duping or from exec) throws an exception in the parent, so it doesn't look like the command ran and returned an error.
Well, I found what it was.
Someone had added an END{} block in a library that was destroying the return codes (by calling system and hence destroying $?). I backed out the change and everything was happy.

want to get the die error message from a command executed via open in Perl

I am trying to fork out a cmd like below
my $h = IO::Handle->new;
$self->{-handle} = $h;
die "IO::Handle->new failed." unless defined $h;
$self->{-pid} = open $h, $self->{-command} . ' 2>&1 |';
$self->fileevent($h, 'readable' => [\&_read_cmd_op, $self]);
sub _read_cmd_op{
my $h = $self->{-handle};
if ( sysread $h, $_, 4096 ) {
my $t = $self->Subwidget('text');
$t->insert('end', $_);
$t->yview('end');
} else {
$self->{-finish} = 1;
}
}
Now the problem is that the '$self{-command}' is invoking
another perl script which if dies I want to know.
Note that the $self{-pid} still exists even if cmd dies.
The above code is in a Perl/TK app, where the $self->{-command} o/p in captured in a
text widget.
Somehow i don't get the die message even in the test widget.
I see it on stdout.
2 questions
How can i get the cmd op/error in the text widget?
How can i know that the command fired via IO::Handle died?
$self->{-pid} is just the pid of the forked process, not some magic object which goes away if the command exits.
I cannot reproduce the problem not getting the die() message. If the snippet above is called with 'perl -e "die 123"', then "123" appears in the text widget (at least on a Unix system).
For getting the exit code you can use something like the following.
} else {
$mw->fileevent($h, 'readable', '');
my $pid = waitpid($self->{-pid},0);
warn "pid $pid finished";
warn "retcode is " . ($? >> 8);
$self->{-finish} = 1;
}
The fileevent call with the empty callback stops further selects on this filehandle. With the waitpid call you wait for the termination of the child process. Once this happens, the exit code is available in the $? variable, like after a normal system() call. So for a non-zero exit code you know that the command died or exited with a false value.

Open3 outputting to std error only and not asynchronously

I am relatively new to perl programming and I am trying to figure out how open3 works. Here is the code.
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open3;
my $dir = "/home/vman/Documents/Dev/perl_scripts/Provenance/temp";
my $fileHandle;
my $bashPid;
print "Starting main program\n";
my $pid = fork();
if($pid)#Parent process2
{
print("Start transfer.\n");
$bashPid = $pid;
#Attaching an strace to the executed command which happens in the child process
open3(\*WRITE, \*READ,\*ERROR,"strace", "-f", "-F", "-e", "trace=open,execve","-p", $bashPid, "-s", "2097152","-q");
while(<READ>)
{
print("Here1\n");
print("$_");
}
while(<ERROR>)
{
print("$_");
}
print("Finish transfer.\n");
}
elsif($pid == 0)
{
if (scalar(#ARGV == 0))
{
exit
}
my $args = join(' ', #ARGV);
exec($args);
}
else
{
die("Could not fork.");
}
close(READ);
close(WRITE);
close(ERROR);
waitpid($bashPid, 0);
print "End of main program\n";
I want to run an strace on a bash process, then capture all the output while it is being outputted. Then I will take that output and parse it to see what files are being changed by which process and I will save those changes in a mysql database. For now all I am trying to do is attach an strace onto an existing bash process and get the output of that strace printed within the bash terminal that is running just to make sure that it is asynchronously reading the output.
One of the problems is that I am getting the output through the ERROR filehandle. I am a little confused on to why this is happening. Am I using the correct order for open3 and if there is an error why is the correct output even making it to stderr?
The second problem I have is that I am getting the output only when exec ends which is no good since it needs to be done while exec is running. I thought open3 runs asynchronously.
As per suggested this is what I did and it works perfectly.
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Run3;
my $bashPid;
print "Starting main program\n";
my $pid = fork();
if($pid)#Parent process
{
print("Start transfer.\n");
$bashPid = $pid;
#Attaching an strace to the executed command which happens in the child process
my $command = "strace -fFe trace=open,execve -p $bashPid -s 2097152 -q";
run3($command, \*STDIN, \*STDOUT, \*STDERR);
if ($?)
{
die "something went horribly wrong";
}
while(<STDERR>)
{
print($_);
}
print("Finish transfer.\n");
}
elsif($pid == 0)#cild process
{
if (scalar(#ARGV == 0))
{
exit
}
my $args = join(' ', #ARGV);
exec($args);
}
else
{
die("Could not fork.");
}
close(STDIN);
close(STDOUT);
close(STDERR);
waitpid($bashPid, 0);
print "End of main program\n";
One of the problems is that I am getting the output through the ERROR filehandle
Correct. strace writes to STDERR.
The second problem I have is that I am getting the output only when exec ends which is no good since it needs to be done while exec is running. I thought open3 runs asynchronously.
That's because you only start reading from the child's STDERR after the child closes its STDOUT when it ends.
In fact, you're lucky you haven't deadlocked yet. By reading one at a time as you are currently, doing, you'll deadlock when strace has output enough to fill the pipe.
You need to read from both the child's STDOUT and STDERR as it comes in. You could do this using with the help of select, polling non-blocking handle or threads. None of those options are as simple as ditching open3 and using a higher-level module that handles this for you. The simpler IPC::Run3 and the fully featured IPC::Run are good choices.

Perl catch runtime error

I am writing a perl script to open a textfile and perform some transformations on it. script is throwing an error saying "No such file or directory exists" whenever text file is unavailable.
I want to catch that error and create textfile then.
while (<>) { #i am passing filename from the batch file
#some task
}
# if the above while loop fails it throws no such file or directory exists error. I want to catch it and do some other task.
Those particular errors are warnings sent to STDERR by the "magic" behind ARGV. Why don't you just redirect STDERR?
perl script.pl foo bar 2>error.log
If that's not good enough, you'll have to start using $SIG{__WARN__} (yuck) or stop using ARGV (<> with no file handle defaults to using ARGV).
for my $argv (#ARGV ? #ARGV : '-') {
open(my $argv_fh, $argv)
or do {
... print message to log file ...
next;
};
while (<$argv_fh>) {
...
}
}
Instead of trying to catch the warning that the file doesn't exist, why not try passing the file path via getopt and test for file existence/readability before opening using file test operators.
edit: updated with example
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my %opts;
getopt('f', \%opts);
die "use -f to specify the file" unless defined $opts{f};
if(! -e $opts{f} ){
print "file doesn't exist\n";
}
elsif(! -r $opts{f} ){
print "file isn't readable\n";
}
elsif(! -f $opts{f} ){
print "file is not a normal file\n";
}
else{
open( my $fh, '<', $opts{f} ) or print "whatever error handling logic\n";
}

Redirecting STDOUT in child process

Have a Parent process which spawns multipe child process via fork. I want the log files by the parent and child process to be separate. The Problem is child process STDOUT gets redirected into the parent log file as well as the child log file. Not sure what i need to change to avoid child process log message to get into the parent log file. Also i dont understand in the below setEnvironment function the purpose of creating OUT and ERR file handle. This is a existing code so i kept as it is. In the parent process and child process i set the variable $g_LOGFILE to contain different file name so that separate log files are created. Also i call setEnvironment function in both parent and child process. I tried by closing STDOUT,STDERR,STDIN in the child process and calling the setenvironment but it was not working properly.
sub setEnvironment()
{
unless ( open(OUT, ">&STDOUT") )
{
print "Cannot redirect STDOUT";
return 2;
}
unless ( open(ERR, ">&STDERR") )
{
print "Cannot redirect STDERR";
return 2;
}
unless ( open(STDOUT, "|tee -ai $g_LOGPATH/$g_LOGFILE") )
{
print "Cannot open log file $g_LOGPATH/$g_LOGFILE");
return 2;
}
unless ( open(STDERR, ">&STDOUT") )
{
print "Cannot redirect STDERR");
return 2 ;
}
STDOUT->autoflush(1);
}
####################### Main Program ######################################
$g_LOGFILE="parent.log";
while ($file = readdir(DIR))
{
my $pid = fork;
if ( $pid ) {
setEnvironment();
#parent process code goes here
printf "%s\n", "parent";
next;
}
$g_LOGFILE="child.log";
setEnvironment();
#child code goes here
printf "%s\n", "child";
exit;
}
wait for #pids
Ok i tested this code alitle. Here is my sample code. In my code there is similar(not exact) problem: all messages are double-written to childs log file.
So my answers to your questions:
The Problem is child process STDOUT gets redirected into the parent log file as well as the child log file.
This because when you open file with pipe (open(STDOUT, "|tee ...) as a underlying result your process fork() to create child process and then exec into program what you run (tee). Forking(for tee) takes STDOUT of master process so tee will write into parent's logfile. So i think you must revoke using STDOUT handle for master process. Or, second way - remove use of tee - its simplest way.
Also i dont understand in the below setEnvironment function the purpose of creating OUT and ERR file handle.
Seems this is someone's woraround about problem above. You can grep -rE '
\bERR\b' . to search in code if it used or not. Probably someone wanted to save real STDOUT and STDERR to further use.
It would appear that the intent of the original code is as follows:
when the script is started from, say, a terminal, then provide aggregate parent and child output to the terminal
additionally, provide a copy of the parent output in parent.log, and a copy of child output in child.log
Note that #Unk's answer is correct as far as 2. goes, and has less moving parts than any code using tee, but fails to achieve 1.
If it is important to achieve both 1. and 2. above, then take your original code and simply add the following at the top of your setEnvironment method:
sub setEnvironment()
{
if ( fileno OUT )
{
unless ( open(STDOUT, ">&OUT") )
{
print "Cannot restore STDOUT";
return 2;
}
unless ( open(STDERR, ">&ERR") )
{
print "Cannot restore STDERR";
return 2;
}
}
else
{
unless ( open(OUT, ">&STDOUT") )
{
print "Cannot redirect STDOUT";
return 2;
}
unless ( open(ERR, ">&STDERR") )
{
print "Cannot redirect STDERR";
return 2;
}
}
unless ( open(STDOUT, "|tee -ai $g_LOGPATH/$g_LOGFILE") )
...
Incidentally, do not forget to also add $pid to #pids if your actual code does not do that already:
...
my $pid = fork;
if ( $pid ) {
push #pids, $pid;
...
Why and how does this work? We simply want to temporarily restore the original STDOUT immediately before rewiring it into tee, so that tee inherits it as its standard output and actually writes directly to the original STDOUT (e.g. your terminal) instead of writing (in the case of the forked children) through the parent's tee (which is where the child's STDOUT normally pointed to before this change, by virtue of inheriting from the paremnt process, and which is what injected those child lines into parent.log.)
So in answer to one of your questions, whoever wrote the code to set OUT and ERR must have had exactly the above in mind. (I cannot help but wonder whether or not the difference in indentation in your original code is indicative of someone having removed, in the past, code similar to the one you have to add back now.)
Here's what you now get at the end of the day:
$ rm -f parent.log child.log
$ perl test.pl
child
parent
child
parent
parent
child
parent
child
parent
$ cat parent.log
parent
parent
parent
parent
parent
$ cat child.log
child
child
child
child
child
You can always redirect STDOUT to log file by closing it first and then reopening:
close STDOUT;
open STDOUT, ">", $logfile;
Small downside to this is that once STDOUT is redirected, you will not see any output on terminal during script execution.
If you want parent and child process have different log files, just perform this redirection in both to different log files after fork(), something like this:
print "Starting, about to fork...\n";
if (fork()) {
print "In master process\n";
close STDOUT;
open STDOUT, ">", "master.log";
print "Master to log\n";
} else {
print "In slave process\n";
close STDOUT;
open STDOUT, ">", "slave.log";
print "Slave to log\n";
}
I have tested that this works as expected on Linux and Windows.
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Capture::Tiny qw/capture_stdout/;
my $child_log = 'clild.log';
my $parent_log = 'parent.log';
my $stdout = capture_stdout {
if(fork()){
my $stdout = capture_stdout {
print "clild\n";
};
open my $fh, '>', $child_log;
print $fh $stdout;
close $fh;
exit;
}
print "parent\n";
};
open my $fh, '>', $parent_log;
print $fh $stdout;
close $fh;
All the other answers are correct (PSIalt's in particular) - I'm merely hoping that I can answer with corrected code that is identifiably close to that in the question. The key things to notice:
"|tee -ai..."
The tee commands prints its standard in to its standard out while also printing to the given file. As PSIalt says, removing it is the easiest way to ensure each process' output goes only to the correct file.
setEnvironment() inside loop for parent
The original code is constantly redirecting STDOUT back to the teeed file. Therefore recapturing STDOUT. Given my code below, if you moved setEnvironment to above #parent process code goes here you would see all but one 'Real STDOUT' and 'Real STDERR' actually appearing in parent.log.
Options
The ideal is to remove any reliance on redirecting STDOUT / STDERR for logging. I would have a dedicated log($level, $msg) function and start to move all code to using it. Initially it's OK if it is simply a façade for the existing behaviour - you can simply switch it out when you reach an appropriate threshold of code covered.
If it's a basic script and doesn't produce stupidly large logs, why not just print everything to STDOUT with some prefix you can grep for (e.g. 'PARENT:' / 'CHILD:')?
It's a bit outside the scope of the question, but consider using a more structured approach to logging. I would consider using a CPAN logging module, e.g. Log::Log4perl. This way, the parent and children can simply request the correct log category, rather than mess around with file handles. Additional advantages:
Standardise output
Allow reconfiguration on the fly - change logging level from ERROR to DEBUG on a running-but-misbehaving system
Easily redirect output - no change is needed to your code to rearrange log files, rotate files, redirect to a socket / database instead, etc...
use strict;
use warnings;
our $g_LOGPATH = '.';
our $g_LOGFILE = "parent.log";
our #pids;
setEnvironment();
for ( 1 .. 5 ) {
my $pid = fork;
if ($pid) {
#parent process code goes here
printf "%s\n", "parent";
print OUT "Real STDOUT\n";
print ERR "Real STDERR\n";
push #pids, $pid;
next;
}
$g_LOGFILE = "child.log";
setEnvironment();
#child code goes here
printf "%s\n", "child";
exit;
}
wait for #pids;
sub setEnvironment {
unless ( open( OUT, ">&STDOUT" ) ) {
print "Cannot redirect STDOUT";
return 2;
}
unless ( open( ERR, ">&STDERR" ) ) {
print "Cannot redirect STDERR";
return 2;
}
unless ( open( STDOUT, '>>', "$g_LOGPATH/$g_LOGFILE" ) ) {
print "Cannot open log file $g_LOGPATH/$g_LOGFILE";
return 2;
}
unless ( open( STDERR, ">&STDOUT" ) ) {
print "Cannot redirect STDERR";
return 2;
}
STDOUT->autoflush(1);
}
child.log:
child
child
child
child
child
parent.log:
parent
parent
parent
parent
parent
STDOUT taken from terminal:
Real STDOUT (x5 lines)
STDERR taken from terminal:
Real STDERR (x5 lines)