File Coverage

blib/lib/Proc/Daemon.pm
Criterion Covered Total %
statement 150 188 79.7
branch 72 136 52.9
condition 22 56 39.2
subroutine 12 12 100.0
pod 10 10 100.0
total 266 402 66.1


line stmt bran cond sub pod time code
1             ################################################################################
2             ## File:
3             ## Daemon.pm
4             ## Authors:
5             ## Earl Hood earl@earlhood.com
6             ## Detlef Pilzecker deti@cpan.org
7             ## Pavel Denisov akreal@cpan.org
8             ## Description:
9             ## Run Perl program(s) as a daemon process, see docs in the Daemon.pod file
10             ################################################################################
11             ## Copyright (C) 1997-2015 by Earl Hood, Detlef Pilzecker and Pavel Denisov.
12             ##
13             ## All rights reserved.
14             ##
15             ## This module is free software. It may be used, redistributed and/or modified
16             ## under the same terms as Perl itself.
17             ################################################################################
18              
19              
20             package Proc::Daemon;
21              
22 11     11   279995 use strict;
  11         42  
  11         308  
23 11     11   8902 use POSIX();
  11         81050  
  11         28277  
24              
25             $Proc::Daemon::VERSION = '0.22';
26              
27              
28             ################################################################################
29             # Create the Daemon object:
30             # my $daemon = Proc::Daemon->new( [ %Daemon_Settings ] )
31             #
32             # %Daemon_Settings are hash key=>values and can be:
33             # work_dir => '/working/daemon/directory' -> defaults to '/'
34             # setgid => 12345 -> defaults to
35             # setuid => 12345 -> defaults to
36             # child_STDIN => '/path/to/daemon/STDIN.file' -> defautls to '
37             # child_STDOUT => '/path/to/daemon/STDOUT.file' -> defaults to '+>/dev/null'
38             # child_STDERR => '/path/to/daemon/STDERR.file' -> defaults to '+>/dev/null'
39             # dont_close_fh => [ 'main::DATA', 'PackageName::DATA', 'STDOUT', ... ]
40             # -> arrayref with file handles you do not want to be closed in the daemon.
41             # dont_close_fd => [ 5, 8, ... ] -> arrayref with file
42             # descriptors you do not want to be closed in the daemon.
43             # pid_file => '/path/to/pid/file.txt' -> defaults to
44             # undef (= write no file).
45             # file_umask => 022 -> defaults to 066
46             # exec_command => 'perl /home/script.pl' -> execute a system command
47             # via Perls *exec PROGRAM* at the end of the Init routine and never return.
48             # Must be an arrayref if you want to create several daemons at once.
49             #
50             # Returns: the blessed object.
51             ################################################################################
52             sub new {
53 18     18 1 458373 my ( $class, %args ) = @_;
54              
55 18         106 my $self = \%args;
56 18         93 bless( $self, $class );
57              
58 18         201 $self->{memory} = {};
59              
60 18         205 return $self;
61             }
62              
63              
64             ################################################################################
65             # Become a daemon:
66             # $daemon->Init
67             #
68             # or, for more daemons with other settings in the same script:
69             # Use a hash as below. The argument must (!) now be a hashref: {...}
70             # even if you don't modify the initial settings (=> use empty hashref).
71             # $daemon->Init( { [ %Daemon_Settings ] } )
72             #
73             # or, if no Daemon->new() object was created and for backward compatibility:
74             # Proc::Daemon::Init( [ { %Daemon_Settings } ] )
75             # In this case the argument must be or a hashref!
76             #
77             # %Daemon_Settings see &new.
78             #
79             # Returns to the parent:
80             # - nothing (parent does exit) if the context is looking for no return value.
81             # - the PID(s) of the daemon(s) created.
82             # Returns to the child (daemon):
83             # its PID (= 0) | never returns if used with 'exec_command'.
84             ################################################################################
85             sub Init {
86 24     24 1 23509 my Proc::Daemon $self = shift;
87 24         93 my $settings_ref = shift;
88              
89              
90             # Check if $self has been blessed into the package, otherwise do it now.
91 24 50 33     456 unless ( ref( $self ) && eval{ $self->isa( 'Proc::Daemon' ) } ) {
    100          
92 0 0       0 $self = ref( $self ) eq 'HASH' ? Proc::Daemon->new( %$self ) : Proc::Daemon->new();
93             }
94             # If $daemon->Init is used again in the same script,
95             # update to the new arguments.
96             elsif ( ref( $settings_ref ) eq 'HASH' ) {
97 6         33 map { $self->{ $_ } = $$settings_ref{ $_ } } keys %$settings_ref;
  18         99  
98             }
99              
100              
101             # Open a filehandle to an anonymous temporary pid file. If this is not
102             # possible (some environments do not allow all users to use anonymous
103             # temporary files), use the pid_file(s) to retrieve the PIDs for the parent.
104 24         65 my $FH_MEMORY;
105 24 0 33     5345 unless ( open( $FH_MEMORY, "+>", undef ) || $self->{pid_file} ) {
106 0         0 die "Can not anonymous temporary pidfile ('$!'), therefore you must add 'pid_file' as an Init() argument, e.g. to: '/tmp/proc_daemon_pids'";
107             }
108              
109              
110             # Get the file descriptors the user does not want to close.
111 24         78 my %dont_close_fd;
112 24 50       124 if ( defined $self->{dont_close_fd} ) {
113             die "The argument 'dont_close_fd' must be arrayref!"
114 0 0       0 if ref( $self->{dont_close_fd} ) ne 'ARRAY';
115 0         0 foreach ( @{ $self->{dont_close_fd} } ) {
  0         0  
116 0 0       0 die "All entries in 'dont_close_fd' must be numeric ('$_')!" if $_ =~ /\D/;
117 0         0 $dont_close_fd{ $_ } = 1;
118             }
119             }
120             # Get the file descriptors of the handles the user does not want to close.
121 24 50       125 if ( defined $self->{dont_close_fh} ) {
122             die "The argument 'dont_close_fh' must be arrayref!"
123 0 0       0 if ref( $self->{dont_close_fh} ) ne 'ARRAY';
124 0         0 foreach ( @{ $self->{dont_close_fh} } ) {
  0         0  
125 0 0       0 if ( defined ( my $fn = fileno $_ ) ) {
126 0         0 $dont_close_fd{ $fn } = 1;
127             }
128             }
129             }
130              
131              
132             # If system commands are to be executed, put them in a list.
133 24 50       166 my @exec_command = ref( $self->{exec_command} ) eq 'ARRAY' ? @{ $self->{exec_command} } : ( $self->{exec_command} );
  0         0  
134 24 50       127 $#exec_command = 0 if $#exec_command < 0;
135              
136              
137             # Create a daemon for every system command.
138 24         89 foreach my $exec_command ( @exec_command ) {
139             # The first parent is running here.
140              
141              
142             # Using this subroutine or loop multiple times we must modify the filenames:
143             # 'child_STDIN', 'child_STDOUT', 'child_STDERR' and 'pid_file' for every
144             # daemon (a higher number will be appended to the filenames).
145 24         216 $self->adjust_settings();
146              
147              
148             # First fork.
149 24         97 my $pid = Fork();
150 24 100 66     1346 if ( defined $pid && $pid == 0 ) {
151             # The first child runs here.
152              
153              
154             # Set the new working directory.
155 10 50       1290 die "Can't to $self->{work_dir}: $!" unless chdir $self->{work_dir};
156              
157             # Set the file creation mask.
158 10         458 $self->{_orig_umask} = umask;
159 10         162 umask($self->{file_umask});
160              
161             # Detach the child from the terminal (no controlling tty), make it the
162             # session-leader and the process-group-leader of a new process group.
163 10 50       1110 die "Cannot detach from controlling terminal" if POSIX::setsid() < 0;
164              
165             # "Is ignoring SIGHUP necessary?
166             #
167             # It's often suggested that the SIGHUP signal should be ignored before
168             # the second fork to avoid premature termination of the process. The
169             # reason is that when the first child terminates, all processes, e.g.
170             # the second child, in the orphaned group will be sent a SIGHUP.
171             #
172             # 'However, as part of the session management system, there are exactly
173             # two cases where SIGHUP is sent on the death of a process:
174             #
175             # 1) When the process that dies is the session leader of a session that
176             # is attached to a terminal device, SIGHUP is sent to all processes
177             # in the foreground process group of that terminal device.
178             # 2) When the death of a process causes a process group to become
179             # orphaned, and one or more processes in the orphaned group are
180             # stopped, then SIGHUP and SIGCONT are sent to all members of the
181             # orphaned group.' [2]
182             #
183             # The first case can be ignored since the child is guaranteed not to have
184             # a controlling terminal. The second case isn't so easy to dismiss.
185             # The process group is orphaned when the first child terminates and
186             # POSIX.1 requires that every STOPPED process in an orphaned process
187             # group be sent a SIGHUP signal followed by a SIGCONT signal. Since the
188             # second child is not STOPPED though, we can safely forego ignoring the
189             # SIGHUP signal. In any case, there are no ill-effects if it is ignored."
190             # Source: http://code.activestate.com/recipes/278731/
191             #
192             # local $SIG{'HUP'} = 'IGNORE';
193              
194             # Second fork.
195             # This second fork is not absolutely necessary, it is more a precaution.
196             # 1. Prevent possibility of reacquiring a controlling terminal.
197             # Without this fork the daemon would remain a session-leader. In
198             # this case there is a potential possibility that the process could
199             # reacquire a controlling terminal. E.g. if it opens a terminal device,
200             # without using the O_NOCTTY flag. In Perl this is normally the case
201             # when you use on this kind of device, instead of
202             # with the O_NOCTTY flag set.
203             # Note: Because of the second fork the daemon will not be a session-
204             # leader and therefore Signals will not be send to other members of
205             # his process group. If you need the functionality of a session-leader
206             # you may want to call POSIX::setsid() manually on your daemon.
207             # 2. Detach the daemon completely from the parent.
208             # The double-fork prevents the daemon from becoming a zombie. It is
209             # needed in this module because the grandparent process can continue.
210             # Without the second fork and if a child exits before the parent
211             # and you forget to call in the parent you will get a zombie
212             # until the parent also terminates. Using the second fork we can be
213             # sure that the parent of the daemon is finished near by or before
214             # the daemon exits.
215 10         1089 $pid = Fork();
216 10 50 33     954 if ( defined $pid && $pid == 0 ) {
217             # Here the second child is running.
218              
219              
220             # Close all file handles and descriptors the user does not want
221             # to preserve.
222 10         77 my $hc_fd; # highest closed file descriptor
223 10         862 close $FH_MEMORY;
224 10         398 foreach ( 0 .. OpenMax() ) {
225 5242890 50       11474565 unless ( $dont_close_fd{ $_ } ) {
226 5242890 100       15278179 if ( $_ == 0 ) { close STDIN }
  10 100       176  
    100          
227 10         133 elsif ( $_ == 1 ) { close STDOUT }
228 10         84 elsif ( $_ == 2 ) { close STDERR }
229 5242860 100       16183181 else { $hc_fd = $_ if POSIX::close( $_ ) }
230             }
231             }
232              
233             # Sets the real group identifier and the effective group
234             # identifier for the daemon process before opening files.
235             # Must set group first because you cannot change group
236             # once you have changed user
237 10 100       339 POSIX::setgid( $self->{setgid} ) if defined $self->{setgid};
238              
239             # Sets the real user identifier and the effective user
240             # identifier for the daemon process before opening files.
241 10 100       232 POSIX::setuid( $self->{setuid} ) if defined $self->{setuid};
242              
243             # Reopen STDIN, STDOUT and STDERR to 'child_STD...'-path or to
244             # /dev/null. Data written on a null special file is discarded.
245             # Reads from the null special file always return end of file.
246 10 50 50     1238 open( STDIN, $self->{child_STDIN} || "
247 10 50 50     1847 open( STDOUT, $self->{child_STDOUT} || "+>/dev/null" ) unless $dont_close_fd{ 1 };
248 10 50 50     834 open( STDERR, $self->{child_STDERR} || "+>/dev/null" ) unless $dont_close_fd{ 2 };
249              
250             # Since is in some cases "secretly" closing
251             # file descriptors without telling it to perl, we need to
252             # re and as many files as we closed with
253             # . Otherwise it can happen (especially with
254             # FH opened by __DATA__ or __END__) that there will be two perl
255             # handles associated with one file, what can cause some
256             # confusion. :-)
257             # see: http://rt.perl.org/rt3/Ticket/Display.html?id=72526
258 10 50       66 if ( $hc_fd ) {
259 10         52 my @fh;
260 10         75 foreach ( 3 .. $hc_fd ) { open $fh[ $_ ], "
  50         1810  
261             # Perl will try to close all handles when @fh leaves scope
262             # here, but the rude ones will sacrifice themselves to avoid
263             # potential damage later.
264             }
265              
266             # Restore the original file creation mask.
267 10         61 umask $self->{_orig_umask};
268              
269             # Execute a system command and never return.
270 10 100       52 if ( $exec_command ) {
271 2 0       0 exec ($exec_command) or die "couldn't exec $exec_command: $!";
272 0         0 exit; # Not a real exit, but needed since Perl warns you if
273             # there is no statement like , , or
274             # following . The function executes a system
275             # command and never returns.
276             }
277              
278              
279             # Return the childs own PID (= 0)
280 8         306 return $pid;
281             }
282              
283              
284             # First child (= second parent) runs here.
285              
286              
287             # Print the PID of the second child into ...
288 0   0     0 $pid ||= '';
289             # ... the anonymous temporary pid file.
290 0 0       0 if ( $FH_MEMORY ) {
291 0         0 print $FH_MEMORY "$pid\n";
292 0         0 close $FH_MEMORY;
293             }
294             # ... the real 'pid_file'.
295 0 0       0 if ( $self->{pid_file} ) {
296 0 0       0 open( my $FH_PIDFILE, "+>", $self->{pid_file} ) ||
297             die "Can not open pidfile (pid_file => '$self->{pid_file}'): $!";
298 0         0 print $FH_PIDFILE $pid;
299 0         0 close $FH_PIDFILE;
300             }
301              
302              
303             # Don't for the second child to exit,
304             # even if we don't have a value in $exec_command.
305             # The second child will become orphan by here, but then it
306             # will be adopted by init(8), which automatically performs a
307             # to remove the zombie when the child exits.
308              
309 0         0 POSIX::_exit(0);
310             }
311              
312              
313             # Only first parent runs here.
314              
315              
316             # A child that terminates, but has not been waited for becomes
317             # a zombie. So we wait for the first child to exit.
318 14         62598 waitpid( $pid, 0 );
319             }
320              
321              
322             # Only first parent runs here.
323              
324              
325             # Exit if the context is looking for no value (void context).
326 14 50       191 exit 0 unless defined wantarray;
327              
328             # Get the daemon PIDs out of the anonymous temporary pid file
329             # or out of the real pid-file(s)
330 14         54 my @pid;
331 14 50       196 if ( $FH_MEMORY ) {
    0          
332 14         560 seek( $FH_MEMORY, 0, 0 );
333 14 50       1182 @pid = map { chomp $_; $_ eq '' ? undef : $_ } <$FH_MEMORY>;
  14         116  
  14         242  
334 14         647 $_ = (/^(\d+)$/)[0] foreach @pid; # untaint
335 14         1381 close $FH_MEMORY;
336             }
337             elsif ( $self->{memory}{pid_file} ) {
338 0         0 foreach ( keys %{ $self->{memory}{pid_file} } ) {
  0         0  
339 0 0       0 open( $FH_MEMORY, "<", $_ ) || die "Can not open pid_file '<$_': $!";
340 0         0 push( @pid, <$FH_MEMORY> );
341 0         0 close $FH_MEMORY;
342             }
343             }
344              
345             # Return the daemon PIDs (from second child/ren) to the first parent.
346 14 50       1193 return ( wantarray ? @pid : $pid[0] );
347             }
348             # For backward capability:
349             *init = \&Init;
350              
351              
352             ################################################################################
353             # Set some defaults and adjust some settings.
354             # Args: ( $self )
355             # Returns: nothing
356             ################################################################################
357             sub adjust_settings {
358 24     24 1 54 my Proc::Daemon $self = shift;
359              
360             # Set default 'work_dir' if needed.
361 24   50     90 $self->{work_dir} ||= '/';
362              
363 24 50       76 $self->fix_filename( 'child_STDIN', 1 ) if $self->{child_STDIN};
364              
365 24 50       173 $self->fix_filename( 'child_STDOUT', 1 ) if $self->{child_STDOUT};
366              
367 24 50       112 $self->fix_filename( 'child_STDERR', 1 ) if $self->{child_STDERR};
368              
369             # Check 'pid_file's name
370 24 50       92 if ( $self->{pid_file} ) {
371 24 50       177 die "Pidfile (pid_file => '$self->{pid_file}') can not be only a number. I must be able to distinguish it from a PID number in &get_pid('...')." if $self->{pid_file} =~ /^\d+$/;
372              
373 24         76 $self->fix_filename( 'pid_file' );
374             }
375              
376 24   100     174 $self->{file_umask} ||= 066;
377              
378 24         51 return;
379             }
380              
381              
382             ################################################################################
383             # - If the keys value is only a filename add the path of 'work_dir'.
384             # - If we have already set a file for this key with the same "path/name",
385             # add a number to the file.
386             # Args: ( $self, $key, $extract_mode )
387             # key: one of 'child_STDIN', 'child_STDOUT', 'child_STDERR', 'pid_file'
388             # extract_mode: true = separate MODE form filename before checking
389             # path/filename; false = no MODE to check
390             # Returns: nothing
391             ################################################################################
392             sub fix_filename {
393 72     72 1 136 my Proc::Daemon $self = shift;
394 72         290 my $key = shift;
395 72         289 my $var = $self->{ $key };
396 72 50       510 my $mode = ( shift ) ? ( $var =~ s/^([\+\<\>\-\|]+)// ? $1 : ( $key eq 'child_STDIN' ? '<' : '+>' ) ) : '';
    100          
    100          
397              
398             # add path to filename
399 72 100 66     698 if ( $var =~ s/^\.\/// || $var !~ /\// ) {
400             $var = $self->{work_dir} =~ /\/$/ ?
401 54 50       276 $self->{work_dir} . $var : $self->{work_dir} . '/' . $var;
402             }
403              
404             # If the file was already in use, modify it with '_number':
405             # filename_X | filename_X.ext
406 72 100       291 if ( $self->{memory}{ $key }{ $var } ) {
407 18         198 $var =~ s/([^\/]+)$//;
408 18         84 my @i = split( /\./, $1 );
409 18 50       72 my $j = $#i ? $#i - 1 : 0;
410              
411 18   50     174 $self->{memory}{ "$key\_num" } ||= 0;
412 18         264 $i[ $j ] =~ s/_$self->{memory}{ "$key\_num" }$//;
413 18         60 $self->{memory}{ "$key\_num" }++;
414 18         60 $i[ $j ] .= '_' . $self->{memory}{ "$key\_num" };
415 18         81 $var .= join( '.', @i );
416             }
417              
418 72         252 $self->{memory}{ $key }{ $var } = 1;
419 72         269 $self->{ $key } = $mode . $var;
420              
421 72         178 return;
422             }
423              
424              
425             ################################################################################
426             # Fork(): Retries to fork over 30 seconds if possible to fork at all and
427             # if necessary.
428             # Returns the child PID to the parent process and 0 to the child process.
429             # If the fork is unsuccessful it Cs and returns C.
430             ################################################################################
431             sub Fork {
432 34     34 1 74 my $pid;
433 34         79 my $loop = 0;
434              
435             FORK: {
436 34 50       66 if ( defined( $pid = fork ) ) {
  34         47231  
437 34         1773 return $pid;
438             }
439              
440             # EAGAIN - fork cannot allocate sufficient memory to copy the parent's
441             # page tables and allocate a task structure for the child.
442             # ENOMEM - fork failed to allocate the necessary kernel structures
443             # because memory is tight.
444             # Last the loop after 30 seconds
445 0 0 0     0 if ( $loop < 6 && ( $! == POSIX::EAGAIN() || $! == POSIX::ENOMEM() ) ) {
      0        
446 0         0 $loop++; sleep 5; redo FORK;
  0         0  
  0         0  
447             }
448             }
449              
450 0         0 warn "Can't fork: $!";
451              
452 0         0 return undef;
453             }
454              
455              
456             ################################################################################
457             # OpenMax( [ NUMBER ] )
458             # Returns the maximum number of possible file descriptors. If sysconf()
459             # does not give me a valid value, I return NUMBER (default is 64).
460             ################################################################################
461             sub OpenMax {
462 10     10 1 747 my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
463              
464 10 50 33     906 return ( ! defined( $openmax ) || $openmax < 0 ) ?
      0        
465             ( shift || 64 ) : $openmax;
466             }
467              
468              
469             ################################################################################
470             # Check if the (daemon) process is alive:
471             # Status( [ number or string ] )
472             #
473             # Examples:
474             # $object->Status() - Tries to get the PID out of the settings in new() and checks it.
475             # $object->Status( 12345 ) - Number of PID to check.
476             # $object->Status( './pid.txt' ) - Path to file containing one PID to check.
477             # $object->Status( 'perl /home/my_perl_daemon.pl' ) - Command line entry of the
478             # running program to check. Requires Proc::ProcessTable to work.
479             #
480             # Returns the PID (alive) or 0 (dead).
481             ################################################################################
482             sub Status {
483 104     104 1 80031842 my Proc::Daemon $self = shift;
484 104         555 my $pid = shift;
485              
486             # Get the process ID.
487 104         877 ( $pid, undef ) = $self->get_pid( $pid );
488              
489             # Return if no PID was found.
490 104 50       633 return 0 if ! $pid;
491              
492             # The kill(2) system call will check whether it's possible to send
493             # a signal to the pid (that means, to be brief, that the process
494             # is owned by the same user, or we are the super-user). This is a
495             # useful way to check that a child process is alive (even if only
496             # as a zombie) and hasn't changed its UID.
497 104 100       2983 return ( kill( 0, $pid ) ? $pid : 0 );
498             }
499              
500              
501             ################################################################################
502             # Kill the (daemon) process:
503             # Kill_Daemon( [ number or string [, SIGNAL ] ] )
504             #
505             # Examples:
506             # $object->Kill_Daemon() - Tries to get the PID out of the settings in new() and kill it.
507             # $object->Kill_Daemon( 12345, 'TERM' ) - Number of PID to kill with signal 'TERM'. The
508             # names or numbers of the signals are the ones listed out by kill -l on your system.
509             # $object->Kill_Daemon( './pid.txt' ) - Path to file containing one PID to kill.
510             # $object->Kill_Daemon( 'perl /home/my_perl_daemon.pl' ) - Command line entry of the
511             # running program to kill. Requires Proc::ProcessTable to work.
512             #
513             # Returns the number of processes successfully killed,
514             # which mostly is not the same as the PID number.
515             ################################################################################
516             sub Kill_Daemon {
517 4     4 1 2882 my Proc::Daemon $self = shift;
518 4         12 my $pid = shift;
519 4   50     64 my $signal = shift || 'KILL';
520 4         8 my $pidfile;
521              
522             # Get the process ID.
523 4         18 ( $pid, $pidfile ) = $self->get_pid( $pid );
524              
525             # Return if no PID was found.
526 4 50       22 return 0 if ! $pid;
527              
528             # Kill the process.
529 4         140 my $killed = kill( $signal, $pid );
530              
531 4 50 33     44 if ( $killed && $pidfile ) {
532             # Set PID in pid file to '0'.
533 4 50       531756 if ( open( my $FH_PIDFILE, "+>", $pidfile ) ) {
534 4         34 print $FH_PIDFILE '0';
535 4         302 close $FH_PIDFILE;
536             }
537 0         0 else { warn "Can not open pidfile (pid_file => '$pidfile'): $!" }
538             }
539              
540 4         40 return $killed;
541             }
542              
543              
544             ################################################################################
545             # Return the PID of a process:
546             # get_pid( number or string )
547             #
548             # Examples:
549             # $object->get_pid() - Tries to get the PID out of the settings in new().
550             # $object->get_pid( 12345 ) - Number of PID to return.
551             # $object->get_pid( './pid.txt' ) - Path to file containing the PID.
552             # $object->get_pid( 'perl /home/my_perl_daemon.pl' ) - Command line entry of
553             # the running program. Requires Proc::ProcessTable to work.
554             #
555             # Returns an array with ( 'the PID | ', 'the pid_file | ' )
556             ################################################################################
557             sub get_pid {
558 118     118 1 6838 my Proc::Daemon $self = shift;
559 118   100     1064 my $string = shift || '';
560 118         279 my ( $pid, $pidfile );
561              
562 118 100       605 if ( $string ) {
563             # $string is already a PID.
564 114 100       3364 if ( $string =~ /^(\d+)$/ ) {
    50          
565 100         1002 $pid = $1; # untaint
566             }
567             # Open the pidfile and get the PID from it.
568             elsif ( open( my $FH_MEMORY, "<", $string ) ) {
569 14         448 $pid = <$FH_MEMORY>;
570 14         1931 close $FH_MEMORY;
571              
572 14 50       117 die "I found no valid PID ('$pid') in the pidfile: '$string'" if $pid =~ /\D/s;
573 14         104 $pid = ($pid =~ /^(\d+)$/)[0]; # untaint
574              
575 14         79 $pidfile = $string;
576             }
577             # Get the PID by the system process table.
578             else {
579 0         0 $pid = $self->get_pid_by_proc_table_attr( 'cmndline', $string );
580             }
581             }
582              
583              
584             # Try to get the PID out of the new() settings.
585 118 100       538 if ( ! $pid ) {
586             # Try to get the PID out of the 'pid_file' setting.
587 4 50 33     196 if ( $self->{pid_file} && open( my $FH_MEMORY, "<", $self->{pid_file} ) ) {
588 4         44 $pid = <$FH_MEMORY>;
589 4         38 close $FH_MEMORY;
590              
591 4 50 33     50 if ($pid && $pid =~ /^(\d+)$/) {
592 4         16 $pid = $1; # untaint
593 4         26 $pidfile = $self->{pid_file};
594             } else {
595 0         0 $pid = undef;
596             }
597             }
598              
599             # Try to get the PID out of the system process
600             # table by the 'exec_command' setting.
601 4 0 33     24 if ( ! $pid && $self->{exec_command} ) {
602 0         0 $pid = $self->get_pid_by_proc_table_attr( 'cmndline', $self->{exec_command} );
603             }
604             }
605              
606 118         1285 return ( $pid, $pidfile );
607             }
608              
609              
610             ################################################################################
611             # This sub requires the Proc::ProcessTable module to be installed!!!
612             #
613             # Search for the PID of a process in the process table:
614             # $object->get_pid_by_proc_table_attr( 'unix_process_table_attribute', 'string that must match' )
615             #
616             # unix_process_table_attribute examples:
617             # For more see the README.... files at http://search.cpan.org/~durist/Proc-ProcessTable/
618             # uid - UID of process
619             # pid - process ID
620             # ppid - parent process ID
621             # fname - file name
622             # state - state of process
623             # cmndline - full command line of process
624             # cwd - current directory of process
625             #
626             # Example:
627             # get_pid_by_proc_table_attr( 'cmndline', 'perl /home/my_perl_daemon.pl' )
628             #
629             # Returns the process PID on success, otherwise .
630             ################################################################################
631             sub get_pid_by_proc_table_attr {
632 4     4 1 20016300 my Proc::Daemon $self = shift;
633 4         38 my ( $command, $match ) = @_;
634 4         26 my $pid;
635              
636             # eval - Module may not be installed
637 4         40 eval {
638 4         6738 require Proc::ProcessTable;
639              
640 4         32558 my $table = Proc::ProcessTable->new()->table;
641              
642 4         269034 foreach ( @$table ) {
643             # fix for Proc::ProcessTable: under some conditions $_->cmndline
644             # returns with space and/or other characters at the end
645 44 100       1022 next unless $_->$command =~ /^$match\s*$/;
646 4         98 $pid = $_->pid;
647 4         194 last;
648             }
649             };
650              
651 4 50       22 warn "- Problem in get_pid_by_proc_table_attr( '$command', '$match' ):\n $@ You may not use a command line entry to get the PID of your process.\n This function requires Proc::ProcessTable (http://search.cpan.org/~durist/Proc-ProcessTable/) to work.\n" if $@;
652              
653 4         28 return $pid;
654             }
655              
656             1;