Commit 287a1740 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

utils: Add function to set O_NONBLOCK on file descriptor


Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 73027ed2
......@@ -213,8 +213,7 @@ def _RunCmdPipe(cmd, env, via_shell, cwd):
child.stderr.fileno(): (err, child.stderr),
}
for fd in fdmap:
status = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, status | os.O_NONBLOCK)
SetNonblockFlag(fd, True)
while fdmap:
pollresult = RetryOnSignal(poller.poll)
......@@ -292,6 +291,25 @@ def SetCloseOnExecFlag(fd, enable):
fcntl.fcntl(fd, fcntl.F_SETFD, flags)
def SetNonblockFlag(fd, enable):
"""Sets or unsets the O_NONBLOCK flag on on a file descriptor.
@type fd: int
@param fd: File descriptor
@type enable: bool
@param enable: Whether to set or unset it
"""
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
if enable:
flags |= os.O_NONBLOCK
else:
flags &= ~os.O_NONBLOCK
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
def RetryOnSignal(fn, *args, **kwargs):
"""Calls a function again if it failed due to EINTR.
......
......@@ -250,6 +250,21 @@ class TestSetCloseOnExecFlag(unittest.TestCase):
fcntl.FD_CLOEXEC)
class TestSetNonblockFlag(unittest.TestCase):
def setUp(self):
self.tmpfile = tempfile.TemporaryFile()
def testEnable(self):
utils.SetNonblockFlag(self.tmpfile.fileno(), True)
self.failUnless(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFL) &
os.O_NONBLOCK)
def testDisable(self):
utils.SetNonblockFlag(self.tmpfile.fileno(), False)
self.failIf(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFL) &
os.O_NONBLOCK)
class TestRemoveFile(unittest.TestCase):
"""Test case for the RemoveFile function"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment