forked from slowfranklin/lockchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockchart.c
388 lines (337 loc) · 7.29 KB
/
lockchart.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright 2012, 2015 EditShare LLC
Copyright 2012, 2015 Ralph Boehme <slow@samba.org>
This program 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.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <err.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdlib.h>
static int verbose = 0;
static char results[9][9];
enum mode {
mread = 0,
mwrite,
mread_write,
n_modes
};
enum lock {
lexclusive = 0,
lshared,
lnone,
n_locks
};
enum locktype {
tsharemode = 0,
tflock,
tfcntl,
n_locktypes
};
int modes[] = {
[mread] = O_RDONLY,
[mwrite] = O_WRONLY,
[mread_write] = O_RDWR
};
int locks[] = {
[lnone] = 0,
[lshared] = O_SHLOCK,
[lexclusive] = O_EXLOCK
};
int flocks[] = {
[lnone] = 0,
[lshared] = LOCK_SH,
[lexclusive] = LOCK_EX
};
int fcntls[] = {
[lnone] = 0,
[lshared] = F_RDLCK,
[lexclusive] = F_WRLCK
};
char *mode_names[] = {
[mread] = "read only",
[mwrite] = "write only",
[mread_write] = "read/write"
};
char *lock_names[] = {
[lnone] = "no",
[lshared] = "shared",
[lexclusive] = "exclusive"
};
char *locktype_names[] = {
[tsharemode] = "sharemode",
[tflock] = "flock",
[tfcntl] = "fcntl"
};
char *tableNames[] = {
"exclusive R ",
" W ",
" RW ",
"shared R ",
" W ",
" RW ",
"none R ",
" W ",
" RW "
};
static void usage(const char *myname)
{
printf("usage: %s [-f] PATH1 PATH2\n"
" -f use ressource fork, default is data fork\n"
" -v verbose, print error information\n"
" -vv extra verbose, print all open calls\n"
" -(s|l|c)(s|l|c)\n"
" Use sharemode (lock on open), flock, or fcntl,\n"
" for lock on first and second open respectively,\n"
" defaulting to sharemode.\n",
myname);
exit(1);
}
bool lock(int fd, enum lock lock, enum locktype locktype)
{
int result;
int flags;
if (lock == lnone) {
return true;
}
switch (locktype) {
case tsharemode:
/*
* This is done upon open.
*/
break;
case tflock:
flags = flocks[lock] | LOCK_NB;
result = flock(fd, flags);
if (result < 0) {
if (verbose) {
printf("flock(%d, %02x)\n", fd, flags);
}
return false;
} else {
if (verbose > 1) {
printf("flock(%d, %02x)\n", fd, flags);
}
}
break;
case tfcntl: {
struct flock flock_lock = {
.l_start = 0,
.l_len = 0,
.l_whence = SEEK_SET,
.l_type = fcntls[lock]
};
result = fcntl(fd, F_SETLK, &flock_lock);
if (result < 0) {
if (verbose) {
printf("fcntl(%d, F_SETLK, {.l_type = %02x})\n",
fd, fcntls[lock]);
}
return false;
} else {
if (verbose > 1) {
printf("fcntl(%d, F_SETLK, {.l_type = %02x})\n",
fd, fcntls[lock]);
}
}
break;
}
default:
break;
}
return true;
}
bool test(char *path1, int flags1, enum lock lock1, enum locktype locktype1,
char *path2, int flags2, enum lock lock2, enum locktype locktype2)
{
int result;
int fd1, fd2;
bool ok;
int pid;
int status;
/*
* Sharemodes need to be applied in open arguments before
* opening, rather than to fd afterwards
*/
if (locktype1 == tsharemode) {
flags1 |= locks[lock1];
}
if (locktype2 == tsharemode) {
flags2 |= locks[lock2];
}
fd1 = open(path1, flags1 | O_NONBLOCK);
if (fd1 < 0) {
if (verbose) {
printf("outer open(%s, %02x)\n", path1, flags1);
}
return false;
} else {
if (verbose > 1) {
printf("outer open(%s, %02x)\n", path1, flags1);
}
}
ok = lock(fd1, lock1, locktype1);
if (!ok) {
return false;
}
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
fd2 = open(path2, flags2 | O_NONBLOCK);
if (fd2 < 0) {
if (verbose) {
printf("inner open(%s, %02x)\n",
path2, flags2);
}
exit(1);
} else {
if (verbose > 1) {
printf("inner open(%s, %02x)\n",
path2, flags2);
}
}
ok = lock(fd2, lock2, locktype2);
if (!ok) {
exit(1);
}
exit(0);
}
waitpid(pid, &status, 0);
result = close(fd1);
if (result != 0) {
perror("close");
exit(1);
}
if (WIFEXITED(status)) {
return !WEXITSTATUS(status);
} else {
return false;
}
}
int main(int argc, char **argv) {
bool resource = false;
char *path1, *path2, *myname = argv[0];
bool ok;
int result;
char ch;
char str1[36], str2[36];
int nlockargs = 0;
enum locktype locktypes[2] = {tsharemode, tsharemode};
char *p;
while ((ch = getopt(argc, argv, "fvslc")) != -1) {
switch (ch) {
case 'f':
resource = true;
break;
case 'v':
++verbose;
break;
case 's':
case 'l':
case 'c':
if (nlockargs >= 2) {
usage(myname);
}
switch (ch) {
case 's':
break;
case 'l':
locktypes[nlockargs] = tflock;
break;
case 'c':
locktypes[nlockargs] = tfcntl;
break;
default:
usage(myname);
}
++nlockargs;
break;
case '?':
case 'h':
default:
usage(myname);
}
}
argc -= optind;
argv += optind;
if (argc < 2) {
usage(myname);
}
if (resource) {
result = asprintf(&path1, "%s/..namedfork/rsrc", argv[0]);
if (result == -1) {
printf("asprintf %s\n", argv[0]);
exit(1);
}
result = asprintf(&path2, "%s/..namedfork/rsrc", argv[1]);
if (result == -1) {
printf("asprintf %s\n", argv[1]);
exit(1);
}
} else {
path1 = argv[0];
path2 = argv[1];
}
if (verbose) {
printf(" %-32s %-32s\n", path1, path2);
}
p = &results[0][0];
for (enum lock lock1 = 0; lock1 < n_locks; ++lock1) {
for (enum mode mode1 = 0; mode1 < n_modes; ++mode1) {
for (enum lock lock2 = 0; lock2 < n_locks; ++lock2) {
for (enum mode mode2 = 0; mode2 < n_modes; ++mode2) {
ok = test(path1, modes[mode1], lock1, locktypes[0],
path2, modes[mode2], lock2, locktypes[1]);
if (verbose) {
snprintf(str1, sizeof(str1), "%s with %s %s",
mode_names[mode1],
lock_names[lock1],
locktype_names[locktypes[0]]);
snprintf(str2, sizeof(str2), "%s with %s %s",
mode_names[mode2],
lock_names[lock2],
locktype_names[locktypes[1]]);
printf("%s : %-36s %-36s \n",
ok ? " ok " : "fail",
str1, str2);
}
*p++ = ok ? '.' : 'x';
}
}
}
}
printf(" \\ %-10s\n"
"%-10s \\ Attempted mode\n"
"Current mode \\ exclusive | shared | none\n"
" R W RW | R W RW | R W RW\n",
locktype_names[locktypes[1]],
locktype_names[locktypes[0]]);
for (int i = 0; i < 9; i++) {
if (i == 3 || i == 6) {
printf("----------\n");
}
printf("%s", tableNames[i]);
for (int j = 0; j < 9; j++) {
printf("%s%c ",
(j == 3 || j == 6) ? " " : "",
results[i][j]);
}
printf("\n");
}
return 0;
}