157056177SMatthew G Knepley"""Disk And Execution MONitor (Daemon) 257056177SMatthew G Knepley 357056177SMatthew G KnepleyConfigurable daemon behaviors: 457056177SMatthew G Knepley 557056177SMatthew G Knepley 1.) The current working directory set to the / directory. 657056177SMatthew G Knepley 2.) The current file creation mode mask set to 0. 757056177SMatthew G Knepley 3.) Close all open files (1024). 857056177SMatthew G Knepley 4.) Redirect standard I/O streams to /dev/null. 957056177SMatthew G Knepley 1057056177SMatthew G KnepleyA failed call to fork() now raises an exception. 1157056177SMatthew G Knepley 1257056177SMatthew G KnepleyReferences: 1357056177SMatthew G Knepley 1) Advanced Programming in the Unix Environment: W. Richard Stevens 1457056177SMatthew G Knepley 2) Unix Programming Frequently Asked Questions: http://www.erlenstar.demon.co.uk/unix/faq_toc.html 1557056177SMatthew G Knepley""" 1657056177SMatthew G Knepley 1757056177SMatthew G Knepley__author__ = "Chad J. Schroeder" 1857056177SMatthew G Knepley__copyright__ = "Copyright (C) 2005 Chad J. Schroeder" 1957056177SMatthew G Knepley__revision__ = "$Id$" 2057056177SMatthew G Knepley__version__ = "0.2" 2157056177SMatthew G Knepley 2257056177SMatthew G Knepley# Standard Python modules. 2357056177SMatthew G Knepleyimport os # Miscellaneous OS interfaces. 2457056177SMatthew G Knepleyimport sys # System-specific parameters and functions. 2557056177SMatthew G Knepley 2657056177SMatthew G Knepley# Default daemon parameters. 2757056177SMatthew G Knepley# File mode creation mask of the daemon. 2857056177SMatthew G KnepleyUMASK = 0 2957056177SMatthew G Knepley 3057056177SMatthew G Knepley# Default working directory for the daemon. 3157056177SMatthew G KnepleyWORKDIR = "/" 3257056177SMatthew G Knepley 3357056177SMatthew G Knepley# Default maximum for the number of available file descriptors. 3457056177SMatthew G KnepleyMAXFD = 1024 3557056177SMatthew G Knepley 3657056177SMatthew G Knepley# The standard I/O file descriptors are redirected to /dev/null by default. 3757056177SMatthew G Knepleyif (hasattr(os, "devnull")): 3857056177SMatthew G Knepley REDIRECT_TO = os.devnull 3957056177SMatthew G Knepleyelse: 4057056177SMatthew G Knepley REDIRECT_TO = "/dev/null" 4157056177SMatthew G Knepley 4257056177SMatthew G Knepleydef createDaemon(workDir = None): 4357056177SMatthew G Knepley """Detach a process from the controlling terminal and run it in the 4457056177SMatthew G Knepley background as a daemon. 4557056177SMatthew G Knepley """ 4657056177SMatthew G Knepley 4757056177SMatthew G Knepley if not workDir is None and os.path.isdir(workDir): 4857056177SMatthew G Knepley global WORKDIR 4957056177SMatthew G Knepley WORKDIR = workDir 5057056177SMatthew G Knepley try: 5157056177SMatthew G Knepley # Fork a child process so the parent can exit. This returns control to 5257056177SMatthew G Knepley # the command-line or shell. It also guarantees that the child will not 5357056177SMatthew G Knepley # be a process group leader, since the child receives a new process ID 5457056177SMatthew G Knepley # and inherits the parent's process group ID. This step is required 5557056177SMatthew G Knepley # to insure that the next call to os.setsid is successful. 5657056177SMatthew G Knepley pid = os.fork() 57*5b6bfdb9SJed Brown except OSError as e: 58*5b6bfdb9SJed Brown raise Exception("%s [%d]" % (e.strerror, e.errno)) 5957056177SMatthew G Knepley 6057056177SMatthew G Knepley if (pid == 0): # The first child. 6157056177SMatthew G Knepley # To become the session leader of this new session and the process group 6257056177SMatthew G Knepley # leader of the new process group, we call os.setsid(). The process is 6357056177SMatthew G Knepley # also guaranteed not to have a controlling terminal. 6457056177SMatthew G Knepley os.setsid() 6557056177SMatthew G Knepley 6657056177SMatthew G Knepley # Is ignoring SIGHUP necessary? 6757056177SMatthew G Knepley # 6857056177SMatthew G Knepley # It's often suggested that the SIGHUP signal should be ignored before 6957056177SMatthew G Knepley # the second fork to avoid premature termination of the process. The 7057056177SMatthew G Knepley # reason is that when the first child terminates, all processes, e.g. 7157056177SMatthew G Knepley # the second child, in the orphaned group will be sent a SIGHUP. 7257056177SMatthew G Knepley # 7357056177SMatthew G Knepley # "However, as part of the session management system, there are exactly 7457056177SMatthew G Knepley # two cases where SIGHUP is sent on the death of a process: 7557056177SMatthew G Knepley # 7657056177SMatthew G Knepley # 1) When the process that dies is the session leader of a session that 7757056177SMatthew G Knepley # is attached to a terminal device, SIGHUP is sent to all processes 7857056177SMatthew G Knepley # in the foreground process group of that terminal device. 7957056177SMatthew G Knepley # 2) When the death of a process causes a process group to become 8057056177SMatthew G Knepley # orphaned, and one or more processes in the orphaned group are 8157056177SMatthew G Knepley # stopped, then SIGHUP and SIGCONT are sent to all members of the 8257056177SMatthew G Knepley # orphaned group." [2] 8357056177SMatthew G Knepley # 8457056177SMatthew G Knepley # The first case can be ignored since the child is guaranteed not to have 8557056177SMatthew G Knepley # a controlling terminal. The second case isn't so easy to dismiss. 8657056177SMatthew G Knepley # The process group is orphaned when the first child terminates and 8757056177SMatthew G Knepley # POSIX.1 requires that every STOPPED process in an orphaned process 8857056177SMatthew G Knepley # group be sent a SIGHUP signal followed by a SIGCONT signal. Since the 8957056177SMatthew G Knepley # second child is not STOPPED though, we can safely forego ignoring the 9057056177SMatthew G Knepley # SIGHUP signal. In any case, there are no ill-effects if it is ignored. 9157056177SMatthew G Knepley # 9257056177SMatthew G Knepley # import signal # Set handlers for asynchronous events. 9357056177SMatthew G Knepley # signal.signal(signal.SIGHUP, signal.SIG_IGN) 9457056177SMatthew G Knepley 9557056177SMatthew G Knepley try: 9657056177SMatthew G Knepley # Fork a second child and exit immediately to prevent zombies. This 9757056177SMatthew G Knepley # causes the second child process to be orphaned, making the init 9857056177SMatthew G Knepley # process responsible for its cleanup. And, since the first child is 9957056177SMatthew G Knepley # a session leader without a controlling terminal, it's possible for 10057056177SMatthew G Knepley # it to acquire one by opening a terminal in the future (System V- 10157056177SMatthew G Knepley # based systems). This second fork guarantees that the child is no 10257056177SMatthew G Knepley # longer a session leader, preventing the daemon from ever acquiring 10357056177SMatthew G Knepley # a controlling terminal. 10457056177SMatthew G Knepley pid = os.fork() # Fork a second child. 105*5b6bfdb9SJed Brown except OSError as e: 106*5b6bfdb9SJed Brown raise Exception("%s [%d]" % (e.strerror, e.errno)) 10757056177SMatthew G Knepley 10857056177SMatthew G Knepley if (pid == 0): # The second child. 10957056177SMatthew G Knepley # Since the current working directory may be a mounted filesystem, we 11057056177SMatthew G Knepley # avoid the issue of not being able to unmount the filesystem at 11157056177SMatthew G Knepley # shutdown time by changing it to the root directory. 11257056177SMatthew G Knepley os.chdir(WORKDIR) 11357056177SMatthew G Knepley # We probably don't want the file mode creation mask inherited from 11457056177SMatthew G Knepley # the parent, so we give the child complete control over permissions. 11557056177SMatthew G Knepley os.umask(UMASK) 11657056177SMatthew G Knepley else: 11757056177SMatthew G Knepley # exit() or _exit()? See below. 11857056177SMatthew G Knepley os._exit(0) # Exit parent (the first child) of the second child. 11957056177SMatthew G Knepley else: 12057056177SMatthew G Knepley # exit() or _exit()? 12157056177SMatthew G Knepley # _exit is like exit(), but it doesn't call any functions registered 12257056177SMatthew G Knepley # with atexit (and on_exit) or any registered signal handlers. It also 12357056177SMatthew G Knepley # closes any open file descriptors. Using exit() may cause all stdio 12457056177SMatthew G Knepley # streams to be flushed twice and any temporary files may be unexpectedly 12557056177SMatthew G Knepley # removed. It's therefore recommended that child branches of a fork() 12657056177SMatthew G Knepley # and the parent branch(es) of a daemon use _exit(). 12757056177SMatthew G Knepley os._exit(0) # Exit parent of the first child. 12857056177SMatthew G Knepley 12957056177SMatthew G Knepley # Close all open file descriptors. This prevents the child from keeping 13057056177SMatthew G Knepley # open any file descriptors inherited from the parent. There is a variety 13157056177SMatthew G Knepley # of methods to accomplish this task. Three are listed below. 13257056177SMatthew G Knepley # 13357056177SMatthew G Knepley # Try the system configuration variable, SC_OPEN_MAX, to obtain the maximum 13457056177SMatthew G Knepley # number of open file descriptors to close. If it doesn't exists, use 13557056177SMatthew G Knepley # the default value (configurable). 13657056177SMatthew G Knepley # 13757056177SMatthew G Knepley # try: 13857056177SMatthew G Knepley # maxfd = os.sysconf("SC_OPEN_MAX") 13957056177SMatthew G Knepley # except (AttributeError, ValueError): 14057056177SMatthew G Knepley # maxfd = MAXFD 14157056177SMatthew G Knepley # 14257056177SMatthew G Knepley # OR 14357056177SMatthew G Knepley # 14457056177SMatthew G Knepley # if (os.sysconf_names.has_key("SC_OPEN_MAX")): 14557056177SMatthew G Knepley # maxfd = os.sysconf("SC_OPEN_MAX") 14657056177SMatthew G Knepley # else: 14757056177SMatthew G Knepley # maxfd = MAXFD 14857056177SMatthew G Knepley # 14957056177SMatthew G Knepley # OR 15057056177SMatthew G Knepley # 15157056177SMatthew G Knepley # Use the getrlimit method to retrieve the maximum file descriptor number 15257056177SMatthew G Knepley # that can be opened by this process. If there is not limit on the 15357056177SMatthew G Knepley # resource, use the default value. 15457056177SMatthew G Knepley # 15557056177SMatthew G Knepley import resource # Resource usage information. 15657056177SMatthew G Knepley maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] 15757056177SMatthew G Knepley if (maxfd == resource.RLIM_INFINITY): 15857056177SMatthew G Knepley maxfd = MAXFD 15957056177SMatthew G Knepley 16057056177SMatthew G Knepley # Iterate through and close all file descriptors. 16157056177SMatthew G Knepley for fd in range(0, maxfd): 16257056177SMatthew G Knepley try: 16357056177SMatthew G Knepley os.close(fd) 16457056177SMatthew G Knepley except OSError: # ERROR, fd wasn't open to begin with (ignored) 16557056177SMatthew G Knepley pass 16657056177SMatthew G Knepley 16757056177SMatthew G Knepley # Redirect the standard I/O file descriptors to the specified file. Since 16857056177SMatthew G Knepley # the daemon has no controlling terminal, most daemons redirect stdin, 16957056177SMatthew G Knepley # stdout, and stderr to /dev/null. This is done to prevent side-effects 17057056177SMatthew G Knepley # from reads and writes to the standard I/O file descriptors. 17157056177SMatthew G Knepley 17257056177SMatthew G Knepley # This call to open is guaranteed to return the lowest file descriptor, 17357056177SMatthew G Knepley # which will be 0 (stdin), since it was closed above. 17457056177SMatthew G Knepley os.open(REDIRECT_TO, os.O_RDWR) # standard input (0) 17557056177SMatthew G Knepley 17657056177SMatthew G Knepley # Duplicate standard input to standard output and standard error. 17757056177SMatthew G Knepley os.dup2(0, 1) # standard output (1) 17857056177SMatthew G Knepley os.dup2(0, 2) # standard error (2) 17957056177SMatthew G Knepley 18057056177SMatthew G Knepley return(0) 18157056177SMatthew G Knepley 18257056177SMatthew G Knepleyif __name__ == "__main__": 18357056177SMatthew G Knepley 18457056177SMatthew G Knepley retCode = createDaemon('.') 18557056177SMatthew G Knepley 18657056177SMatthew G Knepley # The code, as is, will create a new file in the root directory, when 18757056177SMatthew G Knepley # executed with superuser privileges. The file will contain the following 18857056177SMatthew G Knepley # daemon related process parameters: return code, process ID, parent 18957056177SMatthew G Knepley # process group ID, session ID, user ID, effective user ID, real group ID, 19057056177SMatthew G Knepley # and the effective group ID. Notice the relationship between the daemon's 19157056177SMatthew G Knepley # process ID, process group ID, and its parent's process ID. 19257056177SMatthew G Knepley 19357056177SMatthew G Knepley procParams = """ 19457056177SMatthew G Knepley return code = %s 19557056177SMatthew G Knepley process ID = %s 19657056177SMatthew G Knepley parent process ID = %s 19757056177SMatthew G Knepley process group ID = %s 19857056177SMatthew G Knepley session ID = %s 19957056177SMatthew G Knepley user ID = %s 20057056177SMatthew G Knepley effective user ID = %s 20157056177SMatthew G Knepley real group ID = %s 20257056177SMatthew G Knepley effective group ID = %s 20357056177SMatthew G Knepley """ % (retCode, os.getpid(), os.getppid(), os.getpgrp(), os.getsid(0), 20457056177SMatthew G Knepley os.getuid(), os.geteuid(), os.getgid(), os.getegid()) 20557056177SMatthew G Knepley 20657056177SMatthew G Knepley open("createDaemon.log", "w").write(procParams + "\n") 20757056177SMatthew G Knepley 20857056177SMatthew G Knepley sys.exit(retCode) 209