]> git.e2factory.org Git - e2factory.git/commitdiff
e2lib: close unused sync pipe ends before reading from child
authorTobias Ulmer <tu@emlix.com>
Wed, 13 Feb 2019 18:24:56 +0000 (19:24 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 13 Feb 2019 18:25:01 +0000 (19:25 +0100)
Otherwise we could end up in a situation where we wait for a faulty
child, but hang reading our own pipe.

Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua

index 75efc4ff196a293ab3658fc630b05b2c5c03002b..ac9cfcbf6faa9806e15777b9aa5e94ede638eb1c 100644 (file)
@@ -1427,6 +1427,14 @@ function e2lib.callcmd(argv, fdctv, workdir, envdict, nowait)
     local function sync_child(sync_pipes)
         local rc, re
 
+        -- close unused ends early
+        for _,fd in ipairs({2, 3}) do
+            rc, re = eio.close(sync_pipes[fd])
+            if not rc then
+                return false, re
+            end
+        end
+
         -- ping parent
         rc, re = eio.write(sync_pipes[4], "c")
         if not rc then
@@ -1444,8 +1452,8 @@ function e2lib.callcmd(argv, fdctv, workdir, envdict, nowait)
         end
 
         -- cleanup
-        for _,fd in ipairs(sync_pipes) do
-            rc, re = eio.close(fd)
+        for _,fd in ipairs({4, 1}) do
+            rc, re = eio.close(sync_pipes[fd])
             if not rc then
                 return false, re
             end
@@ -1457,6 +1465,18 @@ function e2lib.callcmd(argv, fdctv, workdir, envdict, nowait)
     local function sync_parent(sync_pipes)
         local rc, re
 
+        -- close unused ends
+        -- note: don't start reading/writing before all unused ends
+        -- are closed. If anything goes wrong with the child, we will
+        -- otherwise block forever trying to read from ourselves.
+        -- comment also applies to sync_child()
+        for _,fd in ipairs({1, 4}) do
+            rc, re = eio.close(sync_pipes[fd])
+            if not rc then
+                return false, re
+            end
+        end
+
         -- ping child
         rc, re = eio.write(sync_pipes[2], "p")
         if not rc then
@@ -1474,8 +1494,8 @@ function e2lib.callcmd(argv, fdctv, workdir, envdict, nowait)
         end
 
         -- cleanup
-        for _,fd in ipairs(sync_pipes) do
-            rc, re = eio.close(fd)
+        for _,fd in ipairs({2, 3}) do
+            rc, re = eio.close(sync_pipes[fd])
             if not rc then
                 return false, re
             end