Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify  

# it under the terms of the GNU Lesser General Public License as published  

# by the Free Software Foundation; version 2.1 only. 

# 

# This program is distributed in the hope that it will be useful,  

# but WITHOUT ANY WARRANTY; without even the implied warranty of  

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  

# GNU Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

 

"""Serialization for concurrent operations""" 

 

import os, errno 

import flock 

import util 

 

VERBOSE = True 

 

class LockException(util.SMException): 

    pass 

 

class Lock(object): 

    """Simple file-based lock on a local FS. With shared reader/writer 

    attributes.""" 

 

    BASE_DIR = "/var/lock/sm" 

 

    INSTANCES = {} 

    BASE_INSTANCES = {} 

 

    def __new__(cls, name, ns=None, *args, **kwargs): 

        if ns: 

            if ns not in Lock.INSTANCES: 

                Lock.INSTANCES[ns] = {} 

            instances = Lock.INSTANCES[ns] 

        else: 

            instances= Lock.BASE_INSTANCES 

 

        if name not in instances: 

            instances[name] = LockImplementation(name, ns) 

        return instances[name] 

 

    # These are required to pacify pylint as it doesn't understand the __new__ 

    def acquire(self): 

        raise NotImplementedError("Lock methods implemented in LockImplementation") 

 

    def acquireNoblock(self): 

        raise NotImplementedError("Lock methods implemented in LockImplementation") 

 

    def release(self): 

        raise NotImplementedError("Lock methods implemented in LockImplementation") 

 

    def held(self): 

        raise NotImplementedError("Lock methods implemented in LockImplementation") 

 

    def _mknamespace(ns): 

 

        if ns is None: 

            return ".nil" 

 

        assert not ns.startswith(".") 

        assert ns.find(os.path.sep) < 0 

        return ns 

    _mknamespace = staticmethod(_mknamespace) 

 

    @staticmethod 

    def clearAll(): 

        """ 

        Drop all lock instances, to be used when forking, but not execing 

        """ 

        Lock.INSTANCES = {} 

        Lock.BASE_INSTANCES = {} 

 

    def cleanup(name, ns = None): 

87        if ns: 

83            if ns in Lock.INSTANCES: 

                if name in Lock.INSTANCES[ns]: 

                    del Lock.INSTANCES[ns][name] 

                if len(Lock.INSTANCES[ns]) == 0: 

                    del Lock.INSTANCES[ns] 

        elif name in Lock.BASE_INSTANCES: 

            del Lock.BASE_INSTANCES[name] 

 

        ns = Lock._mknamespace(ns) 

        path = os.path.join(Lock.BASE_DIR, ns, name) 

93        if os.path.exists(path): 

            Lock._unlink(path) 

 

    cleanup = staticmethod(cleanup) 

 

    def cleanupAll(ns = None): 

        ns = Lock._mknamespace(ns) 

        nspath = os.path.join(Lock.BASE_DIR, ns) 

 

        if not os.path.exists(nspath): 

            return 

 

        for file in os.listdir(nspath): 

            path = os.path.join(nspath, file) 

            Lock._unlink(path) 

 

        Lock._rmdir(nspath) 

 

    cleanupAll = staticmethod(cleanupAll) 

 

    # 

    # Lock and attribute file management 

    # 

 

    def _mkdirs(path): 

        """Concurrent makedirs() catching EEXIST.""" 

        if os.path.exists(path): 

            return 

        try: 

            os.makedirs(path) 

        except OSError, e: 

            if e.errno != errno.EEXIST: 

                raise LockException("Failed to makedirs(%s)" % path) 

    _mkdirs = staticmethod(_mkdirs) 

 

    def _unlink(path): 

        """Non-raising unlink().""" 

        util.SMlog("lock: unlinking lock file %s" % path) 

        try: 

            os.unlink(path) 

        except Exception, e: 

            util.SMlog("Failed to unlink(%s): %s" % (path, e)) 

    _unlink = staticmethod(_unlink) 

 

    def _rmdir(path): 

        """Non-raising rmdir().""" 

        util.SMlog("lock: removing lock dir %s" % path) 

        try: 

            os.rmdir(path) 

        except Exception, e: 

            util.SMlog("Failed to rmdir(%s): %s" % (path, e)) 

    _rmdir = staticmethod(_rmdir) 

 

 

class LockImplementation(object): 

 

    def __init__(self, name, ns=None): 

        self.lockfile = None 

 

        self.ns = Lock._mknamespace(ns) 

 

        assert not name.startswith(".") 

        assert name.find(os.path.sep) < 0 

        self.name = name 

 

        self.count = 0 

 

        self._open() 

 

    def _open(self): 

        """Create and open the lockable attribute base, if it doesn't exist. 

        (But don't lock it yet.)""" 

 

        # one directory per namespace 

        self.nspath = os.path.join(Lock.BASE_DIR, self.ns) 

 

        # the lockfile inside that namespace directory per namespace 

        self.lockpath = os.path.join(self.nspath, self.name) 

 

        number_of_enoent_retries = 10 

 

        while True: 

            Lock._mkdirs(self.nspath) 

 

            try: 

                self._open_lockfile() 

            except IOError, e: 

                # If another lock within the namespace has already 

                # cleaned up the namespace by removing the directory, 

                # _open_lockfile raises an ENOENT, in this case we retry. 

186                if e.errno == errno.ENOENT: 

186                    if number_of_enoent_retries > 0: 

                        number_of_enoent_retries -= 1 

                        continue 

                raise 

            break 

 

        fd = self.lockfile.fileno() 

        self.lock = flock.WriteLock(fd) 

 

    def _open_lockfile(self): 

        """Provide a seam, so extreme situations could be tested""" 

        util.SMlog("lock: opening lock file %s" % self.lockpath) 

        self.lockfile = file(self.lockpath, "w+") 

 

    def _close(self): 

        """Close the lock, which implies releasing the lock.""" 

exit        if self.lockfile is not None: 

202            if self.held(): 

                # drop all reference counts 

                self.count = 0 

                self.release() 

            self.lockfile.close() 

            util.SMlog("lock: closed %s" % self.lockpath) 

            self.lockfile = None 

 

    __del__ = _close 

 

    def cleanup(self, name, ns = None): 

        Lock.cleanup(name, ns) 

 

    def cleanupAll(self, ns = None): 

        Lock.cleanupAll(ns) 

 

    # 

    # Actual Locking 

    # 

 

    def acquire(self): 

        """Blocking lock aquisition, with warnings. We don't expect to lock a 

        lot. If so, not to collide. Coarse log statements should be ok 

        and aid debugging.""" 

        if not self.held(): 

226            if not self.lock.trylock(): 

                util.SMlog("Failed to lock %s on first attempt, " % self.lockpath 

                       + "blocked by PID %d" % self.lock.test()) 

                self.lock.lock() 

231            if VERBOSE: 

                util.SMlog("lock: acquired %s" % self.lockpath) 

        self.count += 1 

 

    def acquireNoblock(self): 

        """Acquire lock if possible, or return false if lock already held""" 

        if not self.held(): 

            exists = os.path.exists(self.lockpath) 

            ret = self.lock.trylock() 

244            if VERBOSE: 

                util.SMlog("lock: tried lock %s, acquired: %s (exists: %s)" % \ 

                        (self.lockpath, ret, exists)) 

        else: 

            ret = True 

 

247        if ret: 

            self.count += 1 

 

        return ret 

 

    def held(self): 

        """True if @self acquired the lock, False otherwise.""" 

        return self.lock.held() 

 

    def release(self): 

        """Release a previously acquired lock.""" 

258        if self.count >= 1: 

            self.count -= 1 

 

        if self.count > 0: 

            return 

 

        self.lock.unlock() 

exit        if VERBOSE: 

            util.SMlog("lock: released %s" % self.lockpath)