line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package System::Daemon; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
978
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
69
|
|
4
|
3
|
|
|
3
|
|
9
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
57
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
24
|
use POSIX; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
12
|
|
7
|
3
|
|
|
3
|
|
4581
|
use Carp; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
132
|
|
8
|
3
|
|
|
3
|
|
12
|
use Fcntl ':flock'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
222
|
|
9
|
3
|
|
|
3
|
|
981
|
use System::Daemon::Utils; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
69
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
9
|
use constant NEW_SUFFIX => ".new"; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
2031
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = 0.15; |
14
|
|
|
|
|
|
|
our $AUTHOR = 'justnoxx'; |
15
|
|
|
|
|
|
|
our $ABSTRACT = "Swiss-knife for daemonization"; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $DEBUG = 0; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
3
|
|
|
3
|
1
|
105
|
my ($class, %params) = @_; |
21
|
3
|
|
|
|
|
3
|
my $self = {}; |
22
|
3
|
|
|
|
|
6
|
$self->{daemon_data}->{daemonize} = 1; |
23
|
|
|
|
|
|
|
|
24
|
3
|
50
|
|
|
|
9
|
if ($params{user}) { |
25
|
0
|
|
|
|
|
0
|
$self->{daemon_data}->{user} = $params{user}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
3
|
50
|
|
|
|
6
|
if ($params{group}) { |
29
|
0
|
|
|
|
|
0
|
$self->{daemon_data}->{group} = $params{group}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
3
|
50
|
|
|
|
9
|
if ($params{pidfile}) { |
33
|
3
|
|
|
|
|
3
|
$self->{pidfile} = $params{pidfile}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
3
|
50
|
|
|
|
6
|
if ($params{new}) { |
37
|
0
|
|
|
|
|
0
|
$self->{new} = 1; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
3
|
50
|
|
|
|
6
|
if ($params{mkdir}) { |
41
|
0
|
|
|
|
|
0
|
$self->{daemon_data}->{mkdir} = 1; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
3
|
50
|
|
|
|
6
|
if ($params{procname}) { |
45
|
0
|
|
|
|
|
0
|
$self->{daemon_data}->{procname} = $params{procname}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
3
|
50
|
|
|
|
3
|
if (exists $params{daemonize}) { |
49
|
0
|
|
|
|
|
0
|
$self->{daemon_data}->{daemonize} = $params{daemonize}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
3
|
50
|
|
|
|
9
|
if ($params{cleanup_on_destroy}) { |
53
|
0
|
|
|
|
|
0
|
$self->{daemon_data}->{cleanup_on_destroy} = 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
3
|
bless $self, $class; |
57
|
3
|
|
|
|
|
9
|
return $self; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub pidfile { |
61
|
16
|
|
|
16
|
0
|
30
|
my $self = shift; |
62
|
|
|
|
|
|
|
|
63
|
16
|
50
|
|
|
|
30
|
return unless $self->{pidfile}; |
64
|
|
|
|
|
|
|
|
65
|
16
|
50
|
|
|
|
156
|
$self->{new} ? $self->{pidfile}.NEW_SUFFIX : $self->{pidfile}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub daemonize { |
69
|
3
|
|
|
3
|
1
|
9
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
3
|
50
|
|
|
|
12
|
unless ($self->{daemon_data}->{daemonize}) { |
72
|
0
|
|
|
|
|
0
|
carp "Daemonization disabled"; |
73
|
0
|
|
|
|
|
0
|
return 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
3
|
|
|
|
|
3
|
my $dd = $self->{daemon_data}; |
77
|
|
|
|
|
|
|
|
78
|
3
|
|
|
|
|
6
|
my $process_object = System::Daemon::Utils::process_object(); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# wrapper context |
81
|
3
|
|
|
|
|
13065
|
System::Daemon::Utils::daemon(); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# let's validate user and group |
84
|
1
|
50
|
33
|
|
|
33
|
if ($dd->{user} || $dd->{group}) { |
85
|
|
|
|
|
|
|
System::Daemon::Utils::validate_user_and_group( |
86
|
|
|
|
|
|
|
user => $dd->{user}, |
87
|
|
|
|
|
|
|
group => $dd->{group}, |
88
|
0
|
0
|
|
|
|
0
|
) or do { |
89
|
0
|
|
|
|
|
0
|
croak "Bad user or group"; |
90
|
|
|
|
|
|
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
1
|
50
|
|
|
|
18
|
if ($self->pidfile) { |
94
|
1
|
|
|
|
|
2
|
System::Daemon::Utils::validate_pid_path($self->pidfile, $dd->{mkdir}); |
95
|
|
|
|
|
|
|
} |
96
|
1
|
50
|
|
|
|
5
|
System::Daemon::Utils::make_sandbox($self->pidfile, $dd) if $dd->{mkdir}; |
97
|
|
|
|
|
|
|
# daemon context |
98
|
1
|
50
|
|
|
|
6
|
if ($self->pidfile) { |
99
|
1
|
50
|
|
|
|
10
|
croak "Can't overwrite pid file of my alive instance" unless $self->ok_pid(); |
100
|
1
|
50
|
|
|
|
3
|
if ($self->pidfile) { |
101
|
1
|
|
|
|
|
10
|
open my $LOCK, $self->pidfile; |
102
|
1
|
|
|
|
|
8
|
my $got_lock = flock($LOCK, LOCK_EX | LOCK_NB); |
103
|
1
|
|
|
|
|
3
|
$self->{_lock} = $LOCK; |
104
|
1
|
50
|
|
|
|
3
|
unless ($got_lock) { |
105
|
0
|
|
|
|
|
0
|
warn "Can't get lock ".$self->pidfile."\n"; |
106
|
0
|
|
|
|
|
0
|
exit 1; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
1
|
|
|
|
|
6
|
$self->{original_pid} = $$; |
110
|
|
|
|
|
|
|
System::Daemon::Utils::write_pid($self->pidfile, undef, |
111
|
|
|
|
|
|
|
user => $dd->{user}, |
112
|
|
|
|
|
|
|
group => $dd->{group} |
113
|
1
|
|
|
|
|
2
|
); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
1
|
50
|
33
|
|
|
6
|
if ($dd->{user} || $dd->{group}) { |
117
|
|
|
|
|
|
|
System::Daemon::Utils::apply_rights( |
118
|
|
|
|
|
|
|
user => $dd->{user}, |
119
|
|
|
|
|
|
|
group => $dd->{group} |
120
|
0
|
|
|
|
|
0
|
); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
1
|
50
|
|
|
|
2
|
if ($dd->{procname}) { |
124
|
0
|
|
|
|
|
0
|
$0 = $dd->{procname}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
1
|
50
|
|
|
|
2
|
if ($dd->{cleanup_on_destroy}) { |
128
|
|
|
|
|
|
|
*{System::Daemon::DESTROY} = sub { |
129
|
0
|
|
|
0
|
|
0
|
my $obj = shift; |
130
|
0
|
|
|
|
|
0
|
$obj->cleanup(); |
131
|
0
|
|
|
|
|
0
|
}; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
1
|
|
|
|
|
2
|
System::Daemon::Utils::suppress(); |
135
|
1
|
|
|
|
|
47
|
return 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub exit { |
140
|
1
|
|
|
1
|
1
|
555
|
my ($self, $code) = @_; |
141
|
|
|
|
|
|
|
|
142
|
1
|
|
|
|
|
5
|
$self->finish(); |
143
|
|
|
|
|
|
|
|
144
|
1
|
|
50
|
|
|
11
|
$code ||= 0; |
145
|
1
|
|
|
|
|
8
|
exit $code; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub ok_pid { |
150
|
1
|
|
|
1
|
0
|
2
|
my ($self, $pidfile) = @_; |
151
|
|
|
|
|
|
|
|
152
|
1
|
|
33
|
|
|
8
|
$pidfile ||= $self->pidfile; |
153
|
|
|
|
|
|
|
|
154
|
1
|
50
|
|
|
|
3
|
return 1 unless $pidfile; |
155
|
|
|
|
|
|
|
|
156
|
1
|
50
|
|
|
|
1
|
unless (System::Daemon::Utils::pid_init($self->pidfile)) { |
157
|
0
|
|
|
|
|
0
|
croak "Can't init pidfile"; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
1
|
|
|
|
|
1
|
my $pid; |
161
|
1
|
50
|
|
|
|
4
|
unless ($pid = System::Daemon::Utils::read_pid($pidfile)) { |
162
|
1
|
|
|
|
|
5
|
return 1; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
0
|
return 1; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub cleanup { |
170
|
1
|
|
|
1
|
1
|
289
|
my ($self) = @_; |
171
|
|
|
|
|
|
|
|
172
|
1
|
|
|
|
|
5
|
return $self->finish(); |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub finish { |
177
|
2
|
|
|
2
|
1
|
3
|
my ($self) = @_; |
178
|
|
|
|
|
|
|
|
179
|
2
|
50
|
|
|
|
7
|
if ($self->pidfile) { |
180
|
2
|
|
|
|
|
3
|
my $pid = System::Daemon::Utils::read_pid($self->pidfile); # pid missing OR changed |
181
|
2
|
100
|
|
|
|
8
|
if ($pid ne $self->{original_pid}) { |
182
|
1
|
|
|
|
|
3
|
undef $self->{new}; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
2
|
50
|
|
|
|
7
|
if (-e $self->pidfile.NEW_SUFFIX) { |
186
|
0
|
0
|
|
|
|
0
|
rename $self->pidfile.NEW_SUFFIX, $self->pidfile or confess "rename ".$self->pidfile.NEW_SUFFIX.", ".$self->pidfile; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
else { |
189
|
2
|
|
|
|
|
4
|
System::Daemon::Utils::delete_pidfile($self->pidfile); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub process_object { |
196
|
1
|
|
|
1
|
1
|
33
|
my ($self) = @_; |
197
|
|
|
|
|
|
|
|
198
|
1
|
|
|
|
|
4
|
return System::Daemon::Utils::process_object(); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
__END__ |