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

#!/usr/bin/python 

# 

# 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: 

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

    attributes.""" 

 

    BASE_DIR = "/var/lock/sm" 

 

    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: 

            self._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. 

60                if e.errno == errno.ENOENT: 

60                    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: 

75            if self.held(): 

                self.release() 

            self.lockfile.close() 

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

            self.lockfile = None 

 

    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) 

 

    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._open() 

 

    __del__ = _close 

 

    def cleanup(name, ns = None): 

        ns = Lock._mknamespace(ns) 

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

        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) 

 

    # 

    # 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.""" 

168   171        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() 

        if VERBOSE: 

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

 

    def acquireNoblock(self): 

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

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

        ret = self.lock.trylock() 

        if VERBOSE: 

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

                    (self.lockpath, ret, exists)) 

        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.""" 

        self.lock.unlock() 

        if VERBOSE: 

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

 

if __debug__: 

    import sys 

 

    def test(): 

 

        # Create a Lock 

        lock = Lock("test"); 

 

        # Should not be yet held. 

        assert lock.held() == False 

 

        # Go get it 

        lock.acquire() 

 

        # Second lock shall throw in debug mode. 

        try: 

            lock.acquire() 

        except AssertionError, e: 

            if str(e) != flock.WriteLock.ERROR_ISLOCKED: 

                raise 

        else: 

            raise AssertionError("Reaquired a locked lock") 

 

        lock.release() 

 

        Lock.cleanup() 

 

221    if __name__ == '__main__': 

        print >>sys.stderr, "Running self tests..." 

        test() 

        print >>sys.stderr, "OK."