blob: b82b65135aa3abcf91967d57404c4255dd7e8534 [file] [log] [blame]
Antoine Pitrou5e38aae2010-09-07 16:30:09 +00001import array
Michael W. Hudsonf0089982003-03-03 12:29:42 +00002import unittest
Zachary Ware38c707e2015-04-13 15:00:43 -05003from test.support import import_module, get_attribute
Michael W. Hudsonf0089982003-03-03 12:29:42 +00004import os, struct
R. David Murraya21e4ca2009-03-31 23:16:50 +00005fcntl = import_module('fcntl')
6termios = import_module('termios')
7get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
Michael W. Hudsonf0089982003-03-03 12:29:42 +00008
Neal Norwitz26f42f62003-03-20 04:33:16 +00009try:
Victor Stinnera6d2c762011-06-30 18:20:11 +020010 tty = open("/dev/tty", "rb")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020011except OSError:
Benjamin Petersone549ead2009-03-28 21:42:05 +000012 raise unittest.SkipTest("Unable to open /dev/tty")
Florent Xicluna39d795d2010-08-08 18:06:13 +000013else:
14 # Skip if another process is in foreground
15 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
16 tty.close()
17 rpgrp = struct.unpack("i", r)[0]
18 if rpgrp not in (os.getpgrp(), os.getsid(0)):
19 raise unittest.SkipTest("Neither the process group nor the session "
20 "are attached to /dev/tty")
21 del tty, r, rpgrp
Neal Norwitz26f42f62003-03-20 04:33:16 +000022
Christian Heimese25f35e2008-03-20 10:49:03 +000023try:
24 import pty
25except ImportError:
26 pty = None
27
Michael W. Hudsonf0089982003-03-03 12:29:42 +000028class IoctlTests(unittest.TestCase):
29 def test_ioctl(self):
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000030 # If this process has been put into the background, TIOCGPGRP returns
31 # the session ID instead of the process group id.
32 ids = (os.getpgrp(), os.getsid(0))
Victor Stinnera6d2c762011-06-30 18:20:11 +020033 with open("/dev/tty", "rb") as tty:
Brett Cannondff69852010-10-29 23:54:28 +000034 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
35 rpgrp = struct.unpack("i", r)[0]
36 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000037
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000038 def _check_ioctl_mutate_len(self, nbytes=None):
39 buf = array.array('i')
40 intsize = buf.itemsize
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000041 ids = (os.getpgrp(), os.getsid(0))
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000042 # A fill value unlikely to be in `ids`
43 fill = -12345
44 if nbytes is not None:
45 # Extend the buffer so that it is exactly `nbytes` bytes long
46 buf.extend([fill] * (nbytes // intsize))
47 self.assertEqual(len(buf) * intsize, nbytes) # sanity check
48 else:
49 buf.append(fill)
Victor Stinnera6d2c762011-06-30 18:20:11 +020050 with open("/dev/tty", "rb") as tty:
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000051 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000052 rpgrp = buf[0]
Ezio Melottib3aedd42010-11-20 19:04:17 +000053 self.assertEqual(r, 0)
Benjamin Peterson577473f2010-01-19 00:09:57 +000054 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000055
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000056 def test_ioctl_mutate(self):
57 self._check_ioctl_mutate_len()
58
59 def test_ioctl_mutate_1024(self):
60 # Issue #9758: a mutable buffer of exactly 1024 bytes wouldn't be
61 # copied back after the system call.
62 self._check_ioctl_mutate_len(1024)
63
64 def test_ioctl_mutate_2048(self):
65 # Test with a larger buffer, just for the record.
66 self._check_ioctl_mutate_len(2048)
67
Christian Heimese25f35e2008-03-20 10:49:03 +000068 def test_ioctl_signed_unsigned_code_param(self):
69 if not pty:
Benjamin Petersone549ead2009-03-28 21:42:05 +000070 raise unittest.SkipTest('pty module required')
Christian Heimese25f35e2008-03-20 10:49:03 +000071 mfd, sfd = pty.openpty()
72 try:
73 if termios.TIOCSWINSZ < 0:
74 set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ
75 set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff
76 else:
77 set_winsz_opcode_pos = termios.TIOCSWINSZ
78 set_winsz_opcode_maybe_neg, = struct.unpack("i",
79 struct.pack("I", termios.TIOCSWINSZ))
80
Christian Heimese25f35e2008-03-20 10:49:03 +000081 our_winsz = struct.pack("HHHH",80,25,0,0)
82 # test both with a positive and potentially negative ioctl code
83 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz)
84 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz)
Christian Heimese25f35e2008-03-20 10:49:03 +000085 finally:
86 os.close(mfd)
87 os.close(sfd)
88
Michael W. Hudsonf0089982003-03-03 12:29:42 +000089
90if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -040091 unittest.main()