aboutsummaryrefslogtreecommitdiff
blob: d5d40d5e2fe5328f78a043180e9ee2a546bc3776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/python
import os
import fcntl
import errno
import sys
import time


from catalyst.support import CatalystError, normpath


def writemsg(mystr):
	sys.stderr.write(mystr)
	sys.stderr.flush()


class LockInUse(Exception):
	def __init__(self, message):
		if message:
			#(type,value)=sys.exc_info()[:2]
			#if value!=None:
			    #print
			    #kprint traceback.print_exc(file=sys.stdout)
			print
			print "!!! catalyst lock file in use: "+message
			print


class LockDir:
	locking_method=fcntl.flock
	lock_dirs_in_use=[]
	die_on_failed_lock=True

	def __del__(self):
		#print "Lock.__del__() 1"
		self.clean_my_hardlocks()
		#print "Lock.__del__() 2"
		self.delete_lock_from_path_list()
		#print "Lock.__del__() 3"
		if self.islocked():
			#print "Lock.__del__() 4"
			self.fcntl_unlock()
		#print "Lock.__del__() finnished"

	def __init__(self,lockdir):
		self.locked=False
		self.myfd=None
		self.set_gid(250)
		self.locking_method=LockDir.locking_method
		self.set_lockdir(lockdir)
		self.set_lockfilename(".catalyst_lock")
		self.set_lockfile()

		if LockDir.lock_dirs_in_use.count(lockdir)>0:
			raise "This directory already associated with a lock object"
		else:
			LockDir.lock_dirs_in_use.append(lockdir)

		self.hardlock_paths={}

	def delete_lock_from_path_list(self):
		i=0
		try:
			if LockDir.lock_dirs_in_use:
				for x in LockDir.lock_dirs_in_use:
					if LockDir.lock_dirs_in_use[i] == self.lockdir:
						del LockDir.lock_dirs_in_use[i]
						break
						i=i+1
		except AttributeError:
			pass

	def islocked(self):
		if self.locked:
			return True
		else:
			return False

	def set_gid(self,gid):
		if not self.islocked():
#			if "DEBUG" in self.settings:
#				print "setting gid to", gid
			self.gid=gid

	def set_lockdir(self,lockdir):
		if not os.path.exists(lockdir):
			os.makedirs(lockdir)
		if os.path.isdir(lockdir):
			if not self.islocked():
				if lockdir[-1] == "/":
					lockdir=lockdir[:-1]
				self.lockdir = normpath(lockdir)
#				if "DEBUG" in self.settings:
#					print "setting lockdir to", self.lockdir
		else:
			raise "the lock object needs a path to a dir"

	def set_lockfilename(self,lockfilename):
		if not self.islocked():
			self.lockfilename=lockfilename
#			if "DEBUG" in self.settings:
#				print "setting lockfilename to", self.lockfilename

	def set_lockfile(self):
		if not self.islocked():
			self.lockfile = normpath(self.lockdir+'/'+self.lockfilename)
#			if "DEBUG" in self.settings:
#				print "setting lockfile to", self.lockfile

	def read_lock(self):
		if not self.locking_method == "HARDLOCK":
			self.fcntl_lock("read")
		else:
			print "HARDLOCKING doesnt support shared-read locks"
			print "using exclusive write locks"
			self.hard_lock()

	def write_lock(self):
		if not self.locking_method == "HARDLOCK":
			self.fcntl_lock("write")
		else:
			self.hard_lock()

	def unlock(self):
		if not self.locking_method == "HARDLOCK":
			self.fcntl_unlock()
		else:
			self.hard_unlock()

	def fcntl_lock(self,locktype):
		if self.myfd==None:
			if not os.path.exists(os.path.dirname(self.lockdir)):
				raise CatalystError("DirectoryNotFound: %s"
					% os.path.dirname(self.lockdir), print_traceback=True)
			if not os.path.exists(self.lockfile):
				old_mask=os.umask(000)
				self.myfd = os.open(self.lockfile, os.O_CREAT|os.O_RDWR,0660)
				try:
					if os.stat(self.lockfile).st_gid != self.gid:
						os.chown(self.lockfile,os.getuid(),self.gid)
				except SystemExit, e:
					raise
				except OSError, e:
					if e[0] == 2: #XXX: No such file or directory
						return self.fcntl_locking(locktype)
					else:
						writemsg("Cannot chown a lockfile. This could cause inconvenience later.\n")

				os.umask(old_mask)
			else:
				self.myfd = os.open(self.lockfile, os.O_CREAT|os.O_RDWR,0660)

		try:
			if locktype == "read":
				self.locking_method(self.myfd,fcntl.LOCK_SH|fcntl.LOCK_NB)
			else:
				self.locking_method(self.myfd,fcntl.LOCK_EX|fcntl.LOCK_NB)
		except IOError, e:
			if "errno" not in dir(e):
				raise
			if e.errno == errno.EAGAIN:
				if not LockDir.die_on_failed_lock:
					# Resource temp unavailable; eg, someone beat us to the lock.
					writemsg("waiting for lock on %s\n" % self.lockfile)

					# Try for the exclusive or shared lock again.
					if locktype == "read":
						self.locking_method(self.myfd,fcntl.LOCK_SH)
					else:
						self.locking_method(self.myfd,fcntl.LOCK_EX)
				else:
					raise LockInUse,self.lockfile
			elif e.errno == errno.ENOLCK:
				pass
			else:
				raise
		if not os.path.exists(self.lockfile):
			os.close(self.myfd)
			self.myfd=None
			#writemsg("lockfile recurse\n")
			self.fcntl_lock(locktype)
		else:
			self.locked=True
			#writemsg("Lockfile obtained\n")

	def fcntl_unlock(self):
		import fcntl
		unlinkfile = 1
		if not os.path.exists(self.lockfile):
			print "lockfile does not exist '%s'" % self.lockfile
			#print "fcntl_unlock() , self.myfd:", self.myfd, type(self.myfd)
			if (self.myfd != None):
				#print "fcntl_unlock() trying to close it "
				try:
					os.close(self.myfd)
					self.myfd=None
				except:
					pass
				return False

			try:
				if self.myfd == None:
					self.myfd = os.open(self.lockfile, os.O_WRONLY,0660)
					unlinkfile = 1
					self.locking_method(self.myfd,fcntl.LOCK_UN)
			except SystemExit, e:
				raise e
			except Exception, e:
				#if self.myfd is not None:
					#print "fcntl_unlock() trying to close", self.myfd
					#os.close(self.myfd)
					#self.myfd=None
				#raise IOError, "Failed to unlock file '%s'\n%s" % (self.lockfile, str(e))
				try:
					# This sleep call was added to allow other processes that are
					# waiting for a lock to be able to grab it before it is deleted.
					# lockfile() already accounts for this situation, however, and
					# the sleep here adds more time than is saved overall, so am
					# commenting until it is proved necessary.
					#time.sleep(0.0001)
					if unlinkfile:
						InUse=False
						try:
							self.locking_method(self.myfd,fcntl.LOCK_EX|fcntl.LOCK_NB)
						except:
							print "Read lock may be in effect. skipping lockfile delete..."
							InUse=True
							# We won the lock, so there isn't competition for it.
							# We can safely delete the file.
							#writemsg("Got the lockfile...\n")
							#writemsg("Unlinking...\n")
							self.locking_method(self.myfd,fcntl.LOCK_UN)
					if not InUse:
						os.unlink(self.lockfile)
						os.close(self.myfd)
						self.myfd=None
#						if "DEBUG" in self.settings:
#							print "Unlinked lockfile..."
				except SystemExit, e:
					raise e
				except Exception, e:
					# We really don't care... Someone else has the lock.
					# So it is their problem now.
					print "Failed to get lock... someone took it."
					print str(e)

					# Why test lockfilename?  Because we may have been handed an
					# fd originally, and the caller might not like having their
					# open fd closed automatically on them.
					#if type(lockfilename) == types.StringType:
					#        os.close(myfd)
		#print "fcntl_unlock() trying a last ditch close", self.myfd
		if (self.myfd != None):
			os.close(self.myfd)
			self.myfd=None
			self.locked=False
			time.sleep(.0001)

	def hard_lock(self,max_wait=14400):
		"""Does the NFS, hardlink shuffle to ensure locking on the disk.
		We create a PRIVATE lockfile, that is just a placeholder on the disk.
		Then we HARDLINK the real lockfile to that private file.
		If our file can 2 references, then we have the lock. :)
		Otherwise we lather, rise, and repeat.
		We default to a 4 hour timeout.
		"""

		self.myhardlock = self.hardlock_name(self.lockdir)

		start_time = time.time()
		reported_waiting = False

		while(time.time() < (start_time + max_wait)):
			# We only need it to exist.
			self.myfd = os.open(self.myhardlock, os.O_CREAT|os.O_RDWR,0660)
			os.close(self.myfd)

			self.add_hardlock_file_to_cleanup()
			if not os.path.exists(self.myhardlock):
				raise CatalystError("FileNotFound: Created lockfile is missing: "
					"%(filename)s" % {"filename":self.myhardlock},
					print_traceback=True)
			try:
				os.link(self.myhardlock, self.lockfile)
			except SystemExit:
				raise
			except Exception:
#				if "DEBUG" in self.settings:
#					print "lockfile(): Hardlink: Link failed."
#					print "Exception: ",e
				pass

			if self.hardlink_is_mine(self.myhardlock, self.lockfile):
				# We have the lock.
				if reported_waiting:
					print
				return True

			if reported_waiting:
				writemsg(".")
			else:
				reported_waiting = True
				print
				print "Waiting on (hardlink) lockfile: (one '.' per 3 seconds)"
				print "Lockfile: " + self.lockfile
			time.sleep(3)

		os.unlink(self.myhardlock)
		return False

	def hard_unlock(self):
		try:
			if os.path.exists(self.myhardlock):
				os.unlink(self.myhardlock)
			if os.path.exists(self.lockfile):
				os.unlink(self.lockfile)
		except SystemExit:
			raise
		except:
			writemsg("Something strange happened to our hardlink locks.\n")

	def add_hardlock_file_to_cleanup(self):
		#mypath = self.normpath(path)
		if os.path.isdir(self.lockdir) and os.path.isfile(self.myhardlock):
			self.hardlock_paths[self.lockdir]=self.myhardlock

	def remove_hardlock_file_from_cleanup(self):
		if self.lockdir in self.hardlock_paths:
			del self.hardlock_paths[self.lockdir]
			print self.hardlock_paths

	def hardlock_name(self, path):
		mypath=path+"/.hardlock-"+os.uname()[1]+"-"+str(os.getpid())
		newpath = os.path.normpath(mypath)
		if len(newpath) > 1:
			if newpath[1] == "/":
				newpath = "/"+newpath.lstrip("/")
		return newpath

	def hardlink_is_mine(self,link,lock):
		import stat
		try:
			myhls = os.stat(link)
			mylfs = os.stat(lock)
		except SystemExit:
			raise
		except:
			myhls = None
			mylfs = None

		if myhls:
			if myhls[stat.ST_NLINK] == 2:
				return True
		if mylfs:
			if mylfs[stat.ST_INO] == myhls[stat.ST_INO]:
				return True
		return False

	def hardlink_active(lock):
		if not os.path.exists(lock):
			return False

	def clean_my_hardlocks(self):
		try:
			for x in self.hardlock_paths.keys():
				self.hardlock_cleanup(x)
		except AttributeError:
			pass

	def hardlock_cleanup(self,path):
		#mypid  = str(os.getpid())
		myhost = os.uname()[1]
		mydl = os.listdir(path)
		results = []
		mycount = 0

		mylist = {}
		for x in mydl:
			filepath=path+"/"+x
			if os.path.isfile(filepath):
				parts = filepath.split(".hardlock-")
			if len(parts) == 2:
				filename = parts[0]
				hostpid  = parts[1].split("-")
				host  = "-".join(hostpid[:-1])
				pid   = hostpid[-1]
			if filename not in mylist:
				mylist[filename] = {}

			if host not in mylist[filename]:
				mylist[filename][host] = []
				mylist[filename][host].append(pid)
				mycount += 1
			else:
				mylist[filename][host].append(pid)
				mycount += 1


		results.append("Found %(count)s locks" % {"count":mycount})
		for x in mylist.keys():
			if myhost in mylist[x]:
				mylockname = self.hardlock_name(x)
				if self.hardlink_is_mine(mylockname, self.lockfile) or \
					not os.path.exists(self.lockfile):
					for y in mylist[x].keys():
						for z in mylist[x][y]:
							filename = x+".hardlock-"+y+"-"+z
							if filename == mylockname:
								self.hard_unlock()
								continue
							try:
								# We're sweeping through, unlinking everyone's locks.
								os.unlink(filename)
								results.append("Unlinked: " + filename)
							except SystemExit:
								raise
							except Exception:
								pass
					try:
						os.unlink(x)
						results.append("Unlinked: " + x)
						os.unlink(mylockname)
						results.append("Unlinked: " + mylockname)
					except SystemExit:
						raise
					except Exception:
						pass
				else:
					try:
						os.unlink(mylockname)
						results.append("Unlinked: " + mylockname)
					except SystemExit:
						raise
					except Exception:
						pass
		return results


if __name__ == "__main__":

	def lock_work():
		print
		for i in range(1,6):
			print i,time.time()
			time.sleep(1)
		print

	print "Lock 5 starting"
	Lock1=LockDir("/tmp/lock_path")
	Lock1.write_lock()
	print "Lock1 write lock"

	lock_work()

	Lock1.unlock()
	print "Lock1 unlock"

	Lock1.read_lock()
	print "Lock1 read lock"

	lock_work()

	Lock1.unlock()
	print "Lock1 unlock"

	Lock1.read_lock()
	print "Lock1 read lock"

	Lock1.write_lock()
	print "Lock1 write lock"

	lock_work()

	Lock1.unlock()
	print "Lock1 unlock"

	Lock1.read_lock()
	print "Lock1 read lock"

	lock_work()

	Lock1.unlock()
	print "Lock1 unlock"