summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/daemonize.py
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2017-07-31 12:12:45 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-07-31 15:13:53 +0100
commit914a53cc94c8977b348fd06de12a519187f90ca5 (patch)
treecf52e778da7c8048a8d0c9f200ec13ce88e2685a /bitbake/lib/bb/daemonize.py
parent8625f83e383d3d30c1cb44d6293cc302728692cd (diff)
downloadpoky-914a53cc94c8977b348fd06de12a519187f90ca5.tar.gz
bitbake: daemonize: clean up code
This was originally based on some other code but its mostly misleading comments now. Massively clean this up and accept its now a total fork. There are no funciton changes here, just cleanup. (Bitbake rev: 3daa9001269e5b9156d0eb3299d2628d2d219fff) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/daemonize.py')
-rw-r--r--bitbake/lib/bb/daemonize.py148
1 files changed, 14 insertions, 134 deletions
diff --git a/bitbake/lib/bb/daemonize.py b/bitbake/lib/bb/daemonize.py
index a4664ad76b..a5a9d868d0 100644
--- a/bitbake/lib/bb/daemonize.py
+++ b/bitbake/lib/bb/daemonize.py
@@ -1,51 +1,15 @@
1""" 1"""
2Python Daemonizing helper 2Python Daemonizing helper
3 3
4Configurable daemon behaviors: 4Originally based on code Copyright (C) 2005 Chad J. Schroeder but now heavily modified
5 5to allow a function to be daemonized and return for bitbake use by Richard Purdie
6 1.) The current working directory set to the "/" directory.
7 2.) The current file creation mode mask set to 0.
8 3.) Close all open files (1024).
9 4.) Redirect standard I/O streams to "/dev/null".
10
11A failed call to fork() now raises an exception.
12
13References:
14 1) Advanced Programming in the Unix Environment: W. Richard Stevens
15 http://www.apuebook.com/apue3e.html
16 2) The Linux Programming Interface: Michael Kerrisk
17 http://man7.org/tlpi/index.html
18 3) Unix Programming Frequently Asked Questions:
19 http://www.faqs.org/faqs/unix-faq/programmer/faq/
20
21Modified to allow a function to be daemonized and return for
22bitbake use by Richard Purdie
23""" 6"""
24 7
25__author__ = "Chad J. Schroeder" 8import os
26__copyright__ = "Copyright (C) 2005 Chad J. Schroeder" 9import sys
27__version__ = "0.2"
28
29# Standard Python modules.
30import os # Miscellaneous OS interfaces.
31import sys # System-specific parameters and functions.
32import io 10import io
33import traceback 11import traceback
34 12
35# Default daemon parameters.
36# File mode creation mask of the daemon.
37# For BitBake's children, we do want to inherit the parent umask.
38UMASK = None
39
40# Default maximum for the number of available file descriptors.
41MAXFD = 1024
42
43# The standard I/O file descriptors are redirected to /dev/null by default.
44if (hasattr(os, "devnull")):
45 REDIRECT_TO = os.devnull
46else:
47 REDIRECT_TO = "/dev/null"
48
49def createDaemon(function, logfile): 13def createDaemon(function, logfile):
50 """ 14 """
51 Detach a process from the controlling terminal and run it in the 15 Detach a process from the controlling terminal and run it in the
@@ -67,36 +31,6 @@ def createDaemon(function, logfile):
67 # leader of the new process group, we call os.setsid(). The process is 31 # leader of the new process group, we call os.setsid(). The process is
68 # also guaranteed not to have a controlling terminal. 32 # also guaranteed not to have a controlling terminal.
69 os.setsid() 33 os.setsid()
70
71 # Is ignoring SIGHUP necessary?
72 #
73 # It's often suggested that the SIGHUP signal should be ignored before
74 # the second fork to avoid premature termination of the process. The
75 # reason is that when the first child terminates, all processes, e.g.
76 # the second child, in the orphaned group will be sent a SIGHUP.
77 #
78 # "However, as part of the session management system, there are exactly
79 # two cases where SIGHUP is sent on the death of a process:
80 #
81 # 1) When the process that dies is the session leader of a session that
82 # is attached to a terminal device, SIGHUP is sent to all processes
83 # in the foreground process group of that terminal device.
84 # 2) When the death of a process causes a process group to become
85 # orphaned, and one or more processes in the orphaned group are
86 # stopped, then SIGHUP and SIGCONT are sent to all members of the
87 # orphaned group." [2]
88 #
89 # The first case can be ignored since the child is guaranteed not to have
90 # a controlling terminal. The second case isn't so easy to dismiss.
91 # The process group is orphaned when the first child terminates and
92 # POSIX.1 requires that every STOPPED process in an orphaned process
93 # group be sent a SIGHUP signal followed by a SIGCONT signal. Since the
94 # second child is not STOPPED though, we can safely forego ignoring the
95 # SIGHUP signal. In any case, there are no ill-effects if it is ignored.
96 #
97 # import signal # Set handlers for asynchronous events.
98 # signal.signal(signal.SIGHUP, signal.SIG_IGN)
99
100 try: 34 try:
101 # Fork a second child and exit immediately to prevent zombies. This 35 # Fork a second child and exit immediately to prevent zombies. This
102 # causes the second child process to be orphaned, making the init 36 # causes the second child process to be orphaned, making the init
@@ -110,77 +44,23 @@ def createDaemon(function, logfile):
110 except OSError as e: 44 except OSError as e:
111 raise Exception("%s [%d]" % (e.strerror, e.errno)) 45 raise Exception("%s [%d]" % (e.strerror, e.errno))
112 46
113 if (pid == 0): # The second child. 47 if (pid != 0):
114 # We probably don't want the file mode creation mask inherited from
115 # the parent, so we give the child complete control over permissions.
116 if UMASK is not None:
117 os.umask(UMASK)
118 else:
119 # Parent (the first child) of the second child. 48 # Parent (the first child) of the second child.
49 # exit() or _exit()?
50 # _exit is like exit(), but it doesn't call any functions registered
51 # with atexit (and on_exit) or any registered signal handlers. It also
52 # closes any open file descriptors. Using exit() may cause all stdio
53 # streams to be flushed twice and any temporary files may be unexpectedly
54 # removed. It's therefore recommended that child branches of a fork()
55 # and the parent branch(es) of a daemon use _exit().
120 os._exit(0) 56 os._exit(0)
121 else: 57 else:
122 # exit() or _exit()?
123 # _exit is like exit(), but it doesn't call any functions registered
124 # with atexit (and on_exit) or any registered signal handlers. It also
125 # closes any open file descriptors. Using exit() may cause all stdio
126 # streams to be flushed twice and any temporary files may be unexpectedly
127 # removed. It's therefore recommended that child branches of a fork()
128 # and the parent branch(es) of a daemon use _exit().
129 os.waitpid(pid, 0) 58 os.waitpid(pid, 0)
130 return 59 return
131 60
132 # Close all open file descriptors. This prevents the child from keeping 61 # The second child.
133 # open any file descriptors inherited from the parent. There is a variety
134 # of methods to accomplish this task. Three are listed below.
135 #
136 # Try the system configuration variable, SC_OPEN_MAX, to obtain the maximum
137 # number of open file descriptors to close. If it doesn't exist, use
138 # the default value (configurable).
139 #
140 # try:
141 # maxfd = os.sysconf("SC_OPEN_MAX")
142 # except (AttributeError, ValueError):
143 # maxfd = MAXFD
144 #
145 # OR
146 #
147 # if (os.sysconf_names.has_key("SC_OPEN_MAX")):
148 # maxfd = os.sysconf("SC_OPEN_MAX")
149 # else:
150 # maxfd = MAXFD
151 #
152 # OR
153 #
154 # Use the getrlimit method to retrieve the maximum file descriptor number
155 # that can be opened by this process. If there is no limit on the
156 # resource, use the default value.
157 #
158 import resource # Resource usage information.
159 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
160 if (maxfd == resource.RLIM_INFINITY):
161 maxfd = MAXFD
162
163 # Iterate through and close all file descriptors.
164# for fd in range(0, maxfd):
165# try:
166# os.close(fd)
167# except OSError: # ERROR, fd wasn't open to begin with (ignored)
168# pass
169
170 # Redirect the standard I/O file descriptors to the specified file. Since
171 # the daemon has no controlling terminal, most daemons redirect stdin,
172 # stdout, and stderr to /dev/null. This is done to prevent side-effects
173 # from reads and writes to the standard I/O file descriptors.
174
175 # This call to open is guaranteed to return the lowest file descriptor,
176 # which will be 0 (stdin), since it was closed above.
177# os.open(REDIRECT_TO, os.O_RDWR) # standard input (0)
178
179 # Duplicate standard input to standard output and standard error.
180# os.dup2(0, 1) # standard output (1)
181# os.dup2(0, 2) # standard error (2)
182 62
183 # Replace those fds with our own 63 # Replace standard fds with our own
184 si = open('/dev/null', 'r') 64 si = open('/dev/null', 'r')
185 os.dup2(si.fileno(), sys.stdin.fileno()) 65 os.dup2(si.fileno(), sys.stdin.fileno())
186 66