From e0b2cf04a43f32c79519a05d8b1ec9373b75db51 Mon Sep 17 00:00:00 2001 From: onefang Date: Tue, 12 Nov 2019 23:38:22 +1000 Subject: Rewrite execute(), and also write a fork(). Now execute() properly returns the status. --- apt-panopticon.lua | 61 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/apt-panopticon.lua b/apt-panopticon.lua index a03e89b..def0197 100755 --- a/apt-panopticon.lua +++ b/apt-panopticon.lua @@ -202,7 +202,26 @@ end local execute = function (s) D(" executing " .. s) - return os.execute(s) + --[[ Damn os.execute() + Lua 5.1 says it returns "a status code, which is system-dependent" + Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. + I'm getting 7168 or 0. No idea what the fuck that is. + local ok, rslt, status = os.execute(s) + ]] + local f = io.popen(s .. ' ; echo "$?"', 'r') + local status = "" + local result = "" + -- The last line will be the command's returned status, collect everything else in result. + for l in f:lines() do + result = result .. status .. "\n" + status = l + end + return 0 + status, result +end + +local fork = function(s) + D(" executing " .. s) + os.execute(s .. " &") end local checkExes = function (exe) @@ -311,23 +330,17 @@ checkHEAD = function (host, URL, r, retry) end local cmd = 'curl -I --retry 0 -s --path-as-is --connect-timeout 15 --max-redirs 0 ' .. IP .. ' ' .. '-o /dev/null -D results/"HEADERS_' .. fname .. '" ' .. hdr .. ' -w "#%{http_code} %{ssl_verify_result} %{url_effective}\\n" ' .. PU.scheme .. '://' .. host .. PU.path .. ' >>results/"STATUS_' .. fname .. '"' - local rslt, status = execute(cmd) - os.execute("sleep 2") + local status, result = execute(cmd) os.execute('cat results/"HEADERS_' .. fname .. '" >>results/"STATUS_' .. fname .. '" 2>/dev/null; rm results/"HEADERS_' .. fname .. '" 2>/dev/null') - if "exit" == rslt then - if 28 == status then - E(" TIMEOUT!", PU.scheme, "", host) - timeouts = timeouts + 1 - checkHEAD(host, URL, r, retry + 1, timeouts) - return - elseif 0 ~= status then - E(" The curl command return an error code of " .. status .. ", consult the curl manual for what this means.", PU.scheme, "", host) - checkHEAD(host, URL, r, retry + 1, timeouts) - return - end - elseif "signal" == rslt then - E(" The curl command was interupted by signal " .. status) - return + if 28 == status then + E(" TIMEOUT " .. timeouts + 1 .. ", retry " .. retry + 1, PU.scheme, "", host) + timeouts = timeouts + 1 + checkHEAD(host, URL, r, retry + 1, timeouts) + return + elseif 0 ~= status then + E(" The curl command return an error code of " .. status .. ", consult the curl manual for what this means.", PU.scheme, "", host) + checkHEAD(host, URL, r, retry + 1, timeouts) + return end local rfile, e = io.open("results/STATUS_" .. fname, "r") local code = "000" @@ -434,7 +447,7 @@ checkHost = function (orig, host, path, ip, file) else if orig == host then D("checkHost " .. orig .. "" .. file) - if testing("IPv4") then execute("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " -o " .. orig .. path .. " " .. file .." &") end + if testing("IPv4") then fork("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " -o " .. orig .. path .. " " .. file) end else D("checkHost " .. orig .. " -> " .. host) end local h = mirrors[ph.host] if nil == h then return end @@ -442,16 +455,16 @@ checkHost = function (orig, host, path, ip, file) if "table" == type(v) then for k1, v1 in pairs(v) do if v1 == "A" then - if testing("IPv4") then execute("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " " .. k1 .. " " .. file .." &") end + if testing("IPv4") then fork("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " " .. k1 .. " " .. file) end elseif v1 == "AAAA" then - if testing("IPv6") then execute("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " [" .. k1 .. "] " .. file .. " &") end + if testing("IPv6") then fork("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " [" .. k1 .. "] " .. file) end end end else if v == "A" then - if testing("IPv4") then execute("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " " .. k .. " " .. file .." &") end + if testing("IPv4") then fork("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " " .. k .. " " .. file) end elseif v == "AAAA" then - if testing("IPv6") then execute("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " [" .. k .. "] " .. file .. " &") end + if testing("IPv6") then fork("ionice -c3 ./apt-panopticon.lua " .. sendArgs .. " " .. orig .. path .. " [" .. k .. "] " .. file) end end end end @@ -472,7 +485,7 @@ local downloads = function (cut, host, URL) cm = cm .. " https://" .. host .. URL .. "/" .. s end for i, s in pairs(releases) do - execute(cm .. " &") + fork(cm) cm = "ionice -c3 " .. downloadLock .. lock:format(s) .. download .. log:format(s, s) .. cd if repoExists(s .. k) then for j, k in pairs(releaseFiles) do @@ -480,7 +493,7 @@ local downloads = function (cut, host, URL) end end end - execute(cm .. " &") + fork(cm) end local getMirrors = function () -- cgit v1.1