Move the daemonize function to utils.py
authorIustin Pop <iustin@google.com>
Thu, 10 Apr 2008 14:58:46 +0000 (14:58 +0000)
committerIustin Pop <iustin@google.com>
Thu, 10 Apr 2008 14:58:46 +0000 (14:58 +0000)
Currently, in ganeti-noded we have the createDaemon function. Since
we'll need the same in other daemons, we move this function to utils.py

With the move, a few changes were also done:
  - change the name to Daemonize()
  - add a parameter, logfile, as different daemons will want to log to
    different files
  - remove the try.. except.. around the fork calls, since they were
    only re-raising the OS exception with less data; unless we want to
    actually handle fork error (not just re-raising), these try blocks
    are not useful
  - change the return style at the end of the function

Reviewed-by: imsnah

daemons/ganeti-noded
lib/utils.py

index eff3091..6ecb641 100755 (executable)
@@ -26,7 +26,6 @@
 
 import os
 import sys
-import resource
 import traceback
 import errno
 
@@ -561,7 +560,7 @@ def main():
 
   # become a daemon
   if options.fork:
-    createDaemon()
+    utils.Daemonize(logfile=constants.LOG_NODESERVER)
 
   logger.SetupLogging(twisted_workaround=True, debug=options.debug,
                       program="ganeti-noded")
@@ -573,60 +572,5 @@ def main():
   reactor.run()
 
 
-def createDaemon():
-  """Detach a process from the controlling terminal and run it in the
-  background as a daemon.
-
-  """
-  UMASK = 077
-  WORKDIR = "/"
-  # Default maximum for the number of available file descriptors.
-  if 'SC_OPEN_MAX' in os.sysconf_names:
-    try:
-      MAXFD = os.sysconf('SC_OPEN_MAX')
-      if MAXFD < 0:
-        MAXFD = 1024
-    except OSError:
-      MAXFD = 1024
-  else:
-    MAXFD = 1024
-  # The standard I/O file descriptors are redirected to /dev/null by default.
-  #REDIRECT_TO = getattr(os, "devnull", "/dev/null")
-  REDIRECT_TO = constants.LOG_NODESERVER
-  try:
-    pid = os.fork()
-  except OSError, e:
-    raise Exception("%s [%d]" % (e.strerror, e.errno))
-  if (pid == 0):  # The first child.
-    os.setsid()
-    try:
-      pid = os.fork() # Fork a second child.
-    except OSError, e:
-      raise Exception("%s [%d]" % (e.strerror, e.errno))
-    if (pid == 0):  # The second child.
-      os.chdir(WORKDIR)
-      os.umask(UMASK)
-    else:
-      # exit() or _exit()?  See below.
-      os._exit(0) # Exit parent (the first child) of the second child.
-  else:
-    os._exit(0) # Exit parent of the first child.
-  maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
-  if (maxfd == resource.RLIM_INFINITY):
-    maxfd = MAXFD
-
-  # Iterate through and close all file descriptors.
-  for fd in range(0, maxfd):
-    try:
-      os.close(fd)
-    except OSError: # ERROR, fd wasn't open to begin with (ignored)
-      pass
-  os.open(REDIRECT_TO, os.O_RDWR|os.O_CREAT|os.O_APPEND, 0600)
-  # Duplicate standard input to standard output and standard error.
-  os.dup2(0, 1)     # standard output (1)
-  os.dup2(0, 2)     # standard error (2)
-  return(0)
-
-
 if __name__ == '__main__':
   main()
index aac5cea..f9fb766 100644 (file)
@@ -38,6 +38,7 @@ import pwd
 import itertools
 import select
 import fcntl
+import resource
 
 from cStringIO import StringIO
 
@@ -1092,6 +1093,57 @@ def TestDelay(duration):
   return True
 
 
+def Daemonize(logfile):
+  """Daemonize the current process.
+
+  This detaches the current process from the controlling terminal and
+  runs it in the background as a daemon.
+
+  """
+  UMASK = 077
+  WORKDIR = "/"
+  # Default maximum for the number of available file descriptors.
+  if 'SC_OPEN_MAX' in os.sysconf_names:
+    try:
+      MAXFD = os.sysconf('SC_OPEN_MAX')
+      if MAXFD < 0:
+        MAXFD = 1024
+    except OSError:
+      MAXFD = 1024
+  else:
+    MAXFD = 1024
+
+  # this might fail
+  pid = os.fork()
+  if (pid == 0):  # The first child.
+    os.setsid()
+    # this might fail
+    pid = os.fork() # Fork a second child.
+    if (pid == 0):  # The second child.
+      os.chdir(WORKDIR)
+      os.umask(UMASK)
+    else:
+      # exit() or _exit()?  See below.
+      os._exit(0) # Exit parent (the first child) of the second child.
+  else:
+    os._exit(0) # Exit parent of the first child.
+  maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
+  if (maxfd == resource.RLIM_INFINITY):
+    maxfd = MAXFD
+
+  # Iterate through and close all file descriptors.
+  for fd in range(0, maxfd):
+    try:
+      os.close(fd)
+    except OSError: # ERROR, fd wasn't open to begin with (ignored)
+      pass
+  os.open(logfile, os.O_RDWR|os.O_CREAT|os.O_APPEND, 0600)
+  # Duplicate standard input to standard output and standard error.
+  os.dup2(0, 1)     # standard output (1)
+  os.dup2(0, 2)     # standard error (2)
+  return 0
+
+
 def FindFile(name, search_path, test=os.path.exists):
   """Look for a filesystem object in a given path.