From e39cbcbda11e5ef5891a59e75dd370fb878dd7cf Mon Sep 17 00:00:00 2001 From: Matthias Schiffer <mschiffer@universe-factory.net> Date: Fri, 10 Feb 2017 22:09:59 +0100 Subject: [PATCH] gluon-core: gluon.util: make exec() replace all stdio files with /dev/null --- .../luasrc/usr/lib/lua/gluon/util.lua | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua b/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua index 973b27fa7..efaf0d486 100644 --- a/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua +++ b/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua @@ -13,13 +13,14 @@ local function do_filter_prefix(input, output, prefix) return f end - -local function escape_args(ret, arg0, ...) - if not arg0 then - return ret +local function close_stdio(stream, mode) + local null = nixio.open('/dev/null', mode) + if null then + nixio.dup(null, nixio[stream]) + if null:fileno() > 2 then + null:close() + end end - - return escape_args(ret .. "'" .. string.gsub(arg0, "'", "'\\''") .. "' ", ...) end @@ -76,9 +77,21 @@ function remove_from_set(t, itm) return changed end - function exec(...) - return os.execute(escape_args('', 'exec', ...)) + local pid, errno, error = nixio.fork() + if pid == 0 then + close_stdio('stdin', 'r') + close_stdio('stdout', 'w') + close_stdio('stderr', 'w') + + nixio.execp(...) + os.exit(127) + elseif pid > 0 then + local wpid, status, code = nixio.waitpid(pid) + return wpid and status == 'exited' and code + else + return nil, errno, error + end end -- Removes all lines starting with a prefix from a file, optionally adding a new one -- GitLab