2010-06-18 14:36:19 +02:00
|
|
|
#!/usr/bin/perl
|
2023-02-06 12:47:36 +01:00
|
|
|
################################################################################
|
|
|
|
# pandora_exec - Execute a command with a time limit.
|
2010-06-18 14:36:19 +02:00
|
|
|
#
|
2023-06-23 23:59:49 +02:00
|
|
|
# Copyright (c) 2008-2023 Pandora FMS S.L.
|
2010-06-18 14:36:19 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; version 2.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2023-02-06 12:47:36 +01:00
|
|
|
################################################################################
|
2010-06-18 14:36:19 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2023-02-06 12:47:36 +01:00
|
|
|
use POSIX qw(WEXITSTATUS WIFEXITED);
|
2010-06-18 14:36:19 +02:00
|
|
|
|
2023-02-06 12:47:36 +01:00
|
|
|
# Check command line arguments.
|
2010-06-18 14:36:19 +02:00
|
|
|
if ($#ARGV < 1) {
|
2023-02-06 12:47:36 +01:00
|
|
|
print("Usage: $0 <timeout in seconds> <command>\n");
|
2010-06-18 14:36:19 +02:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @opts = @ARGV;
|
|
|
|
my $timeout = shift(@opts);
|
2023-02-06 12:47:36 +01:00
|
|
|
my $command = ($0 =~ m/_agent_exec$/) ? # For backward compatibility with pandora_agent.
|
|
|
|
join(' ', @opts) :
|
|
|
|
join(' ', map { quotemeta($_) } @opts);
|
|
|
|
|
|
|
|
# Fork:
|
|
|
|
# * The child will run the command.
|
|
|
|
# * The parent will timeout if needed
|
|
|
|
# and exit with the appropriate exit status.
|
|
|
|
my $pid = fork();
|
|
|
|
if ($pid == 0) {
|
|
|
|
setpgrp();
|
|
|
|
exec($command);
|
|
|
|
} else {
|
|
|
|
eval {
|
|
|
|
local $SIG{ALRM} = sub { kill(9, -$pid); exit 1; };
|
|
|
|
alarm $timeout;
|
|
|
|
waitpid($pid, 0);
|
|
|
|
alarm 0;
|
|
|
|
if (WIFEXITED(${^CHILD_ERROR_NATIVE})) {
|
|
|
|
exit WEXITSTATUS(${^CHILD_ERROR_NATIVE});
|
|
|
|
}
|
|
|
|
};
|
2010-06-18 14:36:19 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 12:47:36 +01:00
|
|
|
exit 1;
|