~ubuntu-branches/ubuntu/trusty/zentyal-core/trusty

« back to all changes in this revision

Viewing changes to src/EBox/AbstractDaemon.pm

  • Committer: Package Import Robot
  • Author(s): Jorge Salamero Sanz
  • Date: 2012-08-28 10:03:33 UTC
  • Revision ID: package-import@ubuntu.com-20120828100333-oz3n5kpav4z0tl27
Tags: 2.3.21+quantal1
New upstream release for Quantal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2012 eBox Technologies S.L.
2
 
#
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.
6
 
#
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.
11
 
#
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
15
 
 
16
 
# Class: EBox::AbstractDaemon
17
 
#
18
 
#       This class is intended to be used by those classes which need to
19
 
#       be executed as a standalone daemon.
20
 
#
21
 
package EBox::AbstractDaemon;
22
 
 
23
 
use strict;
24
 
use warnings;
25
 
 
26
 
use EBox;
27
 
use EBox::Config;
28
 
use POSIX;
29
 
 
30
 
use Error qw(:try);
31
 
use File::Slurp;
32
 
 
33
 
 
34
 
use constant PIDPATH => EBox::Config::tmp . '/pids/';
35
 
 
36
 
# Constructor: new
37
 
#
38
 
#        Create a new <EBox::AbstractDaemon> to work with
39
 
#
40
 
# Parameters:
41
 
#
42
 
#        name - String daemon's name (it should unique)
43
 
#
44
 
#        - Named parameters
45
 
#
46
 
# Returns:
47
 
#
48
 
#        <EBox::AbstractDaemon> - the newly created object instance
49
 
#
50
 
sub new
51
 
{
52
 
    my $class = shift;
53
 
    my %opts = @_;
54
 
    my $name = delete $opts{'name'};
55
 
    my $self = { 'name' => $name };
56
 
    bless($self, $class);
57
 
    return $self;
58
 
}
59
 
 
60
 
# Method: init
61
 
#
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.
65
 
#
66
 
sub init
67
 
{
68
 
    my $self =  shift;
69
 
    my ($pid);
70
 
 
71
 
    if ($pid = fork()) {
72
 
        exit 0;
73
 
    }
74
 
 
75
 
    unless (POSIX::setsid) {
76
 
        EBox::debug ('Cannot start new session for ', $self->{'name'});
77
 
        exit 1;
78
 
    }
79
 
 
80
 
    foreach my $fd (0 .. 64) { POSIX::close($fd); }
81
 
 
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");
87
 
    }
88
 
 
89
 
 
90
 
    unless (-d PIDPATH) {
91
 
        mkdir PIDPATH;
92
 
    }
93
 
 
94
 
    my $FD;
95
 
    unless (open($FD ,  '>' .  $self->pidFile)) {
96
 
        EBox::debug ('Cannot save pid');
97
 
        exit 1;
98
 
    }
99
 
 
100
 
    print $FD "$$";
101
 
    close $FD;
102
 
}
103
 
 
104
 
 
105
 
sub pidFile
106
 
{
107
 
    my ($self, $name) = @_;
108
 
 
109
 
    if (ref $self) {
110
 
        $name and
111
 
            throw EBox::Exceptions::Internal('No need to specify daemon name when called as object method');
112
 
        $name = $self->{'name'};
113
 
    }
114
 
 
115
 
    return  PIDPATH . $name . '.pid';
116
 
}
117
 
 
118
 
 
119
 
sub pid
120
 
{
121
 
    my ($self, $name) = @_;
122
 
    my $pidFile;
123
 
 
124
 
    if (ref $self) {
125
 
        $name and
126
 
            throw EBox::Exceptions::Internal('No need to specify daemon name when called as object method');
127
 
        $pidFile = $self->pidFile();
128
 
    }
129
 
    else {
130
 
        $pidFile = $self->pidFile($name);
131
 
    }
132
 
 
133
 
    if (not -r $pidFile) {
134
 
        return undef;
135
 
    }
136
 
 
137
 
    my $pid = File::Slurp::read_file($pidFile);
138
 
    return $pid;
139
 
}
140
 
 
141
 
1;