no more Proc::Daemon
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize / Core.pm
1 package MooseX::Daemonize::Core;
2 use strict;    # because Kwalitee is pedantic
3 use Moose::Role;
4
5 our $VERSION = 0.01;
6
7 use POSIX ();
8
9 has is_daemon => (
10     isa     => 'Bool',
11     is      => 'rw',
12     default => sub { 0 },
13 );
14
15 sub daemon_fork   { fork }
16 sub daemon_detach { 
17     # ignore these signals
18     for (qw(TSTP TTIN TTOU PIPE POLL STOP CONT CHLD)) {
19         $SIG{$_} = 'IGNORE' if (exists $SIG{$_});
20     }
21     
22     POSIX::setsid;  # set session id
23     chdir '/';      # change to root directory
24     umask 0;        # clear the file creation mask            
25     
26     # get the max numnber of possible file descriptors
27     my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
28     $openmax = 64 if !defined($openmax) || $openmax < 0;
29     
30     # close them all 
31     POSIX::close($_) foreach (0 .. $openmax);
32
33     open(STDIN,  "+>/dev/null");
34     open(STDOUT, "+>&STDIN");
35     open(STDERR, "+>&STDIN");    
36 }
37
38 sub daemonize {
39     my ($self) = @_;
40     return if $self->daemon_fork;
41     $self->daemon_detach;
42     $self->is_daemon(1);
43 }
44
45 1;
46 __END__
47
48 =head1 NAME
49
50 MooseX::Daemonize::Core - provides a Role the core daemonization features
51
52 =head1 VERSION
53
54 =head1 SYNOPSIS
55      
56 =head1 DESCRIPTION
57
58 =head1 ATTRIBUTES
59
60 =over
61
62 =item is_daemon Bool
63
64 If true, the process is the backgrounded process. This is useful for example
65 in an after 'start' => sub { } block
66
67 =back
68
69 =head1 METHODS 
70
71 =over
72
73 =item daemon_fork()
74
75 =item daemon_detach()
76
77 =item daemonize()
78
79 Calls C<Proc::Daemon::Init> to daemonize this process. 
80
81 =item setup_signals()
82
83 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
84
85 =item handle_sigint()
86
87 Handle a INT signal, by default calls C<$self->stop()>
88
89 =item handle_sighup()
90
91 Handle a HUP signal. By default calls C<$self->restart()>
92
93 =item meta()
94
95 The C<meta()> method from L<Class::MOP::Class>
96
97 =back
98
99 =head1 DEPENDENCIES
100
101 =for author to fill in:
102     A list of all the other modules that this module relies upon,
103     including any restrictions on versions, and an indication whether
104     the module is part of the standard Perl distribution, part of the
105     module's distribution, or must be installed separately. ]
106
107 Obviously L<Moose>, and L<Proc::Daemon>
108
109 =head1 INCOMPATIBILITIES
110
111 =for author to fill in:
112     A list of any modules that this module cannot be used in conjunction
113     with. This may be due to name conflicts in the interface, or
114     competition for system or program resources, or due to internal
115     limitations of Perl (for example, many modules that use source code
116     filters are mutually incompatible).
117
118 None reported.
119
120
121 =head1 BUGS AND LIMITATIONS
122
123 =for author to fill in:
124     A list of known problems with the module, together with some
125     indication Whether they are likely to be fixed in an upcoming
126     release. Also a list of restrictions on the features the module
127     does provide: data types that cannot be handled, performance issues
128     and the circumstances in which they may arise, practical
129     limitations on the size of data sets, special cases that are not
130     (yet) handled, etc.
131
132 No bugs have been reported.
133
134 Please report any bugs or feature requests to
135 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
136 L<http://rt.cpan.org>.
137
138 =head1 SEE ALSO
139
140 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
141
142 =head1 AUTHOR
143
144 Chris Prather  C<< <perigrin@cpan.org> >>
145
146 =head1 THANKS
147
148 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the 
149 #moose denzians
150
151 Some bug fixes sponsored by Takkle Inc.
152
153 =head1 LICENCE AND COPYRIGHT
154
155 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights 
156 reserved.
157
158 This module is free software; you can redistribute it and/or
159 modify it under the same terms as Perl itself. See L<perlartistic>.
160
161
162 =head1 DISCLAIMER OF WARRANTY
163
164 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
165 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
166 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
167 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
168 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
169 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
170 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
171 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
172 NECESSARY SERVICING, REPAIR, OR CORRECTION.
173
174 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
175 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
176 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
177 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
178 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
179 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
180 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
181 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
182 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
183 SUCH DAMAGES.