From 11327093bfa303006f3d8def04aa03f55e75d6f7 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Tue, 11 Dec 2012 16:09:47 +0000 Subject: modelfunctions - add run_executable function using lua-subprocess --- lib/modelfunctions.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/modelfunctions.lua') 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 -- cgit v1.2.3