MooseX::Daemonize::Core i think is done
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize / Core.pm
1 package MooseX::Daemonize::Core;
2 use Moose::Role;
3
4 our $VERSION = 0.01;
5
6 use POSIX ();
7
8 has is_daemon => (
9     isa     => 'Bool',
10     is      => 'rw',
11     default => sub { 0 },
12 );
13
14 sub daemon_fork { 
15     my $self = shift;
16     if (my $pid = fork) {
17         return $pid;
18     }
19     else {
20         $self->is_deamon(1);
21         return;
22     }
23 }
24 sub daemon_detach { 
25     my $self = shift;
26     
27     return unless $self->is_daemon;
28     
29     (POSIX::setsid)  # set session id
30         || confess "Cannot detach from controlling process";   
31         
32     chdir '/';      # change to root directory
33     umask 0;        # clear the file creation mask            
34     
35     # get the max numnber of possible file descriptors
36     my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
37     $openmax = 64 if !defined($openmax) || $openmax < 0;
38     
39     # close them all 
40     POSIX::close($_) foreach (0 .. $openmax);
41
42     open(STDIN,  "+>/dev/null");
43
44     if (my $stdout_file = $ENV{MX_DAEMON_STDOUT}) {
45         open STDOUT, ">", $stdout_file 
46             or confess "Could not redirect STDOUT to $stdout_file : $!";
47     }
48     else {
49         open(STDOUT, "+>&STDIN");
50     }
51
52     if (my $stderr_file = $ENV{MX_DAEMON_STDERR}) {    
53         open STDERR, ">", "ERR.txt"
54             or confess "Could not redirect STDERR to $stderr_file : $!";        
55     }
56     else {               
57         open(STDERR, "+>&STDIN");    
58     }
59 }
60
61 sub daemonize {
62     my ($self) = @_;
63     $self->daemon_fork; 
64     $self->daemon_detach;
65 }
66
67 1;
68 __END__
69
70 =head1 NAME
71
72 MooseX::Daemonize::Core - provides a Role the core daemonization features
73
74 =head1 VERSION
75
76 =head1 SYNOPSIS
77      
78 =head1 DESCRIPTION
79
80 =head2 Important Note
81
82 This method with not exit the parent process for you, it only forks 
83 and detaches your child (daemon) process. It is your responsibility 
84 to exit the parent process in some way.
85
86 =head1 ATTRIBUTES
87
88 =over
89
90 =item I<is_daemon (is => rw, isa => Bool)>
91
92 This attribute is used to signal if we are within the 
93 daemon process or not. 
94
95 =back
96
97 =head1 METHODS 
98
99 =over
100
101 =item B<daemon_fork>
102
103 This forks off the child process to be daemonized. Just as with 
104 the built in fork, it returns the child pid to the parent process, 
105 0 to the child process. It will also set the is_daemon flag 
106 appropriately.
107
108 =item B<daemon_detach>
109
110 This detaches the new child process from the terminal by doing 
111 the following things. If called from within the parent process
112 (the is_daemon flag is set to false), then it will simply return
113 and do nothing.
114
115 =over 4
116
117 =item Becomes a session leader 
118
119 This detaches the program from the controlling terminal, it is 
120 accomplished by calling POSIX::setsid.
121
122 =item Changes the current working directory to "/"
123
124 This is standard daemon behavior, if you want a different working 
125 directory then simply change it later in your daemons code. 
126
127 =item Clears the file creation mask.
128
129 =item Closes all open file descriptors.
130
131 =item Reopen STDERR, STDOUT & STDIN to /dev/null
132
133 This behavior can be controlled slightly though the MX_DAEMON_STDERR 
134 and MX_DAEMON_STDOUT environment variables. It will look for a filename
135 in either of these variables and redirect STDOUT and/or STDERR to those
136 files. This is useful for debugging and/or testing purposes.
137
138 -back
139
140 =item B<daemonize>
141
142 This will simply call C<daemon_fork> followed by C<daemon_detach>.
143
144 =item meta()
145
146 The C<meta()> method from L<Class::MOP::Class>
147
148 =back
149
150 =head1 DEPENDENCIES
151
152 L<Moose::Role>, L<POSIX>
153
154 =head1 INCOMPATIBILITIES
155
156 None reported.
157
158 =head1 BUGS AND LIMITATIONS
159
160 No bugs have been reported.
161
162 Please report any bugs or feature requests to
163 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
164 L<http://rt.cpan.org>.
165
166 =head1 SEE ALSO
167
168 L<Proc::Daemon>
169
170 This code is based B<HEAVILY> on L<Proc::Daemon>, we originally 
171 depended on it, but we needed some more flexibility, so instead
172 we just stole the code. 
173
174 =head1 AUTHOR
175
176 Stevan Little  C<< <stevan.little@iinteractive.com> >>
177
178 =head1 THANKS
179
180 =head1 LICENCE AND COPYRIGHT
181
182 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights 
183 reserved.
184
185 Portions heavily borrowed from L<Proc::Daemon> which is copyright Earl Hood.
186
187 This module is free software; you can redistribute it and/or
188 modify it under the same terms as Perl itself. See L<perlartistic>.
189
190 =head1 DISCLAIMER OF WARRANTY
191
192 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
193 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
194 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
195 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
196 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
197 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
198 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
199 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
200 NECESSARY SERVICING, REPAIR, OR CORRECTION.
201
202 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
203 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
204 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
205 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
206 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
207 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
208 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
209 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
210 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
211 SUCH DAMAGES.