Moved the runCommand function into the library plus shell detection. Should have been there anyway.

git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@3343 e3e1d417-86f3-4887-817a-d78f3d33393f
This commit is contained in:
stan 2011-05-16 22:54:37 +00:00
parent 154880ac15
commit ee5d8db0cd
2 changed files with 109 additions and 99 deletions

View File

@ -43,6 +43,8 @@ our @ISA = qw(Exporter ZoneMinder::Base);
our %EXPORT_TAGS = (
'functions' => [ qw(
executeShellCommand
getCmdFormat
runCommand
getEventPath
deleteEventFiles
makePath
@ -67,6 +69,7 @@ use ZoneMinder::Debug qw(:all);
use POSIX;
# For running general shell commands
sub executeShellCommand( $ )
{
my $command = shift;
@ -81,6 +84,112 @@ sub executeShellCommand( $ )
return( $status );
}
sub getCmdFormat()
{
Debug( "Testing valid shell syntax\n" );
my ( $name ) = getpwuid( $> );
if ( $name eq ZM_WEB_USER )
{
Debug( "Running as '$name', su commands not needed\n" );
return( "" );
}
my $null_command = "true";
my $prefix = "sudo -u ".ZM_WEB_USER." ";
my $suffix = "";
my $command = $prefix.$null_command.$suffix;
Debug( "Testing \"$command\"\n" );
$command .= " > /dev/null 2>&1";
my $output = qx($command);
my $status = $? >> 8;
if ( !$status )
{
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
return( $prefix, $suffix );
}
else
{
chomp( $output );
Debug( "Test failed, '$output'\n" );
$prefix = "su ".ZM_WEB_USER." --shell=/bin/sh --command='";
$suffix = "'";
$command = $prefix.$null_command.$suffix;
Debug( "Testing \"$command\"\n" );
my $output = qx($command);
my $status = $? >> 8;
if ( !$status )
{
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
return( $prefix, $suffix );
}
else
{
chomp( $output );
Debug( "Test failed, '$output'\n" );
$prefix = "su ".ZM_WEB_USER." -c '";
$suffix = "'";
$command = $prefix.$null_command.$suffix;
Debug( "Testing \"$command\"\n" );
$output = qx($command);
$status = $? >> 8;
if ( !$status )
{
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
return( $prefix, $suffix );
}
else
{
chomp( $output );
Debug( "Test failed, '$output'\n" );
}
}
}
Error( "Unable to find valid 'su' syntax\n" );
exit( -1 );
}
our $testedShellSyntax = 0;
our ( $cmdPrefix, $cmdSuffix );
# For running ZM daemons etc
sub runCommand( $ )
{
if ( !$testedShellSyntax )
{
# Determine the appropriate syntax for the su command
( $cmdPrefix, $cmdSuffix ) = getCmdFormat();
$testedShellSyntax = !undef;
}
my $command = shift;
$command = ZM_PATH_BIN."/".$command;
if ( $cmdPrefix )
{
$command = $cmdPrefix.$command.$cmdSuffix;
}
Debug( "Command: $command\n" );
my $output = qx($command);
my $status = $? >> 8;
chomp( $output );
if ( $status || zmDbgLevel() > 0 )
{
if ( $status )
{
Error( "Unable to run \"$command\", output is \"$output\"\n" );
exit( -1 );
}
else
{
Debug( "Output: $output\n" );
}
}
return( $output );
}
sub getEventPath( $ )
{
my $event = shift;

View File

@ -101,10 +101,6 @@ Info( "Command: $command\n" );
my $retval = 0;
# Determine the appropriate syntax for the su command
my ( $cmd_prefix, $cmd_suffix ) = getCmdFormat();
if ( $command eq "state" )
{
Info( "Updating DB: $state->{Name}\n" );
@ -239,98 +235,3 @@ if ( $command eq "logrot" )
}
exit( $retval );
sub getCmdFormat
{
Debug( "Testing valid shell syntax\n" );
my ( $name ) = getpwuid( $> );
if ( $name eq ZM_WEB_USER )
{
Debug( "Running as '$name', su commands not needed\n" );
return( "" );
}
my $null_command = "true";
my $prefix = "sudo -u ".ZM_WEB_USER." ";
my $suffix = "";
my $command = $prefix.$null_command.$suffix;
Debug( "Testing \"$command\"\n" );
$command .= " > /dev/null 2>&1";
my $output = qx($command);
my $status = $? >> 8;
if ( !$status )
{
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
return( $prefix, $suffix );
}
else
{
chomp( $output );
Debug( "Test failed, '$output'\n" );
$prefix = "su ".ZM_WEB_USER." --shell=/bin/sh --command='";
$suffix = "'";
$command = $prefix.$null_command.$suffix;
Debug( "Testing \"$command\"\n" );
my $output = qx($command);
my $status = $? >> 8;
if ( !$status )
{
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
return( $prefix, $suffix );
}
else
{
chomp( $output );
Debug( "Test failed, '$output'\n" );
$prefix = "su ".ZM_WEB_USER." -c '";
$suffix = "'";
$command = $prefix.$null_command.$suffix;
Debug( "Testing \"$command\"\n" );
$output = qx($command);
$status = $? >> 8;
if ( !$status )
{
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
return( $prefix, $suffix );
}
else
{
chomp( $output );
Debug( "Test failed, '$output'\n" );
}
}
}
Error( "Unable to find valid 'su' syntax\n" );
exit( -1 );
}
sub runCommand
{
my $command = shift;
$command = ZM_PATH_BIN."/".$command;
if ( $cmd_prefix )
{
$command = $cmd_prefix.$command.$cmd_suffix;
}
Debug( "Command: $command\n" );
my $output = qx($command);
my $status = $? >> 8;
chomp( $output );
if ( $status || DBG_LEVEL > 0 )
{
if ( $status )
{
Error( "Unable to run \"$command\", output is \"$output\"\n" );
exit( -1 );
}
else
{
Debug( "Output: $output\n" );
}
}
return( $output );
}