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