From fab1f109d0564b44c950055d3233d6e7a1f3be81 Mon Sep 17 00:00:00 2001 From: onefang Date: Thu, 23 Feb 2023 02:26:36 +1000 Subject: Add yet another timeout watchdog. --- apt-panopticommon.lua | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'apt-panopticommon.lua') diff --git a/apt-panopticommon.lua b/apt-panopticommon.lua index da1fc3d..a2def5a 100644 --- a/apt-panopticommon.lua +++ b/apt-panopticommon.lua @@ -134,7 +134,7 @@ APT.parseArgs = function(args) local arg = {} local sendArgs = "" -- A special test to disable IPv6 tests if IPv6 isn't available. - if 1 == APT.exe('ip -6 addr | grep inet6 | grep " global"'):Do().status then + if 1 == APT.exe('ip -6 addr | grep inet6 | grep " global"'):timeout():Do().status then table.insert(args, '--tests=-IPv6') end if 0 ~= #(args) then @@ -529,7 +529,7 @@ APT.tested = function(prot, test, host) end APT.exe = function(c) - local exe = {status = 0, result = '', log = true, cmd = c .. ' '} + local exe = {status = 0, result = '', log = true, cmd = c .. ' ', command = c} function exe:log() self.log = true @@ -543,6 +543,16 @@ APT.exe = function(c) end return self end + function exe:timeout(c) + -- timeout returns a status of - command status if --preserve-status; "128+9" (actually 137) if --kill-after ends up being done; 124 if it had to TERM; command status if all went well. + -- --kill-after means "send KILL after TERM fails. + if nil == c then + self.cmd = 'timeout --kill-after=10.0 --foreground -v 42.0s ' .. self.cmd + else + self.cmd = 'timeout --kill-after=10.0 --foreground -v ' .. c .. ' ' .. self.cmd + end + return self + end function exe:also(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ';' .. c .. ' ' @@ -579,17 +589,28 @@ APT.exe = function(c) I'm getting 7168 or 0. No idea what the fuck that is. local ok, rslt, status = os.execute(s) ]] - local f = APT.readCmd(self.cmd .. ' ; echo "$?"', 'r') + local f = APT.readCmd(self.cmd, 'r') -- The last line will be the command's returned status, collect everything else in result. self.status = '' -- Otherwise the result starts with 0. + self.result = '\n' + for i,l in ipairs(f) do + self.result = self.result .. l .. "\n" + end + f = APT.readCmd('echo "$?"', 'r') for i,l in ipairs(f) do - self.result = self.result .. self.status .. "\n" - self.status = l + self.status = tonumber(l) + if (137 == self.status) or (124 == self.status) then + print("timeout killed " .. self.status .. ' ' .. self.command) + E("timeout killed " .. self.status .. ' ' .. self.command) + elseif (0 ~= self.status) then + print("status |" .. self.status .. '| ' .. self.command) + E("status |" .. self.status .. '| ' .. self.command) + end end - self.status = tonumber(self.status) return self end - function exe:fork() + function exe:fork(host) + if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end self.cmd = '{ ' .. self.cmd .. '; } &' if true == self.log then D(" forking -   " .. self.cmd .. "") end os.execute(self.cmd) -- cgit v1.1