App/Chart/Glib/Ex/ChildPid.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 191 CC 15 SCAR 26.2
CC
15
Cov
0%
kill_and_wait
42.0
terminate_with_timeout
20.0
_terminate_timeout
12.0
CRAP 13.7
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__
204  
205 =for stopwords subprocess SIGTERM boolean ChildPid SIGSTOP SIGTSTP
206  
207 =head1 NAME
208  
209 App::Chart::Glib::Ex::ChildPid -- object holding child process ID
210  
211 =for test_synopsis my ($pid)
212  
213 =head1 SYNOPSIS
214  
215 use App::Chart::Glib::Ex::ChildPid;
216 my $pobj = App::Chart::Glib::Ex::ChildPid->new (pid => $pid);
217  
218 $pobj->kill ('INT');
219 $pobj->terminate_with_timeout;
220  
221 =head1 DESCRIPTION
222  
223 A C<App::Chart::Glib::Ex::ChildPid> holds the process ID (an integer) for a child
224 process created by C<fork> or similar spawn.
225  
226 The fork and subprocess emulation described in L<perlfork> doesn't work with
227 Glib process watching (as of Perl-Glib 1.200) and cannot be used with
228 C<App::Chart::Glib::Ex::ChildPid>.
229  
230 =head1 FUNCTIONS
231  
232 =over 4
233  
234 =item C<< $pobj->kill () >>
235  
236 =item C<< $pobj->kill ($sig) >>
237  
238 Send signal C<$sig> to the C<$pobj> process. C<$sig> defaults to SIGTERM,
239 otherwise it's a string or number per the core C<kill> function (see
240 L<perlfunc/kill>).
241  
242 =item C<< $pobj->wait () >>
243  
244 =item C<< $pobj->wait ($flags) >>
245  
246 Block and wait for the C<$pobj> process to terminate, as per the core
247 C<waitpid>. The C<exited> signal is also emitted in the usual way if it
248 terminates.
249  
250 =back
251  
252 =head1 PROPERTIES
253  
254 =over 4
255  
256 =item C<pid> (integer, default 0)
257  
258 The process ID held and operated on. It must be an immediate child of the
259 current process, but can be started by either C<fork> or any of the several
260 Perl fork/spawn/open modules.
261  
262 =item C<exited> (boolean, default false)
263  
264 The process ID held and operated on. It must be an immediate child of the
265 current process, but can be started by either C<fork> or any of the several
266 Perl fork/spawn/open modules.
267  
268 =item C<kill-on-destroy> (boolean, default true)
269  
270 If true then when the ChildPid object is destroyed through garbage
271 collection the process is killed and waited if that hasn't already been
272 done.
273  
274 This is true by default to ensure the child process stops when the parent
275 stops, or when you discard the object as no longer wanted.
276  
277 =back
278  
279 =head1 SIGNALS
280  
281 =over 4
282  
283 =item C<exited ($status)>
284  
285 Emitted when the child process exits, either normally or killed by a signal,
286 but not when merely paused by SIGSTOP, SIGTSTP, etc.
287  
288 This is implemented by a C<< Glib::Child->add_watch >> (see
289 L<Glib::MainLoop>). Note that function uses C<SIGCHLD> and if you install
290 your own C<$SIG{'CHLD'}> handler then C<exited> won't run.
291  
292 =back
293  
294 =head1 SEE ALSO
295  
296 L<Glib::MainLoop>, L<IPC::Open3>, L<Proc::SyncExec>
297  
298 =head1 HOME PAGE
299  
300 L<http://user42.tuxfamily.org/chart/index.html>
301  
302 =head1 LICENCE
303  
304 Copyright 2008, 2009, 2010, 2011, 2016 Kevin Ryde
305  
306 Chart is free software; you can redistribute it and/or modify it under the
307 terms of the GNU General Public License as published by the Free Software
308 Foundation; either version 3, or (at your option) any later version.
309  
310 Chart is distributed in the hope that it will be useful, but WITHOUT ANY
311 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
312 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
313 details.
314  
315 You should have received a copy of the GNU General Public License along with
316 Chart; see the file F<COPYING>. Failing that, see
317 L<http://www.gnu.org/licenses/>.
318  
319 =cut