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

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

#!/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 

# 

 

""" 

Fcntl-based Advisory Locking with a proper .trylock() 

 

Python's fcntl module is not good at locking. In particular, proper 

testing and trying of locks isn't well supported. Looks as if we've 

got to grow our own. 

""" 

 

import os, fcntl, struct 

import errno 

 

class Flock: 

    """A C flock struct.""" 

 

    def __init__(self, l_type, l_whence=0, l_start=0, l_len=0, l_pid=0): 

        """See fcntl(2) for field details.""" 

        self.fields = [l_type, l_whence, l_start, l_len, l_pid] 

 

    FORMAT = "hhqql" 

    # struct flock(2) format, tested with python2.4/i686 and 

    # python2.5/x86_64. http://docs.python.org/lib/posix-large-files.html 

 

    def fcntl(self, fd, cmd): 

        """Issues a system fcntl(fd, cmd, self). Updates self with what was 

        returned by the kernel. Otherwise raises IOError(errno).""" 

 

        st = struct.pack(self.FORMAT, *self.fields) 

        st = fcntl.fcntl(fd, cmd, st) 

 

        fields = struct.unpack(self.FORMAT, st) 

        self.__init__(*fields) 

 

    FIELDS = { 'l_type':       0, 

               'l_whence':     1, 

               'l_start':      2, 

               'l_len':        3, 

               'l_pid':        4 } 

 

    def __getattr__(self, name): 

        idx = self.FIELDS[name] 

        return self.fields[idx] 

 

    def __setattr__(self, name, value): 

        idx = self.FIELDS.get(name) 

66        if idx is None: 

            self.__dict__[name] = value 

        else: 

            self.fields[idx] = value 

 

class FcntlLockBase: 

    """Abstract base class for either reader or writer locks. A respective 

    definition of LOCK_TYPE (fcntl.{F_RDLCK|F_WRLCK}) determines the 

    type.""" 

 

    LOCK_TYPE = None 

 

    if __debug__: 

        ERROR_ISLOCKED = "Attempt to acquire lock held." 

        ERROR_NOTLOCKED = "Attempt to unlock lock not held." 

 

    def __init__(self, fd): 

        """Creates a new, unheld lock.""" 

        self.fd = fd 

        # 

        # Subtle: fcntl(2) permits re-locking it as often as you want 

        # once you hold it. This is slightly counterintuitive and we 

        # want clean code, so we add one bit of our own bookkeeping. 

        # 

        self._held = False 

 

    def lock(self): 

        """Blocking lock aquisition.""" 

        assert not self._held, self.ERROR_ISLOCKED 

        Flock(self.LOCK_TYPE).fcntl(self.fd, fcntl.F_SETLKW) 

        self._held = True 

 

    def trylock(self): 

        """Non-blocking lock aquisition. Returns True on success, False 

        otherwise.""" 

exit        if self._held: return False 

        try: 

            Flock(self.LOCK_TYPE).fcntl(self.fd, fcntl.F_SETLK) 

        except IOError, e: 

103            if e.errno in [errno.EACCES, errno.EAGAIN]: 

                return False 

            raise 

        self._held = True 

        return True 

 

    def held(self): 

        """Returns True if @self holds the lock, False otherwise.""" 

        return self._held 

 

    def unlock(self): 

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

        Flock(fcntl.F_UNLCK).fcntl(self.fd, fcntl.F_SETLK) 

        self._held = False 

 

    def test(self): 

        """Returns the PID of the process holding the lock or -1 if the lock 

        is not held.""" 

        if self._held: return os.getpid() 

        flock = Flock(self.LOCK_TYPE) 

        flock.fcntl(self.fd, fcntl.F_GETLK) 

        if flock.l_type == fcntl.F_UNLCK: 

            return -1 

        return flock.l_pid 

 

 

class WriteLock(FcntlLockBase): 

    """A simple global writer (i.e. exclusive) lock.""" 

    LOCK_TYPE = fcntl.F_WRLCK 

 

class ReadLock(FcntlLockBase): 

    """A simple global reader (i.e. shared) lock.""" 

    LOCK_TYPE = fcntl.F_RDLCK 

 

 

# 

# Test/Example 

# 

if __debug__: 

    import sys 

 

    def test_interface(): 

 

        lockfile = file("/tmp/lockfile", "w+") 

 

        # Create a WriteLock 

        fd = lockfile.fileno() 

        lock = WriteLock(fd) 

 

        # It's not yet held. 

        assert lock.test() == None 

        assert lock.held() == False 

 

        # 

        # Let a child aquire it 

        # 

 

        (pin, cout) = os.pipe() 

        (cin, pout) = os.pipe() 

 

        pid = os.fork() 

        if pid == 0: 

            os.close(pin) 

            os.close(pout) 

 

            lock.lock() 

 

            # Synchronize 

            os.write(cout, "SYN") 

 

            # Wait for parent 

            assert os.read(cin, 3) == "ACK", "Faulty parent" 

 

            sys.exit(0) 

 

        os.close(cout) 

        os.close(cin) 

 

        # Wait for child 

        assert os.read(pin, 3) == "SYN", "Faulty child" 

 

        # Lock should be held by child 

        assert lock.test() == pid 

        assert lock.trylock() == False 

        assert lock.held() == False 

 

        # Synchronize child 

        os.write(pout, "ACK") 

 

        # Lock requires our uncooperative child to terminate. 

        lock.lock() 

 

        # We got the lock, so child should have exited, right? 

        #assert os.waitpid(pid, os.WNOHANG) == (pid, 0) 

        # 

        # Won't work but race, because the runtime will explicitly 

        # lockfile.close() before the real exit(2). See 

        # FcntlLockBase.__del__() above. 

 

        # Attempt to re-lock should throw 

        try: 

            lock.lock() 

        except AssertionError, e: 

            if str(e) != WriteLock.ERROR_ISLOCKED: 

                raise 

        else: 

            raise AssertionError("Held locks should not be lockable.") 

 

        # We got the lock.. 

        assert lock.held() == True 

        # .. so trylock should also know. 

        assert lock.trylock() == False 

 

        # Fcntl won't do this, but we do. Users should be able to avoid 

        # relying on it. 

        assert lock.test() == os.getpid() 

 

        # Release the lock. 

        lock.unlock() 

 

        # Attempt to re-unlock should throw. 

        try: 

            lock.unlock() 

        except AssertionError, e: 

            if str(e) != WriteLock.ERROR_NOTLOCKED: 

                raise 

        else: 

            raise AssertionError("Unlocked locks should not unlock.") 

 

    def test_rwlocking(): 

 

        lockfile = file("/tmp/lockfile", "w+") 

 

        fd = lockfile.fileno() 

 

        rdlock = ReadLock(fd) 

        assert rdlock.test() == None 

 

        wrlock = WriteLock(fd) 

        assert wrlock.test() == None 

 

        rdlock.lock() 

        # Same story: need to fork to get this going 

        assert wrlock.test() == None 

        rdlock.unlock() 

 

        # 

        # Let a child aquire it 

        # 

 

        (pin, cout) = os.pipe() 

        (cin, pout) = os.pipe() 

 

        pid = os.fork() 

        if pid == 0: 

            os.close(pin) 

            os.close(pout) 

 

            # Synchronize parent 

            os.write(cout, "SYN") 

 

            wrlock.lock() 

            assert os.read(cin, 3) == "SYN", "Faulty parent" 

 

            # Wait for parent 

            assert os.read(cin, 3) == "ACK", "Faulty parent" 

 

            sys.exit(0) 

 

        os.close(cout) 

        os.close(cin) 

 

        # Wait for child 

        assert os.read(pin, 3) == "SYN", "Faulty child" 

 

        rdlock.lock() 

 

        assert os.write(pout, "SYN") 

 

 

 

284    if __name__ == "__main__": 

        print >>sys.stderr, "Running basic interface tests..." 

        test_interface() 

        print >>sys.stderr, "Running RW-locking stuff not clear from the manpages..." 

        test_rwlocking() 

        print >>sys.stderr, "OK."