X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FDaemonize%2FCore.pm;h=e7d238560394a2472880de05a5712bc554504483;hb=568a7a763acd951378a56fbe67db98463eed88b4;hp=5447f63a782f15e608b6912856babef5a6ed7c4c;hpb=4327fe988c1c6e363f8e8a6eeda373e1950fcd17;p=gitmo%2FMooseX-Daemonize.git diff --git a/lib/MooseX/Daemonize/Core.pm b/lib/MooseX/Daemonize/Core.pm index 5447f63..e7d2385 100644 --- a/lib/MooseX/Daemonize/Core.pm +++ b/lib/MooseX/Daemonize/Core.pm @@ -1,10 +1,10 @@ +use strict; +use warnings; package MooseX::Daemonize::Core; -use strict; # cause Perl::Critic errors are annoying + use MooseX::Getopt; # to load the NoGetopt metaclass use Moose::Role; -our $VERSION = 0.01; - use POSIX (); has is_daemon => ( @@ -18,11 +18,43 @@ has is_daemon => ( default => sub { 0 }, ); +has ignore_zombies => ( + metaclass => 'Getopt', + isa => 'Bool', + is => 'rw', + default => sub { 0 }, +); + +has no_double_fork => ( + metaclass => 'Getopt', + isa => 'Bool', + is => 'rw', + default => sub { 0 }, +); + +has dont_close_all_files => ( + metaclass => 'Getopt', + isa => 'Bool', + is => 'rw', + default => sub { 0 }, +); + +sub _get_options { + my ($self, %options) = @_; + # backwards compatibility.. old code might be calling daemon_fork/_detach with options + foreach my $opt (qw( ignore_zombies no_double_fork dont_close_all_files )) { + $self->$opt( $options{ $opt } ) if ( defined $options{ $opt } ); + } +} + + sub daemon_fork { my ($self, %options) = @_; + $self->_get_options( %options ); + $SIG{CHLD} = 'IGNORE' - if $options{ignore_zombies}; + if $self->ignore_zombies;; if (my $pid = fork) { return $pid; @@ -38,12 +70,13 @@ sub daemon_detach { return unless $self->is_daemon; # return if parent ... + $self->_get_options( %options ); # now we are in the daemon ... (POSIX::setsid) # set session id || confess "Cannot detach from controlling process"; - unless ($options{no_double_fork}) { + unless ( $self->no_double_fork ) { $SIG{'HUP'} = 'IGNORE'; fork && exit; } @@ -51,12 +84,14 @@ sub daemon_detach { chdir '/'; # change to root directory umask 0; # clear the file creation mask - # get the max numnber of possible file descriptors - my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX ); - $openmax = 64 if !defined($openmax) || $openmax < 0; + unless ( $self->dont_close_all_files ) { + # get the max number of possible file descriptors + my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX ); + $openmax = 64 if !defined($openmax) || $openmax < 0; - # close them all - POSIX::close($_) foreach (0 .. $openmax); + # close them all + POSIX::close($_) foreach (0 .. $openmax); + } # fixup STDIN ... @@ -77,7 +112,7 @@ sub daemon_detach { # fixup STDERR ... if (my $stderr_file = $ENV{MX_DAEMON_STDERR}) { - open STDERR, ">", "ERR.txt" + open STDERR, ">", $stderr_file or confess "Could not redirect STDERR to $stderr_file : $!"; } else { @@ -151,38 +186,54 @@ responsibility (see some of the other roles in this distro for that). This attribute is used to signal if we are within the daemon process or not. +=item I rw, isa => Bool)> + +Setting this attribute to true will cause this method to not perform the +typical double-fork, which is extra added protection from your process +accidentally acquiring a controlling terminal. More information can be +found above, and by Googling "double fork daemonize". + +If you the double-fork behavior off, you might want to enable the +I. + +=item I rw, isa => Bool)> + +Setting this attribute to a true value will result in setting the C<$SIG{CHLD}> +handler to C. This tells perl to clean up zombie processes. By +default, and for the most part you don't I it, only when you turn off +the double fork behavior (with the I attribute) +do you sometimes want this behavior. + +=item I rw, isa => Bool)> + +Setting this attribute to true will cause it to skip closing all the +filehandles. This is useful if you are opening things like sockets +and such in the pre-fork. + =back =head1 METHODS =over -=item B +=item B This forks off the child process to be daemonized. Just as with the built in fork, it returns the child pid to the parent process, 0 to the child process. It will also set the is_daemon flag appropriately. -The C<%options> available for this function are: - -=over 4 - -=item I +The C<%options> argument remains for backwards compatibility, but +it is suggested that you use the attributes listed above instead. -Setting this key to a true value will result in setting the C<$SIG{CHLD}> -handler to C. This tells perl to clean up zombie processes. By -default, and for the most part you don't I it, only when you turn off -the double fork behavior (with the I option) in C -do you sometimes want this behavior. - -=back - -=item B +=item B This detaches the new child process from the terminal by doing the following things. +The C<%options> argument remains for backwards compatibility, but +it is suggested that you use the attributes listed above instead. + =over 4 =item Becomes a session leader @@ -203,6 +254,9 @@ directory then simply change it later in your daemons code. =item Closes all open file descriptors. +See the I attribute for information on how to +change this part of the process. + =item Reopen STDERR, STDOUT & STDIN to /dev/null This behavior can be controlled slightly though the MX_DAEMON_STDERR @@ -210,33 +264,17 @@ and MX_DAEMON_STDOUT environment variables. It will look for a filename in either of these variables and redirect STDOUT and/or STDERR to those files. This is useful for debugging and/or testing purposes. -=back - -The C<%options> available for this function are: - -=over 4 - -=item I - -Setting this option to true will cause this method to not perform the -typical double-fork, which is extra added protection from your process -accidentally aquiring a controlling terminal. More information can be -found above, and by Googling "double fork daemonize". - -If you the double-fork behavior off, you might want to enable the -I behavior in the C method. - -=back - B If called from within the parent process (the is_daemon flag is set to false), this method will simply return and do nothing. -=item B +=item B + +This will simply call C followed by C. -This will simply call C followed by C, it will -pass any C<%options> onto both methods. +The C<%options> argument remains for backwards compatibility, but +it is suggested that you use the attributes listed above instead. =item meta() @@ -244,6 +282,8 @@ The C method from L =back +=back + =head1 STUFF YOU SHOULD READ =over 4 @@ -276,7 +316,7 @@ written enough so I decided to reproduce it here. terminal. That said, you don't always want this to be the behavior, so you are -free to specify otherwise using the C<%options>. +free to specify otherwise using the I attribute. =item Note about zombies @@ -285,7 +325,7 @@ by the time you have double forked your daemon process is then owned by the init process. However, sometimes the double-fork is more than you really need, and you want to keep your daemon processes a little closer to you. In this case you have to watch out for zombies, you can avoid then -by just setting the C option (see above). +by just setting the I attribute (see above). =back @@ -337,7 +377,7 @@ Stevan Little C<< >> =head1 LICENCE AND COPYRIGHT -Copyright (c) 2007, Chris Prather C<< >>. All rights +Copyright (c) 2007-2011, Chris Prather C<< >>. All rights reserved. Portions heavily borrowed from L which is copyright Earl Hood.