-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlockdev.c
174 lines (149 loc) · 3.73 KB
/
lockdev.c
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
/* Copyright 2005,2009,2018 Alain Knaff.
* This file is part of mtools.
*
* Mtools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Mtools 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mtools. If not, see <http://www.gnu.org/licenses/>.
*
* Create an advisory lock on the device to prevent concurrent writes.
* Uses either lockf, flock, or fcntl locking methods. See the Makefile
* and the Configure files for how to specify the proper method.
*/
#include "sysincludes.h"
#include "mtools.h"
#include "lockdev.h"
#if (defined HAVE_SIGACTION && defined HAVE_ALARM)
# define ALRM
#endif
#if (defined(HAVE_FLOCK) && defined (LOCK_EX) && (defined(LOCK_NB) || defined(ALRM)))
# ifdef ALRM
# define USE_FLOCK_W
# else
# define USE_FLOCK
# endif
#else /* FLOCK */
#if (defined(HAVE_LOCKF) && (defined(F_TLOCK) || defined(ALRM)))
# ifdef ALRM
# define USE_LOCKF_W
# else
# define USE_LOCKF
# endif
#else /* LOCKF */
#if (defined(F_SETLK) && defined(F_WRLCK))
# if (defined ALRM && defined F_SETLKW)
# define USE_SETLK_W
# else
# define USE_SETLK_W
# endif
#else
#endif /* FCNTL */
#endif /* LOCKF */
#endif /* FLOCK */
#if defined(USE_FLOCK_W) || defined(USE_LOCKF_W) || defined (USE_SETLK_W)
static void alrm(int a UNUSEDP) {
}
#endif
int lock_dev(int fd, int mode, struct device *dev)
{
unsigned int retries = 0;
if(IS_NOLOCK(dev))
return 0;
while(1) {
int ret=0;
#if defined(USE_FLOCK_W) || defined(USE_LOCKF_W) || defined (USE_SETLK_W)
struct sigaction alrm_action, old_alrm_action;
int old_alrm = alarm(0);
memset(&alrm_action, 0, sizeof(alrm_action));
alrm_action.sa_handler = alrm;
alrm_action.sa_flags = 0;
sigaction(SIGALRM, &alrm_action, &old_alrm_action);
alarm(mtools_lock_timeout);
#endif
#ifdef USE_FLOCK
ret = flock(fd, (mode ? LOCK_EX : LOCK_SH)|LOCK_NB);
#endif
#ifdef USE_FLOCK_W
ret = flock(fd, (mode ? LOCK_EX : LOCK_SH));
#endif
#if (defined(USE_LOCKF) || defined(USE_LOCKF_W))
if(mode)
# ifdef USE_LOCKF
ret = lockf(fd, F_TLOCK, 0);
# else
ret = lockf(fd, F_LOCK, 0);
# endif
else
ret = 0;
#endif
#if (defined(USE_SETLK) || defined(USE_SETLK_W))
{
struct flock flk;
flk.l_type = mode ? F_WRLCK : F_RDLCK;
flk.l_whence = 0;
flk.l_start = 0L;
flk.l_len = 0L;
# ifdef USE_SETLK_W
ret = fcntl(fd, F_SETLKW, &flk);
# else
ret = fcntl(fd, F_SETLK, &flk);
# endif
}
#endif
#if defined(USE_FLOCK_W) || defined(USE_LOCKF_W) || defined (USE_SETLK_W)
/* Cancel the alarm */
sigaction(SIGALRM, &old_alrm_action, NULL);
alarm(old_alrm);
#endif
if(ret < 0) {
#if defined(USE_FLOCK_W) || defined(USE_LOCKF_W) || defined (USE_SETLK_W)
/* ALARM fired ==> this means we are still locked */
if(errno == EINTR) {
return 1;
}
#endif
if(
#ifdef EWOULDBLOCK
(errno != EWOULDBLOCK)
#else
1
#endif
&&
#ifdef EAGAIN
(errno != EAGAIN)
#else
1
#endif
&&
#ifdef EINTR
(errno != EINTR)
#else
1
#endif
) {
/* Error other than simply being locked */
return -1;
}
/* Locked ==> continue until timeout */
} else /* no error => we got the lock! */
return 0;
#ifdef HAVE_USLEEP
if(retries++ < mtools_lock_timeout * 10)
usleep(100000);
#else
if(retries++ < mtools_lock_timeout)
sleep(1);
#endif
else
/* waited for too long => give up */
return 1;
}
}