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

This commit is contained in:
aiyion.prime 2021-07-11 22:39:36 +02:00
parent e9e42fa261
commit 436b06d910

View File

@ -202,15 +202,15 @@ function M.subprocess.popen(path, argt, options)
local childfds = {} local childfds = {}
local parentfds = {} local parentfds = {}
for fd, option in pairs(options) do for iostream, option in pairs(options) do
if option == 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 iostream == "stdin" then
childfds[fd] = piper childfds[iostream] = piper
parentfds[fd] = pipew parentfds[iostream] = pipew
else else
childfds[fd] = pipew childfds[iostream] = pipew
parentfds[fd] = piper parentfds[iostream] = piper
end end
end end
end end
@ -224,20 +224,21 @@ function M.subprocess.popen(path, argt, options)
return nil, errmsg, errnum return nil, errmsg, errnum
elseif pid == 0 then elseif pid == 0 then
local null = -1 local null = -1
local stdiostreams = {["stdin"] = 0, ["stdout"] = 1, ["stderr"] = 2}
if M.contains(options, 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, option in pairs(options) do for iostream, option in pairs(options) do
if option == M.subprocess.DEVNULL then if option == M.subprocess.DEVNULL then
posix_unistd.dup2(null, fd) posix_unistd.dup2(null, stdiostreams[iostream])
elseif option == 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[iostream])
posix_unistd.dup2(childfds[fd], fd) posix_unistd.dup2(childfds[iostream], stdiostreams[iostream])
-- close all the dups -- close all the dups
posix_unistd.close(childfds[fd]) posix_unistd.close(childfds[iostream])
end end
end end