Debian Codegraph @

cache/internal/flock/flock_unix.go

raw
1 // Copyright 2015 Tim Heckman. All rights reserved.
2 // Use of this source code is governed by the BSD 3-Clause
3 // license that can be found in the LICENSE file.
4
5 //go:build !aix && !windows
6 // +build !aix,!windows
7
8 package flock
9
10 import (
11 "os"
12 "syscall"
13 )
14
15 // Lock is a blocking call to try and take an exclusive file lock. It will wait
16 // until it is able to obtain the exclusive file lock. It's recommended that
17 // TryLock() be used over this function. This function may block the ability to
18 // query the current Locked() or RLocked() status due to a RW-mutex lock.
19 //
20 // If we are already exclusive-locked, this function short-circuits and returns
21 // immediately assuming it can take the mutex lock.
22 //
23 // If the *Flock has a shared lock (RLock), this may transparently replace the
24 // shared lock with an exclusive lock on some UNIX-like operating systems. Be
25 // careful when using exclusive locks in conjunction with shared locks
26 // (RLock()), because calling Unlock() may accidentally release the exclusive
27 // lock that was once a shared lock.
28 func (f *Flock) Lock() error {
29 return f.lock(&f.l, syscall.LOCK_EX)
30 }
31
32 // RLock is a blocking call to try and take a shared file lock. It will wait
33 // until it is able to obtain the shared file lock. It's recommended that
34 // TryRLock() be used over this function. This function may block the ability to
35 // query the current Locked() or RLocked() status due to a RW-mutex lock.
36 //
37 // If we are already shared-locked, this function short-circuits and returns
38 // immediately assuming it can take the mutex lock.
39 func (f *Flock) RLock() error {
40 return f.lock(&f.r, syscall.LOCK_SH)
41 }
42
43 func (f *Flock) lock(locked *bool, flag int) error {
44 f.m.Lock()
45 defer f.m.Unlock()
46
47 if *locked {
48 return nil
49 }
50
51 if f.fh == nil {
52 if err := f.setFh(); err != nil {
53 return err
54 }
55 defer f.ensureFhState()
56 }
57
58 if err := syscall.Flock(int(f.fh.Fd()), flag); err != nil {
59 shouldRetry, reopenErr := f.reopenFDOnError(err)
60 if reopenErr != nil {
61 return reopenErr
62 }
63
64 if !shouldRetry {
65 return err
66 }
67
68 if err = syscall.Flock(int(f.fh.Fd()), flag); err != nil {
69 return err
70 }
71 }
72
73 *locked = true
74 return nil
75 }
76
77 // Unlock is a function to unlock the file. This file takes a RW-mutex lock, so
78 // while it is running the Locked() and RLocked() functions will be blocked.
79 //
80 // This function short-circuits if we are unlocked already. If not, it calls
81 // syscall.LOCK_UN on the file and closes the file descriptor. It does not
82 // remove the file from disk. It's up to your application to do.
83 //
84 // Please note, if your shared lock became an exclusive lock this may
85 // unintentionally drop the exclusive lock if called by the consumer that
86 // believes they have a shared lock. Please see Lock() for more details.
87 func (f *Flock) Unlock() error {
88 f.m.Lock()
89 defer f.m.Unlock()
90
91 // if we aren't locked or if the lockfile instance is nil
92 // just return a nil error because we are unlocked
93 if (!f.l && !f.r) || f.fh == nil {
94 return nil
95 }
96
97 // mark the file as unlocked
98 if err := syscall.Flock(int(f.fh.Fd()), syscall.LOCK_UN); err != nil {
99 return err
100 }
101
102 f.fh.Close()
103
104 f.l = false
105 f.r = false
106 f.fh = nil
107
108 return nil
109 }
110
111 // TryLock is the preferred function for taking an exclusive file lock. This
112 // function takes an RW-mutex lock before it tries to lock the file, so there is
113 // the possibility that this function may block for a short time if another
114 // goroutine is trying to take any action.
115 //
116 // The actual file lock is non-blocking. If we are unable to get the exclusive
117 // file lock, the function will return false instead of waiting for the lock. If
118 // we get the lock, we also set the *Flock instance as being exclusive-locked.
119 func (f *Flock) TryLock() (bool, error) {
120 return f.try(&f.l, syscall.LOCK_EX)
121 }
122
123 // TryRLock is the preferred function for taking a shared file lock. This
124 // function takes an RW-mutex lock before it tries to lock the file, so there is
125 // the possibility that this function may block for a short time if another
126 // goroutine is trying to take any action.
127 //
128 // The actual file lock is non-blocking. If we are unable to get the shared file
129 // lock, the function will return false instead of waiting for the lock. If we
130 // get the lock, we also set the *Flock instance as being share-locked.
131 func (f *Flock) TryRLock() (bool, error) {
132 return f.try(&f.r, syscall.LOCK_SH)
133 }
134
135 func (f *Flock) try(locked *bool, flag int) (bool, error) {
136 f.m.Lock()
137 defer f.m.Unlock()
138
139 if *locked {
140 return true, nil
141 }
142
143 if f.fh == nil {
144 if err := f.setFh(); err != nil {
145 return false, err
146 }
147 defer f.ensureFhState()
148 }
149
150 var retried bool
151 retry:
152 err := syscall.Flock(int(f.fh.Fd()), flag|syscall.LOCK_NB)
153
154 switch err {
155 case syscall.EWOULDBLOCK:
156 return false, nil
157 case nil:
158 *locked = true
159 return true, nil
160 }
161 if !retried {
162 if shouldRetry, reopenErr := f.reopenFDOnError(err); reopenErr != nil {
163 return false, reopenErr
164 } else if shouldRetry {
165 retried = true
166 goto retry
167 }
168 }
169
170 return false, err
171 }
172
173 // reopenFDOnError determines whether we should reopen the file handle
174 // in readwrite mode and try again. This comes from util-linux/sys-utils/flock.c:
175 //
176 // Since Linux 3.4 (commit 55725513)
177 // Probably NFSv4 where flock() is emulated by fcntl().
178 func (f *Flock) reopenFDOnError(err error) (bool, error) {
179 if err != syscall.EIO && err != syscall.EBADF {
180 return false, nil
181 }
182 if st, err := f.fh.Stat(); err == nil {
183 // if the file is able to be read and written
184 if st.Mode()&0600 == 0600 {
185 f.fh.Close()
186 f.fh = nil
187
188 // reopen in read-write mode and set the filehandle
189 fh, err := os.OpenFile(f.path, os.O_CREATE|os.O_RDWR, os.FileMode(0600))
190 if err != nil {
191 return false, err
192 }
193 f.fh = fh
194 return true, nil
195 }
196 }
197
198 return false, nil
199 }