diff options
Diffstat (limited to 'bitbake-dev/lib/bb/daemonize.py')
-rw-r--r-- | bitbake-dev/lib/bb/daemonize.py | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/bitbake-dev/lib/bb/daemonize.py b/bitbake-dev/lib/bb/daemonize.py new file mode 100644 index 0000000000..6023c9ccd2 --- /dev/null +++ b/bitbake-dev/lib/bb/daemonize.py | |||
@@ -0,0 +1,189 @@ | |||
1 | """ | ||
2 | Python Deamonizing helper | ||
3 | |||
4 | Configurable daemon behaviors: | ||
5 | |||
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 | |||
11 | A failed call to fork() now raises an exception. | ||
12 | |||
13 | References: | ||
14 | 1) Advanced Programming in the Unix Environment: W. Richard Stevens | ||
15 | 2) Unix Programming Frequently Asked Questions: | ||
16 | http://www.erlenstar.demon.co.uk/unix/faq_toc.html | ||
17 | |||
18 | Modified to allow a function to be daemonized and return for | ||
19 | bitbake use by Richard Purdie | ||
20 | """ | ||
21 | |||
22 | __author__ = "Chad J. Schroeder" | ||
23 | __copyright__ = "Copyright (C) 2005 Chad J. Schroeder" | ||
24 | __version__ = "0.2" | ||
25 | |||
26 | # Standard Python modules. | ||
27 | import os # Miscellaneous OS interfaces. | ||
28 | import sys # System-specific parameters and functions. | ||
29 | |||
30 | # Default daemon parameters. | ||
31 | # File mode creation mask of the daemon. | ||
32 | UMASK = 0 | ||
33 | |||
34 | # Default maximum for the number of available file descriptors. | ||
35 | MAXFD = 1024 | ||
36 | |||
37 | # The standard I/O file descriptors are redirected to /dev/null by default. | ||
38 | if (hasattr(os, "devnull")): | ||
39 | REDIRECT_TO = os.devnull | ||
40 | else: | ||
41 | REDIRECT_TO = "/dev/null" | ||
42 | |||
43 | def createDaemon(function, logfile): | ||
44 | """ | ||
45 | Detach a process from the controlling terminal and run it in the | ||
46 | background as a daemon, returning control to the caller. | ||
47 | """ | ||
48 | |||
49 | try: | ||
50 | # Fork a child process so the parent can exit. This returns control to | ||
51 | # the command-line or shell. It also guarantees that the child will not | ||
52 | # be a process group leader, since the child receives a new process ID | ||
53 | # and inherits the parent's process group ID. This step is required | ||
54 | # to insure that the next call to os.setsid is successful. | ||
55 | pid = os.fork() | ||
56 | except OSError, e: | ||
57 | raise Exception, "%s [%d]" % (e.strerror, e.errno) | ||
58 | |||
59 | if (pid == 0): # The first child. | ||
60 | # To become the session leader of this new session and the process group | ||
61 | # leader of the new process group, we call os.setsid(). The process is | ||
62 | # also guaranteed not to have a controlling terminal. | ||
63 | os.setsid() | ||
64 | |||
65 | # Is ignoring SIGHUP necessary? | ||
66 | # | ||
67 | # It's often suggested that the SIGHUP signal should be ignored before | ||
68 | # the second fork to avoid premature termination of the process. The | ||
69 | # reason is that when the first child terminates, all processes, e.g. | ||
70 | # the second child, in the orphaned group will be sent a SIGHUP. | ||
71 | # | ||
72 | # "However, as part of the session management system, there are exactly | ||
73 | # two cases where SIGHUP is sent on the death of a process: | ||
74 | # | ||
75 | # 1) When the process that dies is the session leader of a session that | ||
76 | # is attached to a terminal device, SIGHUP is sent to all processes | ||
77 | # in the foreground process group of that terminal device. | ||
78 | # 2) When the death of a process causes a process group to become | ||
79 | # orphaned, and one or more processes in the orphaned group are | ||
80 | # stopped, then SIGHUP and SIGCONT are sent to all members of the | ||
81 | # orphaned group." [2] | ||
82 | # | ||
83 | # The first case can be ignored since the child is guaranteed not to have | ||
84 | # a controlling terminal. The second case isn't so easy to dismiss. | ||
85 | # The process group is orphaned when the first child terminates and | ||
86 | # POSIX.1 requires that every STOPPED process in an orphaned process | ||
87 | # group be sent a SIGHUP signal followed by a SIGCONT signal. Since the | ||
88 | # second child is not STOPPED though, we can safely forego ignoring the | ||
89 | # SIGHUP signal. In any case, there are no ill-effects if it is ignored. | ||
90 | # | ||
91 | # import signal # Set handlers for asynchronous events. | ||
92 | # signal.signal(signal.SIGHUP, signal.SIG_IGN) | ||
93 | |||
94 | try: | ||
95 | # Fork a second child and exit immediately to prevent zombies. This | ||
96 | # causes the second child process to be orphaned, making the init | ||
97 | # process responsible for its cleanup. And, since the first child is | ||
98 | # a session leader without a controlling terminal, it's possible for | ||
99 | # it to acquire one by opening a terminal in the future (System V- | ||
100 | # based systems). This second fork guarantees that the child is no | ||
101 | # longer a session leader, preventing the daemon from ever acquiring | ||
102 | # a controlling terminal. | ||
103 | pid = os.fork() # Fork a second child. | ||
104 | except OSError, e: | ||
105 | raise Exception, "%s [%d]" % (e.strerror, e.errno) | ||
106 | |||
107 | if (pid == 0): # The second child. | ||
108 | # We probably don't want the file mode creation mask inherited from | ||
109 | # the parent, so we give the child complete control over permissions. | ||
110 | os.umask(UMASK) | ||
111 | else: | ||
112 | # Parent (the first child) of the second child. | ||
113 | os._exit(0) | ||
114 | else: | ||
115 | # exit() or _exit()? | ||
116 | # _exit is like exit(), but it doesn't call any functions registered | ||
117 | # with atexit (and on_exit) or any registered signal handlers. It also | ||
118 | # closes any open file descriptors. Using exit() may cause all stdio | ||
119 | # streams to be flushed twice and any temporary files may be unexpectedly | ||
120 | # removed. It's therefore recommended that child branches of a fork() | ||
121 | # and the parent branch(es) of a daemon use _exit(). | ||
122 | return | ||
123 | |||
124 | # Close all open file descriptors. This prevents the child from keeping | ||
125 | # open any file descriptors inherited from the parent. There is a variety | ||
126 | # of methods to accomplish this task. Three are listed below. | ||
127 | # | ||
128 | # Try the system configuration variable, SC_OPEN_MAX, to obtain the maximum | ||
129 | # number of open file descriptors to close. If it doesn't exists, use | ||
130 | # the default value (configurable). | ||
131 | # | ||
132 | # try: | ||
133 | # maxfd = os.sysconf("SC_OPEN_MAX") | ||
134 | # except (AttributeError, ValueError): | ||
135 | # maxfd = MAXFD | ||
136 | # | ||
137 | # OR | ||
138 | # | ||
139 | # if (os.sysconf_names.has_key("SC_OPEN_MAX")): | ||
140 | # maxfd = os.sysconf("SC_OPEN_MAX") | ||
141 | # else: | ||
142 | # maxfd = MAXFD | ||
143 | # | ||
144 | # OR | ||
145 | # | ||
146 | # Use the getrlimit method to retrieve the maximum file descriptor number | ||
147 | # that can be opened by this process. If there is not limit on the | ||
148 | # resource, use the default value. | ||
149 | # | ||
150 | import resource # Resource usage information. | ||
151 | maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] | ||
152 | if (maxfd == resource.RLIM_INFINITY): | ||
153 | maxfd = MAXFD | ||
154 | |||
155 | # Iterate through and close all file descriptors. | ||
156 | # for fd in range(0, maxfd): | ||
157 | # try: | ||
158 | # os.close(fd) | ||
159 | # except OSError: # ERROR, fd wasn't open to begin with (ignored) | ||
160 | # pass | ||
161 | |||
162 | # Redirect the standard I/O file descriptors to the specified file. Since | ||
163 | # the daemon has no controlling terminal, most daemons redirect stdin, | ||
164 | # stdout, and stderr to /dev/null. This is done to prevent side-effects | ||
165 | # from reads and writes to the standard I/O file descriptors. | ||
166 | |||
167 | # This call to open is guaranteed to return the lowest file descriptor, | ||
168 | # which will be 0 (stdin), since it was closed above. | ||
169 | # os.open(REDIRECT_TO, os.O_RDWR) # standard input (0) | ||
170 | |||
171 | # Duplicate standard input to standard output and standard error. | ||
172 | # os.dup2(0, 1) # standard output (1) | ||
173 | # os.dup2(0, 2) # standard error (2) | ||
174 | |||
175 | |||
176 | si = file('/dev/null', 'r') | ||
177 | so = file(logfile, 'w') | ||
178 | se = so | ||
179 | |||
180 | |||
181 | # Replace those fds with our own | ||
182 | os.dup2(si.fileno(), sys.stdin.fileno()) | ||
183 | os.dup2(so.fileno(), sys.stdout.fileno()) | ||
184 | os.dup2(se.fileno(), sys.stderr.fileno()) | ||
185 | |||
186 | function() | ||
187 | |||
188 | os._exit(0) | ||
189 | |||