Debian Codegraph @

python/bindings/dlm.py

raw
1 from enum import IntEnum, IntFlag
2 import sys, os, errno, platform
3 import ctypes, ctypes.util
4
5 #bindings
6
7 if platform.system() != "Linux":
8 sys.exit("Not supported")
9
10 c_path_libdlm = ctypes.util.find_library("dlm_lt")
11 if not c_path_libdlm:
12 print("Unable to find the specified library.")
13 sys.exit()
14
15 try:
16 c_libdlm = ctypes.CDLL(c_path_libdlm)
17 except OSError:
18 print("Unable to load the system C library")
19 sys.exit()
20
21 class C_DLM_LSHANDLE(ctypes.c_void_p):
22 pass
23
24 class C_DLM_LKSB(ctypes.Structure):
25 _fields_ = [('sb_status', ctypes.c_int),
26 ('sb_lkid', ctypes.c_uint32),
27 ('sb_flags', ctypes.c_char),
28 ('sb_lvbptr', ctypes.c_char_p)]
29
30 C_BAST_CB = ctypes.CFUNCTYPE(None, ctypes.py_object)
31
32 #dlm_create_lockspace
33 c_dlm_create_lockspace = c_libdlm.dlm_create_lockspace
34 c_dlm_create_lockspace.argtypes = [ctypes.c_char_p, #name
35 ctypes.c_uint, #mode
36 ]
37 c_dlm_create_lockspace.restype = C_DLM_LSHANDLE
38
39 #dlm_release_lockspace
40 c_dlm_release_lockspace = c_libdlm.dlm_release_lockspace
41 c_dlm_release_lockspace.argtypes = [ctypes.c_char_p, #name
42 C_DLM_LSHANDLE, #ls
43 ctypes.c_int, #force
44 ]
45 c_dlm_release_lockspace.restype = ctypes.c_int
46
47 #dlm_ls_lock_wait
48 c_dlm_ls_lock_wait = c_libdlm.dlm_ls_lock_wait
49 c_dlm_ls_lock_wait.argtypes = [C_DLM_LSHANDLE, #ls
50 ctypes.c_uint32, #mode
51 ctypes.POINTER(C_DLM_LKSB), #lksb
52 ctypes.c_uint32, #flags
53 ctypes.c_char_p, #name
54 ctypes.c_uint, #namelen
55 ctypes.c_uint32, #parent
56 ctypes.py_object, #bastarg
57 C_BAST_CB, #bastaddr
58 ctypes.c_void_p, #range
59 ]
60 c_dlm_ls_lock_wait.restype = ctypes.c_int
61
62 #dlm_ls_unlock_wait
63 c_dlm_ls_unlock_wait = c_libdlm.dlm_ls_unlock_wait
64 c_dlm_ls_unlock_wait.argtypes = [C_DLM_LSHANDLE, #ls
65 ctypes.c_uint32, #lkid
66 ctypes.c_uint32, #flags
67 ctypes.POINTER(C_DLM_LKSB), #lksb
68 ]
69 c_dlm_ls_unlock_wait.restype = ctypes.c_int
70
71 #classes
72
73 class LockMode(IntEnum):
74 IV = -1
75 NL = 0
76 CR = 1
77 CW = 2
78 PR = 3
79 PW = 4
80 EX = 5
81
82 class LockFlag(IntFlag):
83 NOQUEUE = 0x00000001
84 CANCEL = 0x00000002
85 CONVERT = 0x00000004
86 VALBLK = 0x00000008
87 QUECVT = 0x00000010
88 IVVALBLK = 0x00000020
89 CONVDEADLK = 0x00000040
90 PERSISTENT = 0x00000080
91 NODLCKWT = 0x00000100
92 NODLCKBLK = 0x00000200
93 EXPEDITE = 0x00000400
94 NOQUEUEBAST = 0x00000800
95 HEADQUE = 0x00001000
96 NOORDER = 0x00002000
97 ORPHAN = 0x00004000
98 ALTPR = 0x00008000
99 ALTCW = 0x00010000
100 FORCEUNLOCK = 0x00020000
101 TIMEOUT = 0x00040000
102
103 class LockSBFlag(IntFlag):
104 DEMOTED = 0x01
105 VALNOTVALID = 0x02
106 ALTMODE = 0x04
107
108 class DLMError(OSError):
109
110 def __init__(self, errno):
111 if not errno < 0:
112 raise ValueError()
113
114 errno = abs(errno)
115 super().__init__(errno, os.strerror(errno))
116
117 DLM_LOCK_TO_STR_FORMAT = \
118 """name: {}
119 last_mode: {}
120 last_flags: {}
121 local_locked: {}
122 last_sb: status: {}, lkid: {}, flags: {}, lvb: {}"""
123
124 class Lockspace:
125
126 def __init__(self, name="default", mode=0o600):
127 self.__lsname = name
128 self.__ls = c_dlm_create_lockspace(self.__lsname.encode(), mode)
129 if not self.__ls:
130 raise DLMError(-errno.ENOMEM)
131
132 def release(self, force=2):
133 if not self.__ls:
134 return
135
136 rc = c_dlm_release_lockspace(self.__lsname.encode(), self.__ls,
137 force)
138 if rc:
139 raise DLMError(rc)
140
141 self.__ls = None
142
143 def __del__(self):
144 self.release()
145
146 def __str__(self):
147 return "Lockspace: {}".format(self.__lsname)
148
149 def get_name(self):
150 return self.__lsname
151
152 # lockspace lock factory
153 def create_lock(self, name):
154 class Lock:
155
156 #note name should be 8 byte aligned for now
157 def __init__(self, ls, c_ls, name):
158 self.__local_locked = False
159 self.__last_mode = LockMode.IV
160 self.__last_flags = LockFlag(0)
161
162 self.__lk = C_DLM_LKSB()
163 self.__lk.sb_status = 0
164 self.__lk.sb_lkid = 0
165 self.__lk.sb_flags = LockSBFlag(0)
166 self.__lk.sb_lvbptr = None
167
168 self.__ls = ls
169 self.__c_ls = c_ls
170 self.__name = name
171
172 def __del__(self):
173 if self.__local_locked:
174 self.unlock_wait()
175
176 def __str__(self):
177 sb = self.get_sb()
178 return DLM_LOCK_TO_STR_FORMAT.format(
179 self.__name,
180 str(self.__last_mode),
181 str(self.__last_flags),
182 self.__local_locked,
183 str(sb.status),
184 sb.lkid, str(sb.flags),
185 str(sb.lvb))
186
187 def get_name(self):
188 return self.__name
189
190 def get_ls(self):
191 return self.__ls
192
193 # get a copy of current sb state in high-level python
194 def get_sb(self):
195 class LockSB:
196
197 def __init__(self, status, lkid,
198 flags, lvb):
199 self.status = status
200 self.lkid = lkid
201 self.flags = LockSBFlag(flags[0])
202 self.lvb = lvb
203
204 return LockSB(self.__lk.sb_status,
205 self.__lk.sb_lkid,
206 self.__lk.sb_flags,
207 self.__lk.sb_lvbptr)
208
209 def lock_wait(self, mode=LockMode.EX,
210 flags=LockFlag(0), bast=None,
211 bastarg=None):
212 if bast:
213 bast = C_BAST_CB(bast)
214 else:
215 bast = ctypes.cast(None, C_BAST_CB)
216
217 rc = c_dlm_ls_lock_wait(self.__c_ls, mode,
218 ctypes.byref(self.__lk),
219 flags,
220 self.__name.encode(),
221 len(self.__name), 0,
222 bastarg, bast, None)
223 if rc:
224 raise DLMError(rc)
225
226 self.__last_mode = mode
227 self.__last_flags = flags
228 self.__local_locked = True
229
230 def unlock_wait(self, flags=0):
231 rc = c_dlm_ls_unlock_wait(self.__c_ls,
232 self.__lk.sb_lkid,
233 flags,
234 ctypes.byref(self.__lk))
235 if rc:
236 raise DLMError(rc)
237
238 self.__last_flags = flags
239 self.__local_locked = False
240
241 lock = Lock(self, self.__ls, name)
242 return lock
243
244 # vim: tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab