Skip to content

Commit

Permalink
Force command to also timeout (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard authored Nov 21, 2017
1 parent 161a2ea commit 9338302
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ core:
'use parent' and as fields pragma is not used (see 'base' man)
* Logger refactoring: no more an Exporter based class to simplify its usage and
as Logger object should be commonly shared everywhere it is used.
* Fix command run to also time out while an alarm has been set
* Few code refactoring

inventory:
Expand All @@ -22,6 +23,7 @@ inventory:
* Fix #349: Include last logged user as usual computer user on win32 platform
* Linux distro: Add support for reading os-release file and removing LSB support
* Fix Solaris drives df output parsing adding better zfs handling
* Make backend-collect-timeout working even while waiting on command output

netdiscovery/netinventory:
* Bump NetDiscovery & NetInventory task version to 2.4
Expand Down
9 changes: 6 additions & 3 deletions lib/FusionInventory/Agent/Tools.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ our @EXPORT = qw(
slurp
);

my $nowhere = $OSNAME eq 'MSWin32' ? 'nul' : '/dev/null';

# this trigger some errors under win32:
# Anonymous function called in forbidden scalar context
if ($OSNAME ne 'MSWin32') {
Expand Down Expand Up @@ -296,6 +294,7 @@ sub getDirectoryHandle {
return $handle;
}

my $cmdtemplate = $OSNAME eq 'MSWin32' ? "%s 2>nul" : "exec %s 2>/dev/null";
sub getFileHandle {
my (%params) = @_;

Expand All @@ -320,12 +319,16 @@ sub getFileHandle {
local $ENV{LANG} = 'C';
# Ignore 'Broken Pipe' warnings on Solaris
local $SIG{PIPE} = 'IGNORE' if $OSNAME eq 'solaris';
if (!open $handle, '-|', $params{command} . " 2>$nowhere") {
my $command = sprintf($cmdtemplate, $params{command});
my $cmdpid = open($handle, '-|', $command);
if (!$cmdpid) {
$params{logger}->error(
"Can't run command $params{command}: $ERRNO"
) if $params{logger};
return;
}
# Kill command if a timeout was set
$SIG{ALRM} = sub { kill 'KILL', $cmdpid ; die "alarm\n"; } if $SIG{ALRM};
last SWITCH;
}
if ($params{string}) {
Expand Down

0 comments on commit 9338302

Please sign in to comment.