Paranoid/Process.pm

filter: Statementstmt 58.5% 158 / 270 Branchbran 40.6% 39 / 96 Conditioncond 52.7% 19 / 36 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 76.4% 26 / 34 Podpod 100% 15 / 15 totaltotal 56.2% 267 / 475 CC 65 SCAR 36.6
CC
65
Cov
54%
switchUser
210.0
daemonize
42.0
uninstallSIGH
30.0
CRAP 39.0
« Paranoid/Network/Socket.pm ↑ Summary Paranoid/Filesystem.pm »
1 # Paranoid::Process -- Process management support for paranoid programs
2 #
3 # $Id: lib/Paranoid/Process.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Process;
33  
34 49 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 49 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 49 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 49 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 49 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 49 ▶use POSIX qw(getuid setuid setgid WNOHANG setsid);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 49 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 49 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
46  
47 49 @EXPORT = qw(switchUser daemonize);
48 49 @EXPORT_OK = (
49 @EXPORT, qw(MAXCHILDREN childrenCount installChldHandler
50 sigchld pfork pcommFork ptranslateUser ptranslateGroup pcapture
51 installSIGH uninstallSIGH installSIGD uninstallSIGD
52 ) );
53 49 %EXPORT_TAGS = (
54 all => [@EXPORT_OK],
55 misc => [qw(pcapture)],
56 pfork => [
57 qw(MAXCHILDREN childrenCount installChldHandler
58 sigchld pfork pcommFork daemonize)
59 ],
60 signal => [qw(installSIGH uninstallSIGH installSIGD uninstallSIGD)],
61 user => [qw(switchUser ptranslateUser ptranslateGroup)],
62 );
63  
64 #####################################################################
65 #
66 # Module code follows
67 #
68 #####################################################################
69  
70 {
71 49 my %signals = ( map { $_ => [] } keys %SIG );
72 49 my %original = %SIG;
73  
74 0 ▶ sub _sigHandler {
Subroutine _sigHandler: not called CC=2 SCAR=17.9
75  
76 # Purpose: Runs all code refs assigned to the signal
77 # Returns: Boolean
78 # Usage: _sigHandler($signal);
79  
80 0 my ( $signal, @sargs ) = @_;
81 0 my $sref;
82  
83 0 local $SIG{$signal} = 'IGNORE';
84 0 foreach $sref ( @{ $signals{$signal} } ) {
85 0 &$sref( $signal, @sargs );
86 }
87  
88 0 return 1;
89 }
90  
91 0 ▶ sub installSIGD {
Subroutine installSIGD: not called CC=3 SCAR=24.8
Pod documented
92  
93 # Purpose: Installs dispatcher for sig handlers that have code refs
94 # assigned
95 # Returns: Boolean
96 # Usage: $rv = installSIGD();
97  
98 0 my $sig;
99  
100 0 subPreamble(PDLEVEL1);
101  
102 0 foreach $sig ( keys %signals ) {
103 0 ▶ $SIG{$sig} = \&_sigHandler if scalar @{ $signals{$sig} };
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$signals{$sig};} 0 0 0
104 }
105  
106 0 subPostamble( PDLEVEL1, '$', 1 );
107  
108 0 return 1;
109 }
110  
111 0 ▶ sub uninstallSIGD {
Subroutine uninstallSIGD: not called CC=4 SCAR=30.0
Pod documented
112  
113 # Purpose: Uninstalls the dispatcher
114 # Returns: Boolean
115 # Usage: $rv = uninstallSIGD();
116  
117 0 subPreamble(PDLEVEL1);
118  
119 0 foreach ( keys %original ) {
120 $SIG{$_} = $original{$_}
121 if defined $SIG{$_}
122 0 ▶ and $SIG{$_} eq \&_sigHandler;
Branch: 1 branch
BranchTrueFalseTotal
if defined $SIG{$_} and $SIG{$_} eq \&Paranoid::Process::_sigHandler 0 0 0
Condition0%0/3
defined $SIG{$_} and $SIG{$_} eq \&Paranoid::Process::_sigHandler
!ll&&!rl&&r
000
MC/DC0%0/2
defined $SIG{$_} and $SIG{$_} eq \&Paranoid::Process::_sigHandler
defined $SIG{$_} $SIG{$_} eq \&Paranoid::Process::_sigHandler
Truth table0%0/3
A and B
A= defined $SIG{$_}B= $SIG{$_} eq \&Paranoid::Process::_sigHandler
ABresult
0X0
100
111
123 }
124  
125 0 subPostamble( PDLEVEL1, '$', 1 );
126  
127 0 return 1;
128 }
129  
130 0 ▶ sub installSIGH ($\&) {
Subroutine installSIGH: not called CC=3 SCAR=24.8
Pod documented
131  
132 # Purpose: Assigns a code ref to a signal array
133 # Returns: Boolean
134 # Usage: $rv = installSIGH($signal, $sref);
135  
136 0 my ( $signal, $sref ) = @_;
137 0 my $rv = 1;
138  
139 0 subPreamble( PDLEVEL1, '$\&', $signal, $sref );
140  
141 0 ▶ if ( exists $signals{$signal} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $signals{$signal}) { } 0 0 0
142 0 ▶ if ( grep { $_ eq $sref } @{ $signals{$signal} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $sref;} @{$signals{$signal};}) { } 0 0 0
143 0 pdebug( '%s handler already installed', PDLEVEL2, $signal );
144 } else {
145 0 push @{ $signals{$signal} }, $sref;
146 0 pdebug( '%s handler installed', PDLEVEL2, $signal );
147 }
148 } else {
149 0 pdebug( 'unknown signal: %s', PDLEVEL1, $signal );
150 0 $rv = 0;
151 }
152  
153 0 subPostamble( PDLEVEL1, '$', $rv );
154  
155 0 return $rv;
156 }
157  
158 0 ▶ sub uninstallSIGH ($\&) {
Subroutine uninstallSIGH: not called CC=5 SCAR=34.0
Pod documented
159  
160 # Purpose: Removes a code ref for a signal array
161 # Returns: Boolean
162 # Usage: $rv = uninstallSIGH($signal, $sref);
163  
164 0 my ( $signal, $sref ) = @_;
165 0 my $rv = 1;
166 0 my ( $sigh, $i );
167  
168 0 subPreamble( PDLEVEL1, '$\&', $signal, $sref );
169  
170 0 ▶ if ( exists $signals{$signal} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $signals{$signal}) { } 0 0 0
171 0 ▶ if ( grep { $_ eq $sref } @{ $signals{$signal} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $sref;} @{$signals{$signal};}) { } 0 0 0
172 0 $i = 0;
173 0 foreach $sigh ( @{ $signals{$signal} } ) {
174 0 ▶ if ( $sigh eq $sref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigh eq $sref) 0 0 0
175 0 splice @{ $signals{$signal} }, $i, 1;
176 0 last;
177 }
178 0 $i++;
179 }
180 0 pdebug( '%s handler removed', PDLEVEL2, $signal );
181 } else {
182 0 pdebug( 'no %s handler to remove', PDLEVEL2, $signal );
183 }
184 } else {
185 0 pdebug( 'unknown signal: %s', PDLEVEL1, $signal );
186 0 $rv = 0;
187 }
188  
189 0 subPostamble( PDLEVEL1, '$', $rv );
190  
191 0 return $rv;
192 }
193 }
194  
195 {
196 49 my $maxChildren = 0;
197 49 my $numChildren = 0;
198 49 my @forkedPIDs = ();
199 49 my $chldRef = undef;
200  
201 276 ▶ sub MAXCHILDREN : lvalue {
Subroutine MAXCHILDREN: called CC=1 SCAR=0.0
Pod documented
202  
203 # Purpose: Gets/sets $maxChildren
204 # Returns: $maxChildren
205 # Usage: $max = MAXCHILDREN;
206 # Usage: MAXCHILDREN = 20;
207  
208 276 $maxChildren;
209 }
210 560 ▶ sub childrenCount { return $numChildren }
Subroutine childrenCount: called CC=1 SCAR=0.0
Pod documented
211 221 ▶ sub _incrChildren { $numChildren++ }
Subroutine _incrChildren: called CC=1 SCAR=0.0
212 128 ▶ sub _decrChildren { $numChildren-- }
Subroutine _decrChildren: called CC=1 SCAR=0.0
213  
214 36 ▶ sub _resetChildren {
Subroutine _resetChildren: called CC=1 SCAR=0.0
215 36 @forkedPIDs = ();
216 36 $numChildren = 0;
217 }
218  
219 7 ▶ sub installChldHandler (\&) {
Subroutine installChldHandler: called CC=1 SCAR=0.0
Pod documented
220  
221 # Purpose: Installs a code reference to execute whenever a child
222 # exits
223 # Returns: True (1)
224 # Usage: installChldHandler(&foo);
225  
226 7 $chldRef = shift;
227  
228 7 return 1;
229 }
230 140 ▶ sub _chldHandler { return $chldRef }
Subroutine _chldHandler: called CC=1 SCAR=0.0
231  
232 221 ▶ sub _addPID { push @forkedPIDs, shift }
Subroutine _addPID: called CC=1 SCAR=0.0
233  
234 128 ▶ sub _grepPID {
Subroutine _grepPID: called CC=1 SCAR=0.0
235 128 my $pid = shift;
236 128 return scalar grep { $_ == $pid } @forkedPIDs;
237 }
238  
239 128 ▶ sub _delPID {
Subroutine _delPID: called CC=1 SCAR=0.0
240 128 my $pid = shift;
241 128 @forkedPIDs = grep { $_ != $pid } @forkedPIDs;
242 128 return 1;
243 }
244 }
245  
246 49 ▶sub sigchld {
Subroutine sigchld: called CC=5 SCAR=16.1
Pod documented
247  
248 # Purpose: Default signal handler for SIGCHLD
249 # Returns: True (1)
250 # Usage: $SIG{CHLD} = \&sigchld;
251  
252 140 my ($pid);
253 140 my $sref = _chldHandler();
254  
255 # Remove the signal handler so we're not preempted
256 0 ▶ local $SIG{CHLD} = sub {1};
Subroutine __ANON__: not called CC=1 SCAR=1.2
257  
258 # Process children exit values
259 140 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$pid < 1 or do { do { $pid = waitpid(-1, 1); if ($pid > 0 and _grepPID($pid)) { _decrChildren(); _delPID($pid); &pdebug("child %d reaped w/rv: %s", 9, $pid, $?); &pdebug("children remaining: %s", 9, childrenCount()); &$sref($pid, $?) if defined $sref; } } } 140 128 268
260 268 $pid = waitpid -1, WNOHANG;
261 268 ▶ if ( $pid > 0 and _grepPID($pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid > 0 and _grepPID($pid)) 128 140 268
Condition66%2/3
$pid > 0 and _grepPID($pid)
!ll&&!rl&&r
1400128
MC/DC50%1/2
$pid > 0 and _grepPID($pid)
$pid > 0 _grepPID($pid)
Truth table66%2/3
A and B
A= $pid > 0B= _grepPID($pid)
ABresult
0X0
100
111
262 128 _decrChildren();
263 128 _delPID($pid);
264 128 pdebug( 'child %d reaped w/rv: %s', PDLEVEL1, $pid, $? );
265 128 pdebug( 'children remaining: %s', PDLEVEL1, childrenCount() );
266  
267 # Call the user's sig handler if defined
268 128 ▶ &$sref( $pid, $? ) if defined $sref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sref 10 118 128
269 }
270 } until $pid < 1;
271  
272 140 return 1;
273 }
274  
275 0 ▶sub daemonize {
Subroutine daemonize: not called CC=6 SCAR=37.4
Pod documented
276  
277 # Purpose: Daemonizes process and disassociates with the terminal
278 # Returns: True unless there are errors.
279 # Usage: daemonize();
280  
281 0 my ( $rv, $pid );
282  
283 0 subPreamble(PDLEVEL1);
284  
285 0 $pid = fork;
286  
287 # Exit if we're the parent process
288 0 ▶ exit 0 if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
289  
290 0 ▶ if ( defined $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) { } 0 0 0
291  
292 # Fork was successful, close parent file descriptors
293 0 ▶ $rv = open( STDIN, '/dev/null' ) and open( STDOUT, '>/dev/null' );
Branch: 1 branch
BranchTrueFalseTotal
$rv = open(STDIN, "/dev/null") and open STDOUT, ">/dev/null" 0 0 0
294  
295 # Create a new process group
296 0 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 0 0
297 0 setsid();
298 0 $rv = open STDERR, '>&STDOUT';
299 0 ▶ croak "Can't dup stdout: $!" unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
300 0 chdir '/';
301 }
302  
303 } else {
304 0 Paranoid::ERROR =
305 pdebug( 'Failed to daemonize process: %s', PDLEVEL1, $! );
306 0 $rv = 0;
307 }
308  
309 0 subPostamble( PDLEVEL1, '$', $rv );
310  
311 0 return $rv;
312 }
313  
314 257 ▶sub pfork {
Subroutine pfork: called CC=5 SCAR=16.1
Pod documented
315  
316 # Purpose: Replacement for Perl's fork function. Blocks until a child
317 # exists if MAXCHILDREN is exceeded.
318 # Returns: Return value of children handler if installed, otherwise
319 # undef.
320 # Usage: $rv = pfork();
321  
322 257 my $max = MAXCHILDREN();
323 257 my $rv;
324  
325 257 subPreamble(PDLEVEL1);
326  
327 # Check children limits and wait, if necessary
328 257 ▶ if ($max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max) 77 180 257
329 77 ▶ while ( $max <= childrenCount() ) { sleep 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($max <= childrenCount()) 63 77 140
330 }
331  
332 # Fork and return
333 257 $rv = fork;
334 257 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 257 0 257
335 257 ▶ if ( $rv > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv > 0) { } 221 36 257
336 221 _incrChildren();
337 221 _addPID($rv);
338 } else {
339 36 _resetChildren();
340 }
341 }
342  
343 257 subPostamble( PDLEVEL1, '$', $rv );
344  
345 257 return $rv;
346 }
347  
348 2 ▶sub pcommFork (\$\$) {
Subroutine pcommFork: called CC=5 SCAR=17.2
Pod documented
349  
350 # Purpose: Creates pipes for bi-directional communiation, then calls
351 # pfork()
352 # Returns: Return value of children handler if installed, otherwise
353 # undef.
354 # Usage: $rv = pcommFork($procr, $procw);
355  
356 2 my ( $procr, $procw ) = @_;
357 2 my ( $tp, $fp, $tc, $fc, $sfd, $rv );
358  
359 2 subPreamble( PDLEVEL1, '\*\*', $procr, $procw );
360  
361 2 ▶ if ( pipe( $fc, $tp ) and pipe( $fp, $tc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pipe $fc, $tp and pipe $fp, $tc) { } 2 0 2
Condition33%1/3
pipe $fc, $tp and pipe $fp, $tc
!ll&&!rl&&r
002
MC/DC0%0/2
pipe $fc, $tp and pipe $fp, $tc
pipe $fc, $tp pipe $fp, $tc
Truth table33%1/3
A and B
A= pipe $fc, $tpB= pipe $fp, $tc
ABresult
0X0
100
111
362 2 $sfd = select $tc;
363 2 $| = 1;
364 2 select $tp;
365 2 $| = 1;
366 2 select $sfd;
367  
368 2 $rv = pfork();
369 2 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 2 0 2
370 2 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 1 1 2
371 1 close $tp;
372 1 close $fp;
373 1 $$procw = $tc;
374 1 $$procr = $fc;
375 } else {
376 1 close $tc;
377 1 close $fc;
378 1 $$procw = $tp;
379 1 $$procr = $fp;
380 }
381 } else {
382 0 $$procr = undef;
383 0 $$procw = undef;
384 0 close $tp;
385 0 close $fp;
386 0 close $tc;
387 0 close $fc;
388 }
389 } else {
390 0 pdebug( 'failed to create pipes: %s', PDLEVEL1, $! );
391 }
392  
393 2 subPostamble( PDLEVEL1, '$', $rv );
394  
395 2 return $rv;
396 }
397  
398 35 ▶sub ptranslateUser {
Subroutine ptranslateUser: called CC=8 SCAR=20.9
Pod documented
399  
400 # Purpose: Translates a string account name into the UID
401 # Returns: UID if found, undef if not
402 # Usage: $uid = ptranslateUser($user);
403  
404 35 my $user = shift;
405 35 my ( $uuid, @pwentry, $rv );
406  
407 35 subPreamble( PDLEVEL1, '$', $user );
408  
409 35 ▶ if ( defined $user and length $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user and length $user) 35 0 35
Condition33%1/3
defined $user and length $user
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table33%1/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
410  
411 35 setpwent;
412 35 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
(defined $uuid or not @pwentry) or do { do { @pwentry = getpwent; $uuid = $pwentry[2] if @pwentry and $user eq $pwentry[0] } } 35 357 392
Condition100%3/3
defined $uuid or not @pwentry
l!l&&r!l&&!r
1817357
MC/DC100%2/2
defined $uuid or not @pwentry
defined $uuid not @pwentry
Truth table100%3/3
A or not(B)
A= defined $uuidB= not @pwentry
ABresult
1X1
011
000
413 392 @pwentry = getpwent;
414 392 ▶ $uuid = $pwentry[2] if @pwentry && $user eq $pwentry[0];
Branch: 1 branch
BranchTrueFalseTotal
if @pwentry and $user eq $pwentry[0] 18 374 392
Condition100%3/3
@pwentry and $user eq $pwentry[0]
!ll&&!rl&&r
1735718
MC/DC100%2/2
@pwentry and $user eq $pwentry[0]
@pwentry $user eq $pwentry[0]
Truth table100%3/3
A and B
A= @pwentryB= $user eq $pwentry[0]
ABresult
0X0
100
111
415 } until defined $uuid || !@pwentry;
416 35 endpwent;
417 35 ▶ $rv = $uuid if defined $uuid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uuid 18 17 35
418 }
419  
420 35 subPostamble( PDLEVEL1, '$', $rv );
421  
422 35 return $rv;
423 }
424  
425 35 ▶sub ptranslateGroup {
Subroutine ptranslateGroup: called CC=8 SCAR=20.9
Pod documented
426  
427 # Purpose: Translates a string group name into the UID
428 # Returns: GID if found, undef if not
429 # Usage: $gid = ptranslateGroup($group);
430  
431 35 my $group = shift;
432 35 my ( $ugid, @pwentry, $rv );
433  
434 35 subPreamble( PDLEVEL1, '$', $group );
435  
436 35 ▶ if ( defined $group and length $group ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group and length $group) 35 0 35
Condition33%1/3
defined $group and length $group
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $group and length $group
defined $group length $group
Truth table33%1/3
A and B
A= defined $groupB= length $group
ABresult
0X0
100
111
437  
438 35 setgrent;
439 35 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
(defined $ugid or not @pwentry) or do { do { @pwentry = getgrent; $ugid = $pwentry[2] if @pwentry and $group eq $pwentry[0] } } 35 720 755
Condition100%3/3
defined $ugid or not @pwentry
l!l&&r!l&&!r
1718720
MC/DC100%2/2
defined $ugid or not @pwentry
defined $ugid not @pwentry
Truth table100%3/3
A or not(B)
A= defined $ugidB= not @pwentry
ABresult
1X1
011
000
440 755 @pwentry = getgrent;
441 755 ▶ $ugid = $pwentry[2] if @pwentry && $group eq $pwentry[0];
Branch: 1 branch
BranchTrueFalseTotal
if @pwentry and $group eq $pwentry[0] 17 738 755
Condition100%3/3
@pwentry and $group eq $pwentry[0]
!ll&&!rl&&r
1872017
MC/DC100%2/2
@pwentry and $group eq $pwentry[0]
@pwentry $group eq $pwentry[0]
Truth table100%3/3
A and B
A= @pwentryB= $group eq $pwentry[0]
ABresult
0X0
100
111
442 } until defined $ugid || !@pwentry;
443 35 endgrent;
444 35 ▶ $rv = $ugid if defined $ugid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ugid 17 18 35
445 }
446  
447 35 subPostamble( PDLEVEL1, '$', $rv );
448  
449 35 return $rv;
450 }
451  
452 0 ▶sub switchUser {
Subroutine switchUser: not called CC=14 SCAR=53.5
Pod documented
453  
454 # Purpose: Switches to the user/group specified
455 # Returns: True (1) if successful, False (0) if not
456 # Usage: $rv = swithUser($user);
457 # Usage: $rv = swithUser($user, $group);
458  
459 0 my $user = shift;
460 0 my $group = shift;
461 0 my $rv = 1;
462 0 my ( @pwentry, $duid, $dgid );
463  
464 0 subPreamble( PDLEVEL1, '$;$', $user, $group );
465  
466 # Validate arguments
467 0 ▶ croak 'Mandatory argument of either user or group must be passed'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user or defined $group 0 0 0
Condition0%0/3
defined $user or defined $group
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $user or defined $group
defined $user defined $group
Truth table0%0/3
A or B
A= defined $userB= defined $group
ABresult
1X1
011
000
468 unless defined $user || defined $group;
469  
470 # First switch the group
471 0 ▶ if ( defined $group ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group) 0 0 0
472  
473 # Look up named group
474 0 ▶ unless ( $group =~ /^\d+$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group =~ /^\d+$/s) 0 0 0
475 0 $dgid = ptranslateGroup($group);
476 0 ▶ unless ( defined $dgid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dgid) 0 0 0
477 0 Paranoid::ERROR = pdebug( 'couldn\'t identify group (%s)',
478 PDLEVEL1, $group );
479 0 $rv = 0;
480 }
481 }
482  
483 # Switch to group
484 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
485 0 pdebug( 'switching to GID %s', PDLEVEL2, $dgid );
486 0 ▶ unless ( setgid($dgid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (setgid($dgid)) 0 0 0
487 0 Paranoid::ERROR =
488 pdebug( 'couldn\'t switch to group (%s): %s',
489 PDLEVEL1, $group, $! );
490 0 $rv = 0;
491 }
492 }
493 }
494  
495 # Second, switch the user
496 0 ▶ if ( $rv && defined $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and defined $user) 0 0 0
Condition0%0/3
$rv and defined $user
!ll&&!rl&&r
000
MC/DC0%0/2
$rv and defined $user
$rv defined $user
Truth table0%0/3
A and B
A= $rvB= defined $user
ABresult
0X0
100
111
497  
498 # Look up named user
499 0 ▶ unless ( $user =~ /^\d+$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user =~ /^\d+$/s) 0 0 0
500 0 $duid = ptranslateUser($user);
501 0 ▶ unless ( defined $duid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $duid) 0 0 0
502 0 Paranoid::ERROR =
503 pdebug( 'couldn\'t identify user (%s)', PDLEVEL1, $user );
504 0 $rv = 0;
505 }
506 }
507  
508 # Switch to user
509 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
510 0 pdebug( 'switching to UID %s', PDLEVEL2, $duid );
511 0 ▶ unless ( setuid($duid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (setuid($duid)) 0 0 0
512 0 Paranoid::ERROR = pdebug( 'couldn\'t switch to user (%s): %s',
513 PDLEVEL1, $user, $! );
514 0 $rv = 0;
515 }
516 }
517 }
518  
519 0 subPostamble( PDLEVEL1, '$', $rv );
520  
521 0 return $rv;
522 }
523  
524 4 ▶sub pcapture ($\$\$) {
Subroutine pcapture: called CC=9 SCAR=22.7
Pod documented
525  
526 # Purpose: Captures the output and exit code of the specified shell
527 # command. Output incorporates STDERR via redirection.
528 # Returns: True (1) if command exits cleanly, False (0) otherwise
529 # Usage: $rv = pcapture($cmd, $crv, $out);
530  
531 4 my $cmd = shift;
532 4 my $cref = shift;
533 4 my $oref = shift;
534 4 my $rv = -1;
535 4 my ( $sigchld, $cored, $signal );
536  
537 4 subPreamble( PDLEVEL1, '$\$\$', $cmd, $cref, $oref );
538 4 pdebug( 'entering w/(%s)(%s)(%s)', PDLEVEL1, $cmd, $cref, $oref );
539 4 pIn();
540  
541 4 ▶ if ( defined $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd) 4 0 4
542  
543 # Massage the command string
544 4 $cmd = "( $cmd ) 2>&1";
545  
546 # Backup SIGCHLD handler and set it to something safe
547 4 ▶ if ( defined $SIG{CHLD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $SIG{"CHLD"}) 4 0 4
548 4 $sigchld = $SIG{CHLD};
549 4 ▶ $SIG{CHLD} = sub {1};
Subroutine __ANON__: called CC=1 SCAR=0.0
550 }
551  
552 # Execute and snarf the output
553 4 pdebug( 'executing command', PDLEVEL2 );
554 4 $$oref = `$cmd`;
555 4 $$cref = $?;
556 4 $cored = $$cref & 128;
557 4 $signal = $$cref & 127;
558 4 pdebug( 'command exited with raw rv: %s', PDLEVEL2, $$cref );
559  
560 # Restore SIGCHLD handler
561 4 ▶ $SIG{CHLD} = $sigchld if defined $SIG{CHLD};
Branch: 1 branch
BranchTrueFalseTotal
if defined $SIG{"CHLD"} 4 0 4
562  
563 # Check the return value
564 4 ▶ if ( $$cref == -1 or $$cref == 32512 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$cref == -1 or $$cref == 32512) { } 1 3 4
elsif ($signal) { } 0 3 3
Condition66%2/3
$$cref == -1 or $$cref == 32512
l!l&&r!l&&!r
013
MC/DC50%1/2
$$cref == -1 or $$cref == 32512
$$cref == -1 $$cref == 32512
Truth table66%2/3
A or B
A= $$cref == -1B= $$cref == 32512
ABresult
1X1
011
000
565  
566 # Command failed to execute
567 1 Paranoid::ERROR =
568 pdebug( 'command failed to execute: %s', PDLEVEL1, $! );
569 1 $rv = -1;
570  
571 } elsif ($signal) {
572  
573 # Exited with signal (and core?)
574 0 Paranoid::ERROR =
575 pdebug( 'command died with signal: %s', PDLEVEL1, $signal );
576 0 ▶ pdebug( "command exited with core dump", PDLEVEL1 ) if $cored;
Branch: 1 branch
BranchTrueFalseTotal
if $cored 0 0 0
577 0 $rv = -1;
578  
579 } else {
580  
581 # Command exited normally
582 3 $$cref >>= 8;
583 3 ▶ $rv = $$cref == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$cref == 0 ? : 1 2 3
584 3 pdebug( 'command exited with rv: %s', PDLEVEL1, $$cref );
585 }
586  
587 4 subPostamble( PDLEVEL1, '$', $rv );
588  
589 4 return $rv;
590 }
591 }
592  
593 49 1;
594  
595 __END__
« Paranoid/Network/Socket.pm ↑ Summary Paranoid/Filesystem.pm »