| 1 | # -*-perl-*- | ||
| 2 | ########################################################################### | ||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Reaper - support for reaping child processes via $SIG{CHLD} | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Reaper qw( reaper reapPid pidStatus ); | ||
| 12 | |||
| 13 | my $pid = fork; | ||
| 14 | if ( $pid == 0 ) { # child | ||
| 15 | exec $some_command; | ||
| 16 | } | ||
| 17 | reapPid ( $pid ); | ||
| 18 | |||
| 19 | ... | ||
| 20 | |||
| 21 | if ( defined(my $exit = pidStatus($pid)) ) { | ||
| 22 | # child exited, check the code... | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | perl has an annoying little problem with child processes -- well, it | ||
| 29 | is not actually a problem specific to perl, but it is somewhat more | ||
| 30 | difficult with perl: reaping child processes after they exit so they | ||
| 31 | don't hang around as zombies forever, and doing it in a way that | ||
| 32 | accurately captures the exit code of the child. | ||
| 33 | |||
| 34 | The right way to do it is to install a $SIG{CHLD} handler which calls | ||
| 35 | waitpid to reap the child process and store $? at that point. But the | ||
| 36 | problem is that different modules may step on each other in installing | ||
| 37 | their own version of the handler, there's no uniform way of doing this. | ||
| 38 | |||
| 39 | For some situations, a local $SIG{CHLD} handler is sufficient, but | ||
| 40 | often times the handler is no longer in scope at the time the child | ||
| 41 | process exits -- since the child may exit at any time. The local | ||
| 42 | handler is dynamically scoped, not lexically, so it depends entirely | ||
| 43 | on what subroutine is being executed at the time the signal is caught. | ||
| 44 | |||
| 45 | So the Reaper module provides a $SIG{CHLD} handler that can be | ||
| 46 | installed globally as well as locally. It also supports chaining of | ||
| 47 | signal handlers, meaning it will not just replace an existing | ||
| 48 | $SIG{CHLD} handler. It still requires applications to do the right | ||
| 49 | thing in using this module and not installing their own versions. At | ||
| 50 | least it provides a consistent implementation that can be shared | ||
| 51 | between various modules. | ||
| 52 | |||
| 53 | =head1 FUNCTIONS IN DETAIL | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =cut | ||
| 58 | #' | ||
| 59 | ########################################################################### | ||
| 60 | |||
| 61 | use 5.006; | ||
| 62 | |||
| 63 | use strict; | ||
| 64 | |||
| 65 | package Reaper; | ||
| 66 | use base qw( Exporter ); | ||
| 67 | |||
| 68 | use POSIX ":sys_wait_h"; | ||
| 69 | |||
| 70 | |||
| 71 | use vars qw( $VERSION ); | ||
| 72 | $VERSION = '1.00'; | ||
| 73 | |||
| 74 | |||
| 75 | # Auto-exported symbols: | ||
| 76 | @Reaper::EXPORT = qw( ); | ||
| 77 | |||
| 78 | # Caller must request these ones: | ||
| 79 | @Reaper::EXPORT_OK = qw( reaper reapPid pidStatus ); | ||
| 80 | |||
| 81 | # Hash to track what processes have been registered for reaping. | ||
| 82 | my %REAP_PIDS = (); | ||
| 83 | |||
| 84 | # Hash to store exit status from children | ||
| 85 | # Lexically scoped so it must be accessed via pidStatus() | ||
| 86 | my %PID_STATUS = (); | ||
| 87 | |||
| 88 | |||
| 89 | sub _storePidStatus ($$) | ||
| 90 | { | ||
| 91 | my ($pid, $status) = @_; | ||
| 92 | delete $REAP_PIDS{$pid}; | ||
| 93 | $PID_STATUS{$pid} = $status; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | ###################################################################### | ||
| 98 | # Subroutine installed as the actual $SIG{CHLD} handler. | ||
| 99 | # | ||
| 100 | # Will only wait on processes registered via reapPid(). | ||
| 101 | # Other $SIG{CHLD} handlers may also have been installed, | ||
| 102 | # in which case we must chain to them so they can wait on the required | ||
| 103 | # processes. | ||
| 104 | # For this mechanism to work, any handlers installed after this one | ||
| 105 | # must also chain to this handler, and must not steal the status | ||
| 106 | # of the children this one is trying to monitor, i.e. don't do this: | ||
| 107 | # | ||
| 108 | # do { $kid = waitpid ( -1, WNOHANG ); } until $kid == -1; | ||
| 109 | # | ||
| 110 | # | ||
| 111 | ###################################################################### | ||
| 112 | my $chaining = 0; | ||
| 113 | sub REAPER | ||
| 114 | { | ||
| 115 | my ($sig, $chain) = @_; | ||
| 116 | |||
| 117 | return if ( $chaining ); # Prevent infinite recursion | ||
| 118 | |||
| 119 | # Standard mode is to only wait for requested pids, otherwise we | ||
| 120 | # still the exit status from another $SIG{CHLD} handler. | ||
| 121 | if ( my @pids = keys %REAP_PIDS ) { | ||
| 122 | foreach my $pid ( @pids ) { | ||
| 123 | my $pidwait = waitpid ( $pid, WNOHANG ); | ||
| 124 | if ( defined($pidwait) && $pidwait == $pid ) { | ||
| 125 | _storePidStatus ( $pid => $? ); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | # Chain to the next handler, if any... | ||
| 131 | if ( defined($chain) && ref($chain) eq 'CODE' ) { | ||
| 132 | local $@; | ||
| 133 | $chaining = 1; | ||
| 134 | eval { $chain-> ( $sig ); }; | ||
| 135 | $chaining = 0; | ||
| 136 | if ( $@ ) { | ||
| 137 | print STDERR ( "(in reaper) $@\n" ); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | # Must reinstall each time... | ||
| 142 | #$SIG{CHLD} = sub { Reaper::REAPER ( shift, $chain ); }; | ||
| 143 | |||
| 144 | 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | ###################################################################### | ||
| 149 | =pod | ||
| 150 | |||
| 151 | =item * reaper BLOCK | ||
| 152 | |||
| 153 | Install a local $SIG{CHLD} handler for a block of code, e.g.: | ||
| 154 | |||
| 155 | reaper { | ||
| 156 | do_something(); | ||
| 157 | ... | ||
| 158 | }; | ||
| 159 | |||
| 160 | Any children that exit while the block is being executed (whether | ||
| 161 | started within that block or not) will cause the local $SIG{CHLD} to | ||
| 162 | be executed. The child exit status will be saved, and will be | ||
| 163 | available via the pidStatus() call. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | ###################################################################### | ||
| 167 | sub reaper (&) | ||
| 168 | { | ||
| 169 | my $block = shift; | ||
| 170 | my $chain = $SIG{CHLD}; | ||
| 171 | local $SIG{CHLD} = sub { Reaper::REAPER ( shift, $chain ); }; | ||
| 172 | $block->(); | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | ###################################################################### | ||
| 177 | =pod | ||
| 178 | |||
| 179 | =item * reapPid PIDLIST | ||
| 180 | |||
| 181 | Register one or more PIDs to be reaped. The reaper will only try | ||
| 182 | to reap PIDs that have been registered, so that it does not steal | ||
| 183 | the exit status for a pid from another handler. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | ###################################################################### | ||
| 187 | sub reapPid (@) | ||
| 188 | { | ||
| 189 | my (@pids) = @_; | ||
| 190 | |||
| 191 | foreach my $pid ( @pids ) { | ||
| 192 | my $r = ref($pid); | ||
| 193 | if ( defined($r) ) { | ||
| 194 | local $@; | ||
| 195 | eval { | ||
| 196 | # special cases for @pids passed as references. | ||
| 197 | if ( $r =~ /^IO::Pipe/ ) { | ||
| 198 | $pid = ${*$pid}{'io_pipe_pid'}; # See IO/Pipe.pm | ||
| 199 | } | ||
| 200 | else { $pid = undef; } | ||
| 201 | }; | ||
| 202 | if ( $@ ) { $pid = undef; }; | ||
| 203 | } | ||
| 204 | |||
| 205 | if ( defined($pid) && $pid > 0 ) { | ||
| 206 | $REAP_PIDS{$pid} = 1; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | 1; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | ###################################################################### | ||
| 214 | =pod | ||
| 215 | |||
| 216 | =item * pidStatus PID | ||
| 217 | |||
| 218 | Return the exit status of a specific PID. If no status for the PID is | ||
| 219 | available (i.e. the process is still running), returns undef. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | ###################################################################### | ||
| 223 | sub pidStatus ($) | ||
| 224 | { | ||
| 225 | my $pid = shift; | ||
| 226 | |||
| 227 | return $PID_STATUS{$pid} if exists $PID_STATUS{$pid}; | ||
| 228 | |||
| 229 | my $pidwait = waitpid ( $pid, WNOHANG ); | ||
| 230 | if ( defined($pidwait) && $pidwait == $pid ) { | ||
| 231 | return _storePidStatus ( $pid => $? ); | ||
| 232 | } | ||
| 233 | |||
| 234 | return undef; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | ###################################################################### | ||
| 239 | # automatically install REAPER | ||
| 240 | # as the global handler | ||
| 241 | ###################################################################### | ||
| 242 | $SIG{CHLD} = sub { Reaper::REAPER ( shift, $SIG{CHLD} ); }; | ||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | ########################################################################### | ||
| 247 | # End of package | ||
| 248 | ########################################################################### | ||
| 249 | package main; | ||
| 250 | 1; | ||
| 251 | __END__ |