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-d78f3d33393fpull/27/merge
parent
154880ac15
commit
ee5d8db0cd
|
@ -43,6 +43,8 @@ our @ISA = qw(Exporter ZoneMinder::Base);
|
||||||
our %EXPORT_TAGS = (
|
our %EXPORT_TAGS = (
|
||||||
'functions' => [ qw(
|
'functions' => [ qw(
|
||||||
executeShellCommand
|
executeShellCommand
|
||||||
|
getCmdFormat
|
||||||
|
runCommand
|
||||||
getEventPath
|
getEventPath
|
||||||
deleteEventFiles
|
deleteEventFiles
|
||||||
makePath
|
makePath
|
||||||
|
@ -67,6 +69,7 @@ use ZoneMinder::Debug qw(:all);
|
||||||
|
|
||||||
use POSIX;
|
use POSIX;
|
||||||
|
|
||||||
|
# For running general shell commands
|
||||||
sub executeShellCommand( $ )
|
sub executeShellCommand( $ )
|
||||||
{
|
{
|
||||||
my $command = shift;
|
my $command = shift;
|
||||||
|
@ -81,6 +84,112 @@ sub executeShellCommand( $ )
|
||||||
return( $status );
|
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( $ )
|
sub getEventPath( $ )
|
||||||
{
|
{
|
||||||
my $event = shift;
|
my $event = shift;
|
||||||
|
|
|
@ -101,10 +101,6 @@ Info( "Command: $command\n" );
|
||||||
|
|
||||||
my $retval = 0;
|
my $retval = 0;
|
||||||
|
|
||||||
# Determine the appropriate syntax for the su command
|
|
||||||
|
|
||||||
my ( $cmd_prefix, $cmd_suffix ) = getCmdFormat();
|
|
||||||
|
|
||||||
if ( $command eq "state" )
|
if ( $command eq "state" )
|
||||||
{
|
{
|
||||||
Info( "Updating DB: $state->{Name}\n" );
|
Info( "Updating DB: $state->{Name}\n" );
|
||||||
|
@ -239,98 +235,3 @@ if ( $command eq "logrot" )
|
||||||
}
|
}
|
||||||
|
|
||||||
exit( $retval );
|
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 );
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue