]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
qa/workunits/fs/misc: Add a workunit for file lock interruption 2628/head
authorYan, Zheng <zyan@redhat.com>
Wed, 15 Oct 2014 04:00:58 +0000 (12:00 +0800)
committerYan, Zheng <zyan@redhat.com>
Wed, 15 Oct 2014 04:46:31 +0000 (12:46 +0800)
Signed-off-by: Yan, Zheng <zyan@redhat.com>
qa/workunits/fs/misc/filelock_interrupt.py [new file with mode: 0755]

diff --git a/qa/workunits/fs/misc/filelock_interrupt.py b/qa/workunits/fs/misc/filelock_interrupt.py
new file mode 100755 (executable)
index 0000000..386fbd7
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+
+import time
+import fcntl
+import errno
+import signal
+import struct
+
+"""
+introduced by Linux 3.15
+"""
+fcntl.F_OFD_GETLK=36
+fcntl.F_OFD_SETLK=37
+fcntl.F_OFD_SETLKW=38
+
+def handler(signum, frame):
+    pass
+
+def main():
+    f1 = open("testfile", 'w')
+    f2 = open("testfile", 'w')
+
+    fcntl.flock(f1, fcntl.LOCK_SH | fcntl.LOCK_NB)
+
+    """
+    is flock interruptable?
+    """
+    signal.signal(signal.SIGALRM, handler);
+    signal.alarm(5);
+    try:
+        fcntl.flock(f2, fcntl.LOCK_EX)
+    except IOError, e:
+        if e.errno == errno.EINTR:
+            pass
+    else:
+        raise RuntimeError("expect flock to block")
+
+    fcntl.flock(f1, fcntl.LOCK_UN)
+
+    lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 10, 0, 0)
+    fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
+
+    lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 10, 10, 0, 0)
+    fcntl.fcntl(f2, fcntl.F_OFD_SETLK, lockdata)
+
+    """
+    is poxis lock interruptable?
+    """
+    signal.signal(signal.SIGALRM, handler);
+    signal.alarm(5);
+    try:
+        lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
+        fcntl.fcntl(f2, fcntl.F_OFD_SETLKW, lockdata)
+    except IOError, e:
+        if e.errno == errno.EINTR:
+            pass
+    else:
+        raise RuntimeError("expect posix lock to block")
+
+    """
+    file handler 2 should still hold lock on 10~10
+    """
+    try:
+        lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 10, 10, 0, 0)
+        fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
+    except IOError, e:
+        if e.errno == errno.EAGAIN:
+            pass
+    else:
+        raise RuntimeError("expect file handler 2 to hold lock on 10~10")
+
+    lockdata = struct.pack('hhllhh', fcntl.F_UNLCK, 0, 0, 0, 0, 0)
+    fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
+    fcntl.fcntl(f2, fcntl.F_OFD_SETLK, lockdata)
+
+    print 'ok'
+
+main()