line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SysV::Init::Service; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Pragmas. |
4
|
7
|
|
|
7
|
|
116149
|
use strict; |
|
7
|
|
|
|
|
41
|
|
|
7
|
|
|
|
|
199
|
|
5
|
7
|
|
|
7
|
|
26
|
use warnings; |
|
7
|
|
|
|
|
6
|
|
|
7
|
|
|
|
|
157
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# Modules. |
8
|
7
|
|
|
7
|
|
3125
|
use Capture::Tiny qw(capture); |
|
7
|
|
|
|
|
166340
|
|
|
7
|
|
|
|
|
404
|
|
9
|
7
|
|
|
7
|
|
1236
|
use Class::Utils qw(set_params); |
|
7
|
|
|
|
|
50776
|
|
|
7
|
|
|
|
|
237
|
|
10
|
7
|
|
|
7
|
|
201
|
use English qw(-no_match_vars); |
|
7
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
39
|
|
11
|
7
|
|
|
7
|
|
2140
|
use Error::Pure qw(err); |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
247
|
|
12
|
7
|
|
|
7
|
|
1390
|
use File::Spec::Functions qw(catfile); |
|
7
|
|
|
|
|
1651
|
|
|
7
|
|
|
|
|
3417
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Version. |
15
|
|
|
|
|
|
|
our $VERSION = 0.05; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Construct. |
18
|
|
|
|
|
|
|
sub new { |
19
|
12
|
|
|
12
|
1
|
9113
|
my ($class, @params) = @_; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Create object. |
22
|
12
|
|
|
|
|
37
|
my $self = bless {}, $class; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Service. |
25
|
12
|
|
|
|
|
41
|
$self->{'service'} = undef; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Service directory. |
28
|
12
|
|
|
|
|
32
|
$self->{'service_dir'} = '/etc/init.d'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Process parameters. |
31
|
12
|
|
|
|
|
51
|
set_params($self, @params); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Check for service. |
34
|
10
|
100
|
|
|
|
138
|
if (! defined $self->{'service'}) { |
35
|
1
|
|
|
|
|
3
|
err "Parameter 'service' is required."; |
36
|
|
|
|
|
|
|
} |
37
|
9
|
100
|
|
|
|
42
|
if ($self->{'service'} =~ m/\.sh$/ms) { |
38
|
1
|
|
|
|
|
4
|
err "Service with .sh suffix doesn't possible."; |
39
|
|
|
|
|
|
|
} |
40
|
8
|
|
|
|
|
53
|
$self->{'_service_path'} = catfile($self->{'service_dir'}, |
41
|
|
|
|
|
|
|
$self->{'service'}); |
42
|
8
|
100
|
|
|
|
183
|
if (! -x $self->{'_service_path'}) { |
43
|
1
|
|
|
|
|
6
|
err "Service '$self->{'service'}' doesn't present."; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Object. |
47
|
7
|
|
|
|
|
23
|
return $self; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Get service commands. |
51
|
|
|
|
|
|
|
sub commands { |
52
|
3
|
|
|
3
|
1
|
39
|
my $self = shift; |
53
|
|
|
|
|
|
|
my ($stdout, $stderr, $exit_code) = capture { |
54
|
3
|
|
|
3
|
|
13891
|
system $self->{'_service_path'}; |
55
|
3
|
|
|
|
|
137
|
}; |
56
|
3
|
100
|
|
|
|
2257
|
if ($stderr) { |
57
|
1
|
|
|
|
|
5
|
$stdout .= $stderr; |
58
|
|
|
|
|
|
|
} |
59
|
3
|
|
|
|
|
32
|
my @commands; |
60
|
3
|
100
|
|
|
|
55
|
if ($stdout =~ m/{([\w\|\-]+)}/ms) { |
|
|
50
|
|
|
|
|
|
61
|
2
|
|
|
|
|
16
|
@commands = split m/\|/ms, $1; |
62
|
|
|
|
|
|
|
} elsif ($stdout =~ m/([\w\-]+)\s*$/ms) { |
63
|
1
|
|
|
|
|
7
|
@commands = $1; |
64
|
|
|
|
|
|
|
} |
65
|
3
|
|
|
|
|
38
|
return sort @commands; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Get service name. |
69
|
|
|
|
|
|
|
sub name { |
70
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
71
|
1
|
|
|
|
|
3
|
return $self->{'service'}; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Start service. |
75
|
|
|
|
|
|
|
sub start { |
76
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
77
|
0
|
|
|
|
|
0
|
return $self->_service_command('start'); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Get status. |
81
|
|
|
|
|
|
|
sub status { |
82
|
2
|
|
|
2
|
1
|
16
|
my $self = shift; |
83
|
2
|
|
|
|
|
6
|
return $self->_service_command('status'); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Stop service. |
87
|
|
|
|
|
|
|
sub stop { |
88
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
89
|
0
|
|
|
|
|
0
|
return $self->_service_command('stop'); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Common command. |
93
|
|
|
|
|
|
|
sub _service_command { |
94
|
2
|
|
|
2
|
|
3
|
my ($self, $command) = @_; |
95
|
|
|
|
|
|
|
my ($stdout, $stderr, $exit_code) = capture { |
96
|
2
|
|
|
2
|
|
10978
|
system $self->{'_service_path'}.' '.$command; |
97
|
2
|
|
|
|
|
57
|
}; |
98
|
2
|
|
|
|
|
1236
|
my $ret_code = $exit_code >> 8; |
99
|
2
|
50
|
|
|
|
9
|
if ($stderr) { |
100
|
0
|
|
|
|
|
0
|
chomp $stderr; |
101
|
0
|
|
|
|
|
0
|
err "Problem with service '$self->{'service'}' $command.", |
102
|
|
|
|
|
|
|
'STDERR', $stderr, |
103
|
|
|
|
|
|
|
'Exit code', $ret_code; |
104
|
|
|
|
|
|
|
} |
105
|
2
|
|
|
|
|
15
|
return $ret_code; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |