| 1 | # SIGCHLD without SA_RESTART to interrupt poll() is probably a bit dangerous | ||
| 2 | # on slack library code | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | # Copyright 2008, 2009, 2010, 2011, 2016 Kevin Ryde | ||
| 7 | |||
| 8 | # This file is part of Chart. | ||
| 9 | # | ||
| 10 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 11 | # terms of the GNU General Public License as published by the Free Software | ||
| 12 | # Foundation; either version 3, or (at your option) any later version. | ||
| 13 | # | ||
| 14 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 15 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 16 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 17 | # details. | ||
| 18 | # | ||
| 19 | # You should have received a copy of the GNU General Public License along | ||
| 20 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | |||
| 22 | |||
| 23 | package App::Chart::Glib::Ex::ChildPid; | ||
| 24 | use 5.008; | ||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | use Glib; | ||
| 28 | use Carp; | ||
| 29 | use Time::HiRes; | ||
| 30 | use POSIX (); | ||
| 31 | |||
| 32 | use Glib::Ex::SourceIds; | ||
| 33 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 34 | |||
| 35 | # uncomment this to run the ### lines | ||
| 36 | #use Smart::Comments; | ||
| 37 | |||
| 38 | use Glib::Object::Subclass | ||
| 39 | 'Glib::Object', | ||
| 40 | signals => { exited => { param_types => ['Glib::Int'], | ||
| 41 | return_type => undef }, | ||
| 42 | }, | ||
| 43 | properties => [ Glib::ParamSpec->int | ||
| 44 | ('pid', | ||
| 45 | 'pid', | ||
| 46 | 'Blurb.', | ||
| 47 | 0, POSIX::INT_MAX(), # range | ||
| 48 | 0, # default | ||
| 49 | Glib::G_PARAM_READWRITE), | ||
| 50 | |||
| 51 | Glib::ParamSpec->boolean | ||
| 52 | ('exited', | ||
| 53 | 'exited', | ||
| 54 | 'Blurb.', | ||
| 55 | 0, # default | ||
| 56 | Glib::G_PARAM_READWRITE), | ||
| 57 | |||
| 58 | Glib::ParamSpec->boolean | ||
| 59 | ('kill-on-destroy', | ||
| 60 | 'kill-on-destroy', | ||
| 61 | 'Blurb.', | ||
| 62 | 1, # default | ||
| 63 | Glib::G_PARAM_READWRITE), | ||
| 64 | |||
| 65 | Glib::ParamSpec->float | ||
| 66 | ('terminate-timeout', | ||
| 67 | 'terminate-timeout', | ||
| 68 | 'Blurb.', | ||
| 69 | 0, POSIX::FLT_MAX(), # range | ||
| 70 | 0, # default | ||
| 71 | Glib::G_PARAM_READWRITE), | ||
| 72 | |||
| 73 | ]; | ||
| 74 | |||
| 75 | # sub INIT_INSTANCE { | ||
| 76 | # my ($self) = @_; | ||
| 77 | # } | ||
| 78 | |||
| 79 | sub FINALIZE_INSTANCE { | ||
| 80 | my ($self) = @_; | ||
| 81 | if ($self->get('kill-on-destroy') && ! $self->{'exited'}) { | ||
| 82 | $self->kill_and_wait; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | sub SET_PROPERTY { | ||
| 87 | my ($self, $pspec, $newval) = @_; | ||
| 88 | my $pname = $pspec->get_name; | ||
| 89 | ### ChildPid set: $pname | ||
| 90 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 91 | |||
| 92 | if ($pname eq 'pid') { | ||
| 93 | my $pid = $newval; | ||
| 94 | $self->{'watch_ids'} = $pid && do { | ||
| 95 | Glib::Ex::SourceIds->new | ||
| 96 | (Glib::Child->watch_add ($pid, \&_do_child_watch, | ||
| 97 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 98 | }; | ||
| 99 | |||
| 100 | $self->{'exited'} = 0; | ||
| 101 | $self->notify ('exited'); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _do_child_watch { | ||
| 106 | my ($pid, $status, $ref_weak_self) = @_; | ||
| 107 | my $self = $$ref_weak_self || return; | ||
| 108 | $self->{'exited'} = 1; | ||
| 109 | $self->notify ('exited'); | ||
| 110 | $self->signal_emit ('exited', $status); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub kill { | ||
| 114 | my ($self, $sig) = @_; | ||
| 115 | my $pid = $self->{'pid'} || return 0; | ||
| 116 | if (! defined $sig) { $sig = 'TERM'; } | ||
| 117 | return kill ($sig, $pid); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub wait { | ||
| 121 | my ($self, $flags) = @_; | ||
| 122 | delete $self->{'watch_ids'}; | ||
| 123 | my $pid = delete $self->{'pid'} | ||
| 124 | || do { | ||
| 125 | ## no critic (RequireLocalizedPunctuationVars), it's an output | ||
| 126 | $! = POSIX::ECHILD(); | ||
| 127 | return -1; | ||
| 128 | }; | ||
| 129 | |||
| 130 | ### ChildPid wait: $pid | ||
| 131 | my $status = waitpid ($pid, $flags || 0); | ||
| 132 | if ($status != -1) { | ||
| 133 | $self->{'exited'} = 1; | ||
| 134 | $self->notify ('exited'); | ||
| 135 | $self->signal_emit ('exited', $status); | ||
| 136 | } | ||
| 137 | return $status; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub kill_and_wait { | ||
| 141 | my ($self, $sig) = @_; | ||
| 142 | my $pid = $self->{'pid'}; | ||
| 143 | if (! $pid || $self->{'exited'}) { | ||
| 144 | ## no critic (RequireLocalizedPunctuationVars), it's an output | ||
| 145 | $! = POSIX::ECHILD(); | ||
| 146 | return -1; | ||
| 147 | } | ||
| 148 | if ($self->kill($sig) != 1) { return -1; } | ||
| 149 | Time::HiRes::usleep (10000); | ||
| 150 | my $sleeps = 0; | ||
| 151 | for (;;) { | ||
| 152 | my $status = $self->wait (POSIX::WNOHANG()); | ||
| 153 | if ($status != 0) { return $status; } | ||
| 154 | if ($sleeps >= 3) { last; } | ||
| 155 | sleep 1; | ||
| 156 | $sleeps++; | ||
| 157 | } | ||
| 158 | if ($self->kill ('KILL') != 1) { return -1; } | ||
| 159 | return $self->wait; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub terminate_with_timeout { | ||
| 163 | my ($self, $sig) = @_; | ||
| 164 | my $pid = $self->{'pid'}; | ||
| 165 | if (! $pid || $self->{'exited'}) { | ||
| 166 | ## no critic (RequireLocalizedPunctuationVars), it's an output | ||
| 167 | $! = POSIX::ECHILD(); | ||
| 168 | return -1; | ||
| 169 | } | ||
| 170 | if (! defined $sig) { $sig = 'TERM'; } | ||
| 171 | $self->{'terminate_sig'} = $sig; | ||
| 172 | if ($self->kill($sig) != 1) { | ||
| 173 | return -1; | ||
| 174 | } | ||
| 175 | |||
| 176 | $self->{'terminate_ids'} = Glib::Ex::SourceIds->new | ||
| 177 | (Glib::Timeout->add ($self->get('terminate-timeout'), | ||
| 178 | \&_terminate_timeout, | ||
| 179 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 180 | return 1; | ||
| 181 | } | ||
| 182 | sub _terminate_timeout { | ||
| 183 | my ($ref_weak_self) = @_; | ||
| 184 | my $self = $$ref_weak_self || return; | ||
| 185 | my $sig = $self->{'terminate_sig'}; | ||
| 186 | |||
| 187 | if ($sig eq 'KILL' || $sig eq POSIX::SIGKILL()) { | ||
| 188 | Glib::Log->warning (__PACKAGE__, 'child did not die with SIGKILL'); | ||
| 189 | |||
| 190 | } else { | ||
| 191 | if ($sig eq 'TERM' || $sig eq POSIX::SIGTERM()) { | ||
| 192 | $sig = 'KILL'; | ||
| 193 | } else { | ||
| 194 | $sig = 'TERM'; | ||
| 195 | } | ||
| 196 | $self->terminate_with_timeout ($sig); | ||
| 197 | } | ||
| 198 | |||
| 199 | return 0; # Glib::SOURCE_REMOVE | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | __END__ |