/
lockfile.py
251 lines (210 loc) · 8.44 KB
/
lockfile.py
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
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2005 Divmod, Inc.
# Copyright (c) 2008-2011 Twisted Matrix Laboratories
# Copyright (c) 2012- Spyder Project Contributors
#
# Distributed under the terms of the MIT (Expat) License
# (see LICENSE.txt in this directory and NOTICE.txt in the root for details)
# -----------------------------------------------------------------------------
"""
Filesystem-based interprocess mutex.
Taken from the Twisted project.
Distributed under the MIT (Expat) license.
Changes by the Spyder Team to the original module:
* Rewrite kill Windows function to make it more reliable.
* Detect if the process that owns the lock is an Spyder one.
Adapted from src/twisted/python/lockfile.py of the
`Twisted project <https://github.com/twisted/twisted>`_.
"""
__metaclass__ = type
import errno, os
from time import time as _uniquefloat
from spyder.py3compat import PY2, to_binary_string
from spyder.utils.programs import is_spyder_process
def unique():
if PY2:
return str(long(_uniquefloat() * 1000))
else:
return str(int(_uniquefloat() * 1000))
from os import rename
if not os.name == 'nt':
from os import kill
from os import symlink
from os import readlink
from os import remove as rmlink
_windows = False
else:
_windows = True
import ctypes
from ctypes import wintypes
# https://docs.microsoft.com/en-us/windows/desktop/ProcThread/process-security-and-access-rights
PROCESS_QUERY_INFORMATION = 0x400
# GetExitCodeProcess uses a special exit code to indicate that the
# process is still running.
STILL_ACTIVE = 259
def _is_pid_running(pid):
"""Taken from https://www.madebuild.org/blog/?p=30"""
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
if handle == 0:
return False
# If the process exited recently, a pid may still exist for the
# handle. So, check if we can get the exit code.
exit_code = wintypes.DWORD()
retval = kernel32.GetExitCodeProcess(handle,
ctypes.byref(exit_code))
is_running = (retval == 0)
kernel32.CloseHandle(handle)
# See if we couldn't get the exit code or the exit code indicates
# that the process is still running.
return is_running or exit_code.value == STILL_ACTIVE
def kill(pid, signal): # analysis:ignore
if not _is_pid_running(pid):
raise OSError(errno.ESRCH, None)
else:
return
_open = open
# XXX Implement an atomic thingamajig for win32
def symlink(value, filename): #analysis:ignore
newlinkname = filename+"."+unique()+'.newlink'
newvalname = os.path.join(newlinkname, "symlink")
os.mkdir(newlinkname)
f = _open(newvalname, 'wb')
f.write(to_binary_string(value))
f.flush()
f.close()
try:
rename(newlinkname, filename)
except:
# This is needed to avoid an error when we don't
# have permissions to write in ~/.spyder
# See issues 6319 and 9093
try:
os.remove(newvalname)
os.rmdir(newlinkname)
except (IOError, OSError):
return
raise
def readlink(filename): #analysis:ignore
try:
fObj = _open(os.path.join(filename, 'symlink'), 'rb')
except IOError as e:
if e.errno == errno.ENOENT or e.errno == errno.EIO:
raise OSError(e.errno, None)
raise
else:
result = fObj.read().decode()
fObj.close()
return result
def rmlink(filename): #analysis:ignore
os.remove(os.path.join(filename, 'symlink'))
os.rmdir(filename)
class FilesystemLock:
"""
A mutex.
This relies on the filesystem property that creating
a symlink is an atomic operation and that it will
fail if the symlink already exists. Deleting the
symlink will release the lock.
@ivar name: The name of the file associated with this lock.
@ivar clean: Indicates whether this lock was released cleanly by its
last owner. Only meaningful after C{lock} has been called and
returns True.
@ivar locked: Indicates whether the lock is currently held by this
object.
"""
clean = None
locked = False
def __init__(self, name):
self.name = name
def lock(self):
"""
Acquire this lock.
@rtype: C{bool}
@return: True if the lock is acquired, false otherwise.
@raise: Any exception os.symlink() may raise, other than
EEXIST.
"""
clean = True
while True:
try:
symlink(str(os.getpid()), self.name)
except OSError as e:
if _windows and e.errno in (errno.EACCES, errno.EIO):
# The lock is in the middle of being deleted because we're
# on Windows where lock removal isn't atomic. Give up, we
# don't know how long this is going to take.
return False
if e.errno == errno.EEXIST:
try:
pid = readlink(self.name)
except OSError as e:
if e.errno == errno.ENOENT:
# The lock has vanished, try to claim it in the
# next iteration through the loop.
continue
raise
except IOError as e:
if _windows and e.errno == errno.EACCES:
# The lock is in the middle of being
# deleted because we're on Windows where
# lock removal isn't atomic. Give up, we
# don't know how long this is going to
# take.
return False
raise
try:
if kill is not None:
kill(int(pid), 0)
if not is_spyder_process(int(pid)):
raise(OSError(errno.ESRCH, 'No such process'))
except OSError as e:
if e.errno == errno.ESRCH:
# The owner has vanished, try to claim it in the
# next iteration through the loop.
try:
rmlink(self.name)
except OSError as e:
if e.errno == errno.ENOENT:
# Another process cleaned up the lock.
# Race them to acquire it in the next
# iteration through the loop.
continue
raise
clean = False
continue
raise
return False
raise
self.locked = True
self.clean = clean
return True
def unlock(self):
"""
Release this lock.
This deletes the directory with the given name.
@raise: Any exception os.readlink() may raise, or
ValueError if the lock is not owned by this process.
"""
pid = readlink(self.name)
if int(pid) != os.getpid():
raise ValueError("Lock %r not owned by this process" % (self.name,))
rmlink(self.name)
self.locked = False
def isLocked(name):
"""Determine if the lock of the given name is held or not.
@type name: C{str}
@param name: The filesystem path to the lock to test
@rtype: C{bool}
@return: True if the lock is held, False otherwise.
"""
l = FilesystemLock(name)
result = None
try:
result = l.lock()
finally:
if result:
l.unlock()
return not result
__all__ = ['FilesystemLock', 'isLocked']