diff options
author | Ted Trask <ttrask01@yahoo.com> | 2013-01-01 21:02:37 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2013-01-01 21:02:37 +0000 |
commit | d0c8873fcfad019ba3c70d090f2fe926a61536b3 (patch) | |
tree | 669d7773ee08c2b60c005e393b06821eb67ba246 | |
parent | 5310fb9895c9e0850fc66a025cad38c76f628589 (diff) | |
download | acf-lib-d0c8873fcfad019ba3c70d090f2fe926a61536b3.tar.bz2 acf-lib-d0c8873fcfad019ba3c70d090f2fe926a61536b3.tar.xz |
Replace io.popen with subprocess.call_capture
-rw-r--r-- | apk.lua | 20 | ||||
-rw-r--r-- | processinfo.lua | 44 |
2 files changed, 23 insertions, 41 deletions
@@ -1,15 +1,11 @@ -- apk library module (..., package.seeall) -local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin " +require("subprocess") delete = function(package) local success = false - local cmdresult - local cmd = path .. "apk del " .. package .. " 2>&1" - local f = io.popen( cmd ) - cmdresult = f:read("*a") or "" - f:close() + local code, cmdresult = subprocess.call_capture({"apk", "del", package, stderr=subprocess.STDOUT}) if string.find(cmdresult, "^OK") then cmdresult = "ERROR: Package not found\n"..cmdresult elseif not string.find(cmdresult, "ERROR") then @@ -20,11 +16,7 @@ end install = function(package) local success = true - local cmdresult - local cmd = path .. "apk add " .. package .. " 2>&1" - local f = io.popen( cmd ) - cmdresult = f:read("*a") - f:close() + local code, cmdresult = subprocess.call_capture({"apk", "add", package, stderr=subprocess.STDOUT}) if string.find(cmdresult, "^ERROR") then success = false end @@ -32,11 +24,7 @@ install = function(package) end version = function(package) - local cmdresult - local cmd = path .. "apk info -ve " .. package .. " 2>&1" - local f = io.popen( cmd ) - cmdresult = f:read("*a") - f:close() + local code, cmdresult = subprocess.call_capture({"apk", "info", "-ve", package, stderr=subprocess.STDOUT}) if string.find(cmdresult, "^%s*$") then cmdresult = nil end diff --git a/processinfo.lua b/processinfo.lua index d4f20a9..af5c853 100644 --- a/processinfo.lua +++ b/processinfo.lua @@ -5,8 +5,7 @@ require("posix") fs = require("acf.fs") format = require("acf.format") apk = require("acf.apk") - -local path = "PATH=/usr/bin:/bin:/usr/sbin:/sbin " +require("subprocess") function package_version(packagename) local result = apk.version(packagename) @@ -20,9 +19,7 @@ end function process_autostart(servicename) local result local errtxt = "Not programmed to autostart" - local f = io.popen( "/sbin/rc-update show" ) - local cmdresult = f:read("*a") or "" - f:close() + local code, cmdresult = subprocess.call_capture({"rc-update", "show"}) for line in string.gmatch(cmdresult, "[^\n]+") do if string.match(line, "^%s*"..format.escapemagiccharacters(servicename).."%s+|") then local runlevels = string.match(line, "|(.*)") @@ -42,9 +39,7 @@ end function read_initrunlevels() local config = {} - local f = io.popen( "/sbin/rc-update show -v" ) - local cmdresult = f:read("*a") or "" - f:close() + local code, cmdresult = subprocess.call_capture({"rc-update", "show", "-v"}) for line in string.gmatch(cmdresult, "([^\n]*)\n?") do local service = string.match(line, "^%s*(%S+)") local runlevels = string.match(line, "|%s*(%S.*)") @@ -66,15 +61,14 @@ function add_runlevels(servicename, runlevels) cmderrors = "Invalid service name" else if runlevels and #runlevels > 0 then - local cmd = {path, "rc-update add"} - cmd[#cmd+1] = format.escapespecialcharacters(servicename) + local cmd = {"rc-update", "add"} + cmd[#cmd+1] = servicename for i,lev in ipairs(runlevels) do cmd[#cmd+1] = lev end - cmd[#cmd+1] = "2>&1" - local f = io.popen(table.concat(cmd, " ")) - cmdresult = f:read("*a") - f:close() + cmd.stderr = subprocess.STDOUT + local code + code, cmdresult = subprocess.call_capture(cmd) cmdresult = string.gsub(cmdresult, "\n+$", "") else cmdresult = "No runlevels added" @@ -90,15 +84,14 @@ function delete_runlevels(servicename, runlevels) cmderrors = "Invalid service name" else if runlevels and #runlevels > 0 then - local cmd = {path, "rc-update del"} - cmd[#cmd+1] = format.escapespecialcharacters(servicename) + local cmd = {"rc-update", "del"} + cmd[#cmd+1] = servicename for i,lev in ipairs(runlevels) do cmd[#cmd+1] = lev end - cmd[#cmd+1] = "2>&1" - local f = io.popen(table.concat(cmd, " ")) - cmdresult = f:read("*a") - f:close() + cmd.stderr = subprocess.STDOUT + local code + code, cmdresult = subprocess.call_capture(cmd) cmdresult = string.gsub(cmdresult, "\n+$", "") else cmdresult = "No runlevels deleted" @@ -116,11 +109,12 @@ function daemoncontrol (process, action) elseif not action then cmderrors = "Invalid action" else - local file = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/" .. - format.escapespecialcharacters(process) .. " " .. format.escapespecialcharacters(string.lower(action)) .. " 2>&1" ) - if file ~= nil then - cmdresult = file:read( "*a" ) - file:close() + local res, err = pcall(function() + local code + code, cmdresult = subprocess.call_capture({"/etc/init.d/" .. process, string.lower(action), stderr=subprocess.STDOUT}) + end) + if not res or err then + cmdresult = string.gsub(err or "Unknown failure", ": No such file or directory", "-ash: /etc/init.d/"..process..": not found") end end return cmdresult,cmderrors |