4 # Copyright (C) 2006, 2007, 2010, 2011, 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Utility functions for processes.
35 from cStringIO import StringIO
37 from ganeti import errors
38 from ganeti import constants
39 from ganeti import compat
41 from ganeti.utils import retry as utils_retry
42 from ganeti.utils import wrapper as utils_wrapper
43 from ganeti.utils import text as utils_text
44 from ganeti.utils import io as utils_io
45 from ganeti.utils import algo as utils_algo
48 #: when set to True, L{RunCmd} is disabled
53 _TIMEOUT_KILL) = range(3)
57 """Disables the use of fork(2).
60 global _no_fork # pylint: disable=W0603
65 class RunResult(object):
66 """Holds the result of running external programs.
69 @ivar exit_code: the exit code of the program, or None (if the program
71 @type signal: int or None
72 @ivar signal: the signal that caused the program to finish, or None
73 (if the program wasn't terminated by a signal)
75 @ivar stdout: the standard output of the program
77 @ivar stderr: the standard error of the program
79 @ivar failed: True in case the program was
80 terminated by a signal or exited with a non-zero exit code
81 @ivar fail_reason: a string detailing the termination reason
84 __slots__ = ["exit_code", "signal", "stdout", "stderr",
85 "failed", "fail_reason", "cmd"]
87 def __init__(self, exit_code, signal_, stdout, stderr, cmd, timeout_action,
90 self.exit_code = exit_code
94 self.failed = (signal_ is not None or exit_code != 0)
97 if self.signal is not None:
98 fail_msgs.append("terminated by signal %s" % self.signal)
99 elif self.exit_code is not None:
100 fail_msgs.append("exited with exit code %s" % self.exit_code)
102 fail_msgs.append("unable to determine termination reason")
104 if timeout_action == _TIMEOUT_TERM:
105 fail_msgs.append("terminated after timeout of %.2f seconds" % timeout)
106 elif timeout_action == _TIMEOUT_KILL:
107 fail_msgs.append(("force termination after timeout of %.2f seconds"
108 " and linger for another %.2f seconds") %
109 (timeout, constants.CHILD_LINGER_TIMEOUT))
111 if fail_msgs and self.failed:
112 self.fail_reason = utils_text.CommaJoin(fail_msgs)
114 self.fail_reason = None
117 logging.debug("Command '%s' failed (%s); output: %s",
118 self.cmd, self.fail_reason, self.output)
120 def _GetOutput(self):
121 """Returns the combined stdout and stderr for easier usage.
124 return self.stdout + self.stderr
126 output = property(_GetOutput, None, None, "Return full output")
129 def _BuildCmdEnvironment(env, reset):
130 """Builds the environment for an external program.
136 cmd_env = os.environ.copy()
137 cmd_env["LC_ALL"] = "C"
145 def RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False,
146 interactive=False, timeout=None, noclose_fds=None,
147 input_fd=None, postfork_fn=None):
148 """Execute a (shell) command.
150 The command should not read from its standard input, as it will be
153 @type cmd: string or list
154 @param cmd: Command to run
156 @param env: Additional environment variables
158 @param output: if desired, the output of the command can be
159 saved in a file instead of the RunResult instance; this
160 parameter denotes the file name (if not None)
162 @param cwd: if specified, will be used as the working
163 directory for the command; the default will be /
164 @type reset_env: boolean
165 @param reset_env: whether to reset or keep the default os environment
166 @type interactive: boolean
167 @param interactive: whether we pipe stdin, stdout and stderr
168 (default behaviour) or run the command interactive
170 @param timeout: If not None, timeout in seconds until child process gets
172 @type noclose_fds: list
173 @param noclose_fds: list of additional (fd >=3) file descriptors to leave
174 open for the child process
175 @type input_fd: C{file}-like object or numeric file descriptor
176 @param input_fd: File descriptor for process' standard input
177 @type postfork_fn: Callable receiving PID as parameter
178 @param postfork_fn: Callback run after fork but before timeout
180 @return: RunResult instance
181 @raise errors.ProgrammerError: if we call this when forks are disabled
185 raise errors.ProgrammerError("utils.RunCmd() called with fork() disabled")
187 if output and interactive:
188 raise errors.ProgrammerError("Parameters 'output' and 'interactive' can"
189 " not be provided at the same time")
191 if not (output is None or input_fd is None):
192 # The current logic in "_RunCmdFile", which is used when output is defined,
193 # does not support input files (not hard to implement, though)
194 raise errors.ProgrammerError("Parameters 'output' and 'input_fd' can"
195 " not be used at the same time")
197 if isinstance(cmd, basestring):
201 cmd = [str(val) for val in cmd]
202 strcmd = utils_text.ShellQuoteArgs(cmd)
206 logging.info("RunCmd %s, output file '%s'", strcmd, output)
208 logging.info("RunCmd %s", strcmd)
210 cmd_env = _BuildCmdEnvironment(env, reset_env)
214 out, err, status, timeout_action = _RunCmdPipe(cmd, cmd_env, shell, cwd,
215 interactive, timeout,
216 noclose_fds, input_fd,
217 postfork_fn=postfork_fn)
220 raise errors.ProgrammerError("postfork_fn is not supported if output"
221 " should be captured")
222 assert input_fd is None
223 timeout_action = _TIMEOUT_NONE
224 status = _RunCmdFile(cmd, cmd_env, shell, output, cwd, noclose_fds)
227 if err.errno == errno.ENOENT:
228 raise errors.OpExecError("Can't execute '%s': not found (%s)" %
240 return RunResult(exitcode, signal_, out, err, strcmd, timeout_action, timeout)
243 def SetupDaemonEnv(cwd="/", umask=077):
244 """Setup a daemon's environment.
246 This should be called between the first and second fork, due to
249 @param cwd: the directory to which to chdir
250 @param umask: the umask to setup
258 def SetupDaemonFDs(output_file, output_fd):
259 """Setups up a daemon's file descriptors.
261 @param output_file: if not None, the file to which to redirect
263 @param output_fd: if not None, the file descriptor for stdout/stderr
266 # check that at most one is defined
267 assert [output_file, output_fd].count(None) >= 1
269 # Open /dev/null (read-only, only for stdin)
270 devnull_fd = os.open(os.devnull, os.O_RDONLY)
274 if output_fd is not None:
276 elif output_file is not None:
279 output_fd = os.open(output_file,
280 os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600)
281 except EnvironmentError, err:
282 raise Exception("Opening output file failed: %s" % err)
284 output_fd = os.open(os.devnull, os.O_WRONLY)
286 # Redirect standard I/O
287 os.dup2(devnull_fd, 0)
288 os.dup2(output_fd, 1)
289 os.dup2(output_fd, 2)
292 utils_wrapper.CloseFdNoError(devnull_fd)
294 if output_close and output_fd > 2:
295 utils_wrapper.CloseFdNoError(output_fd)
298 def StartDaemon(cmd, env=None, cwd="/", output=None, output_fd=None,
300 """Start a daemon process after forking twice.
302 @type cmd: string or list
303 @param cmd: Command to run
305 @param env: Additional environment variables
307 @param cwd: Working directory for the program
309 @param output: Path to file in which to save the output
311 @param output_fd: File descriptor for output
312 @type pidfile: string
313 @param pidfile: Process ID file
315 @return: Daemon process ID
316 @raise errors.ProgrammerError: if we call this when forks are disabled
320 raise errors.ProgrammerError("utils.StartDaemon() called with fork()"
323 if output and not (bool(output) ^ (output_fd is not None)):
324 raise errors.ProgrammerError("Only one of 'output' and 'output_fd' can be"
327 if isinstance(cmd, basestring):
328 cmd = ["/bin/sh", "-c", cmd]
330 strcmd = utils_text.ShellQuoteArgs(cmd)
333 logging.debug("StartDaemon %s, output file '%s'", strcmd, output)
335 logging.debug("StartDaemon %s", strcmd)
337 cmd_env = _BuildCmdEnvironment(env, False)
339 # Create pipe for sending PID back
340 (pidpipe_read, pidpipe_write) = os.pipe()
343 # Create pipe for sending error messages
344 (errpipe_read, errpipe_write) = os.pipe()
351 # Child process, won't return
352 _StartDaemonChild(errpipe_read, errpipe_write,
353 pidpipe_read, pidpipe_write,
355 output, output_fd, pidfile)
357 # Well, maybe child process failed
358 os._exit(1) # pylint: disable=W0212
360 utils_wrapper.CloseFdNoError(errpipe_write)
362 # Wait for daemon to be started (or an error message to
363 # arrive) and read up to 100 KB as an error message
364 errormsg = utils_wrapper.RetryOnSignal(os.read, errpipe_read,
367 utils_wrapper.CloseFdNoError(errpipe_read)
369 utils_wrapper.CloseFdNoError(pidpipe_write)
371 # Read up to 128 bytes for PID
372 pidtext = utils_wrapper.RetryOnSignal(os.read, pidpipe_read, 128)
374 utils_wrapper.CloseFdNoError(pidpipe_read)
376 # Try to avoid zombies by waiting for child process
383 raise errors.OpExecError("Error when starting daemon process: %r" %
388 except (ValueError, TypeError), err:
389 raise errors.OpExecError("Error while trying to parse PID %r: %s" %
393 def _StartDaemonChild(errpipe_read, errpipe_write,
394 pidpipe_read, pidpipe_write,
396 output, fd_output, pidfile):
397 """Child process for starting daemon.
401 # Close parent's side
402 utils_wrapper.CloseFdNoError(errpipe_read)
403 utils_wrapper.CloseFdNoError(pidpipe_read)
405 # First child process
408 # And fork for the second time
411 # Exit first child process
412 os._exit(0) # pylint: disable=W0212
414 # Make sure pipe is closed on execv* (and thereby notifies
416 utils_wrapper.SetCloseOnExecFlag(errpipe_write, True)
418 # List of file descriptors to be left open
419 noclose_fds = [errpipe_write]
423 fd_pidfile = utils_io.WritePidFile(pidfile)
425 # Keeping the file open to hold the lock
426 noclose_fds.append(fd_pidfile)
428 utils_wrapper.SetCloseOnExecFlag(fd_pidfile, False)
432 SetupDaemonFDs(output, fd_output)
434 # Send daemon PID to parent
435 utils_wrapper.RetryOnSignal(os.write, pidpipe_write, str(os.getpid()))
437 # Close all file descriptors except stdio and error message pipe
438 CloseFDs(noclose_fds=noclose_fds)
440 # Change working directory
444 os.execvp(args[0], args)
446 os.execvpe(args[0], args, env)
447 except: # pylint: disable=W0702
449 # Report errors to original process
450 WriteErrorToFD(errpipe_write, str(sys.exc_info()[1]))
451 except: # pylint: disable=W0702
452 # Ignore errors in error handling
455 os._exit(1) # pylint: disable=W0212
458 def WriteErrorToFD(fd, err):
459 """Possibly write an error message to a fd.
461 @type fd: None or int (file descriptor)
462 @param fd: if not None, the error will be written to this fd
463 @param err: string, the error message
470 err = "<unknown error>"
472 utils_wrapper.RetryOnSignal(os.write, fd, err)
475 def _CheckIfAlive(child):
476 """Raises L{utils_retry.RetryAgain} if child is still alive.
478 @raises utils_retry.RetryAgain: If child is still alive
481 if child.poll() is None:
482 raise utils_retry.RetryAgain()
485 def _WaitForProcess(child, timeout):
486 """Waits for the child to terminate or until we reach timeout.
490 utils_retry.Retry(_CheckIfAlive, (1.0, 1.2, 5.0), max(0, timeout),
492 except utils_retry.RetryTimeout:
496 def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout, noclose_fds,
497 input_fd, postfork_fn=None,
498 _linger_timeout=constants.CHILD_LINGER_TIMEOUT):
499 """Run a command and return its output.
501 @type cmd: string or list
502 @param cmd: Command to run
504 @param env: The environment to use
505 @type via_shell: bool
506 @param via_shell: if we should run via the shell
508 @param cwd: the working directory for the program
509 @type interactive: boolean
510 @param interactive: Run command interactive (without piping)
512 @param timeout: Timeout after the programm gets terminated
513 @type noclose_fds: list
514 @param noclose_fds: list of additional (fd >=3) file descriptors to leave
515 open for the child process
516 @type input_fd: C{file}-like object or numeric file descriptor
517 @param input_fd: File descriptor for process' standard input
518 @type postfork_fn: Callable receiving PID as parameter
519 @param postfork_fn: Function run after fork but before timeout
521 @return: (out, err, status)
524 poller = select.poll()
530 stderr = subprocess.PIPE
531 stdout = subprocess.PIPE
538 stdin = subprocess.PIPE
541 preexec_fn = lambda: CloseFDs(noclose_fds)
547 child = subprocess.Popen(cmd, shell=via_shell,
551 close_fds=close_fds, env=env,
553 preexec_fn=preexec_fn)
556 postfork_fn(child.pid)
561 linger_timeout = None
566 poll_timeout = utils_algo.RunningTimeout(timeout, True).Remaining
568 msg_timeout = ("Command %s (%d) run into execution timeout, terminating" %
570 msg_linger = ("Command %s (%d) run into linger timeout, killing" %
573 timeout_action = _TIMEOUT_NONE
575 # subprocess: "If the stdin argument is PIPE, this attribute is a file object
576 # that provides input to the child process. Otherwise, it is None."
577 assert (stdin == subprocess.PIPE) ^ (child.stdin is None), \
578 "subprocess' stdin did not behave as documented"
581 if child.stdin is not None:
583 poller.register(child.stdout, select.POLLIN)
584 poller.register(child.stderr, select.POLLIN)
586 child.stdout.fileno(): (out, child.stdout),
587 child.stderr.fileno(): (err, child.stderr),
590 utils_wrapper.SetNonblockFlag(fd, True)
594 pt = poll_timeout() * 1000
596 if linger_timeout is None:
597 logging.warning(msg_timeout)
598 if child.poll() is None:
599 timeout_action = _TIMEOUT_TERM
600 utils_wrapper.IgnoreProcessNotFound(os.kill, child.pid,
603 utils_algo.RunningTimeout(_linger_timeout, True).Remaining
604 pt = linger_timeout() * 1000
610 pollresult = utils_wrapper.RetryOnSignal(poller.poll, pt)
612 for fd, event in pollresult:
613 if event & select.POLLIN or event & select.POLLPRI:
614 data = fdmap[fd][1].read()
615 # no data from read signifies EOF (the same as POLLHUP)
617 poller.unregister(fd)
620 fdmap[fd][0].write(data)
621 if (event & select.POLLNVAL or event & select.POLLHUP or
622 event & select.POLLERR):
623 poller.unregister(fd)
626 if timeout is not None:
627 assert callable(poll_timeout)
629 # We have no I/O left but it might still run
630 if child.poll() is None:
631 _WaitForProcess(child, poll_timeout())
633 # Terminate if still alive after timeout
634 if child.poll() is None:
635 if linger_timeout is None:
636 logging.warning(msg_timeout)
637 timeout_action = _TIMEOUT_TERM
638 utils_wrapper.IgnoreProcessNotFound(os.kill, child.pid, signal.SIGTERM)
641 lt = linger_timeout()
642 _WaitForProcess(child, lt)
644 # Okay, still alive after timeout and linger timeout? Kill it!
645 if child.poll() is None:
646 timeout_action = _TIMEOUT_KILL
647 logging.warning(msg_linger)
648 utils_wrapper.IgnoreProcessNotFound(os.kill, child.pid, signal.SIGKILL)
653 status = child.wait()
654 return out, err, status, timeout_action
657 def _RunCmdFile(cmd, env, via_shell, output, cwd, noclose_fds):
658 """Run a command and save its output to a file.
660 @type cmd: string or list
661 @param cmd: Command to run
663 @param env: The environment to use
664 @type via_shell: bool
665 @param via_shell: if we should run via the shell
667 @param output: the filename in which to save the output
669 @param cwd: the working directory for the program
670 @type noclose_fds: list
671 @param noclose_fds: list of additional (fd >=3) file descriptors to leave
672 open for the child process
674 @return: the exit status
677 fh = open(output, "a")
680 preexec_fn = lambda: CloseFDs(noclose_fds + [fh.fileno()])
687 child = subprocess.Popen(cmd, shell=via_shell,
688 stderr=subprocess.STDOUT,
690 stdin=subprocess.PIPE,
691 close_fds=close_fds, env=env,
693 preexec_fn=preexec_fn)
696 status = child.wait()
702 def RunParts(dir_name, env=None, reset_env=False):
703 """Run Scripts or programs in a directory
705 @type dir_name: string
706 @param dir_name: absolute path to a directory
708 @param env: The environment to use
709 @type reset_env: boolean
710 @param reset_env: whether to reset or keep the default os environment
711 @rtype: list of tuples
712 @return: list of (name, (one of RUNDIR_STATUS), RunResult)
718 dir_contents = utils_io.ListVisibleFiles(dir_name)
720 logging.warning("RunParts: skipping %s (cannot list: %s)", dir_name, err)
723 for relname in sorted(dir_contents):
724 fname = utils_io.PathJoin(dir_name, relname)
725 if not (constants.EXT_PLUGIN_MASK.match(relname) is not None and
726 utils_wrapper.IsExecutable(fname)):
727 rr.append((relname, constants.RUNPARTS_SKIP, None))
730 result = RunCmd([fname], env=env, reset_env=reset_env)
731 except Exception, err: # pylint: disable=W0703
732 rr.append((relname, constants.RUNPARTS_ERR, str(err)))
734 rr.append((relname, constants.RUNPARTS_RUN, result))
739 def _GetProcStatusPath(pid):
740 """Returns the path for a PID's proc status file.
743 @param pid: Process ID
747 return "/proc/%d/status" % pid
750 def IsProcessAlive(pid):
751 """Check if a given pid exists on the system.
753 @note: zombie status is not handled, so zombie processes
754 will be returned as alive
756 @param pid: the process ID to check
758 @return: True if the process exists
765 except EnvironmentError, err:
766 if err.errno in (errno.ENOENT, errno.ENOTDIR):
768 elif err.errno == errno.EINVAL:
769 raise utils_retry.RetryAgain(err)
772 assert isinstance(pid, int), "pid must be an integer"
776 # /proc in a multiprocessor environment can have strange behaviors.
777 # Retry the os.stat a few times until we get a good result.
779 return utils_retry.Retry(_TryStat, (0.01, 1.5, 0.1), 0.5,
780 args=[_GetProcStatusPath(pid)])
781 except utils_retry.RetryTimeout, err:
785 def _ParseSigsetT(sigset):
786 """Parse a rendered sigset_t value.
788 This is the opposite of the Linux kernel's fs/proc/array.c:render_sigset_t
792 @param sigset: Rendered signal set from /proc/$pid/status
794 @return: Set of all enabled signal numbers
800 for ch in reversed(sigset):
803 # The following could be done in a loop, but it's easier to read and
804 # understand in the unrolled form
806 result.add(signum + 1)
808 result.add(signum + 2)
810 result.add(signum + 3)
812 result.add(signum + 4)
819 def _GetProcStatusField(pstatus, field):
820 """Retrieves a field from the contents of a proc status file.
822 @type pstatus: string
823 @param pstatus: Contents of /proc/$pid/status
825 @param field: Name of field whose value should be returned
829 for line in pstatus.splitlines():
830 parts = line.split(":", 1)
832 if len(parts) < 2 or parts[0] != field:
835 return parts[1].strip()
840 def IsProcessHandlingSignal(pid, signum, status_path=None):
841 """Checks whether a process is handling a signal.
844 @param pid: Process ID
846 @param signum: Signal number
850 if status_path is None:
851 status_path = _GetProcStatusPath(pid)
854 proc_status = utils_io.ReadFile(status_path)
855 except EnvironmentError, err:
856 # In at least one case, reading /proc/$pid/status failed with ESRCH.
857 if err.errno in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL, errno.ESRCH):
861 sigcgt = _GetProcStatusField(proc_status, "SigCgt")
863 raise RuntimeError("%s is missing 'SigCgt' field" % status_path)
865 # Now check whether signal is handled
866 return signum in _ParseSigsetT(sigcgt)
869 def Daemonize(logfile):
870 """Daemonize the current process.
872 This detaches the current process from the controlling terminal and
873 runs it in the background as a daemon.
876 @param logfile: the logfile to which we should redirect stdout/stderr
877 @rtype: tuple; (int, callable)
878 @return: File descriptor of pipe(2) which must be closed to notify parent
879 process and a callable to reopen log files
882 # pylint: disable=W0212
883 # yes, we really want os._exit
885 # TODO: do another attempt to merge Daemonize and StartDaemon, or at
886 # least abstract the pipe functionality between them
888 # Create pipe for sending error messages
889 (rpipe, wpipe) = os.pipe()
893 if (pid == 0): # The first child.
897 pid = os.fork() # Fork a second child.
898 if (pid == 0): # The second child.
899 utils_wrapper.CloseFdNoError(rpipe)
901 # exit() or _exit()? See below.
902 os._exit(0) # Exit parent (the first child) of the second child.
904 utils_wrapper.CloseFdNoError(wpipe)
905 # Wait for daemon to be started (or an error message to
906 # arrive) and read up to 100 KB as an error message
907 errormsg = utils_wrapper.RetryOnSignal(os.read, rpipe, 100 * 1024)
909 sys.stderr.write("Error when starting daemon process: %r\n" % errormsg)
913 os._exit(rcode) # Exit parent of the first child.
915 reopen_fn = compat.partial(SetupDaemonFDs, logfile, None)
917 # Open logs for the first time
920 return (wpipe, reopen_fn)
923 def KillProcess(pid, signal_=signal.SIGTERM, timeout=30,
925 """Kill a process given by its pid.
928 @param pid: The PID to terminate.
930 @param signal_: The signal to send, by default SIGTERM
932 @param timeout: The timeout after which, if the process is still alive,
933 a SIGKILL will be sent. If not positive, no such checking
935 @type waitpid: boolean
936 @param waitpid: If true, we should waitpid on this process after
937 sending signals, since it's our own child and otherwise it
938 would remain as zombie
941 def _helper(pid, signal_, wait):
942 """Simple helper to encapsulate the kill/waitpid sequence"""
943 if utils_wrapper.IgnoreProcessNotFound(os.kill, pid, signal_) and wait:
945 os.waitpid(pid, os.WNOHANG)
950 # kill with pid=0 == suicide
951 raise errors.ProgrammerError("Invalid pid given '%s'" % pid)
953 if not IsProcessAlive(pid):
956 _helper(pid, signal_, waitpid)
962 if not IsProcessAlive(pid):
966 (result_pid, _) = os.waitpid(pid, os.WNOHANG)
968 raise utils_retry.RetryAgain()
973 raise utils_retry.RetryAgain()
976 # Wait up to $timeout seconds
977 utils_retry.Retry(_CheckProcess, (0.01, 1.5, 0.1), timeout)
978 except utils_retry.RetryTimeout:
981 if IsProcessAlive(pid):
982 # Kill process if it's still alive
983 _helper(pid, signal.SIGKILL, waitpid)
986 def RunInSeparateProcess(fn, *args):
987 """Runs a function in a separate process.
989 Note: Only boolean return values are supported.
992 @param fn: Function to be called
994 @return: Function's result
1001 # In case the function uses temporary files
1002 utils_wrapper.ResetTempfileModule()
1005 result = int(bool(fn(*args)))
1006 assert result in (0, 1)
1007 except: # pylint: disable=W0702
1008 logging.exception("Error while calling function in separate process")
1009 # 0 and 1 are reserved for the return value
1012 os._exit(result) # pylint: disable=W0212
1016 # Avoid zombies and check exit code
1017 (_, status) = os.waitpid(pid, 0)
1019 if os.WIFSIGNALED(status):
1021 signum = os.WTERMSIG(status)
1023 exitcode = os.WEXITSTATUS(status)
1026 if not (exitcode in (0, 1) and signum is None):
1027 raise errors.GenericError("Child program failed (code=%s, signal=%s)" %
1030 return bool(exitcode)
1033 def CloseFDs(noclose_fds=None):
1034 """Close file descriptors.
1036 This closes all file descriptors above 2 (i.e. except
1039 @type noclose_fds: list or None
1040 @param noclose_fds: if given, it denotes a list of file descriptor
1041 that should not be closed
1044 # Default maximum for the number of available file descriptors.
1045 if 'SC_OPEN_MAX' in os.sysconf_names:
1047 MAXFD = os.sysconf('SC_OPEN_MAX')
1055 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
1056 if (maxfd == resource.RLIM_INFINITY):
1059 # Iterate through and close all file descriptors (except the standard ones)
1060 for fd in range(3, maxfd):
1061 if noclose_fds and fd in noclose_fds:
1063 utils_wrapper.CloseFdNoError(fd)