2019-07-17 16:24:14 +00:00
|
|
|
|
|
|
|
import fcntl
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
import mock
|
|
|
|
import unittest2
|
|
|
|
|
2019-08-10 16:36:10 +00:00
|
|
|
import mitogen.core
|
2019-07-17 16:24:14 +00:00
|
|
|
import mitogen.parent
|
2019-07-20 23:42:43 +00:00
|
|
|
from mitogen.core import b
|
2019-07-17 16:24:14 +00:00
|
|
|
|
|
|
|
import testlib
|
|
|
|
|
|
|
|
|
2019-08-03 04:12:33 +00:00
|
|
|
def _osx_mode(n):
|
|
|
|
"""
|
|
|
|
fstat(2) on UNIX sockets on OSX return different mode bits depending on
|
|
|
|
which side is being inspected, so zero those bits for comparison.
|
|
|
|
"""
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
n &= ~int('0777', 8)
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
2019-07-17 16:24:14 +00:00
|
|
|
def run_fd_check(func, fd, mode, on_start=None):
|
2019-08-03 04:12:33 +00:00
|
|
|
"""
|
|
|
|
Run ``tests/data/fd_check.py`` using `func`. The subprocess writes
|
|
|
|
information about the `fd` it received to a temporary file.
|
|
|
|
|
|
|
|
:param func:
|
|
|
|
Function like `create_child()` used to start child.
|
|
|
|
:param fd:
|
|
|
|
FD child should read/write from, and report information about.
|
|
|
|
:param mode:
|
|
|
|
"read" or "write", depending on whether the FD is readable or writeable
|
|
|
|
from the perspective of the child. If "read", `on_start()` should write
|
|
|
|
"TEST" to it and the child reads "TEST" from it, otherwise `on_start()`
|
|
|
|
should read "TEST" from it and the child writes "TEST" to it.
|
|
|
|
:param on_start:
|
|
|
|
Function invoked as `on_start(proc)`
|
|
|
|
:returns:
|
|
|
|
Tuple of `(proc, info, on_start_result)`, where:
|
|
|
|
|
|
|
|
* `proc`: the :class:`mitogen.parent.Process` returned by `func`.
|
|
|
|
* `info`: dict containing information returned by the child:
|
|
|
|
* `buf`: "TEST" that was read in "read" mode
|
|
|
|
* `flags`: :attr:`fcntl.F_GETFL` flags for `fd`
|
|
|
|
* `st_mode`: st_mode field from :func:`os.fstat`
|
|
|
|
* `st_dev`: st_dev field from :func:`os.fstat`
|
|
|
|
* `st_ino`: st_ino field from :func:`os.fstat`
|
|
|
|
* `ttyname`: :func:`os.ttyname` invoked on `fd`.
|
|
|
|
* `controlling_tty`: :func:os.ttyname` invoked on ``/dev/tty``
|
|
|
|
from within the child.
|
|
|
|
"""
|
2019-07-17 16:24:14 +00:00
|
|
|
tf = tempfile.NamedTemporaryFile()
|
|
|
|
args = [
|
|
|
|
sys.executable,
|
|
|
|
testlib.data_path('fd_check.py'),
|
|
|
|
tf.name,
|
|
|
|
str(fd),
|
|
|
|
mode,
|
|
|
|
]
|
|
|
|
|
|
|
|
proc = func(args=args)
|
|
|
|
os = None
|
|
|
|
if on_start:
|
|
|
|
os = on_start(proc)
|
|
|
|
proc.proc.wait()
|
|
|
|
try:
|
|
|
|
return proc, eval(tf.read()), os
|
|
|
|
finally:
|
|
|
|
tf.close()
|
|
|
|
|
|
|
|
|
|
|
|
def close_proc(proc):
|
2019-07-20 23:42:43 +00:00
|
|
|
proc.stdin.close()
|
|
|
|
proc.stdout.close()
|
|
|
|
if proc.stderr:
|
|
|
|
prco.stderr.close()
|
2019-07-17 16:24:14 +00:00
|
|
|
|
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
def wait_read(fp, n):
|
2019-07-17 16:24:14 +00:00
|
|
|
poller = mitogen.core.Poller()
|
|
|
|
try:
|
2019-07-20 23:42:43 +00:00
|
|
|
poller.start_receive(fp.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
for _ in poller.poll():
|
2019-07-20 23:42:43 +00:00
|
|
|
return os.read(fp.fileno(), n)
|
2019-07-17 16:24:14 +00:00
|
|
|
assert False
|
|
|
|
finally:
|
|
|
|
poller.close()
|
|
|
|
|
|
|
|
|
|
|
|
class StdinSockMixin(object):
|
|
|
|
def test_stdin(self):
|
|
|
|
proc, info, _ = run_fd_check(self.func, 0, 'read',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: proc.stdin.send(b('TEST')))
|
|
|
|
st = os.fstat(proc.stdin.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISSOCK(st.st_mode))
|
|
|
|
self.assertEquals(st.st_dev, info['st_dev'])
|
2019-08-03 04:12:33 +00:00
|
|
|
self.assertEquals(st.st_mode, _osx_mode(info['st_mode']))
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stdin.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(info['buf'], 'TEST')
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
|
|
|
|
|
|
|
|
|
|
|
class StdoutSockMixin(object):
|
|
|
|
def test_stdout(self):
|
|
|
|
proc, info, buf = run_fd_check(self.func, 1, 'write',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: wait_read(proc.stdout, 4))
|
|
|
|
st = os.fstat(proc.stdout.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISSOCK(st.st_mode))
|
|
|
|
self.assertEquals(st.st_dev, info['st_dev'])
|
2019-08-03 04:12:33 +00:00
|
|
|
self.assertEquals(st.st_mode, _osx_mode(info['st_mode']))
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
|
|
|
|
|
|
|
|
|
|
|
class CreateChildTest(StdinSockMixin, StdoutSockMixin, testlib.TestCase):
|
|
|
|
func = staticmethod(mitogen.parent.create_child)
|
|
|
|
|
|
|
|
def test_stderr(self):
|
|
|
|
proc, info, _ = run_fd_check(self.func, 2, 'write')
|
|
|
|
st = os.fstat(sys.stderr.fileno())
|
|
|
|
self.assertEquals(st.st_dev, info['st_dev'])
|
|
|
|
self.assertEquals(st.st_mode, info['st_mode'])
|
|
|
|
self.assertEquals(st.st_ino, info['st_ino'])
|
|
|
|
|
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
class CreateChildMergedTest(StdinSockMixin, StdoutSockMixin,
|
2019-07-17 16:24:14 +00:00
|
|
|
testlib.TestCase):
|
2019-07-20 23:42:43 +00:00
|
|
|
def func(self, *args, **kwargs):
|
2019-07-25 11:02:00 +00:00
|
|
|
kwargs['merge_stdio'] = True
|
|
|
|
return mitogen.parent.create_child(*args, **kwargs)
|
2019-07-17 16:24:14 +00:00
|
|
|
|
|
|
|
def test_stderr(self):
|
|
|
|
proc, info, buf = run_fd_check(self.func, 2, 'write',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: wait_read(proc.stdout, 4))
|
|
|
|
self.assertEquals(None, proc.stderr)
|
|
|
|
st = os.fstat(proc.stdout.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISSOCK(st.st_mode))
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
|
|
|
|
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
class CreateChildStderrPipeTest(StdinSockMixin, StdoutSockMixin,
|
|
|
|
testlib.TestCase):
|
|
|
|
def func(self, *args, **kwargs):
|
2019-07-25 12:54:02 +00:00
|
|
|
kwargs['stderr_pipe'] = True
|
|
|
|
return mitogen.parent.create_child(*args, **kwargs)
|
2019-07-17 16:24:14 +00:00
|
|
|
|
|
|
|
def test_stderr(self):
|
|
|
|
proc, info, buf = run_fd_check(self.func, 2, 'write',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: wait_read(proc.stderr, 4))
|
|
|
|
st = os.fstat(proc.stderr.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISFIFO(st.st_mode))
|
|
|
|
self.assertEquals(st.st_dev, info['st_dev'])
|
|
|
|
self.assertEquals(st.st_mode, info['st_mode'])
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stderr.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertFalse(flags & os.O_WRONLY)
|
|
|
|
self.assertFalse(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
|
|
|
self.assertTrue(info['flags'] & os.O_WRONLY)
|
|
|
|
|
|
|
|
|
|
|
|
class TtyCreateChildTest(testlib.TestCase):
|
|
|
|
func = staticmethod(mitogen.parent.tty_create_child)
|
|
|
|
|
2019-08-04 01:41:05 +00:00
|
|
|
def test_dev_tty_open_succeeds(self):
|
|
|
|
# In the early days of UNIX, a process that lacked a controlling TTY
|
|
|
|
# would acquire one simply by opening an existing TTY. Linux and OS X
|
|
|
|
# continue to follow this behaviour, however at least FreeBSD moved to
|
|
|
|
# requiring an explicit ioctl(). Linux supports it, but we don't yet
|
|
|
|
# use it there and anyway the behaviour will never change, so no point
|
|
|
|
# in fixing things that aren't broken. Below we test that
|
|
|
|
# getpass-loving apps like sudo and ssh get our slave PTY when they
|
|
|
|
# attempt to open /dev/tty, which is what they both do on attempting to
|
|
|
|
# read a password.
|
|
|
|
tf = tempfile.NamedTemporaryFile()
|
|
|
|
try:
|
|
|
|
proc = self.func([
|
|
|
|
'bash', '-c', 'exec 2>%s; echo hi > /dev/tty' % (tf.name,)
|
|
|
|
])
|
|
|
|
mitogen.core.set_block(proc.stdin.fileno())
|
|
|
|
# read(3) below due to https://bugs.python.org/issue37696
|
|
|
|
self.assertEquals(mitogen.core.b('hi\n'), proc.stdin.read(3))
|
|
|
|
waited_pid, status = os.waitpid(proc.pid, 0)
|
|
|
|
self.assertEquals(proc.pid, waited_pid)
|
|
|
|
self.assertEquals(0, status)
|
|
|
|
self.assertEquals(mitogen.core.b(''), tf.read())
|
|
|
|
proc.stdout.close()
|
|
|
|
finally:
|
|
|
|
tf.close()
|
|
|
|
|
2019-07-17 16:24:14 +00:00
|
|
|
def test_stdin(self):
|
|
|
|
proc, info, _ = run_fd_check(self.func, 0, 'read',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: proc.stdin.write(b('TEST')))
|
|
|
|
st = os.fstat(proc.stdin.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISCHR(st.st_mode))
|
|
|
|
self.assertTrue(stat.S_ISCHR(info['st_mode']))
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(info['ttyname'],
|
|
|
|
mitogen.core.UnicodeType))
|
2019-08-03 04:12:33 +00:00
|
|
|
self.assertTrue(os.isatty(proc.stdin.fileno()))
|
2019-07-17 16:24:14 +00:00
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stdin.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
|
|
|
self.assertTrue(info['buf'], 'TEST')
|
|
|
|
|
|
|
|
def test_stdout(self):
|
|
|
|
proc, info, buf = run_fd_check(self.func, 1, 'write',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: wait_read(proc.stdout, 4))
|
2019-07-17 16:24:14 +00:00
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
st = os.fstat(proc.stdout.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISCHR(st.st_mode))
|
|
|
|
self.assertTrue(stat.S_ISCHR(info['st_mode']))
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(info['ttyname'],
|
|
|
|
mitogen.core.UnicodeType))
|
2019-08-03 04:12:33 +00:00
|
|
|
self.assertTrue(os.isatty(proc.stdout.fileno()))
|
2019-07-17 16:24:14 +00:00
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
|
|
|
|
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
|
|
|
|
|
|
|
def test_stderr(self):
|
2019-08-03 04:12:33 +00:00
|
|
|
# proc.stderr is None in the parent since there is no separate stderr
|
|
|
|
# stream. In the child, FD 2/stderr is connected to the TTY.
|
2019-07-17 16:24:14 +00:00
|
|
|
proc, info, buf = run_fd_check(self.func, 2, 'write',
|
2019-07-20 23:42:43 +00:00
|
|
|
lambda proc: wait_read(proc.stdout, 4))
|
2019-07-17 16:24:14 +00:00
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
st = os.fstat(proc.stdout.fileno())
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(stat.S_ISCHR(st.st_mode))
|
|
|
|
self.assertTrue(stat.S_ISCHR(info['st_mode']))
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(info['ttyname'],
|
|
|
|
mitogen.core.UnicodeType))
|
2019-08-03 04:12:33 +00:00
|
|
|
self.assertTrue(os.isatty(proc.stdout.fileno()))
|
2019-07-17 16:24:14 +00:00
|
|
|
|
2019-07-20 23:42:43 +00:00
|
|
|
flags = fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETFL)
|
2019-07-17 16:24:14 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
|
|
|
|
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
|
|
|
|
|
|
|
def test_dev_tty_open_succeeds(self):
|
|
|
|
# In the early days of UNIX, a process that lacked a controlling TTY
|
|
|
|
# would acquire one simply by opening an existing TTY. Linux and OS X
|
|
|
|
# continue to follow this behaviour, however at least FreeBSD moved to
|
|
|
|
# requiring an explicit ioctl(). Linux supports it, but we don't yet
|
|
|
|
# use it there and anyway the behaviour will never change, so no point
|
|
|
|
# in fixing things that aren't broken. Below we test that
|
|
|
|
# getpass-loving apps like sudo and ssh get our slave PTY when they
|
|
|
|
# attempt to open /dev/tty, which is what they both do on attempting to
|
|
|
|
# read a password.
|
|
|
|
tf = tempfile.NamedTemporaryFile()
|
|
|
|
try:
|
|
|
|
proc = self.func([
|
|
|
|
'bash', '-c', 'exec 2>%s; echo hi > /dev/tty' % (tf.name,)
|
|
|
|
])
|
2019-07-20 23:42:43 +00:00
|
|
|
self.assertEquals(mitogen.core.b('hi\n'), wait_read(proc.stdout, 3))
|
2019-07-17 16:24:14 +00:00
|
|
|
waited_pid, status = os.waitpid(proc.pid, 0)
|
|
|
|
self.assertEquals(proc.pid, waited_pid)
|
|
|
|
self.assertEquals(0, status)
|
|
|
|
self.assertEquals(mitogen.core.b(''), tf.read())
|
2019-07-20 23:42:43 +00:00
|
|
|
proc.stdout.close()
|
2019-07-17 16:24:14 +00:00
|
|
|
finally:
|
|
|
|
tf.close()
|
|
|
|
|
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
class StderrDiagTtyMixin(object):
|
|
|
|
def test_stderr(self):
|
2019-08-03 04:12:33 +00:00
|
|
|
# proc.stderr is the PTY master, FD 2 in the child is the PTY slave
|
2019-07-21 02:13:13 +00:00
|
|
|
proc, info, buf = run_fd_check(self.func, 2, 'write',
|
|
|
|
lambda proc: wait_read(proc.stderr, 4))
|
2019-07-20 23:42:43 +00:00
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
st = os.fstat(proc.stderr.fileno())
|
|
|
|
self.assertTrue(stat.S_ISCHR(st.st_mode))
|
|
|
|
self.assertTrue(stat.S_ISCHR(info['st_mode']))
|
2019-07-20 23:42:43 +00:00
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
self.assertTrue(isinstance(info['ttyname'],
|
|
|
|
mitogen.core.UnicodeType))
|
2019-08-03 04:12:33 +00:00
|
|
|
self.assertTrue(os.isatty(proc.stderr.fileno()))
|
2019-07-20 23:42:43 +00:00
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
flags = fcntl.fcntl(proc.stderr.fileno(), fcntl.F_GETFL)
|
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(info['flags'] & os.O_RDWR)
|
2019-07-20 23:42:43 +00:00
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
self.assertTrue(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
2019-07-20 23:42:43 +00:00
|
|
|
|
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
class HybridTtyCreateChildTest(StdinSockMixin, StdoutSockMixin,
|
|
|
|
StderrDiagTtyMixin, testlib.TestCase):
|
|
|
|
func = staticmethod(mitogen.parent.hybrid_tty_create_child)
|
2019-07-20 23:42:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-21 02:13:13 +00:00
|
|
|
if 0:
|
|
|
|
# issue #410
|
2019-07-20 23:42:43 +00:00
|
|
|
class SelinuxHybridTtyCreateChildTest(StderrDiagTtyMixin, testlib.TestCase):
|
|
|
|
func = staticmethod(mitogen.parent.selinux_hybrid_tty_create_child)
|
|
|
|
|
|
|
|
def test_stdin(self):
|
|
|
|
proc, info, buf = run_fd_check(self.func, 0, 'read',
|
|
|
|
lambda proc: proc.transmit_side.write('TEST'))
|
|
|
|
st = os.fstat(proc.transmit_side.fd)
|
|
|
|
self.assertTrue(stat.S_ISFIFO(st.st_mode))
|
|
|
|
self.assertEquals(st.st_dev, info['st_dev'])
|
|
|
|
self.assertEquals(st.st_mode, info['st_mode'])
|
|
|
|
flags = fcntl.fcntl(proc.transmit_side.fd, fcntl.F_GETFL)
|
|
|
|
self.assertTrue(flags & os.O_WRONLY)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
|
|
|
self.assertFalse(info['flags'] & os.O_WRONLY)
|
|
|
|
self.assertFalse(info['flags'] & os.O_RDWR)
|
|
|
|
|
|
|
|
def test_stdout(self):
|
|
|
|
proc, info, buf = run_fd_check(self.func, 1, 'write',
|
|
|
|
lambda proc: wait_read(proc.receive_side, 4))
|
|
|
|
st = os.fstat(proc.receive_side.fd)
|
|
|
|
self.assertTrue(stat.S_ISFIFO(st.st_mode))
|
|
|
|
self.assertEquals(st.st_dev, info['st_dev'])
|
|
|
|
self.assertEquals(st.st_mode, info['st_mode'])
|
|
|
|
flags = fcntl.fcntl(proc.receive_side.fd, fcntl.F_GETFL)
|
|
|
|
self.assertFalse(flags & os.O_WRONLY)
|
|
|
|
self.assertFalse(flags & os.O_RDWR)
|
|
|
|
self.assertTrue(info['flags'] & os.O_WRONLY)
|
|
|
|
self.assertTrue(buf, 'TEST')
|
2019-07-17 16:24:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest2.main()
|