1
# Copyright (C) 2008-2012 eBox Technologies S.L.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License, version 2, as
5
# published by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Class: EBox::AbstractDaemon
18
# This class is intended to be used by those classes which need to
19
# be executed as a standalone daemon.
21
package EBox::AbstractDaemon;
34
use constant PIDPATH => EBox::Config::tmp . '/pids/';
38
# Create a new <EBox::AbstractDaemon> to work with
42
# name - String daemon's name (it should unique)
48
# <EBox::AbstractDaemon> - the newly created object instance
54
my $name = delete $opts{'name'};
55
my $self = { 'name' => $name };
62
# Spawn the daemon. Clossing the first 64 file descriptors apart
63
# from standard input/output/error and writes the pid on a file
64
# under <EBox::Config::tmp> pids subdirectory.
75
unless (POSIX::setsid) {
76
EBox::debug ('Cannot start new session for ', $self->{'name'});
80
foreach my $fd (0 .. 64) { POSIX::close($fd); }
82
my $tmp = EBox::Config::tmp();
83
open(STDIN, "+<$tmp/stdin");
84
if (EBox::Config::boolean('debug')) {
85
open(STDOUT, "+>$tmp/stout");
86
open(STDERR, "+>$tmp/stderr");
95
unless (open($FD , '>' . $self->pidFile)) {
96
EBox::debug ('Cannot save pid');
107
my ($self, $name) = @_;
111
throw EBox::Exceptions::Internal('No need to specify daemon name when called as object method');
112
$name = $self->{'name'};
115
return PIDPATH . $name . '.pid';
121
my ($self, $name) = @_;
126
throw EBox::Exceptions::Internal('No need to specify daemon name when called as object method');
127
$pidFile = $self->pidFile();
130
$pidFile = $self->pidFile($name);
133
if (not -r $pidFile) {
137
my $pid = File::Slurp::read_file($pidFile);