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

This commit is contained in:
aiyion.prime 2021-07-11 22:17:26 +02:00
parent 63d4409316
commit a66e8b815d

View File

@ -198,12 +198,12 @@ 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.subprocess.popen(policies, path, ...) function M.subprocess.popen(path, argt, options)
local childfds = {} local childfds = {}
local parentfds = {} local parentfds = {}
for fd, policy in pairs(policies) do for fd, option in pairs(options) do
if policy == M.subprocess.PIPE then if option == 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,15 +224,15 @@ function M.subprocess.popen(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.subprocess.DEVNULL) then if M.contains(options, 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_RDWR) null = posix_fcntl.open('/dev/null', posix_fcntl.O_RDWR)
end end
for fd, policy in pairs(policies) do for fd, option in pairs(options) do
if policy == M.subprocess.DEVNULL then if option == M.subprocess.DEVNULL then
posix_unistd.dup2(null, fd) posix_unistd.dup2(null, fd)
elseif policy == M.subprocess.PIPE then elseif option == 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)
@ -246,7 +246,7 @@ function M.subprocess.popen(policies, path, ...)
posix_unistd.close(null) posix_unistd.close(null)
end end
posix_unistd.execp(path, ...) posix_unistd.execp(path, argt)
posix_unistd._exit(127) posix_unistd._exit(127)
end end