-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocktest.c
executable file
·329 lines (282 loc) · 6.12 KB
/
locktest.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/file.h>
#define DEFAULT_DELAY 10 /* sleep time if not specified */
#define FILE_PERMS 0644 /* perms assigned if file created */
#define LOCKFN_FLOCK "flock"
#define LOCKFN_LOCKF "lockf"
#define LOCKFN_FCNTL "fcntl"
/* exit codes */
enum {
EXIT_OK, /* all went well */
EXIT_SYNTAX, /* command syntax error */
EXIT_OPEN, /* error opening the file */
EXIT_LOCK /* error acquiring or releasing the lock */
};
/* a pointer to a lock function */
int (*lockfn_t)(int fd);
/* a lock/unlock function pair */
struct lockfn_set_t {
int (*lock)(int fd);
int (*unlock)(int fd);
};
/* program configuration */
struct config_t {
const char *name;
const char *file;
unsigned int delay;
struct lockfn_set_t *fn;
};
/* prototypes */
struct config_t *configuration();
void show_usage(const char *prog);
int open_file(struct config_t *config);
int flock_lock(int fd);
int flock_unlock(int fd);
int lockf_lock(int fd);
int lockf_unlock(int fd);
int fcntl_lock(int fd);
int fcntl_unlock(int fd);
void init_lock(struct flock *fl);
/* function pair for 'flock' locking */
static struct lockfn_set_t flock_set = { flock_lock, flock_unlock };
/* function pair for 'lockf' locking */
static struct lockfn_set_t lockf_set = { lockf_lock, lockf_unlock };
/* function pair for 'fcntl' locking */
static struct lockfn_set_t fcntl_set = { fcntl_lock, fcntl_unlock };
/**
* Main entry point
*/
int
main(int argc, char *argv[])
{
int fd;
struct config_t *config;
config = configuration(argc, argv);
if (config == NULL) {
return EXIT_SYNTAX;
}
printf("opening file %s\n", config->file);
fd = open_file(config);
if (fd < 0) {
fputs("failed to open file\n", stderr);
return EXIT_OPEN;
}
printf("using %s to acquire lock\n", config->name);
if (config->fn->lock(fd) < 0) {
fputs("failed to acquire lock\n", stderr);
return EXIT_LOCK;
}
puts("lock acquired");
printf("sleeping for %d seconds\n", config->delay);
sleep(config->delay);
if (config->fn->unlock(fd) < 0) {
fputs("failed to release lock\n", stderr);
return EXIT_LOCK;
}
puts("lock released");
close(fd);
return EXIT_OK;
}
/**
* Fills in a program configuration structure from command line args.
* return: pointer to a static struct
*/
struct config_t *
configuration(int argc, char *argv[])
{
const char *prog;
int opt;
static struct config_t config;
prog = argv[0];
argc--;
argv++;
if (argc < 2 || argc > 3) {
show_usage(prog);
return NULL;
}
if (strcmp(LOCKFN_FLOCK, argv[0]) == 0) {
config.fn = &flock_set;
}
else if (strcmp(LOCKFN_LOCKF, argv[0]) == 0) {
config.fn = &lockf_set;
}
else if (strcmp(LOCKFN_FCNTL, argv[0]) == 0) {
config.fn = &fcntl_set;
}
else {
show_usage(prog);
return NULL;
}
config.name = argv[0];
config.file = argv[1];
if (argc == 3) {
config.delay = strtol(argv[2], NULL, 10);
if (config.delay == 0) {
show_usage(prog);
return NULL;
}
}
else {
config.delay = DEFAULT_DELAY;
}
return &config;
}
/**
* Show program usage instructions.
*/
void
show_usage(const char *prog)
{
fprintf(stderr, "usage: %s lock-fn filename [duration]\n", prog);
fprintf(stderr, "where 'lock-fn' is one of %s, %s, or %s\n",
LOCKFN_FLOCK, LOCKFN_LOCKF, LOCKFN_FCNTL);
fprintf(stderr, "and 'duration' is the delay (seconds) after "
"acquiring the lock (default %d)\n", DEFAULT_DELAY);
}
/**
* Opens the file specified by the configuration, creating if necessary.
* return: file descriptor or -1 if an error occurred
*/
int
open_file(struct config_t *config)
{
int fd;
fd = open(config->file, O_WRONLY | O_CREAT, FILE_PERMS);
if (fd < 0) {
perror("open");
return -1;
}
return fd;
}
/**
* Locks a file using flock(2)
* param: fd descriptor of the file to lock
* return: return code from flock
*/
int
flock_lock(int fd)
{
int rc;
rc = flock(fd, LOCK_EX | LOCK_NB);
if (rc < 0 || errno == EWOULDBLOCK) {
puts("waiting for lock");
rc = flock(fd, LOCK_EX);
}
if (rc < 0) {
perror("flock");
}
return rc;
}
/**
* Unlocks a file using flock(2)
* param: fd descriptor of the file to lock
* return: return code from flock
*/
int
flock_unlock(int fd)
{
int rc;
rc = flock(fd, LOCK_UN);
if (rc < 0) {
perror("flock");
}
return rc;
}
/**
* Locks a file using lockf(3)
* param: fd descriptor of the file to lock
* return: return code from lockf
*/
int
lockf_lock(int fd)
{
int rc;
if (lseek(fd, 0, SEEK_SET) < 0) {
perror("lseek");
return -1;
}
rc = lockf(fd, F_TLOCK, 0);
if (rc < 0 && (errno == EACCES || errno == EAGAIN)) {
rc = lockf(fd, F_LOCK, 0);
}
if (rc < 0) {
perror("lockf");
}
return rc;
}
/**
* Unlocks a file using lockf(3)
* param: fd descriptor of the file to lock
* return: return code from lockf
*/
int
lockf_unlock(int fd)
{
int rc;
if (lseek(fd, 0, SEEK_SET) < 0) {
perror("lseek");
return -1;
}
rc = lockf(fd, F_ULOCK, 0);
if (rc < 0) {
perror("lockf");
}
return rc;
}
/**
* Locks a file using fcntl(2)
* param: fd descriptor of the file to lock
* return: return code from fcntl
*/
int
fcntl_lock(int fd)
{
int rc;
struct flock fl;
init_lock(&fl);
rc = fcntl(fd, F_SETLK, &fl);
if (rc < 0 && (errno == EACCES || errno == EAGAIN)) {
puts("waiting for lock");
rc = fcntl(fd, F_SETLKW, &fl);
}
if (rc < 0) {
perror("fcntl");
}
return rc;
}
/**
* Unlocks a file using fcntl(2)
* param: fd descriptor of the file to lock
* return: return code from fcntl
*/
int
fcntl_unlock(int fd)
{
int rc;
struct flock fl;
init_lock(&fl);
rc = fcntl(fd, F_UNLCK, &fl);
if (rc < 0) {
perror("fcntl");
}
return rc;
}
/**
* Initializes a lock structure used by fcntl
* param: fl pointer to subject lock structure
*/
void
init_lock(struct flock *fl)
{
fl->l_type = F_WRLCK;
fl->l_whence = SEEK_SET;
fl->l_start = 0;
fl->l_len = 0;
fl->l_pid = getpid();
}