Use set_cloexec() everywhere.
This commit is contained in:
parent
6bedfe1934
commit
bca3a65157
|
@ -5,6 +5,7 @@ Python external execution contexts.
|
||||||
import Queue
|
import Queue
|
||||||
import cPickle
|
import cPickle
|
||||||
import cStringIO
|
import cStringIO
|
||||||
|
import fcntl
|
||||||
import hmac
|
import hmac
|
||||||
import imp
|
import imp
|
||||||
import logging
|
import logging
|
||||||
|
@ -74,6 +75,11 @@ class Dead(object):
|
||||||
_DEAD = Dead()
|
_DEAD = Dead()
|
||||||
|
|
||||||
|
|
||||||
|
def set_cloexec(fd):
|
||||||
|
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
|
||||||
|
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
|
||||||
|
|
||||||
|
|
||||||
def write_all(fd, s):
|
def write_all(fd, s):
|
||||||
written = 0
|
written = 0
|
||||||
while written < len(s):
|
while written < len(s):
|
||||||
|
@ -477,6 +483,8 @@ class Waker(BasicStream):
|
||||||
def __init__(self, broker):
|
def __init__(self, broker):
|
||||||
self._broker = broker
|
self._broker = broker
|
||||||
rfd, wfd = os.pipe()
|
rfd, wfd = os.pipe()
|
||||||
|
set_cloexec(rfd)
|
||||||
|
set_cloexec(wfd)
|
||||||
self.read_side = Side(self, rfd)
|
self.read_side = Side(self, rfd)
|
||||||
self.write_side = Side(self, wfd)
|
self.write_side = Side(self, wfd)
|
||||||
broker.UpdateStream(self)
|
broker.UpdateStream(self)
|
||||||
|
@ -494,14 +502,18 @@ class Waker(BasicStream):
|
||||||
class IoLogger(BasicStream):
|
class IoLogger(BasicStream):
|
||||||
_buf = ''
|
_buf = ''
|
||||||
|
|
||||||
def __init__(self, broker, name):
|
def __init__(self, broker, name, dest_fd):
|
||||||
self._broker = broker
|
self._broker = broker
|
||||||
self._name = name
|
self._name = name
|
||||||
self._log = logging.getLogger(name)
|
self._log = logging.getLogger(name)
|
||||||
rfd, wfd = os.pipe()
|
rfd, wfd = os.pipe()
|
||||||
|
|
||||||
|
set_cloexec(rfd)
|
||||||
|
os.dup2(wfd, dest_fd)
|
||||||
|
os.close(wfd)
|
||||||
|
|
||||||
self.read_side = Side(self, rfd)
|
self.read_side = Side(self, rfd)
|
||||||
self.write_side = Side(self, wfd)
|
self.write_side = Side(self, dest_fd)
|
||||||
self._broker.UpdateStream(self)
|
self._broker.UpdateStream(self)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -655,14 +667,11 @@ class ExternalContext(object):
|
||||||
sys.meta_path.append(self.importer)
|
sys.meta_path.append(self.importer)
|
||||||
|
|
||||||
def _SetupStdio(self):
|
def _SetupStdio(self):
|
||||||
self.stdout_log = IoLogger(self.broker, 'stdout')
|
self.stdout_log = IoLogger(self.broker, 'stdout', 1)
|
||||||
self.stderr_log = IoLogger(self.broker, 'stderr')
|
self.stderr_log = IoLogger(self.broker, 'stderr', 2)
|
||||||
os.dup2(self.stdout_log.write_side.fd, 1)
|
|
||||||
os.dup2(self.stderr_log.write_side.fd, 2)
|
|
||||||
|
|
||||||
# Why is this necessary?
|
# Reopen with line buffering.
|
||||||
sys.stdout = os.fdopen(self.stdout_log.write_side.fd, 'w', 0)
|
sys.stdout = open('/dev/stdout', 'w', 1)
|
||||||
sys.stderr = os.fdopen(self.stderr_log.write_side.fd, 'w', 0)
|
|
||||||
|
|
||||||
fp = file('/dev/null')
|
fp = file('/dev/null')
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -49,6 +49,7 @@ class Listener(econtext.core.BasicStream):
|
||||||
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self._sock.bind(address or ('0.0.0.0', 0))
|
self._sock.bind(address or ('0.0.0.0', 0))
|
||||||
self._sock.listen(backlog)
|
self._sock.listen(backlog)
|
||||||
|
econtext.core.set_cloexec(self._sock.fileno())
|
||||||
self._listen_addr = self._sock.getsockname()
|
self._listen_addr = self._sock.getsockname()
|
||||||
self.read_side = econtext.core.Side(self, self._sock.fileno())
|
self.read_side = econtext.core.Side(self, self._sock.fileno())
|
||||||
broker.UpdateStream(self)
|
broker.UpdateStream(self)
|
||||||
|
|
|
@ -2,12 +2,14 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import econtext
|
import econtext
|
||||||
|
import econtext.core
|
||||||
import econtext.master
|
import econtext.master
|
||||||
|
|
||||||
|
|
||||||
def log_to_file(path, level=logging.DEBUG):
|
def log_to_file(path, level=logging.DEBUG):
|
||||||
log = logging.getLogger('')
|
log = logging.getLogger('')
|
||||||
fp = open(path, 'w', 1)
|
fp = open(path, 'w', 1)
|
||||||
|
econtext.core.set_cloexec(fp.fileno())
|
||||||
log.setLevel(level)
|
log.setLevel(level)
|
||||||
log.handlers.insert(0, logging.StreamHandler(fp))
|
log.handlers.insert(0, logging.StreamHandler(fp))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue