]> git.e2factory.org Git - e2factory.git/commitdiff
init: wrap io.open/popen with variants setting FD_CLOEXEC
authorTobias Ulmer <tu@emlix.com>
Wed, 7 Oct 2015 18:53:24 +0000 (20:53 +0200)
committerTobias Ulmer <tu@emlix.com>
Fri, 9 Oct 2015 17:21:35 +0000 (19:21 +0200)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
Changelog
generic/e2lib.lua

index a04461c32806e665bc2dc7210b61b0bd1ac2b9db..b528ff952f5c15079ad5bcdac502ae831cebaf35 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -1,4 +1,5 @@
 NEXT:
+ * Provide io.open/io.popen wrappers that set FD_CLOEXEC
  * On startup, close all file descriptors except for stdin, stdout, and stderr
    as a precautionary measure.
  * luafile.open() sets FD_CLOEXEC by default
index 78d01d9a9fb410b677efc4ee0fba3ecebb8f1d3b..eca58d40b5cea622201d078c58372529421958e5 100644 (file)
@@ -141,6 +141,31 @@ function e2lib.init()
     e2util.closefrom(3)
     -- ignore errors, no /proc should not prevent factory from working
 
+    -- Overwrite io functions that create a file descriptor to set the
+    -- CLOEXEC flag by default.
+    local realopen = io.open
+    local realpopen = io.popen
+
+    function io.open(...)
+        local ret = {realopen(...)} -- closure
+        if ret[1] then
+            if not luafile.cloexec(ret[1]) then
+                return nil, "Setting CLOEXEC failed"
+            end
+        end
+        return unpack(ret)
+    end
+
+    function io.popen(...)
+        local ret = {realpopen(...)} -- closure
+        if ret[1] then
+            if not luafile.cloexec(ret[1]) then
+                return nil, "Setting CLOEXEC failed"
+            end
+        end
+        return unpack(ret)
+    end
+
     e2lib.globals.warn_category = {
         WDEFAULT = false,
         WDEPRECATED = false,