Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / fs / misc / filelock_interrupt.py
1 #!/usr/bin/python
2
3 import errno
4 import fcntl
5 import signal
6 import struct
7
8 """
9 introduced by Linux 3.15
10 """
11 fcntl.F_OFD_GETLK = 36
12 fcntl.F_OFD_SETLK = 37
13 fcntl.F_OFD_SETLKW = 38
14
15
16 def handler(signum, frame):
17     pass
18
19
20 def main():
21     f1 = open("testfile", 'w')
22     f2 = open("testfile", 'w')
23
24     fcntl.flock(f1, fcntl.LOCK_SH | fcntl.LOCK_NB)
25
26     """
27     is flock interruptable?
28     """
29     signal.signal(signal.SIGALRM, handler)
30     signal.alarm(5)
31     try:
32         fcntl.flock(f2, fcntl.LOCK_EX)
33     except IOError as e:
34         if e.errno != errno.EINTR:
35             raise
36     else:
37         raise RuntimeError("expect flock to block")
38
39     fcntl.flock(f1, fcntl.LOCK_UN)
40
41     lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 10, 0, 0)
42     try:
43         fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
44     except IOError as e:
45         if e.errno != errno.EINVAL:
46             raise
47         else:
48             print('kernel does not support fcntl.F_OFD_SETLK')
49             return
50
51     lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 10, 10, 0, 0)
52     fcntl.fcntl(f2, fcntl.F_OFD_SETLK, lockdata)
53
54     """
55     is poxis lock interruptable?
56     """
57     signal.signal(signal.SIGALRM, handler)
58     signal.alarm(5)
59     try:
60         lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
61         fcntl.fcntl(f2, fcntl.F_OFD_SETLKW, lockdata)
62     except IOError as e:
63         if e.errno != errno.EINTR:
64             raise
65     else:
66         raise RuntimeError("expect posix lock to block")
67
68     """
69     file handler 2 should still hold lock on 10~10
70     """
71     try:
72         lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 10, 10, 0, 0)
73         fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
74     except IOError as e:
75         if e.errno == errno.EAGAIN:
76             pass
77     else:
78         raise RuntimeError("expect file handler 2 to hold lock on 10~10")
79
80     lockdata = struct.pack('hhllhh', fcntl.F_UNLCK, 0, 0, 0, 0, 0)
81     fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
82     fcntl.fcntl(f2, fcntl.F_OFD_SETLK, lockdata)
83
84     print('ok')
85
86
87 main()