Skip to content

Commit

Permalink
Added: Fallback method for sysvinit script that don't provide status …
Browse files Browse the repository at this point in the history
…command (iMSCP::Provider::Service::Sysvinit)

[ci skip]
  • Loading branch information
nuxwin committed Jun 11, 2016
1 parent bde919d commit bca59c4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -8,6 +8,7 @@ BACKEND
Added: beforeMountLogsFolder, afterMountLogsFolder, beforeUnmountLogsFolder and afterUmountMountLogsFolder events
Added: Caller info for __DIE__ and __WARN__ signal handlers
Added: Debug info for loading of listener files (Event::Manager)
Added: Fallback method for sysvinit script that don't provide status command (iMSCP::Provider::Service::Sysvinit)
Added: 'fixpermissions' option for the iMSCP::Dir::make() method
Added: iMSCP::Database::mysql::useDatabase() method - Allow to select database on which we want operate on
Added: iMSCP::DbTasksProcessor - Allows to process db tasks without spawning new process
Expand Down
87 changes: 83 additions & 4 deletions engine/PerlLib/iMSCP/Provider/Service/Sysvinit.pm
Expand Up @@ -27,7 +27,7 @@ use strict;
use warnings;
use Carp;
use File::Spec;
use iMSCP::Debug 'error';
use iMSCP::Debug qw/ debug error /;
use iMSCP::Execute;
use iMSCP::File;
use iMSCP::LsbRelease;
Expand All @@ -36,8 +36,8 @@ use Scalar::Defer;
# Paths in which sysvinit script must be searched
my $initScriptPaths = lazy
{
# Fixme: iMSCP::LsbRelease is Linux specific. We must rewrite it to support all platforms below.
my $id = iMSCP::LsbRelease->getInstance()->getId( 'short' );

if ($id =~ /^(?:FreeBSD|DragonFly)$/) {
[ '/etc/rc.d', '/usr/local/etc/rc.d' ];
} elsif ($id eq 'HP-UX') {
Expand Down Expand Up @@ -74,7 +74,7 @@ sub getInstance

no strict 'refs';
my $instance = \${"${self}::_instance"};
${$instance} = bless ( \my $this, $self ) unless defined ${$instance};
${$instance} = bless ( { }, $self ) unless defined ${$instance};
${$instance};
}

Expand Down Expand Up @@ -230,7 +230,14 @@ sub isRunning
my ($self, $service) = @_;

defined $service or die( 'parameter $service is not defined' );
$self->_exec( $self->getInitScriptPath( $service ), 'status' ) == 0;

if (defined $self->{'_pid_pattern'}) {
my $ret = $self->_getPid( $self->{'_pid_pattern'} );
$self->{'_pid_pattern'} = undef;
return $ret;
}

$self->_exec( $self->getInitScriptPath( $service ), 'status' );
}

=item getInitScriptPath($service)
Expand All @@ -250,6 +257,24 @@ sub getInitScriptPath
$self->_searchInitScript( $service );
}

=item setPidPattern($pattern)
Set PID pattern for next _getPid() invocation
Param string $pattern Process PID pattern
Return int 0
=cut

sub setPidPattern
{
my ($self, $pattern) = @_;

defined $pattern or die( '$pattern parameter is not defined' );
$self->{'_pid_pattern'} = $pattern;
0;
}

=back
=head1 PRIVATE METHODS
Expand Down Expand Up @@ -320,6 +345,60 @@ sub _exec
$ret;
}

=item _getPs()
Get proper 'ps' invocation for the platform
Return int Command exit status
=cut

sub _getPs
{
my ($self) = shift;

# Fixme: iMSCP::LsbRelease is Linux specific. We must rewrite it to support all platforms below.
my $id = iMSCP::LsbRelease->getInstance()->getId( 'short' );
if ($id eq 'OpenWrt') {
'ps www';
} elsif ($id =~ /^(?:FreeBSD|NetBSD|OpenBSD|Darwin|DragonFly)$/) {
'ps auxwww';
} else {
'ps -ef'
}
}

=item _getPid($pattern)
Get the process ID for a running process.
Param string $pattern
Return int|undef Process ID or undef if not found
=cut

sub _getPid
{
my ($self, $pattern) = @_;

defined $pattern or die( '$pattern parameter is not defined' );

my $ps = $self->_getPs();
open my $fh, '-|', $ps or die( sprintf( 'Could not pipe to %s: %s', $ps, $! ) );

my $regex = qr/$pattern/;
while(<$fh>) {
if (/$regex/) {
my $line = $_;
debug( sprintf( 'Process matched line: %s', $line ) );
$line =~ s/^\s+//;
return (split /\s+/, $line)[1];
}
}

undef;
}

=back
=head1 AUTHOR
Expand Down
8 changes: 4 additions & 4 deletions engine/PerlLib/iMSCP/Service.pm
Expand Up @@ -304,11 +304,11 @@ sub isSystemd
$init eq 'systemd';
}

=item getProvider($providerName)
=item getProvider($providerName = $init)
Get a particular service provider instance
Get service provider instance
Param string Provider name (sysvinit|upstart|systemd)
Param string $providerName OPTIONAL Provider name (sysvinit|upstart|systemd)
Return iMSCP::Provider::Service::Sysvinit
=cut
Expand All @@ -317,7 +317,7 @@ sub getProvider
{
my ($self, $providerName) = @_;

$providerName = ucfirst( lc( $providerName ) );
$providerName = ucfirst( lc( $providerName // $init ) );
my $id = iMSCP::LsbRelease->getInstance->getId( 'short' );
$id = 'Debian' if $id eq 'Ubuntu';
my $provider = "iMSCP::Provider::Service::${id}::${providerName}";
Expand Down

0 comments on commit bca59c4

Please sign in to comment.