Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 1 | import unittest |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2 | from test.test_support import TestSkipped, run_unittest |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 3 | import os, struct |
| 4 | try: |
| 5 | import fcntl, termios |
| 6 | except ImportError: |
| 7 | raise TestSkipped("No fcntl or termios module") |
| 8 | if not hasattr(termios,'TIOCGPGRP'): |
| 9 | raise TestSkipped("termios module doesn't have TIOCGPGRP") |
| 10 | |
Neal Norwitz | 26f42f6 | 2003-03-20 04:33:16 +0000 | [diff] [blame] | 11 | try: |
| 12 | tty = open("/dev/tty", "r") |
| 13 | tty.close() |
| 14 | except IOError: |
| 15 | raise TestSkipped("Unable to open /dev/tty") |
| 16 | |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 17 | class IoctlTests(unittest.TestCase): |
| 18 | def test_ioctl(self): |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 19 | # If this process has been put into the background, TIOCGPGRP returns |
| 20 | # the session ID instead of the process group id. |
| 21 | ids = (os.getpgrp(), os.getsid(0)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 22 | tty = open("/dev/tty", "r") |
| 23 | r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 24 | rpgrp = struct.unpack("i", r)[0] |
| 25 | self.assert_(rpgrp in ids, "%s not in %s" % (rpgrp, ids)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 26 | |
| 27 | def test_ioctl_mutate(self): |
| 28 | import array |
| 29 | buf = array.array('i', [0]) |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 30 | ids = (os.getpgrp(), os.getsid(0)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 31 | tty = open("/dev/tty", "r") |
| 32 | r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1) |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 33 | rpgrp = buf[0] |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 34 | self.assertEquals(r, 0) |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 35 | self.assert_(rpgrp in ids, "%s not in %s" % (rpgrp, ids)) |
Michael W. Hudson | f008998 | 2003-03-03 12:29:42 +0000 | [diff] [blame] | 36 | |
| 37 | def test_main(): |
| 38 | run_unittest(IoctlTests) |
| 39 | |
| 40 | if __name__ == "__main__": |
| 41 | test_main() |