From 4f6b63abf59c2478968506e0e779d0992323534f Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Mon, 24 Mar 2008 10:41:16 +0000 Subject: * Switched from explicit cleanup functions to "resource acquisition by definition", implemented by the new ScopedResource class. This change improves robustness with respect to signals and unexpected errors and makes the code cleaner. git-svn-id: http://svn.openslx.org/svn/openslx/openslx/trunk@1678 95ad53e4-c205-0410-b2fa-d234c58c8868 --- lib/OpenSLX/Basics.pm | 68 +++++++-------------------------------------------- 1 file changed, 9 insertions(+), 59 deletions(-) (limited to 'lib/OpenSLX/Basics.pm') diff --git a/lib/OpenSLX/Basics.pm b/lib/OpenSLX/Basics.pm index 230bf72b..9af056e6 100644 --- a/lib/OpenSLX/Basics.pm +++ b/lib/OpenSLX/Basics.pm @@ -30,12 +30,10 @@ $VERSION = 1.01; &vlog &checkParams &instantiateClass - &addCleanupFunction &removeCleanupFunction ); our (%openslxConfig, %cmdlineConfig, %openslxPath); -use sigtrap qw( die normal-signals error-signals ); use subs qw(die warn); use open ':utf8'; @@ -139,8 +137,6 @@ my %openslxCmdlineArgs = ( 'verbose-level=i' => \$cmdlineConfig{'verbose-level'}, ); -my %cleanupFunctions; - # filehandle used for logging: my $openslxLog = *STDERR; @@ -313,27 +309,20 @@ sub callInSubprocess my $pid = fork(); if (!$pid) { - - # child... - # ...execute the given function and exit: - my $ok = eval { $childFunc->(); 1 }; - if (!$ok) { - print STDERR "*** $@"; - exit 5; - } + # child -> execute the given function and exit: + eval { $childFunc->(); 1 } + or die $@; exit 0; } - # parent... - # ...pass on interrupt- and terminate-signals to child... - local $SIG{INT} = sub { kill 'INT', $pid; waitpid($pid, 0); exit $? }; - local $SIG{TERM} = sub { kill 'TERM', $pid; waitpid($pid, 0); exit $? }; + # parent -> pass on interrupt- and terminate-signals to child ... + $SIG{INT} = sub { kill 'INT', $pid; }; + $SIG{TERM} = sub { kill 'TERM', $pid; }; - # ...and wait for child to do its work: + # ... and wait until child has done its work waitpid($pid, 0); - if ($?) { - exit $?; - } + exit $? if $?; + return; } @@ -354,36 +343,6 @@ sub executeInSubprocess return $pid; } -# ------------------------------------------------------------------------------ -sub addCleanupFunction -{ - my $name = shift; - my $func = shift; - - $cleanupFunctions{$name} = $func; - return; -} - -# ------------------------------------------------------------------------------ -sub removeCleanupFunction -{ - my $name = shift; - - delete $cleanupFunctions{$name}; - return; -} - -# ------------------------------------------------------------------------------ -sub invokeCleanupFunctions -{ - my @funcNames = keys %cleanupFunctions; - foreach my $name (@funcNames) { - vlog(2, "invoking cleanup function '$name'..."); - $cleanupFunctions{$name}->(); - } - return; -} - # ------------------------------------------------------------------------------ sub slxsystem { @@ -426,7 +385,6 @@ sub warn # ------------------------------------------------------------------------------ sub confess { - invokeCleanupFunctions(); _doThrowOrWarn('confess', @_); return; } @@ -434,7 +392,6 @@ sub confess # ------------------------------------------------------------------------------ sub croak { - invokeCleanupFunctions(); _doThrowOrWarn('croak', @_); return; } @@ -442,7 +399,6 @@ sub croak # ------------------------------------------------------------------------------ sub die { - invokeCleanupFunctions(); _doThrowOrWarn('die', @_); return; } @@ -636,10 +592,4 @@ sub instantiateClass return $class->new; } -# ------------------------------------------------------------------------------ -END -{ - invokeCleanupFunctions() if %cleanupFunctions; -} - 1; -- cgit v1.2.3-55-g7522