From 0260032ce37a960f7c619a1bbc891434ff84c927 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Tue, 5 Oct 2010 13:05:50 +0200 Subject: [PATCH] Abstract some daemon functionality This patch abstracts the chdir/umask/setsid functionality, which is identical in the code functions, just that Daemonize did the chdir/umask in the second child; with this change it does it in the first, as StartDaemon. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- lib/utils.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/utils.py b/lib/utils.py index 8f7c031d1..3a6d67a72 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -237,6 +237,21 @@ def RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False, return RunResult(exitcode, signal_, out, err, strcmd) +def SetupDaemonEnv(cwd="/", umask=077): + """Setup a daemon's environment. + + This should be called between the first and second fork, due to + setsid usage. + + @param cwd: the directory to which to chdir + @param umask: the umask to setup + + """ + os.chdir(cwd) + os.umask(umask) + os.setsid() + + def StartDaemon(cmd, env=None, cwd="/", output=None, output_fd=None, pidfile=None): """Start a daemon process after forking twice. @@ -344,9 +359,7 @@ def _StartDaemonChild(errpipe_read, errpipe_write, _CloseFDNoErr(pidpipe_read) # First child process - os.chdir("/") - os.umask(077) - os.setsid() + SetupDaemonEnv() # And fork for the second time pid = os.fork() @@ -354,7 +367,8 @@ def _StartDaemonChild(errpipe_read, errpipe_write, # Exit first child process os._exit(0) # pylint: disable-msg=W0212 - # Make sure pipe is closed on execv* (and thereby notifies original process) + # Make sure pipe is closed on execv* (and thereby notifies + # original process) SetCloseOnExecFlag(errpipe_write, True) # List of file descriptors to be left open @@ -2132,18 +2146,16 @@ def Daemonize(logfile): """ # pylint: disable-msg=W0212 # yes, we really want os._exit - UMASK = 077 - WORKDIR = "/" # this might fail pid = os.fork() if (pid == 0): # The first child. - os.setsid() + SetupDaemonEnv() + # this might fail pid = os.fork() # Fork a second child. if (pid == 0): # The second child. - os.chdir(WORKDIR) - os.umask(UMASK) + pass # nothing special to do in the child else: # exit() or _exit()? See below. os._exit(0) # Exit parent (the first child) of the second child. -- GitLab