From 8ff612c270502fccaacea1ef8ad125492a11b9cf Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 4 Apr 2008 15:29:29 +0000
Subject: [PATCH] Allow utils.Daemonize() to not close some fds

This patch implements an optional parameter to utils.Daemonize() which
allows that function to not close some file descriptors.

This will allow the master daemon to open the listening socket before
fork - in order to be able to notify errors and return a meaningful exit
code, and then when we fork we don't close that fd.

Reviewed-by: imsnah
---
 lib/utils.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/utils.py b/lib/utils.py
index 509f8be77..a1f15d50b 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1083,7 +1083,7 @@ def TestDelay(duration):
   return True
 
 
-def Daemonize(logfile):
+def Daemonize(logfile, noclose_fds=None):
   """Daemonize the current process.
 
   This detaches the current process from the controlling terminal and
@@ -1123,6 +1123,8 @@ def Daemonize(logfile):
 
   # Iterate through and close all file descriptors.
   for fd in range(0, maxfd):
+    if noclose_fds and fd in noclose_fds:
+      continue
     try:
       os.close(fd)
     except OSError: # ERROR, fd wasn't open to begin with (ignored)
-- 
GitLab