fixup! gluon-core: implement popen3() in gluon/util.lua

This commit is contained in:
aiyion.prime 2021-07-06 12:22:25 +02:00
parent c1df15690b
commit 0b3f0e9b4e

View File

@ -190,20 +190,20 @@ function M.log(message, verbose)
posix_syslog.syslog(posix_syslog.LOG_INFO, message) posix_syslog.syslog(posix_syslog.LOG_INFO, message)
end end
M.PipePolicies = { M.subprocess = {}
DISCARD=-1,
INHERIT=0, M.subprocess.DEVNULL = -1
CREATE=1 M.subprocess.INHERIT = nil
} M.subprocess.PIPE = 1
-- Execute a program found using command PATH search, like the shell. -- Execute a program found using command PATH search, like the shell.
-- Return the pid, as well as the I/O streams as pipes or nil on error. -- Return the pid, as well as the I/O streams as pipes or nil on error.
function M.popen3(policies, path, ...) function M.subprocess.popen(policies, path, ...)
local childfds = {} local childfds = {}
local parentfds = {} local parentfds = {}
for fd, policy in pairs(policies) do for fd, policy in pairs(policies) do
if policy==M.PipePolicies.CREATE then if policy==M.subprocess.PIPE then
local piper, pipew = posix_unistd.pipe() local piper, pipew = posix_unistd.pipe()
if fd==posix_unistd.STDIN_FILENO then if fd==posix_unistd.STDIN_FILENO then
childfds[fd]=piper childfds[fd]=piper
@ -224,17 +224,17 @@ function M.popen3(policies, path, ...)
return nil, errmsg, errnum return nil, errmsg, errnum
elseif pid == 0 then elseif pid == 0 then
local null=-1 local null=-1
if M.contains(policies, M.PipePolicies.DISCARD) then if M.contains(policies, M.subprocess.DEVNULL) then
-- only open, if there's anything to discard -- only open, if there's anything to discard
null = posix_fcntl.open('/dev/null', posix_fcntl.O_WRONLY) null = posix_fcntl.open('/dev/null', posix_fcntl.O_WRONLY)
end end
for fd, policy in pairs(policies) do for fd, policy in pairs(policies) do
if policy==M.PipePolicies.DISCARD then if policy==M.subprocess.DEVNULL then
if fd~=posix_unistd.STDIN_FILENO then if fd~=posix_unistd.STDIN_FILENO then
posix_unistd.dup2(null, fd) posix_unistd.dup2(null, fd)
end end
elseif policy==M.PipePolicies.CREATE then elseif policy==M.subprocess.PIPE then
-- only close these, if they exist -- only close these, if they exist
posix_unistd.close(parentfds[fd]) posix_unistd.close(parentfds[fd])
posix_unistd.dup2(childfds[fd], fd) posix_unistd.dup2(childfds[fd], fd)