Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | """ |
| 6 | Python Daemonizing helper |
| 7 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 8 | Originally based on code Copyright (C) 2005 Chad J. Schroeder but now heavily modified |
| 9 | to allow a function to be daemonized and return for bitbake use by Richard Purdie |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | """ |
| 11 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 12 | import os |
| 13 | import sys |
| 14 | import io |
| 15 | import traceback |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 17 | import bb |
| 18 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | def createDaemon(function, logfile): |
| 20 | """ |
| 21 | Detach a process from the controlling terminal and run it in the |
| 22 | background as a daemon, returning control to the caller. |
| 23 | """ |
| 24 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 25 | # Ensure stdout/stderror are flushed before forking to avoid duplicate output |
| 26 | sys.stdout.flush() |
| 27 | sys.stderr.flush() |
| 28 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | try: |
| 30 | # Fork a child process so the parent can exit. This returns control to |
| 31 | # the command-line or shell. It also guarantees that the child will not |
| 32 | # be a process group leader, since the child receives a new process ID |
| 33 | # and inherits the parent's process group ID. This step is required |
| 34 | # to insure that the next call to os.setsid is successful. |
| 35 | pid = os.fork() |
| 36 | except OSError as e: |
| 37 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 38 | |
| 39 | if (pid == 0): # The first child. |
| 40 | # To become the session leader of this new session and the process group |
| 41 | # leader of the new process group, we call os.setsid(). The process is |
| 42 | # also guaranteed not to have a controlling terminal. |
| 43 | os.setsid() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | try: |
| 45 | # Fork a second child and exit immediately to prevent zombies. This |
| 46 | # causes the second child process to be orphaned, making the init |
| 47 | # process responsible for its cleanup. And, since the first child is |
| 48 | # a session leader without a controlling terminal, it's possible for |
| 49 | # it to acquire one by opening a terminal in the future (System V- |
| 50 | # based systems). This second fork guarantees that the child is no |
| 51 | # longer a session leader, preventing the daemon from ever acquiring |
| 52 | # a controlling terminal. |
| 53 | pid = os.fork() # Fork a second child. |
| 54 | except OSError as e: |
| 55 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 56 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 57 | if (pid != 0): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | # Parent (the first child) of the second child. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 59 | # exit() or _exit()? |
| 60 | # _exit is like exit(), but it doesn't call any functions registered |
| 61 | # with atexit (and on_exit) or any registered signal handlers. It also |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 62 | # closes any open file descriptors, but doesn't flush any buffered output. |
| 63 | # Using exit() may cause all any temporary files to be unexpectedly |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 64 | # removed. It's therefore recommended that child branches of a fork() |
| 65 | # and the parent branch(es) of a daemon use _exit(). |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | os._exit(0) |
| 67 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 68 | os.waitpid(pid, 0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | return |
| 70 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 71 | # The second child. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 73 | # Replace standard fds with our own |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 74 | with open('/dev/null', 'r') as si: |
| 75 | os.dup2(si.fileno(), sys.stdin.fileno()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | try: |
| 78 | so = open(logfile, 'a+') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 79 | os.dup2(so.fileno(), sys.stdout.fileno()) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 80 | os.dup2(so.fileno(), sys.stderr.fileno()) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 81 | except io.UnsupportedOperation: |
| 82 | sys.stdout = open(logfile, 'a+') |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 83 | |
| 84 | # Have stdout and stderr be the same so log output matches chronologically |
| 85 | # and there aren't two seperate buffers |
| 86 | sys.stderr = sys.stdout |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 88 | try: |
| 89 | function() |
| 90 | except Exception as e: |
| 91 | traceback.print_exc() |
| 92 | finally: |
| 93 | bb.event.print_ui_queue() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 94 | # os._exit() doesn't flush open files like os.exit() does. Manually flush |
| 95 | # stdout and stderr so that any logging output will be seen, particularly |
| 96 | # exception tracebacks. |
| 97 | sys.stdout.flush() |
| 98 | sys.stderr.flush() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 99 | os._exit(0) |