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.
_CloseFDNoErr(pidpipe_read)
# First child process
- os.chdir("/")
- os.umask(077)
- os.setsid()
+ SetupDaemonEnv()
# And fork for the second time
pid = os.fork()
# 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
"""
# 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.