From 4f99c4fe6c106420fc53bad33acf355a603e822c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lesimple?= Date: Tue, 21 Jun 2022 14:54:16 +0000 Subject: [PATCH] fix: ping: force a deadline, and restore default sighandlers --- bin/plugin/open/ping | 17 +++++++++++++---- doc/sphinx/plugins/open/ping.rst | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/bin/plugin/open/ping b/bin/plugin/open/ping index 38c8760..783990d 100755 --- a/bin/plugin/open/ping +++ b/bin/plugin/open/ping @@ -25,7 +25,7 @@ Usage: --osh SCRIPT_NAME [--host HOST] [-c COUNT] [-s PKTSZ] [-t TTL] [-w TIMEOU --host HOST Remote host to ping -c COUNT Number of pings to send (default: infinite) -t TTL TTL to set in the ICMP packet (default: OS dependent) - -w TIMEOUT Exit unconditionally after this amount of seconds + -w TIMEOUT Exit unconditionally after this amount of seconds (default & max: 86400) EOF ); @@ -45,6 +45,11 @@ if (not $host) { osh_exit 'ERR_MISSING_PARAMETER', "Missing required host parameter"; } +# restore default handlers (exit) for HUP and PIPE, we don't want to have this +# plugin looping endlessly in the void when there's no longer a terminal attached +$SIG{'PIPE'} = 'DEFAULT'; +$SIG{'HUP'} = 'DEFAULT'; + my @command = qw{ ping }; if ($count and $count > 0) { push @command, ('-c', $count); @@ -55,9 +60,13 @@ if ($packetsize and $packetsize > 0 and $packetsize < 10000) { if ($ttl and $ttl > 0 and $ttl < 256) { push @command, ('-t', $ttl); } -if ($deadline and $deadline > 0 and $deadline <= 3600) { - push @command, (OVH::Bastion::is_freebsd() ? '-t' : '-w', $deadline); -} + +# ensure there's always a deadline, to avoid having a plugin running for months, +# especially because this one caches stdout to be able to compute stats at the +# end, and return these in the JSON output +$deadline = 86400 if (!$deadline || $deadline < 0 || $deadline > 86400); +push @command, (OVH::Bastion::is_freebsd() ? '-t' : '-w', $deadline); + push @command, $host; osh_info "Pinging $host..."; diff --git a/doc/sphinx/plugins/open/ping.rst b/doc/sphinx/plugins/open/ping.rst index c6f2191..f568441 100644 --- a/doc/sphinx/plugins/open/ping.rst +++ b/doc/sphinx/plugins/open/ping.rst @@ -28,5 +28,5 @@ Ping a remote host from the bastion .. option:: -w TIMEOUT - Exit unconditionally after this amount of seconds + Exit unconditionally after this amount of seconds (default & max: 86400)