Commit ca8e0e60 authored by Anael Beutot's avatar Anael Beutot
Browse files

Use close_fds from utils in ForkedJob

parent fd7105da
Loading
Loading
Loading
Loading
+3 −36
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ from threading import Thread, Event
from StringIO import StringIO

from cloudcontrol.node.exc import JobError
from cloudcontrol.node.utils import num_to_sig
from cloudcontrol.node.utils import num_to_sig, close_fds


logger = logging.getLogger(__name__)
@@ -252,40 +252,6 @@ class ForkedJob(object):
        args = args + (tb.getvalue(),)
        self.fatal(fmt, *args, status=kwargs.get('status', 1))

    def close_fds(self):
        """Close all fds uneeded fds in children.

        If global debug variable in configuration is set to True, then it will
        prevent standard input/output from being closed thus allowing logging on
        stderr.
        """
        # get max fd
        limit = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
        if limit == resource.RLIM_INFINITY:
            max_fd = 2048
        else:
            max_fd = limit

        exclude_fd = self.open_fds
        if self.job_manager.main.config.debug:
            exclude_fd += [0, 1, 2]  # debug
        for fd in xrange(max_fd, -1, -1):
            if fd in exclude_fd:
                continue
            try:
                os.close(fd)
            except OSError as exc:
                if exc.errno != errno.EBADF:
                    raise
                # wasn't open
        if not self.job_manager.main.config.debug:
            sys.stdin = open(os.devnull)
            sys.stdout = open(os.devnull)
            sys.stderr = open(os.devnull)
            assert sys.stdin.fileno() == 0
            assert sys.stdout.fileno() == 1
            assert sys.stderr.fileno() == 2

    def reset_signal_mask(self):
        signal.signal(signal.SIGTERM, lambda *args: os._exit(1))
        signal.signal(signal.SIGUSR1, signal.SIG_IGN)
@@ -314,7 +280,8 @@ class ForkedJob(object):
            # way to block signals in python :(
            try:
                self.reset_signal_mask()
                self.close_fds()
                close_fds(exclude_fds=self.open_fds,
                          debug=self.job_manager.main.config.debug)
                self.after_fork()
            except:
                traceback.print_exc()