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

This commit is contained in:
aiyion.prime 2021-07-06 11:37:53 +02:00
parent ba2886e487
commit e2ec7f6e5c

View File

@ -199,24 +199,24 @@ M.PipePolicies = {
-- 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.popen3(policies, path, ...)
local intern = {} local childfds = {}
local extern = {} local parentfds = {}
for fd, policy in pairs(policies) do for fd, policy in pairs(policies) do
if M.PipePolicies.CREATE==policy then if M.PipePolicies.CREATE==policy then
local piper, pipew = posix_unistd.pipe() local piper, pipew = posix_unistd.pipe()
if posix_unistd.STDIN_FILENO==fd then if posix_unistd.STDIN_FILENO==fd then
intern[fd]=piper childfds[fd]=piper
extern[fd]=pipew parentfds[fd]=pipew
else else
intern[fd]=pipew childfds[fd]=pipew
extern[fd]=piper parentfds[fd]=piper
end end
end end
end end
-- intern: r0, w1, w2 -- childfds: r0, w1, w2
-- extern: w0, r1, r2 -- parentfds: w0, r1, r2
local pid, errmsg, errnum = posix_unistd.fork() local pid, errmsg, errnum = posix_unistd.fork()
@ -236,10 +236,10 @@ function M.popen3(policies, path, ...)
end end
elseif M.PipePolicies.CREATE==policy then elseif M.PipePolicies.CREATE==policy then
-- only close these, if they exist -- only close these, if they exist
posix_unistd.close(extern[fd]) posix_unistd.close(parentfds[fd])
posix_unistd.dup2(intern[fd], fd) posix_unistd.dup2(childfds[fd], fd)
-- close all the dups -- close all the dups
posix_unistd.close(intern[fd]) posix_unistd.close(childfds[fd])
end end
end end
@ -252,10 +252,10 @@ function M.popen3(policies, path, ...)
posix_unistd._exit(127) posix_unistd._exit(127)
end end
for _, v in pairs(intern) do for _, v in pairs(childfds) do
posix_unistd.close(v) posix_unistd.close(v)
end end
return pid, extern return pid, parentfds
end end
return M return M