blib/lib/MooseX/Daemonize/Core.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 62.5% 20 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 84.7% 100 / 118 SCAR 30.8
CC
20
Cov
84%
daemon_detach
23.5
_get_options
3.0
daemon_fork
3.0
CRAP 21.7
« blib/lib/MooseX/Daemonize.pm ↑ Summary blib/lib/Test/MooseX/Daemonize.pm »
1 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package MooseX::Daemonize::Core;
4 # ABSTRACT: A Role with the core daemonization features
5  
6 13 our $VERSION = '0.22';
7  
8 13 ▶use MooseX::Getopt; # to load the NoGetopt metaclass
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 has is_daemon => (
14 # NOTE:
15 # this should never be accessible
16 # from the command line
17 # - SL
18 metaclass => 'NoGetopt',
19 isa => 'Bool',
20 is => 'rw',
21 12 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 13 );
23  
24 has ignore_zombies => (
25 metaclass => 'Getopt',
26 isa => 'Bool',
27 is => 'rw',
28 12 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 13 );
30  
31 has no_double_fork => (
32 metaclass => 'Getopt',
33 isa => 'Bool',
34 is => 'rw',
35 12 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 13 );
37  
38 has dont_close_all_files => (
39 metaclass => 'Getopt',
40 isa => 'Bool',
41 is => 'rw',
42 12 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 13 );
44  
45 sub _get_options {
46 17 ▶ my ($self, %options) = @_;
Subroutine _get_options: called CC=3 SCAR=11.0
47 # backwards compatibility.. old code might be calling daemon_fork/_detach with options
48 17 foreach my $opt (qw( ignore_zombies no_double_fork dont_close_all_files )) {
49 51 ▶ $self->$opt( $options{ $opt } ) if ( defined $options{ $opt } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{$opt} 6 45 51
50 }
51 }
52  
53  
54 sub daemon_fork {
55 11 ▶ my ($self, %options) = @_;
Subroutine daemon_fork: called CC=3 SCAR=11.0
Pod documented
56  
57 11 $self->_get_options( %options );
58  
59 11 ▶ $SIG{CHLD} = 'IGNORE'
Branch: 1 branch
BranchTrueFalseTotal
if $self->ignore_zombies 4 7 11
60 if $self->ignore_zombies;;
61  
62 11 ▶ if (my $pid = fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 5 6 11
63 5 return $pid;
64 }
65 else {
66 6 $self->is_daemon(1);
67 6 return;
68 }
69 }
70  
71 sub daemon_detach {
72 11 ▶ my ($self, %options) = @_;
Subroutine daemon_detach: called CC=16 SCAR=31.6
Pod documented
73  
74 11 ▶ return unless $self->is_daemon; # return if parent ...
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_daemon 5 6 11
75  
76 6 $self->_get_options( %options );
77 # now we are in the daemon ...
78  
79 6 ▶ POSIX::setsid == -1 # set session id
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() == -1 and confess("Cannot detach from controlling process") 0 6 6
80 and confess "Cannot detach from controlling process";
81  
82 6 ▶ unless ( $self->no_double_fork ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_double_fork) 4 2 6
83 4 $SIG{'HUP'} = 'IGNORE';
84 4 ▶ fork && exit;
Branch: 1 branch
BranchTrueFalseTotal
fork and exit 3 1 4
85 }
86  
87 3 chdir '/'; # change to root directory
88 3 umask 0; # clear the file creation mask
89  
90 3 ▶ unless ( $self->dont_close_all_files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dont_close_all_files) 3 0 3
91 # get the max number of possible file descriptors
92 3 my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
93 3 ▶ $openmax = 64 if !defined($openmax) || $openmax < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $openmax or $openmax < 0 0 3 3
Condition33%1/3
not defined $openmax or $openmax < 0
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $openmax or $openmax < 0
not defined $openmax $openmax < 0
Truth table33%1/3
not(A) or B
A= not defined $openmaxB= $openmax < 0
ABresult
1X1
011
000
94  
95 # close them all
96 3 POSIX::close($_) foreach (0 .. $openmax);
97 }
98  
99 # fixup STDIN ...
100  
101 3 ▶ open(STDIN, "+>/dev/null")
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "+>/dev/null" or confess("Could not redirect STDOUT to /dev/null") 0 3 3
102 or confess "Could not redirect STDOUT to /dev/null";
103  
104 # fixup STDOUT ...
105  
106 3 ▶ if (my $stdout_file = $ENV{MX_DAEMON_STDOUT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stdout_file = $ENV{'MX_DAEMON_STDOUT'}) { } 3 0 3
107 3 ▶ open STDOUT, ">", $stdout_file
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $stdout_file or confess("Could not redirect STDOUT to $stdout_file : $!") 0 3 3
108 or confess "Could not redirect STDOUT to $stdout_file : $!";
109 }
110 else {
111 0 ▶ open(STDOUT, "+>&STDIN")
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, "+>&STDIN" or confess("Could not redirect STDOUT to /dev/null") 0 0 0
112 or confess "Could not redirect STDOUT to /dev/null";
113 }
114  
115 # fixup STDERR ...
116  
117 3 ▶ if (my $stderr_file = $ENV{MX_DAEMON_STDERR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stderr_file = $ENV{'MX_DAEMON_STDERR'}) { } 3 0 3
118 3 ▶ open STDERR, ">", $stderr_file
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", $stderr_file or confess("Could not redirect STDERR to $stderr_file : $!") 0 3 3
119 or confess "Could not redirect STDERR to $stderr_file : $!";
120 }
121 else {
122 0 ▶ open(STDERR, "+>&STDIN")
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, "+>&STDIN" or confess("Could not redirect STDERR to /dev/null") 0 0 0
123 or confess "Could not redirect STDERR to /dev/null"; ;
124 }
125  
126 # do a little house cleaning ...
127  
128 # Avoid 'stdin reopened for output'
129 # warning with newer perls
130 3 open( NULL, '/dev/null' );
131 3 <NULL> if (0);
132  
133 # return success
134 3 return 1;
135 }
136  
137 sub daemonize {
138 11 ▶ my ($self, %options) = @_;
Subroutine daemonize: called CC=1 SCAR=0.0
Pod documented
139 11 $self->daemon_fork(%options);
140 11 $self->daemon_detach(%options);
141 }
142  
143 13 1;
144  
145 __END__
146  
147 =pod
148  
149 =encoding UTF-8
150  
151 =head1 NAME
152  
153 MooseX::Daemonize::Core - A Role with the core daemonization features
154  
155 =head1 VERSION
156  
157 version 0.22
158  
159 =head1 SYNOPSIS
160  
161 package My::Daemon;
162 use Moose;
163  
164 with 'MooseX::Daemonize::Core';
165  
166 sub start {
167 my $self = shift;
168 # daemonize me ...
169 $self->daemonize;
170 # return from the parent,...
171 return unless $self->is_daemon;
172 # but continue on in the child (daemon)
173 }
174  
175 =head1 DESCRIPTION
176  
177 This is the basic daemonization Role, it provides a few methods (see
178 below) and the minimum features needed to properly daemonize your code.
179  
180 =head2 Important Notes
181  
182 None of the methods in this role will exit the parent process for you,
183 it only forks and detaches your child (daemon) process. It is your
184 responsibility to exit the parent process in some way.
185  
186 There is no PID or PID file management in this role, that is your
187 responsibility (see some of the other roles in this distro for that).
188  
189 =head1 ATTRIBUTES
190  
191 =over
192  
193 =item I<is_daemon (is => rw, isa => Bool)>
194  
195 This attribute is used to signal if we are within the
196 daemon process or not.
197  
198 =item I<no_double_fork (is => rw, isa => Bool)>
199  
200 Setting this attribute to true will cause this method to not perform the
201 typical double-fork, which is extra added protection from your process
202 accidentally acquiring a controlling terminal. More information can be
203 found above, and by Googling "double fork daemonize".
204  
205 If you the double-fork behavior off, you might want to enable the
206 I<ignore_zombies>.
207  
208 =item I<ignore_zombies (is => rw, isa => Bool)>
209  
210 Setting this attribute to a true value will result in setting the C<$SIG{CHLD}>
211 handler to C<IGNORE>. This tells perl to clean up zombie processes. By
212 default, and for the most part you don't I<need> it, only when you turn off
213 the double fork behavior (with the I<no_double_fork> attribute)
214 do you sometimes want this behavior.
215  
216 =item I<dont_close_all_files (is => rw, isa => Bool)>
217  
218 Setting this attribute to true will cause it to skip closing all the
219 filehandles. This is useful if you are opening things like sockets
220 and such in the pre-fork.
221  
222 =back
223  
224 =head1 METHODS
225  
226 =over
227  
228 =item B<daemon_fork (?%options)>
229  
230 This forks off the child process to be daemonized. Just as with
231 the built in fork, it returns the child pid to the parent process,
232 0 to the child process. It will also set the is_daemon flag
233 appropriately.
234  
235 The C<%options> argument remains for backwards compatibility, but
236 it is suggested that you use the attributes listed above instead.
237  
238 =item B<daemon_detach (?%options)>
239  
240 This detaches the new child process from the terminal by doing
241 the following things.
242  
243 The C<%options> argument remains for backwards compatibility, but
244 it is suggested that you use the attributes listed above instead.
245  
246 =over 4
247  
248 =item Becomes a session leader
249  
250 This detaches the program from the controlling terminal, it is
251 accomplished by calling C<POSIX::setsid>.
252  
253 =item Performing the double-fork
254  
255 See below for information on how to change this part of the process.
256  
257 =item Changes the current working directory to "/"
258  
259 This is standard daemon behavior, if you want a different working
260 directory then simply change it later in your daemons code.
261  
262 =item Clears the file creation mask.
263  
264 =item Closes all open file descriptors.
265  
266 See the I<dont_close_all_files> attribute for information on how to
267 change this part of the process.
268  
269 =item Reopen STDERR, STDOUT & STDIN to /dev/null
270  
271 This behavior can be controlled slightly though the C<MX_DAEMON_STDERR>
272 and C<MX_DAEMON_STDOUT> environment variables. It will look for a filename
273 in either of these variables and redirect C<STDOUT> and/or C<STDERR> to those
274 files. This is useful for debugging and/or testing purposes.
275  
276 B<NOTE>
277  
278 If called from within the parent process (the C<is_daemon> flag is set to
279 false), this method will simply return and do nothing.
280  
281 =item B<daemonize (?%options)>
282  
283 This will simply call C<daemon_fork> followed by C<daemon_detach>.
284  
285 The C<%options> argument remains for backwards compatibility, but
286 it is suggested that you use the attributes listed above instead.
287  
288 =item meta()
289  
290 The C<meta()> method from L<Class::MOP::Class>
291  
292 =back
293  
294 =back
295  
296 =head1 STUFF YOU SHOULD READ
297  
298 =over 4
299  
300 =item Note about double fork
301  
302 Taken from L<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012>
303 in a comment entitled I<The second fork _is_ necessary by Jonathan Bartlett>,
304 it is not the definitive statement on the issue, but it's clear and well
305 written enough so I decided to reproduce it here.
306  
307 The first fork accomplishes two things - allow the shell to return,
308 and allow you to do a setsid().
309  
310 The setsid() removes yourself from your controlling terminal. You
311 see, before, you were still listed as a job of your previous process,
312 and therefore the user might accidentally send you a signal. setsid()
313 gives you a new session, and removes the existing controlling terminal.
314  
315 The problem is, you are now a session leader. As a session leader, if
316 you open a file descriptor that is a terminal, it will become your
317 controlling terminal (oops!). Therefore, the second fork makes you NOT
318 be a session leader. Only session leaders can acquire a controlling
319 terminal, so you can open up any file you wish without worrying that
320 it will make you a controlling terminal.
321  
322 So - first fork - allow shell to return, and permit you to call setsid()
323  
324 Second fork - prevent you from accidentally reacquiring a controlling
325 terminal.
326  
327 That said, you don't always want this to be the behavior, so you are
328 free to specify otherwise using the I<no_double_fork> attribute.
329  
330 =item Note about zombies
331  
332 Doing the double fork (see above) tends to get rid of your zombies since
333 by the time you have double forked your daemon process is then owned by
334 the init process. However, sometimes the double-fork is more than you
335 really need, and you want to keep your daemon processes a little closer
336 to you. In this case you have to watch out for zombies, you can avoid then
337 by just setting the I<ignore_zombies> attribute (see above).
338  
339 =back
340  
341 =head1 ENVIRONMENT VARIABLES
342  
343 These variables are best just used for debugging and/or testing, but
344 not used for actual logging. For that, you should reopen C<STDOUT>/C<STDERR> on
345 your own.
346  
347 =over 4
348  
349 =item B<MX_DAEMON_STDOUT>
350  
351 A filename to redirect the daemon C<STDOUT> to.
352  
353 =item B<MX_DAEMON_STDERR>
354  
355 A filename to redirect the daemon C<STDERR> to.
356  
357 =back
358  
359 =head1 DEPENDENCIES
360  
361 L<Moose::Role>, L<POSIX>
362  
363 =head1 INCOMPATIBILITIES
364  
365 =head1 SEE ALSO
366  
367 L<Proc::Daemon>
368  
369 This code is based B<HEAVILY> on L<Proc::Daemon>, we originally
370 depended on it, but we needed some more flexibility, so instead
371 we just stole the code.
372  
373 =head1 SUPPORT
374  
375 Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Daemonize>
376 (or L<bug-MooseX-Daemonize@rt.cpan.org|mailto:bug-MooseX-Daemonize@rt.cpan.org>).
377  
378 There is also a mailing list available for users of this distribution, at
379 L<http://lists.perl.org/list/moose.html>.
380  
381 There is also an irc channel available for users of this distribution, at
382 L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
383  
384 =head1 AUTHORS
385  
386 =over 4
387  
388 =item *
389  
390 Stevan Little <stevan.little@iinteractive.com>
391  
392 =item *
393  
394 Chris Prather <chris@prather.org>
395  
396 =back
397  
398 =head1 COPYRIGHT AND LICENCE
399  
400 This software is copyright (c) 2007 by Chris Prather.
401  
402 This is free software; you can redistribute it and/or modify it under
403 the same terms as the Perl 5 programming language system itself.
404  
405 Portions heavily borrowed from L<Proc::Daemon> which is copyright Earl Hood.
406  
407 =cut
« blib/lib/MooseX/Daemonize.pm ↑ Summary blib/lib/Test/MooseX/Daemonize.pm »