xref: /petsc/src/benchmarks/daemon.py (revision 570561775d7e036871484d853ebebd30b1fc7539)
1*57056177SMatthew G Knepley"""Disk And Execution MONitor (Daemon)
2*57056177SMatthew G Knepley
3*57056177SMatthew G KnepleyConfigurable daemon behaviors:
4*57056177SMatthew G Knepley
5*57056177SMatthew G Knepley   1.) The current working directory set to the / directory.
6*57056177SMatthew G Knepley   2.) The current file creation mode mask set to 0.
7*57056177SMatthew G Knepley   3.) Close all open files (1024).
8*57056177SMatthew G Knepley   4.) Redirect standard I/O streams to /dev/null.
9*57056177SMatthew G Knepley
10*57056177SMatthew G KnepleyA failed call to fork() now raises an exception.
11*57056177SMatthew G Knepley
12*57056177SMatthew G KnepleyReferences:
13*57056177SMatthew G Knepley   1) Advanced Programming in the Unix Environment: W. Richard Stevens
14*57056177SMatthew G Knepley   2) Unix Programming Frequently Asked Questions: http://www.erlenstar.demon.co.uk/unix/faq_toc.html
15*57056177SMatthew G Knepley"""
16*57056177SMatthew G Knepley
17*57056177SMatthew G Knepley__author__ = "Chad J. Schroeder"
18*57056177SMatthew G Knepley__copyright__ = "Copyright (C) 2005 Chad J. Schroeder"
19*57056177SMatthew G Knepley__revision__ = "$Id$"
20*57056177SMatthew G Knepley__version__ = "0.2"
21*57056177SMatthew G Knepley
22*57056177SMatthew G Knepley# Standard Python modules.
23*57056177SMatthew G Knepleyimport os               # Miscellaneous OS interfaces.
24*57056177SMatthew G Knepleyimport sys              # System-specific parameters and functions.
25*57056177SMatthew G Knepley
26*57056177SMatthew G Knepley# Default daemon parameters.
27*57056177SMatthew G Knepley# File mode creation mask of the daemon.
28*57056177SMatthew G KnepleyUMASK = 0
29*57056177SMatthew G Knepley
30*57056177SMatthew G Knepley# Default working directory for the daemon.
31*57056177SMatthew G KnepleyWORKDIR = "/"
32*57056177SMatthew G Knepley
33*57056177SMatthew G Knepley# Default maximum for the number of available file descriptors.
34*57056177SMatthew G KnepleyMAXFD = 1024
35*57056177SMatthew G Knepley
36*57056177SMatthew G Knepley# The standard I/O file descriptors are redirected to /dev/null by default.
37*57056177SMatthew G Knepleyif (hasattr(os, "devnull")):
38*57056177SMatthew G Knepley   REDIRECT_TO = os.devnull
39*57056177SMatthew G Knepleyelse:
40*57056177SMatthew G Knepley   REDIRECT_TO = "/dev/null"
41*57056177SMatthew G Knepley
42*57056177SMatthew G Knepleydef createDaemon(workDir = None):
43*57056177SMatthew G Knepley   """Detach a process from the controlling terminal and run it in the
44*57056177SMatthew G Knepley   background as a daemon.
45*57056177SMatthew G Knepley   """
46*57056177SMatthew G Knepley
47*57056177SMatthew G Knepley   if not workDir is None and os.path.isdir(workDir):
48*57056177SMatthew G Knepley       global WORKDIR
49*57056177SMatthew G Knepley       WORKDIR = workDir
50*57056177SMatthew G Knepley   try:
51*57056177SMatthew G Knepley      # Fork a child process so the parent can exit.  This returns control to
52*57056177SMatthew G Knepley      # the command-line or shell.  It also guarantees that the child will not
53*57056177SMatthew G Knepley      # be a process group leader, since the child receives a new process ID
54*57056177SMatthew G Knepley      # and inherits the parent's process group ID.  This step is required
55*57056177SMatthew G Knepley      # to insure that the next call to os.setsid is successful.
56*57056177SMatthew G Knepley      pid = os.fork()
57*57056177SMatthew G Knepley   except OSError, e:
58*57056177SMatthew G Knepley      raise Exception, "%s [%d]" % (e.strerror, e.errno)
59*57056177SMatthew G Knepley
60*57056177SMatthew G Knepley   if (pid == 0):	# The first child.
61*57056177SMatthew G Knepley      # To become the session leader of this new session and the process group
62*57056177SMatthew G Knepley      # leader of the new process group, we call os.setsid().  The process is
63*57056177SMatthew G Knepley      # also guaranteed not to have a controlling terminal.
64*57056177SMatthew G Knepley      os.setsid()
65*57056177SMatthew G Knepley
66*57056177SMatthew G Knepley      # Is ignoring SIGHUP necessary?
67*57056177SMatthew G Knepley      #
68*57056177SMatthew G Knepley      # It's often suggested that the SIGHUP signal should be ignored before
69*57056177SMatthew G Knepley      # the second fork to avoid premature termination of the process.  The
70*57056177SMatthew G Knepley      # reason is that when the first child terminates, all processes, e.g.
71*57056177SMatthew G Knepley      # the second child, in the orphaned group will be sent a SIGHUP.
72*57056177SMatthew G Knepley      #
73*57056177SMatthew G Knepley      # "However, as part of the session management system, there are exactly
74*57056177SMatthew G Knepley      # two cases where SIGHUP is sent on the death of a process:
75*57056177SMatthew G Knepley      #
76*57056177SMatthew G Knepley      #   1) When the process that dies is the session leader of a session that
77*57056177SMatthew G Knepley      #      is attached to a terminal device, SIGHUP is sent to all processes
78*57056177SMatthew G Knepley      #      in the foreground process group of that terminal device.
79*57056177SMatthew G Knepley      #   2) When the death of a process causes a process group to become
80*57056177SMatthew G Knepley      #      orphaned, and one or more processes in the orphaned group are
81*57056177SMatthew G Knepley      #      stopped, then SIGHUP and SIGCONT are sent to all members of the
82*57056177SMatthew G Knepley      #      orphaned group." [2]
83*57056177SMatthew G Knepley      #
84*57056177SMatthew G Knepley      # The first case can be ignored since the child is guaranteed not to have
85*57056177SMatthew G Knepley      # a controlling terminal.  The second case isn't so easy to dismiss.
86*57056177SMatthew G Knepley      # The process group is orphaned when the first child terminates and
87*57056177SMatthew G Knepley      # POSIX.1 requires that every STOPPED process in an orphaned process
88*57056177SMatthew G Knepley      # group be sent a SIGHUP signal followed by a SIGCONT signal.  Since the
89*57056177SMatthew G Knepley      # second child is not STOPPED though, we can safely forego ignoring the
90*57056177SMatthew G Knepley      # SIGHUP signal.  In any case, there are no ill-effects if it is ignored.
91*57056177SMatthew G Knepley      #
92*57056177SMatthew G Knepley      # import signal           # Set handlers for asynchronous events.
93*57056177SMatthew G Knepley      # signal.signal(signal.SIGHUP, signal.SIG_IGN)
94*57056177SMatthew G Knepley
95*57056177SMatthew G Knepley      try:
96*57056177SMatthew G Knepley         # Fork a second child and exit immediately to prevent zombies.  This
97*57056177SMatthew G Knepley         # causes the second child process to be orphaned, making the init
98*57056177SMatthew G Knepley         # process responsible for its cleanup.  And, since the first child is
99*57056177SMatthew G Knepley         # a session leader without a controlling terminal, it's possible for
100*57056177SMatthew G Knepley         # it to acquire one by opening a terminal in the future (System V-
101*57056177SMatthew G Knepley         # based systems).  This second fork guarantees that the child is no
102*57056177SMatthew G Knepley         # longer a session leader, preventing the daemon from ever acquiring
103*57056177SMatthew G Knepley         # a controlling terminal.
104*57056177SMatthew G Knepley         pid = os.fork()	# Fork a second child.
105*57056177SMatthew G Knepley      except OSError, e:
106*57056177SMatthew G Knepley         raise Exception, "%s [%d]" % (e.strerror, e.errno)
107*57056177SMatthew G Knepley
108*57056177SMatthew G Knepley      if (pid == 0):	# The second child.
109*57056177SMatthew G Knepley         # Since the current working directory may be a mounted filesystem, we
110*57056177SMatthew G Knepley         # avoid the issue of not being able to unmount the filesystem at
111*57056177SMatthew G Knepley         # shutdown time by changing it to the root directory.
112*57056177SMatthew G Knepley         os.chdir(WORKDIR)
113*57056177SMatthew G Knepley         # We probably don't want the file mode creation mask inherited from
114*57056177SMatthew G Knepley         # the parent, so we give the child complete control over permissions.
115*57056177SMatthew G Knepley         os.umask(UMASK)
116*57056177SMatthew G Knepley      else:
117*57056177SMatthew G Knepley         # exit() or _exit()?  See below.
118*57056177SMatthew G Knepley         os._exit(0)	# Exit parent (the first child) of the second child.
119*57056177SMatthew G Knepley   else:
120*57056177SMatthew G Knepley      # exit() or _exit()?
121*57056177SMatthew G Knepley      # _exit is like exit(), but it doesn't call any functions registered
122*57056177SMatthew G Knepley      # with atexit (and on_exit) or any registered signal handlers.  It also
123*57056177SMatthew G Knepley      # closes any open file descriptors.  Using exit() may cause all stdio
124*57056177SMatthew G Knepley      # streams to be flushed twice and any temporary files may be unexpectedly
125*57056177SMatthew G Knepley      # removed.  It's therefore recommended that child branches of a fork()
126*57056177SMatthew G Knepley      # and the parent branch(es) of a daemon use _exit().
127*57056177SMatthew G Knepley      os._exit(0)	# Exit parent of the first child.
128*57056177SMatthew G Knepley
129*57056177SMatthew G Knepley   # Close all open file descriptors.  This prevents the child from keeping
130*57056177SMatthew G Knepley   # open any file descriptors inherited from the parent.  There is a variety
131*57056177SMatthew G Knepley   # of methods to accomplish this task.  Three are listed below.
132*57056177SMatthew G Knepley   #
133*57056177SMatthew G Knepley   # Try the system configuration variable, SC_OPEN_MAX, to obtain the maximum
134*57056177SMatthew G Knepley   # number of open file descriptors to close.  If it doesn't exists, use
135*57056177SMatthew G Knepley   # the default value (configurable).
136*57056177SMatthew G Knepley   #
137*57056177SMatthew G Knepley   # try:
138*57056177SMatthew G Knepley   #    maxfd = os.sysconf("SC_OPEN_MAX")
139*57056177SMatthew G Knepley   # except (AttributeError, ValueError):
140*57056177SMatthew G Knepley   #    maxfd = MAXFD
141*57056177SMatthew G Knepley   #
142*57056177SMatthew G Knepley   # OR
143*57056177SMatthew G Knepley   #
144*57056177SMatthew G Knepley   # if (os.sysconf_names.has_key("SC_OPEN_MAX")):
145*57056177SMatthew G Knepley   #    maxfd = os.sysconf("SC_OPEN_MAX")
146*57056177SMatthew G Knepley   # else:
147*57056177SMatthew G Knepley   #    maxfd = MAXFD
148*57056177SMatthew G Knepley   #
149*57056177SMatthew G Knepley   # OR
150*57056177SMatthew G Knepley   #
151*57056177SMatthew G Knepley   # Use the getrlimit method to retrieve the maximum file descriptor number
152*57056177SMatthew G Knepley   # that can be opened by this process.  If there is not limit on the
153*57056177SMatthew G Knepley   # resource, use the default value.
154*57056177SMatthew G Knepley   #
155*57056177SMatthew G Knepley   import resource		# Resource usage information.
156*57056177SMatthew G Knepley   maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
157*57056177SMatthew G Knepley   if (maxfd == resource.RLIM_INFINITY):
158*57056177SMatthew G Knepley      maxfd = MAXFD
159*57056177SMatthew G Knepley
160*57056177SMatthew G Knepley   # Iterate through and close all file descriptors.
161*57056177SMatthew G Knepley   for fd in range(0, maxfd):
162*57056177SMatthew G Knepley      try:
163*57056177SMatthew G Knepley         os.close(fd)
164*57056177SMatthew G Knepley      except OSError:	# ERROR, fd wasn't open to begin with (ignored)
165*57056177SMatthew G Knepley         pass
166*57056177SMatthew G Knepley
167*57056177SMatthew G Knepley   # Redirect the standard I/O file descriptors to the specified file.  Since
168*57056177SMatthew G Knepley   # the daemon has no controlling terminal, most daemons redirect stdin,
169*57056177SMatthew G Knepley   # stdout, and stderr to /dev/null.  This is done to prevent side-effects
170*57056177SMatthew G Knepley   # from reads and writes to the standard I/O file descriptors.
171*57056177SMatthew G Knepley
172*57056177SMatthew G Knepley   # This call to open is guaranteed to return the lowest file descriptor,
173*57056177SMatthew G Knepley   # which will be 0 (stdin), since it was closed above.
174*57056177SMatthew G Knepley   os.open(REDIRECT_TO, os.O_RDWR)	# standard input (0)
175*57056177SMatthew G Knepley
176*57056177SMatthew G Knepley   # Duplicate standard input to standard output and standard error.
177*57056177SMatthew G Knepley   os.dup2(0, 1)			# standard output (1)
178*57056177SMatthew G Knepley   os.dup2(0, 2)			# standard error (2)
179*57056177SMatthew G Knepley
180*57056177SMatthew G Knepley   return(0)
181*57056177SMatthew G Knepley
182*57056177SMatthew G Knepleyif __name__ == "__main__":
183*57056177SMatthew G Knepley
184*57056177SMatthew G Knepley   retCode = createDaemon('.')
185*57056177SMatthew G Knepley
186*57056177SMatthew G Knepley   # The code, as is, will create a new file in the root directory, when
187*57056177SMatthew G Knepley   # executed with superuser privileges.  The file will contain the following
188*57056177SMatthew G Knepley   # daemon related process parameters: return code, process ID, parent
189*57056177SMatthew G Knepley   # process group ID, session ID, user ID, effective user ID, real group ID,
190*57056177SMatthew G Knepley   # and the effective group ID.  Notice the relationship between the daemon's
191*57056177SMatthew G Knepley   # process ID, process group ID, and its parent's process ID.
192*57056177SMatthew G Knepley
193*57056177SMatthew G Knepley   procParams = """
194*57056177SMatthew G Knepley   return code = %s
195*57056177SMatthew G Knepley   process ID = %s
196*57056177SMatthew G Knepley   parent process ID = %s
197*57056177SMatthew G Knepley   process group ID = %s
198*57056177SMatthew G Knepley   session ID = %s
199*57056177SMatthew G Knepley   user ID = %s
200*57056177SMatthew G Knepley   effective user ID = %s
201*57056177SMatthew G Knepley   real group ID = %s
202*57056177SMatthew G Knepley   effective group ID = %s
203*57056177SMatthew G Knepley   """ % (retCode, os.getpid(), os.getppid(), os.getpgrp(), os.getsid(0),
204*57056177SMatthew G Knepley   os.getuid(), os.geteuid(), os.getgid(), os.getegid())
205*57056177SMatthew G Knepley
206*57056177SMatthew G Knepley   open("createDaemon.log", "w").write(procParams + "\n")
207*57056177SMatthew G Knepley
208*57056177SMatthew G Knepley   sys.exit(retCode)
209