| 1 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Proc::ChildPid; | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use Carp; | ||
| 21 | use Time::HiRes; | ||
| 22 | use POSIX qw(WNOHANG ECHILD); | ||
| 23 | |||
| 24 | # uncomment this to run the ### lines | ||
| 25 | #use Smart::Comments; | ||
| 26 | |||
| 27 | |||
| 28 | sub new { | ||
| 29 | my ($class, $pid) = @_; | ||
| 30 | return bless { pid => $pid }, $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub DESTROY { | ||
| 34 | my ($self) = @_; | ||
| 35 | ## no critic (RequireInitializationForLocalVars) | ||
| 36 | local $?; # don't let waitpid() clobber prospective exit code during exit() | ||
| 37 | |||
| 38 | ### ChildPid DESTROY | ||
| 39 | $self->kill_and_wait ('TERM'); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub pid { | ||
| 43 | my ($self) = @_; | ||
| 44 | return $self->{'pid'}; | ||
| 45 | } | ||
| 46 | |||
| 47 | # sub poll { | ||
| 48 | # my ($self) = @_; | ||
| 49 | # return $self->kill(0) == 1; | ||
| 50 | # } | ||
| 51 | |||
| 52 | sub kill_and_wait { | ||
| 53 | my ($self, $sig) = @_; | ||
| 54 | if (! $self->{'pid'}) { | ||
| 55 | $! = ECHILD; | ||
| 56 | return -1; | ||
| 57 | } | ||
| 58 | if ($self->kill ($sig) != 1) { return -1; } | ||
| 59 | Time::HiRes::usleep (10000); | ||
| 60 | my $sleeps = 0; | ||
| 61 | for (;;) { | ||
| 62 | my $status = $self->wait (WNOHANG); | ||
| 63 | if ($status != 0) { return $status; } | ||
| 64 | if ($sleeps >= 3) { last; } | ||
| 65 | sleep 1; | ||
| 66 | $sleeps++; | ||
| 67 | } | ||
| 68 | if ($self->kill ('KILL') != 1) { return -1; } | ||
| 69 | return $self->wait; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub kill { | ||
| 73 | my ($self, $sig) = @_; | ||
| 74 | my $pid = $self->{'pid'} || return 0; | ||
| 75 | if (! defined $sig) { $sig = 'TERM'; } | ||
| 76 | return kill ($sig, $pid); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub wait { | ||
| 80 | my ($self, $flags) = @_; | ||
| 81 | my $pid = delete $self->{'pid'} | ||
| 82 | || do { $! = ECHILD; return -1; }; | ||
| 83 | if (! defined $flags) { $flags = 0; } | ||
| 84 | ### ChildPid wait: $pid | ||
| 85 | return waitpid ($pid, $flags); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ | ||
| 90 | |||
| 91 | =head1 NAME | ||
| 92 | |||
| 93 | App::Chart::Proc::ChildPid -- child subprocess pid object | ||
| 94 | |||
| 95 | =for test_synopsis my ($pid) | ||
| 96 | |||
| 97 | =head1 SYNOPSIS | ||
| 98 | |||
| 99 | use App::Chart::Proc::ChildPid; | ||
| 100 | my $cp = App::Chart::Proc::ChildPid->new ($pid); | ||
| 101 | $cp->kill_and_wait; | ||
| 102 | |||
| 103 | =head1 DESCRIPTION | ||
| 104 | |||
| 105 | C<App::Chart::Proc::ChildPid> keeps hold of a process ID which is a child of the | ||
| 106 | current process. If the ChildPid object is destroyed the child is killed | ||
| 107 | and waited, thus protecting against creation of zombies. | ||
| 108 | |||
| 109 | =head1 FUNCTIONS | ||
| 110 | |||
| 111 | =over 4 | ||
| 112 | |||
| 113 | =item C<< App::Chart::Proc::ChildPid->new ($pid) >> | ||
| 114 | |||
| 115 | Create and return a new ChildPid holding process ID C<$pid>. There's no | ||
| 116 | check that C<$pid> is actually a child of the current process. | ||
| 117 | |||
| 118 | =item C<< $cp->kill () >> | ||
| 119 | |||
| 120 | =item C<< $cp->kill ($sig) >> | ||
| 121 | |||
| 122 | Do a C<kill> on the child process, sending it C<SIGTERM> or the given signal | ||
| 123 | C<$sig> (a signal name or number). The return is as per the core C<kill> | ||
| 124 | function, ie. 1 if successful, or 0 if no processes signalled (because the | ||
| 125 | child has been waited). | ||
| 126 | |||
| 127 | =item C<< $cp->wait() >> | ||
| 128 | |||
| 129 | Do a C<wait> on the child process and return its exit status. If the child | ||
| 130 | has already been waited the return is -1 with C<$!> set to C<ECHILD> (no | ||
| 131 | such child). | ||
| 132 | |||
| 133 | =item C<< $cp->kill_and_wait() >> | ||
| 134 | |||
| 135 | Do a C<kill> and C<wait> combination on the child process and return its | ||
| 136 | exit status. A C<SIGTERM> is sent first, and if that doesn't kill the | ||
| 137 | process after a few seconds a C<SIGKILL> is sent (which it can't ignore). | ||
| 138 | |||
| 139 | If the child has already been waited the return is -1 with C<$!> set to | ||
| 140 | C<ECHILD> (no such child). | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | L<App::Chart::Gtk2::Subprocess> | ||
| 147 | |||
| 148 | =cut |