From a06e989281515344c562675b8b311ae627b96661 Mon Sep 17 00:00:00 2001 From: onefang Date: Wed, 10 Nov 2021 02:33:07 +1000 Subject: io.popen returns nil sometimes, deal with that. --- apt-panopticommon.lua | 19 ++++++++++++++++--- apt-panopticon.lua | 37 +++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/apt-panopticommon.lua b/apt-panopticommon.lua index 221c7e3..9de97b4 100644 --- a/apt-panopticommon.lua +++ b/apt-panopticommon.lua @@ -483,6 +483,17 @@ local E = APT.E local C = APT.C +APT.readCmd = function(cmd) + local result = {} + local output = io.popen(cmd) + if nil ~= output then + for l in output:lines() do + table.insert(result, l) + end + end + return result +end + APT.debians = {} APT.mirrors = {} @@ -559,10 +570,10 @@ 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 = io.popen(self.cmd .. ' ; echo "$?"', 'r') + local f = APT.readCmd(self.cmd .. ' ; echo "$?"', 'r') -- The last line will be the command's returned status, collect everything else in result. self.status = '' -- Otherwise the result starts with 0. - for l in f:lines() do + for i,l in ipairs(f) do self.result = self.result .. self.status .. "\n" self.status = l end @@ -579,7 +590,9 @@ APT.exe = function(c) end APT.checkExes = function (exe) - local count = io.popen('ps x | grep "' .. exe .. '" | grep -v " grep " | grep -v "flock -n apt-panopticon.lock " | wc -l'):read("*l") + local count = 0 + local ps = io.popen('ps x | grep "' .. exe .. '" | grep -v " grep " | grep -v "flock -n apt-panopticon.lock " | wc -l') + if nil ~= ps then count = ps:read("*l") end D(count .. " " .. exe .. " commands still running.") return tonumber(count) end diff --git a/apt-panopticon.lua b/apt-panopticon.lua index 8543210..482a2ac 100755 --- a/apt-panopticon.lua +++ b/apt-panopticon.lua @@ -175,24 +175,21 @@ gatherIPs = function (host) -- Takes about 30 seconds to look up the lot. -- I tested using dig's -f option, it didn't seem much faster. -- The sort -r assumes that deb.devuan.org is the first alphabetically. - local dig = io.popen('dig +keepopen +noall +nottlid +answer ' .. host .. ' A ' .. host .. ' AAAA ' .. host .. ' CNAME ' .. host .. ' SRV | sort -r | uniq') - repeat - IPs = dig:read("*l") - if nil ~= IPs then - for k, t, v in IPs:gmatch("([%w_%-%.]*)%.%s*IN%s*(%a*)%s*(.*)") do - if "." == v:sub(-1, -1) then v = v:sub(1, -2) end - if nil == IP[k] then IP[k] = {} end - IP[k][v] = t - D(" DNS record " .. host .. " == " .. k .. " type " .. t .. " -> " .. v) - if t == "CNAME" then - gatherIPs(v) - IP[k][v] = IP[v] - elseif v == "SRV" then - print("SVR record found, now what do we do?") - end + local dig = APT.readCmd('dig +keepopen +noall +nottlid +answer ' .. host .. ' A ' .. host .. ' AAAA ' .. host .. ' CNAME ' .. host .. ' SRV | sort -r | uniq') + for i,IPs in ipairs(dig) do + for k, t, v in IPs:gmatch("([%w_%-%.]*)%.%s*IN%s*(%a*)%s*(.*)") do + if "." == v:sub(-1, -1) then v = v:sub(1, -2) end + if nil == IP[k] then IP[k] = {} end + IP[k][v] = t + D(" DNS record " .. host .. " == " .. k .. " type " .. t .. " -> " .. v) + if t == "CNAME" then + gatherIPs(v) + IP[k][v] = IP[v] + elseif v == "SRV" then + print("SVR record found, now what do we do?") end end - until nil == IPs + end end -- If this is the DNS-RR domain name, gather the IPs for the mirrors that mirror_list.txt says should be in it. @@ -1179,8 +1176,8 @@ else local APT_args = APT.args local APT_logFile = APT.logFile local debians = {} - local srvs = io.popen('ls -1 results/*.lua') - for l in srvs:lines() do + local srvs = APT.readCmd('ls -1 results/*.lua') + for ii,l in ipairs(srvs) do local hst = l:sub(9, -5) if nil ~= l:find('_R%.lua') then hst = hst:sub(1, -3) end if (hst:find('_') == nil) and (nil == APT.mirrors[hst]) then @@ -1197,8 +1194,8 @@ else end end end - local files = io.popen('ls -1 results/LOG_' .. hst .. '_*.html') - for ll in files:lines() do + local files = APT.readCmd('ls -1 results/LOG_' .. hst .. '_*.html') + for iii,ll in ipairs(files) do local dn = false for i, a in pairs(ips) do if type(a) == 'table' then -- cgit v1.1