blob: 7b7067eb7b61d4cec7bb97b7c9b31ba82ee7247e [file] [log] [blame]
Antoine Pitrou5e38aae2010-09-07 16:30:09 +00001import array
Michael W. Hudsonf0089982003-03-03 12:29:42 +00002import unittest
Hai Shif7ba40b2020-06-25 18:38:51 +08003from test.support import get_attribute
4from test.support.import_helper import import_module
Michael W. Hudsonf0089982003-03-03 12:29:42 +00005import os, struct
R. David Murraya21e4ca2009-03-31 23:16:50 +00006fcntl = import_module('fcntl')
7termios = import_module('termios')
8get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
Michael W. Hudsonf0089982003-03-03 12:29:42 +00009
Neal Norwitz26f42f62003-03-20 04:33:16 +000010try:
Victor Stinnera6d2c762011-06-30 18:20:11 +020011 tty = open("/dev/tty", "rb")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020012except OSError:
Benjamin Petersone549ead2009-03-28 21:42:05 +000013 raise unittest.SkipTest("Unable to open /dev/tty")
Florent Xicluna39d795d2010-08-08 18:06:13 +000014else:
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020015 with tty:
16 # Skip if another process is in foreground
17 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
Florent Xicluna39d795d2010-08-08 18:06:13 +000018 rpgrp = struct.unpack("i", r)[0]
19 if rpgrp not in (os.getpgrp(), os.getsid(0)):
20 raise unittest.SkipTest("Neither the process group nor the session "
21 "are attached to /dev/tty")
22 del tty, r, rpgrp
Neal Norwitz26f42f62003-03-20 04:33:16 +000023
Christian Heimese25f35e2008-03-20 10:49:03 +000024try:
25 import pty
26except ImportError:
27 pty = None
28
Michael W. Hudsonf0089982003-03-03 12:29:42 +000029class IoctlTests(unittest.TestCase):
30 def test_ioctl(self):
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000031 # If this process has been put into the background, TIOCGPGRP returns
32 # the session ID instead of the process group id.
33 ids = (os.getpgrp(), os.getsid(0))
Victor Stinnera6d2c762011-06-30 18:20:11 +020034 with open("/dev/tty", "rb") as tty:
Brett Cannondff69852010-10-29 23:54:28 +000035 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
36 rpgrp = struct.unpack("i", r)[0]
37 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000038
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000039 def _check_ioctl_mutate_len(self, nbytes=None):
40 buf = array.array('i')
41 intsize = buf.itemsize
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000042 ids = (os.getpgrp(), os.getsid(0))
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000043 # A fill value unlikely to be in `ids`
44 fill = -12345
45 if nbytes is not None:
46 # Extend the buffer so that it is exactly `nbytes` bytes long
47 buf.extend([fill] * (nbytes // intsize))
48 self.assertEqual(len(buf) * intsize, nbytes) # sanity check
49 else:
50 buf.append(fill)
Victor Stinnera6d2c762011-06-30 18:20:11 +020051 with open("/dev/tty", "rb") as tty:
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +030052 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, True)
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000053 rpgrp = buf[0]
Ezio Melottib3aedd42010-11-20 19:04:17 +000054 self.assertEqual(r, 0)
Benjamin Peterson577473f2010-01-19 00:09:57 +000055 self.assertIn(rpgrp, ids)
Michael W. Hudsonf0089982003-03-03 12:29:42 +000056
Antoine Pitrou5e38aae2010-09-07 16:30:09 +000057 def test_ioctl_mutate(self):
58 self._check_ioctl_mutate_len()
59
60 def test_ioctl_mutate_1024(self):
61 # Issue #9758: a mutable buffer of exactly 1024 bytes wouldn't be
62 # copied back after the system call.
63 self._check_ioctl_mutate_len(1024)
64
65 def test_ioctl_mutate_2048(self):
66 # Test with a larger buffer, just for the record.
67 self._check_ioctl_mutate_len(2048)
68
Christian Heimese25f35e2008-03-20 10:49:03 +000069 def test_ioctl_signed_unsigned_code_param(self):
70 if not pty:
Benjamin Petersone549ead2009-03-28 21:42:05 +000071 raise unittest.SkipTest('pty module required')
Christian Heimese25f35e2008-03-20 10:49:03 +000072 mfd, sfd = pty.openpty()
73 try:
74 if termios.TIOCSWINSZ < 0:
75 set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ
76 set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff
77 else:
78 set_winsz_opcode_pos = termios.TIOCSWINSZ
79 set_winsz_opcode_maybe_neg, = struct.unpack("i",
80 struct.pack("I", termios.TIOCSWINSZ))
81
Christian Heimese25f35e2008-03-20 10:49:03 +000082 our_winsz = struct.pack("HHHH",80,25,0,0)
83 # test both with a positive and potentially negative ioctl code
84 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz)
85 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz)
Christian Heimese25f35e2008-03-20 10:49:03 +000086 finally:
87 os.close(mfd)
88 os.close(sfd)
89
Michael W. Hudsonf0089982003-03-03 12:29:42 +000090
91if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -040092 unittest.main()