Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 1 | """Test program for the fcntl C module. |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 2 | """ |
Gregory P. Smith | e5aefa4 | 2013-03-31 10:10:50 -0700 | [diff] [blame] | 3 | import platform |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 4 | import os |
| 5 | import struct |
| 6 | import sys |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7 | import unittest |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 8 | from multiprocessing import Process |
Serhiy Storchaka | bedce35 | 2021-09-19 22:36:03 +0300 | [diff] [blame] | 9 | from test.support import verbose, cpython_only |
Hai Shi | a089d21 | 2020-07-06 17:15:08 +0800 | [diff] [blame] | 10 | from test.support.import_helper import import_module |
| 11 | from test.support.os_helper import TESTFN, unlink |
| 12 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 13 | |
Ezio Melotti | 78ede7c | 2013-08-23 23:06:31 +0300 | [diff] [blame] | 14 | # Skip test if no fcntl module. |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 15 | fcntl = import_module('fcntl') |
| 16 | |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 17 | |
Martin v. Löwis | a660a34 | 2001-10-18 22:07:48 +0000 | [diff] [blame] | 18 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 19 | def get_lockdata(): |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 20 | try: |
| 21 | os.O_LARGEFILE |
| 22 | except AttributeError: |
| 23 | start_len = "ll" |
Hye-Shik Chang | ac89f6e | 2005-04-04 15:21:04 +0000 | [diff] [blame] | 24 | else: |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 25 | start_len = "qq" |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 26 | |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 27 | if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd')) |
Victor Stinner | e674747 | 2011-08-21 00:39:18 +0200 | [diff] [blame] | 28 | or sys.platform == 'darwin'): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 29 | if struct.calcsize('l') == 8: |
| 30 | off_t = 'l' |
| 31 | pid_t = 'i' |
| 32 | else: |
| 33 | off_t = 'lxxxx' |
| 34 | pid_t = 'l' |
| 35 | lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0, |
| 36 | fcntl.F_WRLCK, 0) |
doko@ubuntu.com | 1dfb918 | 2013-08-03 16:12:33 +0200 | [diff] [blame] | 37 | elif sys.platform.startswith('gnukfreebsd'): |
| 38 | lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0) |
Michael Felt | b7eec94 | 2019-04-08 02:51:33 +0200 | [diff] [blame] | 39 | elif sys.platform in ['hp-uxB', 'unixware7']: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 40 | lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 41 | else: |
| 42 | lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) |
| 43 | if lockdata: |
| 44 | if verbose: |
| 45 | print('struct.pack: ', repr(lockdata)) |
| 46 | return lockdata |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 47 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 48 | lockdata = get_lockdata() |
Roger E. Masse | fb01d4b | 1996-12-17 17:41:09 +0000 | [diff] [blame] | 49 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 50 | class BadFile: |
| 51 | def __init__(self, fn): |
| 52 | self.fn = fn |
| 53 | def fileno(self): |
| 54 | return self.fn |
| 55 | |
Dong-hee Na | 9960230 | 2019-11-19 17:12:42 +0900 | [diff] [blame] | 56 | def try_lockf_on_other_process_fail(fname, cmd): |
| 57 | f = open(fname, 'wb+') |
| 58 | try: |
| 59 | fcntl.lockf(f, cmd) |
| 60 | except BlockingIOError: |
| 61 | pass |
| 62 | finally: |
| 63 | f.close() |
| 64 | |
| 65 | def try_lockf_on_other_process(fname, cmd): |
| 66 | f = open(fname, 'wb+') |
| 67 | fcntl.lockf(f, cmd) |
| 68 | fcntl.lockf(f, fcntl.LOCK_UN) |
| 69 | f.close() |
| 70 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 71 | class TestFcntl(unittest.TestCase): |
| 72 | |
| 73 | def setUp(self): |
| 74 | self.f = None |
| 75 | |
| 76 | def tearDown(self): |
Antoine Pitrou | 61f77b5 | 2009-05-24 15:46:13 +0000 | [diff] [blame] | 77 | if self.f and not self.f.closed: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 78 | self.f.close() |
| 79 | unlink(TESTFN) |
| 80 | |
| 81 | def test_fcntl_fileno(self): |
| 82 | # the example from the library docs |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 83 | self.f = open(TESTFN, 'wb') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 84 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 85 | if verbose: |
| 86 | print('Status from fcntl with O_NONBLOCK: ', rv) |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 87 | rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata) |
| 88 | if verbose: |
| 89 | print('String from fcntl with F_SETLKW: ', repr(rv)) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 90 | self.f.close() |
| 91 | |
| 92 | def test_fcntl_file_descriptor(self): |
| 93 | # again, but pass the file rather than numeric descriptor |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 94 | self.f = open(TESTFN, 'wb') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 95 | rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK) |
Ezio Melotti | 892584e | 2013-08-23 23:09:32 +0300 | [diff] [blame] | 96 | if verbose: |
| 97 | print('Status from fcntl with O_NONBLOCK: ', rv) |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 98 | rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata) |
Ezio Melotti | 892584e | 2013-08-23 23:09:32 +0300 | [diff] [blame] | 99 | if verbose: |
| 100 | print('String from fcntl with F_SETLKW: ', repr(rv)) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 101 | self.f.close() |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 102 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 103 | def test_fcntl_bad_file(self): |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 104 | with self.assertRaises(ValueError): |
| 105 | fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 106 | with self.assertRaises(ValueError): |
| 107 | fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK) |
| 108 | with self.assertRaises(TypeError): |
| 109 | fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK) |
| 110 | with self.assertRaises(TypeError): |
| 111 | fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK) |
| 112 | |
| 113 | @cpython_only |
| 114 | def test_fcntl_bad_file_overflow(self): |
| 115 | from _testcapi import INT_MAX, INT_MIN |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 116 | # Issue 15989 |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 117 | with self.assertRaises(OverflowError): |
| 118 | fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 119 | with self.assertRaises(OverflowError): |
| 120 | fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK) |
| 121 | with self.assertRaises(OverflowError): |
| 122 | fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK) |
| 123 | with self.assertRaises(OverflowError): |
| 124 | fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK) |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 125 | |
Gregory P. Smith | e5aefa4 | 2013-03-31 10:10:50 -0700 | [diff] [blame] | 126 | @unittest.skipIf( |
| 127 | platform.machine().startswith('arm') and platform.system() == 'Linux', |
| 128 | "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT") |
Antoine Pitrou | 61f77b5 | 2009-05-24 15:46:13 +0000 | [diff] [blame] | 129 | def test_fcntl_64_bit(self): |
| 130 | # Issue #1309352: fcntl shouldn't fail when the third arg fits in a |
| 131 | # C 'long' but not in a C 'int'. |
| 132 | try: |
| 133 | cmd = fcntl.F_NOTIFY |
| 134 | # This flag is larger than 2**31 in 64-bit builds |
| 135 | flags = fcntl.DN_MULTISHOT |
| 136 | except AttributeError: |
| 137 | self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable") |
| 138 | fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY) |
| 139 | try: |
| 140 | fcntl.fcntl(fd, cmd, flags) |
| 141 | finally: |
| 142 | os.close(fd) |
| 143 | |
Christian Heimes | 0a956f1 | 2013-12-05 16:13:03 +0100 | [diff] [blame] | 144 | def test_flock(self): |
Christian Heimes | 2e7d4f0 | 2013-12-07 18:19:21 +0100 | [diff] [blame] | 145 | # Solaris needs readable file for shared lock |
| 146 | self.f = open(TESTFN, 'wb+') |
Christian Heimes | 0a956f1 | 2013-12-05 16:13:03 +0100 | [diff] [blame] | 147 | fileno = self.f.fileno() |
| 148 | fcntl.flock(fileno, fcntl.LOCK_SH) |
| 149 | fcntl.flock(fileno, fcntl.LOCK_UN) |
| 150 | fcntl.flock(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB) |
| 151 | fcntl.flock(self.f, fcntl.LOCK_UN) |
| 152 | fcntl.flock(fileno, fcntl.LOCK_EX) |
| 153 | fcntl.flock(fileno, fcntl.LOCK_UN) |
| 154 | |
| 155 | self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH) |
| 156 | self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH) |
Serhiy Storchaka | f28ba36 | 2014-02-07 10:10:55 +0200 | [diff] [blame] | 157 | |
Dong-hee Na | 9960230 | 2019-11-19 17:12:42 +0900 | [diff] [blame] | 158 | @unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError") |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 159 | def test_lockf_exclusive(self): |
| 160 | self.f = open(TESTFN, 'wb+') |
| 161 | cmd = fcntl.LOCK_EX | fcntl.LOCK_NB |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 162 | fcntl.lockf(self.f, cmd) |
Dong-hee Na | 9960230 | 2019-11-19 17:12:42 +0900 | [diff] [blame] | 163 | p = Process(target=try_lockf_on_other_process_fail, args=(TESTFN, cmd)) |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 164 | p.start() |
| 165 | p.join() |
| 166 | fcntl.lockf(self.f, fcntl.LOCK_UN) |
| 167 | self.assertEqual(p.exitcode, 0) |
| 168 | |
Dong-hee Na | 9960230 | 2019-11-19 17:12:42 +0900 | [diff] [blame] | 169 | @unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError") |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 170 | def test_lockf_share(self): |
| 171 | self.f = open(TESTFN, 'wb+') |
| 172 | cmd = fcntl.LOCK_SH | fcntl.LOCK_NB |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 173 | fcntl.lockf(self.f, cmd) |
Dong-hee Na | 9960230 | 2019-11-19 17:12:42 +0900 | [diff] [blame] | 174 | p = Process(target=try_lockf_on_other_process, args=(TESTFN, cmd)) |
Dong-hee Na | befa032 | 2019-11-08 05:31:41 +0900 | [diff] [blame] | 175 | p.start() |
| 176 | p.join() |
| 177 | fcntl.lockf(self.f, fcntl.LOCK_UN) |
| 178 | self.assertEqual(p.exitcode, 0) |
| 179 | |
Serhiy Storchaka | f28ba36 | 2014-02-07 10:10:55 +0200 | [diff] [blame] | 180 | @cpython_only |
| 181 | def test_flock_overflow(self): |
| 182 | import _testcapi |
Christian Heimes | 0a956f1 | 2013-12-05 16:13:03 +0100 | [diff] [blame] | 183 | self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1, |
| 184 | fcntl.LOCK_SH) |
| 185 | |
Vinay Sharma | 13f37f2 | 2019-08-29 07:26:17 +0530 | [diff] [blame] | 186 | @unittest.skipIf(sys.platform != 'darwin', "F_GETPATH is only available on macos") |
| 187 | def test_fcntl_f_getpath(self): |
| 188 | self.f = open(TESTFN, 'wb') |
Benjamin Peterson | 465e5d5 | 2019-08-28 22:06:49 -0700 | [diff] [blame] | 189 | expected = os.path.abspath(TESTFN).encode('utf-8') |
| 190 | res = fcntl.fcntl(self.f.fileno(), fcntl.F_GETPATH, bytes(len(expected))) |
| 191 | self.assertEqual(expected, res) |
Fred Drake | bc7809b | 2001-05-09 21:11:59 +0000 | [diff] [blame] | 192 | |
Gregory P. Smith | 786addd | 2020-10-20 17:37:20 -0700 | [diff] [blame] | 193 | @unittest.skipUnless( |
| 194 | hasattr(fcntl, "F_SETPIPE_SZ") and hasattr(fcntl, "F_GETPIPE_SZ"), |
| 195 | "F_SETPIPE_SZ and F_GETPIPE_SZ are not available on all platforms.") |
Ruben Vorderman | 23c0fb8 | 2020-10-20 01:30:02 +0200 | [diff] [blame] | 196 | def test_fcntl_f_pipesize(self): |
| 197 | test_pipe_r, test_pipe_w = os.pipe() |
Gregory P. Smith | 786addd | 2020-10-20 17:37:20 -0700 | [diff] [blame] | 198 | try: |
| 199 | # Get the default pipesize with F_GETPIPE_SZ |
| 200 | pipesize_default = fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ) |
| 201 | pipesize = pipesize_default // 2 # A new value to detect change. |
| 202 | if pipesize < 512: # the POSIX minimum |
| 203 | raise unittest.SkitTest( |
| 204 | 'default pipesize too small to perform test.') |
| 205 | fcntl.fcntl(test_pipe_w, fcntl.F_SETPIPE_SZ, pipesize) |
| 206 | self.assertEqual(fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ), |
| 207 | pipesize) |
| 208 | finally: |
| 209 | os.close(test_pipe_r) |
| 210 | os.close(test_pipe_w) |
Ruben Vorderman | 23c0fb8 | 2020-10-20 01:30:02 +0200 | [diff] [blame] | 211 | |
| 212 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 213 | if __name__ == '__main__': |
Serhiy Storchaka | bedce35 | 2021-09-19 22:36:03 +0300 | [diff] [blame] | 214 | unittest.main() |