blob: f01e6ec7cca5802ea1b2850113580755444ee085 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005"""
6Python Daemonizing helper
7
Brad Bishopd7bf8c12018-02-25 22:55:05 -05008Originally based on code Copyright (C) 2005 Chad J. Schroeder but now heavily modified
9to allow a function to be daemonized and return for bitbake use by Richard Purdie
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010"""
11
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012import os
13import sys
14import io
15import traceback
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
17def createDaemon(function, logfile):
18 """
19 Detach a process from the controlling terminal and run it in the
20 background as a daemon, returning control to the caller.
21 """
22
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080023 # Ensure stdout/stderror are flushed before forking to avoid duplicate output
24 sys.stdout.flush()
25 sys.stderr.flush()
26
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 try:
28 # Fork a child process so the parent can exit. This returns control to
29 # the command-line or shell. It also guarantees that the child will not
30 # be a process group leader, since the child receives a new process ID
31 # and inherits the parent's process group ID. This step is required
32 # to insure that the next call to os.setsid is successful.
33 pid = os.fork()
34 except OSError as e:
35 raise Exception("%s [%d]" % (e.strerror, e.errno))
36
37 if (pid == 0): # The first child.
38 # To become the session leader of this new session and the process group
39 # leader of the new process group, we call os.setsid(). The process is
40 # also guaranteed not to have a controlling terminal.
41 os.setsid()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 try:
43 # Fork a second child and exit immediately to prevent zombies. This
44 # causes the second child process to be orphaned, making the init
45 # process responsible for its cleanup. And, since the first child is
46 # a session leader without a controlling terminal, it's possible for
47 # it to acquire one by opening a terminal in the future (System V-
48 # based systems). This second fork guarantees that the child is no
49 # longer a session leader, preventing the daemon from ever acquiring
50 # a controlling terminal.
51 pid = os.fork() # Fork a second child.
52 except OSError as e:
53 raise Exception("%s [%d]" % (e.strerror, e.errno))
54
Brad Bishopd7bf8c12018-02-25 22:55:05 -050055 if (pid != 0):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 # Parent (the first child) of the second child.
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057 # exit() or _exit()?
58 # _exit is like exit(), but it doesn't call any functions registered
59 # with atexit (and on_exit) or any registered signal handlers. It also
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080060 # closes any open file descriptors, but doesn't flush any buffered output.
61 # Using exit() may cause all any temporary files to be unexpectedly
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 # removed. It's therefore recommended that child branches of a fork()
63 # and the parent branch(es) of a daemon use _exit().
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 os._exit(0)
65 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050066 os.waitpid(pid, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 return
68
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069 # The second child.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 # Replace standard fds with our own
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080072 with open('/dev/null', 'r') as si:
73 os.dup2(si.fileno(), sys.stdin.fileno())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
Brad Bishopd7bf8c12018-02-25 22:55:05 -050075 try:
76 so = open(logfile, 'a+')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 os.dup2(so.fileno(), sys.stdout.fileno())
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080078 os.dup2(so.fileno(), sys.stderr.fileno())
Brad Bishopd7bf8c12018-02-25 22:55:05 -050079 except io.UnsupportedOperation:
80 sys.stdout = open(logfile, 'a+')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080081
82 # Have stdout and stderr be the same so log output matches chronologically
83 # and there aren't two seperate buffers
84 sys.stderr = sys.stdout
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Brad Bishopd7bf8c12018-02-25 22:55:05 -050086 try:
87 function()
88 except Exception as e:
89 traceback.print_exc()
90 finally:
91 bb.event.print_ui_queue()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080092 # os._exit() doesn't flush open files like os.exit() does. Manually flush
93 # stdout and stderr so that any logging output will be seen, particularly
94 # exception tracebacks.
95 sys.stdout.flush()
96 sys.stderr.flush()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 os._exit(0)