diff options
author | Ted Trask <ttrask01@yahoo.com> | 2012-12-11 16:09:47 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2012-12-11 16:09:47 +0000 |
commit | 11327093bfa303006f3d8def04aa03f55e75d6f7 (patch) | |
tree | 9410cc3a95fde6c683c4b4f74a1daf4f49805af3 /lib/modelfunctions.lua | |
parent | 0b7cc381df2d64a74159e0f4e405e045e9a72a6d (diff) | |
download | acf-core-11327093bfa303006f3d8def04aa03f55e75d6f7.tar.bz2 acf-core-11327093bfa303006f3d8def04aa03f55e75d6f7.tar.xz |
modelfunctions - add run_executable function using lua-subprocess
Diffstat (limited to 'lib/modelfunctions.lua')
-rw-r--r-- | lib/modelfunctions.lua | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/modelfunctions.lua b/lib/modelfunctions.lua index bc98ef4..8981bdd 100644 --- a/lib/modelfunctions.lua +++ b/lib/modelfunctions.lua @@ -4,6 +4,8 @@ module(..., package.seeall) fs = require("acf.fs") format = require("acf.format") processinfo = require("acf.processinfo") +require("posix") +require("subprocess") function getenabled(servicename) local result = cfe({ label = "Program status", name=servicename }) @@ -232,3 +234,36 @@ function write_file_with_audit (self, path, str) return end + +-- Run an executable and return the output and errtxt +-- args should be an array where args[1] is the executable +-- output will never be nil +-- errtxt will be nil for success and non-nil for failure +run_executable = function(args) + local output = "" + local errtxt + local res, err = pcall(function() + -- For security, set the path + posix.setenv("PATH", "/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin") + + args.stdout = subprocess.PIPE + args.stderr = subprocess.PIPE + local proc, errmsg, errno = subprocess.popen(args) + if proc then + proc:wait() + output = (proc.stdout:read("*a")) or "" + proc.stdout:close() + errtxt = (proc.stderr:read("*a")) + proc.stderr:close() + if proc.exitcode == 0 and string.find(errtxt, "^%s*$") then + errtxt = nil + end + else + errtxt = errmsg or "Unknown failure" + end + end) + if not res or err then + errtxt = err or "Unknown failure" + end + return output, errtxt +end |