File Coverage

blib/lib/SysV/Init/Service.pm
Criterion Covered Total %
statement 54 60 90.0
branch 12 14 85.7
condition n/a
subroutine 14 16 87.5
pod 6 6 100.0
total 86 96 89.5


line stmt bran cond sub pod time code
1             package SysV::Init::Service;
2              
3             # Pragmas.
4 6     6   174996 use strict;
  6         11  
  6         155  
5 6     6   32 use warnings;
  6         10  
  6         187  
6              
7             # Modules.
8 6     6   4998 use Capture::Tiny qw(capture);
  6         198741  
  6         422  
9 6     6   1619 use Class::Utils qw(set_params);
  6         51294  
  6         296  
10 6     6   165 use English qw(-no_match_vars);
  6         14  
  6         43  
11 6     6   2857 use Error::Pure qw(err);
  6         13  
  6         269  
12 6     6   1673 use File::Spec::Functions qw(catfile);
  6         1542  
  6         4369  
13              
14             # Version.
15             our $VERSION = 0.06;
16              
17             # Construct.
18             sub new {
19 12     12 1 12624 my ($class, @params) = @_;
20              
21             # Create object.
22 12         43 my $self = bless {}, $class;
23              
24             # Service.
25 12         54 $self->{'service'} = undef;
26              
27             # Service directory.
28 12         43 $self->{'service_dir'} = '/etc/init.d';
29              
30             # Process parameters.
31 12         94 set_params($self, @params);
32              
33             # Check for service.
34 10 100       198 if (! defined $self->{'service'}) {
35 1         4 err "Parameter 'service' is required.";
36             }
37 9 100       53 if ($self->{'service'} =~ m/\.sh$/ms) {
38 1         5 err "Service with .sh suffix doesn't possible.";
39             }
40             $self->{'_service_path'} = catfile($self->{'service_dir'},
41 8         83 $self->{'service'});
42 8 100       331 if (! -x $self->{'_service_path'}) {
43 1         6 err "Service '$self->{'service'}' doesn't present.";
44             }
45              
46             # Object.
47 7         36 return $self;
48             }
49              
50             # Get service commands.
51             sub commands {
52 3     3 1 48 my $self = shift;
53             my ($stdout, $stderr, $exit_code) = capture {
54 3     3   33439 system $self->{'_service_path'};
55 3         203 };
56 3 100       4558 if ($stderr) {
57 1         8 $stdout .= $stderr;
58             }
59 3         7 my @commands;
60 3 100       122 if ($stdout =~ m/{([\w\|\-]+)}/ms) {
    50          
61 2         33 @commands = split m/\|/ms, $1;
62             } elsif ($stdout =~ m/([\w\-]+)\s*$/ms) {
63 1         16 @commands = $1;
64             }
65 3         86 return sort @commands;
66             }
67              
68             # Get service name.
69             sub name {
70 1     1 1 7 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 46 my $self = shift;
83 2         11 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   7 my ($self, $command) = @_;
95             my ($stdout, $stderr, $exit_code) = capture {
96 2     2   294899 system $self->{'_service_path'}.' '.$command;
97 2         104 };
98 2         3298 my $ret_code = $exit_code >> 8;
99 2 50       23 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         46 return $ret_code;
106             }
107              
108             1;
109              
110             __END__