blib/lib/XAS/Lib/Process/Unix.pm

filter: Statementstmt 12.7% 17 / 133 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 5 / 15 Podpod 100% 7 / 7 totaltotal 13.8% 29 / 210 CC 27 SCAR 34.7
CC
27
Cov
9%
stat_process
72.0
start_process
56.0
pause_process
30.0
CRAP 32.1
« blib/lib/XAS/Lib/Stomp/Utils.pm ↑ Summary blib/lib/XAS/Lib/Net/Client.pm »
1 package XAS::Lib::Process::Unix;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX qw(setsid);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use XAS::Class
11 1 debug => 0,
12 version => $VERSION,
13 base => 'XAS::Base',
14 utils => ':env dotid compress run_cmd trim',
15 mixins => 'start_process stop_process pause_process resume_process
16 stat_process kill_process init_process _parse_command
17 _poll_child RUNNING STOPPED PAUSED SHUTDOWN',
18 constant => {
19 RUNNING => 0,
20 STOPPED => 1,
21 PAUSED => 2,
22 SHUTDOWN => 3,
23 }
24 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # ----------------------------------------------------------------------
27 # Public Methods
28 # ----------------------------------------------------------------------
29  
30 sub start_process {
31 0 ▶ my $self = shift;
Subroutine start_process: not called CC=7 SCAR=40.3
Pod documented
32  
33 0 my $pid;
34 0 my $alias = $self->alias;
35 0 my $umask = oct($self->umask);
36 0 my $env = $self->environment;
37 0 my @args = $self->_parse_command;
38 0 my $priority = $self->priority;
39 0 my $uid = getpwnam($self->user);
40 0 my $gid = getgrnam($self->group);
41 0 my $directory = $self->directory->path;
42  
43 0 $self->log->debug("$alias: command @args");
44  
45 my $spawn = sub {
46  
47 0 ▶ setsid(); # become a session lead
Subroutine __ANON__: not called CC=1 SCAR=6.9
48  
49 0 eval { # set priority, fail silently
50 0 my $p = getpriority(0, $$);
51 0 setpriority(0, $$, $p + $priority);
52 };
53  
54 0 $( = $) = $gid; # set new group id
55 0 $< = $> = $uid; # set new user id
56  
57 0 env_create($env); # create the new environment
58  
59 0 chdir($directory); # change directory
60 0 umask($umask); # set protection mask
61 0 exec(@args); # become a new process
62  
63 0 exit 0;
64  
65 0 };
66  
67 # save the current environment
68  
69 0 my $oldenv = env_store();
70  
71 0 ▶ if ($self->redirect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->redirect) { } 0 0 0
72  
73 0 my $child;
74 my $parent;
75  
76 # create a socket pair
77  
78 0 ▶ unless (($child, $parent) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = "IO::Socket"->socketpair(1, 1, 0)) 0 0 0
79  
80 0 $self->throw_msg(
81 dotid($self->class) . '.start_process.socketpair',
82 'unexpected',
83 "unable to create a socketpair, reason: $!"
84 );
85  
86 }
87  
88 0 ▶ unless ($pid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) { } 0 0 0
89  
90 # child
91  
92 0 ▶ unless (defined($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
93  
94 0 $self->throw_msg(
95 dotid($self->class) . '.start_process.creation',
96 'unexpected',
97 'unable to spawn a new process',
98 );
99  
100 }
101  
102 # close unneeded fd's
103  
104 0 close($child);
105  
106 # redirect stdin, stdout and stderr to the parent socket
107 # with stderr combined with stdout
108  
109 0 open(STDIN, '<&', $parent);
110 0 open(STDOUT, '>&', $parent);
111 0 open(STDERR, '>&', $parent);
112  
113 # not needed any longer
114  
115 0 close($parent);
116  
117 0 $spawn->();
118  
119 } else {
120  
121 # parent
122  
123 # close unneeded fd's
124  
125 0 close($parent);
126  
127 # listen on the child socket
128  
129 0 $self->input_handle($child);
130 0 $self->output_handle($child);
131  
132 # setup POE's I/O handling
133  
134 0 $self->_process_output();
135 0 $self->_process_input();
136  
137 }
138  
139 } else {
140  
141 0 ▶ unless ($pid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid = fork) 0 0 0
142  
143 # child
144  
145 0 ▶ unless (defined($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
146  
147 0 $self->throw_msg(
148 dotid($self->class) . '.start_process.creation',
149 'unexpected',
150 'unable to spawn a new process',
151 );
152  
153 }
154  
155 # redirect the standard file handles to dev null
156  
157 0 open(STDIN, '<', '/dev/null');
158 0 open(STDOUT, '>', '/dev/null');
159 0 open(STDERR, '>', '/dev/null');
160  
161 0 $spawn->();
162  
163 }
164  
165 }
166  
167 0 $self->status(RUNNING);
168  
169 0 $poe_kernel->sig_child($pid, 'poll_child');
170 0 $self->{pid} = $pid;
171  
172 0 $self->log->info_msg('process_started', $alias, $self->pid);
173  
174 # recover the old environment
175  
176 0 env_restore($oldenv);
177  
178 }
179  
180 sub stat_process {
181 0 ▶ my $self = shift;
Subroutine stat_process: not called CC=8 SCAR=42.8
Pod documented
182  
183 0 my $stat = 0;
184  
185 0 ▶ if (my $pid = $self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->pid) 0 0 0
186  
187 0 my $cmd = "ps -p $pid -o state=";
188 0 my (@output, $rc) = run_cmd($cmd);
189  
190 0 ▶ if ($rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == 0) 0 0 0
191  
192 0 my $line = trim($output[0]);
193  
194 # UNIX states
195 # from man ps
196 #
197 # D Uninterruptible sleep (usually IO)
198 # R Running or runnable (on run queue)
199 # S Interruptible sleep (waiting for an event to complete)
200 # T Stopped, either by a job control signal or because it
201 # is being traced.
202 # W paging (not valid since the 2.6.xx kernel)
203 # X dead (should never be seen)
204 # Z Defunct ("zombie") process, terminated but not reaped
205 # by its parent.
206  
207 0 ▶ $stat = 6 if ($line eq 'T'); # suspended ready
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "T" 0 0 0
208 0 ▶ $stat = 5 if ($line eq 'D'); # suspended blocked
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "D" 0 0 0
209 # $stat = 4 if ($line eq '?'); # blocked
210 0 ▶ $stat = 3 if ($line eq 'R'); # running
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "R" 0 0 0
211 0 ▶ $stat = 2 if ($line eq 'S'); # ready
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "S" 0 0 0
212 0 ▶ $stat = 1 if ($line eq 'Z'); # other
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "Z" 0 0 0
213  
214 }
215  
216 }
217  
218 0 return $stat;
219  
220 }
221  
222 sub pause_process {
223 0 ▶ my $self = shift;
Subroutine pause_process: not called CC=5 SCAR=34.0
Pod documented
224  
225 0 my $alias = $self->alias;
226  
227 0 ▶ if ($self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid) 0 0 0
228  
229 0 my $pid = ($self->pid * -1);
230 0 my $code = $self->stat_process();
231  
232 0 ▶ if (($code == 3) || ($code == 2)) { # process is running or ready
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 3 or $code == 2) 0 0 0
Condition0%0/3
$code == 3 or $code == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$code == 3 or $code == 2
$code == 3 $code == 2
Truth table0%0/3
A or B
A= $code == 3B= $code == 2
ABresult
1X1
011
000
233  
234 0 ▶ if (kill('TSTP', $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill "TSTP", $pid) 0 0 0
235  
236 0 $self->status(PAUSED);
237 0 $self->log->warn_msg('process_paused', $alias, $self->pid);
238  
239 }
240  
241 }
242  
243 }
244  
245 }
246  
247 sub resume_process {
248 0 ▶ my $self = shift;
Subroutine resume_process: not called CC=4 SCAR=30.0
Pod documented
249  
250 0 my $alias = $self->alias;
251  
252 0 ▶ if ($self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid) 0 0 0
253  
254 0 my $pid = ($self->pid * -1);
255 0 my $code = $self->stat_process();
256  
257 0 ▶ unless ($code == 6) { # process is suspended ready
Branch: 1 branch
BranchTrueFalseTotal
unless ($code == 6) 0 0 0
258  
259 0 ▶ if (kill('CONT', $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill "CONT", $pid) 0 0 0
260  
261 0 $self->status(RUNNING);
262 0 $self->log->warn_msg('process_started', $alias, $self->pid);
263  
264 }
265  
266 }
267  
268 }
269  
270 }
271  
272 sub stop_process {
273 0 ▶ my $self = shift;
Subroutine stop_process: not called CC=3 SCAR=24.8
Pod documented
274  
275 0 my $alias = $self->alias;
276  
277 0 ▶ if ($self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid) 0 0 0
278  
279 0 my $pid = ($self->pid * -1);
280  
281 0 ▶ if (kill('TERM', $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill "TERM", $pid) 0 0 0
282  
283 0 $self->status(STOPPED);
284 0 $self->retries(0);
285 0 $self->log->warn_msg('process_stopped', $alias, $self->pid);
286  
287 }
288  
289 }
290  
291 }
292  
293 sub kill_process {
294 0 ▶ my $self = shift;
Subroutine kill_process: not called CC=3 SCAR=24.8
Pod documented
295  
296 0 my $alias = $self->alias;
297  
298 0 ▶ if ($self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid) 0 0 0
299  
300 0 my $pid = ($self->pid * -1);
301  
302 0 ▶ if (kill('KILL', $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill "KILL", $pid) 0 0 0
303  
304 0 $self->status(STOPPED);
305 0 $self->retries(0);
306 0 $self->log->warn_msg('process_killed', $alias, $self->pid);
307  
308 }
309  
310 }
311  
312 }
313  
314 sub init_process {
315 0 ▶ my $self = shift;
Subroutine init_process: not called CC=1 SCAR=6.9
Pod documented
316  
317 }
318  
319 # ----------------------------------------------------------------------
320 # Private Events
321 # ----------------------------------------------------------------------
322  
323 sub _poll_child {
324 0 ▶ my ($self, $signal, $pid, $exitcode) = @_[OBJECT,ARG0,ARG1,ARG2];
Subroutine _poll_child: not called CC=1 SCAR=6.9
325  
326 0 my $alias = $self->alias;
327  
328 0 $self->log->debug("$alias: entering poll_child");
329  
330 0 $self->status(STOPPED);
331  
332 # notify 'child_exit' that we are done
333  
334 0 $poe_kernel->post($alias, 'child_exit', 'CHLD', $pid, $exitcode);
335  
336 }
337  
338 # ----------------------------------------------------------------------
339 # Private Methods
340 # ----------------------------------------------------------------------
341  
342 sub _parse_command {
343 0 ▶ my $self = shift;
Subroutine _parse_command: not called CC=3 SCAR=24.8
344  
345 0 my @args = split(' ', $self->command);
346  
347 0 my @extensions = ('');
348 0 my @path = split(':', $ENV{PATH});
349 0 my $is_absolute_re = '^/';
350 0 my $has_dir_element_re = "/";
351
352 # Stolen from Proc::Background
353 #
354 # If there is only one element in the @args array, then it may be a
355 # command to be passed to the shell and should not be checked, in
356 # case the command sets environmental variables in the beginning,
357 # i.e. 'VAR=arg ls -l'. If there is more than one element in the
358 # array, then check that the first element is a valid executable
359 # that can be found through the PATH and find the absolute path to
360 # the executable. If the executable is found, then replace the
361 # first element it with the absolute path.
362  
363 0 ▶ if (scalar(@args) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 1) 0 0 0
364  
365 0 ▶ $args[0] = $self->_resolve_path($args[0], $is_absolute_re, $has_dir_element_re, \@extensions, \@path) or return;
Branch: 1 branch
BranchTrueFalseTotal
$args[0] = $self->_resolve_path($args[0], $is_absolute_re, $has_dir_element_re, \@extensions, \@path) or return 0 0 0
366  
367 }
368  
369 0 return @args;
370  
371 }
372  
373 1 1;
374  
375 __END__
376  
377 =head1 NAME
378  
379 XAS::Lib::Process::Unix - A mixin class for process management within the XAS environment
380  
381 =head1 DESCRIPTION
382  
383 This module is a mixin class to handle the needs for process management
384 under a Unix like system.
385  
386 =head1 METHODS
387  
388 =head2 init_process
389  
390 This method initializes the module so that it can function properly.
391  
392 =head2 start_process
393  
394 This method does the neccessary things to spawn a new process.
395  
396 =head2 stat_process
397  
398 This method returns the status of the process. These are the possible
399 values.
400  
401 =over 4
402  
403 =item B<6>
404  
405 This status indicates that the process is stopped, either by a
406 job control signal or it is being traced.
407  
408 =item B<5>
409  
410 This status indicates that the process is in a uninterruptible sleep,
411 usually waiting for I/O.
412  
413 =item B<4>
414  
415 Not implemented on Unix.
416  
417 =item B<3>
418  
419 This status indicates that the process is running or runnable.
420  
421 =item B<2>
422  
423 This indicates that the process is in a interruptible sleep, waiting for
424 an event to complete.
425  
426 =item B<1>
427  
428 This indicates that the process is a zombie. Terminated but not yet
429 reaped.
430  
431 =item B<0>
432  
433 This indicates that the process is in an unknown state.
434  
435 =back
436  
437 =head2 stop_process
438  
439 This method will send a 'TERM' signal to the process.
440  
441 =head2 pause_process
442  
443 This method will send a 'TSTP' signal to the process.
444  
445 =head2 resume_process
446  
447 This method will send a 'CONT' signal to the process.
448  
449 =head2 kill_process
450  
451 This method will send a 'KILL' signal to the process.
452  
453 =head1 SEE ALSO
454  
455 =over 4
456  
457 =item L<XAS::Lib::Process|XAS::Lib::Process>
458  
459 =item L<XAS|XAS>
460  
461 =back
462  
463 =head1 AUTHOR
464  
465 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
466  
467 =head1 COPYRIGHT AND LICENSE
468  
469 Copyright (C) 2015 Kevin L. Esteb
470  
471 This is free software; you can redistribute it and/or modify it under
472 the terms of the Artistic License 2.0. For details, see the full text
473 of the license at http://www.perlfoundation.org/artistic_license_2_0.
474  
475 =cut
« blib/lib/XAS/Lib/Stomp/Utils.pm ↑ Summary blib/lib/XAS/Lib/Net/Client.pm »