blob: 9ab68c67241f464e0cf115389407ea48e616b378 [file] [log] [blame]
Roger E. Massefb01d4b1996-12-17 17:41:09 +00001"""Test program for the fcntl C module.
Roger E. Massefb01d4b1996-12-17 17:41:09 +00002"""
Gregory P. Smithe5aefa42013-03-31 10:10:50 -07003import platform
Christian Heimese25f35e2008-03-20 10:49:03 +00004import os
5import struct
6import sys
Christian Heimesdd15f6c2008-03-16 00:07:10 +00007import unittest
Dong-hee Nabefa0322019-11-08 05:31:41 +09008from multiprocessing import Process
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02009from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
10 cpython_only)
R. David Murraya21e4ca2009-03-31 23:16:50 +000011
Ezio Melotti78ede7c2013-08-23 23:06:31 +030012# Skip test if no fcntl module.
R. David Murraya21e4ca2009-03-31 23:16:50 +000013fcntl = import_module('fcntl')
14
Roger E. Massefb01d4b1996-12-17 17:41:09 +000015
Martin v. Löwisa660a342001-10-18 22:07:48 +000016
Christian Heimesdd15f6c2008-03-16 00:07:10 +000017def get_lockdata():
Antoine Pitrou6103ab12009-10-24 20:11:21 +000018 try:
19 os.O_LARGEFILE
20 except AttributeError:
21 start_len = "ll"
Hye-Shik Changac89f6e2005-04-04 15:21:04 +000022 else:
Antoine Pitrou6103ab12009-10-24 20:11:21 +000023 start_len = "qq"
Fred Drakebc7809b2001-05-09 21:11:59 +000024
Benjamin Peterson288d1da2017-09-28 22:44:27 -070025 if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd'))
Victor Stinnere6747472011-08-21 00:39:18 +020026 or sys.platform == 'darwin'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000027 if struct.calcsize('l') == 8:
28 off_t = 'l'
29 pid_t = 'i'
30 else:
31 off_t = 'lxxxx'
32 pid_t = 'l'
33 lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
34 fcntl.F_WRLCK, 0)
doko@ubuntu.com1dfb9182013-08-03 16:12:33 +020035 elif sys.platform.startswith('gnukfreebsd'):
36 lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0)
Michael Feltb7eec942019-04-08 02:51:33 +020037 elif sys.platform in ['hp-uxB', 'unixware7']:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000038 lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000039 else:
40 lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
41 if lockdata:
42 if verbose:
43 print('struct.pack: ', repr(lockdata))
44 return lockdata
Fred Drakebc7809b2001-05-09 21:11:59 +000045
Christian Heimesdd15f6c2008-03-16 00:07:10 +000046lockdata = get_lockdata()
Roger E. Massefb01d4b1996-12-17 17:41:09 +000047
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020048class BadFile:
49 def __init__(self, fn):
50 self.fn = fn
51 def fileno(self):
52 return self.fn
53
Dong-hee Na99602302019-11-19 17:12:42 +090054def try_lockf_on_other_process_fail(fname, cmd):
55 f = open(fname, 'wb+')
56 try:
57 fcntl.lockf(f, cmd)
58 except BlockingIOError:
59 pass
60 finally:
61 f.close()
62
63def try_lockf_on_other_process(fname, cmd):
64 f = open(fname, 'wb+')
65 fcntl.lockf(f, cmd)
66 fcntl.lockf(f, fcntl.LOCK_UN)
67 f.close()
68
Christian Heimesdd15f6c2008-03-16 00:07:10 +000069class TestFcntl(unittest.TestCase):
70
71 def setUp(self):
72 self.f = None
73
74 def tearDown(self):
Antoine Pitrou61f77b52009-05-24 15:46:13 +000075 if self.f and not self.f.closed:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000076 self.f.close()
77 unlink(TESTFN)
78
79 def test_fcntl_fileno(self):
80 # the example from the library docs
Victor Stinnera6d2c762011-06-30 18:20:11 +020081 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000082 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
83 if verbose:
84 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020085 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
86 if verbose:
87 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000088 self.f.close()
89
90 def test_fcntl_file_descriptor(self):
91 # again, but pass the file rather than numeric descriptor
Victor Stinnera6d2c762011-06-30 18:20:11 +020092 self.f = open(TESTFN, 'wb')
Christian Heimesdd15f6c2008-03-16 00:07:10 +000093 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
Ezio Melotti892584e2013-08-23 23:09:32 +030094 if verbose:
95 print('Status from fcntl with O_NONBLOCK: ', rv)
Jesus Ceaf1af7052012-10-05 02:48:46 +020096 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
Ezio Melotti892584e2013-08-23 23:09:32 +030097 if verbose:
98 print('String from fcntl with F_SETLKW: ', repr(rv))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000099 self.f.close()
Fred Drakebc7809b2001-05-09 21:11:59 +0000100
Serhiy Storchaka78980432013-01-15 01:12:17 +0200101 def test_fcntl_bad_file(self):
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200102 with self.assertRaises(ValueError):
103 fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK)
104 with self.assertRaises(ValueError):
105 fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK)
106 with self.assertRaises(TypeError):
107 fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK)
108 with self.assertRaises(TypeError):
109 fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
110
111 @cpython_only
112 def test_fcntl_bad_file_overflow(self):
113 from _testcapi import INT_MAX, INT_MIN
Serhiy Storchaka78980432013-01-15 01:12:17 +0200114 # Issue 15989
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200115 with self.assertRaises(OverflowError):
116 fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK)
117 with self.assertRaises(OverflowError):
118 fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK)
119 with self.assertRaises(OverflowError):
120 fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK)
121 with self.assertRaises(OverflowError):
122 fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK)
Serhiy Storchaka78980432013-01-15 01:12:17 +0200123
Gregory P. Smithe5aefa42013-03-31 10:10:50 -0700124 @unittest.skipIf(
125 platform.machine().startswith('arm') and platform.system() == 'Linux',
126 "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
Antoine Pitrou61f77b52009-05-24 15:46:13 +0000127 def test_fcntl_64_bit(self):
128 # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
129 # C 'long' but not in a C 'int'.
130 try:
131 cmd = fcntl.F_NOTIFY
132 # This flag is larger than 2**31 in 64-bit builds
133 flags = fcntl.DN_MULTISHOT
134 except AttributeError:
135 self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
136 fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
137 try:
138 fcntl.fcntl(fd, cmd, flags)
139 finally:
140 os.close(fd)
141
Christian Heimes0a956f12013-12-05 16:13:03 +0100142 def test_flock(self):
Christian Heimes2e7d4f02013-12-07 18:19:21 +0100143 # Solaris needs readable file for shared lock
144 self.f = open(TESTFN, 'wb+')
Christian Heimes0a956f12013-12-05 16:13:03 +0100145 fileno = self.f.fileno()
146 fcntl.flock(fileno, fcntl.LOCK_SH)
147 fcntl.flock(fileno, fcntl.LOCK_UN)
148 fcntl.flock(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
149 fcntl.flock(self.f, fcntl.LOCK_UN)
150 fcntl.flock(fileno, fcntl.LOCK_EX)
151 fcntl.flock(fileno, fcntl.LOCK_UN)
152
153 self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
154 self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
Serhiy Storchakaf28ba362014-02-07 10:10:55 +0200155
Dong-hee Na99602302019-11-19 17:12:42 +0900156 @unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
Dong-hee Nabefa0322019-11-08 05:31:41 +0900157 def test_lockf_exclusive(self):
158 self.f = open(TESTFN, 'wb+')
159 cmd = fcntl.LOCK_EX | fcntl.LOCK_NB
Dong-hee Nabefa0322019-11-08 05:31:41 +0900160 fcntl.lockf(self.f, cmd)
Dong-hee Na99602302019-11-19 17:12:42 +0900161 p = Process(target=try_lockf_on_other_process_fail, args=(TESTFN, cmd))
Dong-hee Nabefa0322019-11-08 05:31:41 +0900162 p.start()
163 p.join()
164 fcntl.lockf(self.f, fcntl.LOCK_UN)
165 self.assertEqual(p.exitcode, 0)
166
Dong-hee Na99602302019-11-19 17:12:42 +0900167 @unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
Dong-hee Nabefa0322019-11-08 05:31:41 +0900168 def test_lockf_share(self):
169 self.f = open(TESTFN, 'wb+')
170 cmd = fcntl.LOCK_SH | fcntl.LOCK_NB
Dong-hee Nabefa0322019-11-08 05:31:41 +0900171 fcntl.lockf(self.f, cmd)
Dong-hee Na99602302019-11-19 17:12:42 +0900172 p = Process(target=try_lockf_on_other_process, args=(TESTFN, cmd))
Dong-hee Nabefa0322019-11-08 05:31:41 +0900173 p.start()
174 p.join()
175 fcntl.lockf(self.f, fcntl.LOCK_UN)
176 self.assertEqual(p.exitcode, 0)
177
Serhiy Storchakaf28ba362014-02-07 10:10:55 +0200178 @cpython_only
179 def test_flock_overflow(self):
180 import _testcapi
Christian Heimes0a956f12013-12-05 16:13:03 +0100181 self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1,
182 fcntl.LOCK_SH)
183
Vinay Sharma13f37f22019-08-29 07:26:17 +0530184 @unittest.skipIf(sys.platform != 'darwin', "F_GETPATH is only available on macos")
185 def test_fcntl_f_getpath(self):
186 self.f = open(TESTFN, 'wb')
Benjamin Peterson465e5d52019-08-28 22:06:49 -0700187 expected = os.path.abspath(TESTFN).encode('utf-8')
188 res = fcntl.fcntl(self.f.fileno(), fcntl.F_GETPATH, bytes(len(expected)))
189 self.assertEqual(expected, res)
Fred Drakebc7809b2001-05-09 21:11:59 +0000190
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000191def test_main():
192 run_unittest(TestFcntl)
Fred Drakebc7809b2001-05-09 21:11:59 +0000193
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000194if __name__ == '__main__':
195 test_main()