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