File Coverage

blib/lib/SysV/Init/Service.pm
Criterion Covered Total %
statement 52 58 89.6
branch 12 14 85.7
condition n/a
subroutine 12 14 85.7
pod 6 6 100.0
total 82 92 89.1


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