1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
8 * Synchronized byte range lock exerciser
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/tcp.h>
23 #include <sys/types.h>
29 #define HEX_2_ASC(x) ((x) > 9) ? (x)-10+'a' : (x)+'0'
30 #define FILE_SIZE 1024
31 #define PLATFORM_INIT() /*no-op*/
32 #define PLATFORM_CLEANUP() /*no-op*/
37 #define inet_aton(STRING, INADDRP) \
38 (((INADDRP)->s_addr = inet_addr(STRING)) == -1 ? 0 : 1)
40 /* this assumes 32 bit pointers */
41 #define PTR_TO_U64(P) ((unsigned __int64)(unsigned int)(P))
42 #define U64_TO_PTR(T,U) ((T)(void *)(unsigned int)(U))
44 #if __BYTE_ORDER == __LITTLE_ENDIAN
45 #define bswap_uint16(x) (uint16_t)bswap_16(x)
46 #define bswap_uint32(x) (uint32_t)bswap_32(x)
47 #define bswap_uint64(x) (uint64_t)bswap_64(x)
49 #define bswap_uint16(x) x
50 #define bswap_uint32(x) x
51 #define bswap_uint64(x) x
55 #define SOCKET_READ read
56 #define SOCKET_WRITE write
57 #define SOCKET_CLOSE(S) (close(S))
58 #define INVALID_SOCKET -1
63 #define INVALID_HANDLE -1
64 #define OPEN(N,F) (open(N, F|O_CREAT|O_BINARY, 0644))
65 #define SEEK(H, O) (lseek(H, O, SEEK_SET))
66 #define READ(H, B, L) (read(H, B, L))
67 #define WRITE(H, B, L) (write(H, B, L))
68 #define CLOSE(H) (close(H))
70 #define RAND() (rand())
71 #define SRAND(s) (srand(s))
72 #define SLEEP(s) (sleep(s))
74 #define MIN(A,B) (((A)<(B))?(A):(B))
75 #define MAX(A,B) (((A)>(B))?(A):(B))
77 #define ALLOC_ALIGNED(S) (memalign(65536, S))
78 #define FREE_ALIGNED(P) (free(P))
81 static char *filename = 0;
83 static int server = 1;
85 static int testnumber = -1;
86 static int saved_errno = 0;
88 static SOCKET s_fd = -1; /* listen socket */
89 static SOCKET c_fd = -1; /* IPC socket */
90 static HANDLE f_fd = INVALID_HANDLE; /* shared file */
112 #define FLAGS 2 /* index 2 is also used for do_open() flag, see below */
115 * flags for Mac OS X do_open()
132 * When adding tests be sure to add to both the descriptions AND tests array.
133 * Also, be sure to undo whatever is set for each test (eg unlock any locks)
134 * There is no need to have a matching client command for each server command
138 char *descriptions[] = {
139 /* 1 */"Add a lock to an empty lock list",
140 /* 2 */"Add a lock to the start and end of a list - no overlaps",
141 /* 3 */"Add a lock to the middle of a list - no overlap",
142 /* 4 */"Add different lock types to middle of the list - overlap exact match",
143 /* 5 */"Add new lock which completely overlaps any old lock in the list",
144 /* 6 */"Add new lock which itself is completely overlaped by any old lock in the list",
145 /* 7 */"Add new lock which starts before any old lock in the list",
146 /* 8 */"Add new lock which starts in the middle of any old lock in the list and ends after",
147 /* 9 */"Add different new lock types which completely overlaps any old lock in the list",
148 /* 10 */"Add different new locks which are completely overlaped by an old lock in the list",
149 /* 11 */"Add different new lock types which start before the old lock in the list",
150 /* 12 */"Add different new lock types which start in the middle of an old lock in the list and end after",
151 /* 13 */"Add new lock, differing types and processes, to middle of the list - exact overlap match",
152 /* 14 */"Add new lock, differing types and processes, which completely overlap any of the locks in the list",
153 /* 15 */"Add new lock, differing types and processes, which are completely overlaped by locks in the list",
154 /* 16 */"Add new lock, differing types and processes, which start before a lock in the list",
155 /* 17 */"Add new lock, differing types and processes, which starts in the middle of a lock, and ends after",
156 /* 18 */"Acquire write locks with overlapping ranges",
157 /* 19 */"Acquire write locks with non-overlapping ranges extending beyond EOF",
158 /* 20 */"Acquire write locks with overlapping ranges extending beyond EOF",
159 /* 21 */"Acquire write locks on whole files",
160 /* 22 */"Acquire write lock on whole file and range write lock",
161 /* 23 */"Acquire read locks with non-overlapping ranges",
162 /* 24 */"Acquire read locks with overlapping ranges",
163 /* 25 */"Acquire read and write locks with no overlapping ranges",
164 /* 26 */"Acquire read and write locks with overlapping ranges",
165 /* 27 */"Acquire whole file write lock and then close without unlocking (and attempt a lock)",
166 /* 28 */"Acquire two read locks, close and reopen the file, and test if the inital lock is still there",
167 /* 29 */"Verify that F_GETLK for F_WRLCK doesn't require that file be opened for write",
169 /* 30 */"Close the opened file and open the file with SHLOCK, other client will try to open with SHLOCK too",
170 /* 31 */"Close the opened file and open the file with SHLOCK, other client will try to open with EXLOCK",
171 /* 32 */"Close the opened file and open the file with EXLOCK, other client will try to open with EXLOCK too"
175 static int64_t tests[][6] =
176 /* test # Action [offset|flags] length expected server/client */
178 /* Various simple tests exercising the list */
180 /* SECTION 1: WRITE and UNLOCK with the same process (SERVER) */
181 /* Add a lock to an empty list */
182 {1, WRLOCK, 1, 10, PASS, SERVER },
183 {1, UNLOCK, 1, 10, PASS, SERVER },
185 /* Add a lock to the start and end of a list - 1, 13 - no overlap */
186 {2, WRLOCK, 10, 10, PASS, SERVER },
187 {2, WRLOCK, 30, 10, PASS, SERVER },
188 {2, WRLOCK, 50, 10, PASS, SERVER },
189 {2, WRLOCK, 1, 5, PASS, SERVER },
190 {2, WRLOCK, 70, 5, PASS, SERVER },
192 {2, UNLOCK, 10, 10, PASS, SERVER },
193 {2, UNLOCK, 30, 10, PASS, SERVER },
194 {2, UNLOCK, 50, 10, PASS, SERVER },
195 {2, UNLOCK, 1, 5, PASS, SERVER },
196 {2, UNLOCK, 70, 5, PASS, SERVER },
198 /* Add a lock to the middle of a list - no overlap */
199 {3, WRLOCK, 10, 10, PASS, SERVER },
200 {3, WRLOCK, 30, 10, PASS, SERVER },
201 {3, WRLOCK, 50, 10, PASS, SERVER },
202 {3, WRLOCK, 42, 5, PASS, SERVER },
204 {3, UNLOCK, 10, 10, PASS, SERVER },
205 {3, UNLOCK, 30, 10, PASS, SERVER },
206 {3, UNLOCK, 50, 10, PASS, SERVER },
207 {3, UNLOCK, 42, 5, PASS, SERVER },
209 /* Add different lock types to middle of the list - overlap exact match - 3 */
210 {4, WRLOCK, 10, 10, PASS, SERVER },
211 {4, WRLOCK, 30, 10, PASS, SERVER },
212 {4, WRLOCK, 50, 10, PASS, SERVER },
213 /* Exact match - same lock type */
214 {4, WRLOCK, 30, 10, PASS, SERVER },
215 /* Exact match - different lock type */
216 {4, RDLOCK, 30, 10, PASS, SERVER },
217 /* Exact match - unlock */
218 {4, UNLOCK, 30, 10, PASS, SERVER },
219 /* New lock - as above, inserting in the list again */
220 {4, WRLOCK, 30, 10, PASS, SERVER },
222 {4, UNLOCK, 10, 10, PASS, SERVER },
223 {4, UNLOCK, 30, 10, PASS, SERVER },
224 {4, UNLOCK, 50, 10, PASS, SERVER },
226 /* Add new lock which completely overlaps any old lock in the list - 4,5,6 */
227 {5, WRLOCK, 10, 10, PASS, SERVER },
228 {5, WRLOCK, 30, 10, PASS, SERVER },
229 {5, WRLOCK, 50, 10, PASS, SERVER },
230 /* The start is the same, end overlaps */
231 {5, WRLOCK, 30, 15, PASS, SERVER },
232 /* The start is before, end is the same */
233 {5, WRLOCK, 25, 20, PASS, SERVER },
234 /* Both start and end overlap */
235 {5, WRLOCK, 22, 26, PASS, SERVER },
237 {5, UNLOCK, 10, 10, PASS, SERVER },
238 {5, UNLOCK, 22, 26, PASS, SERVER },
239 {5, UNLOCK, 50, 10, PASS, SERVER },
241 /* Add new lock which itself is completely overlaped by any old lock in the list - 7,8,10 */
242 {6, WRLOCK, 10, 10, PASS, SERVER },
243 {6, WRLOCK, 30, 10, PASS, SERVER },
244 {6, WRLOCK, 50, 10, PASS, SERVER },
245 /* The start is the same, end is in the middle of old lock - NOP */
246 {6, WRLOCK, 30, 5, PASS, SERVER },
247 /* The start and end are in the middle of old lock - NOP */
248 {6, WRLOCK, 32, 6, PASS, SERVER },
249 /* Start in the middle and end is the same - NOP */
250 {6, WRLOCK, 32, 8, PASS, SERVER },
252 {6, UNLOCK, 10, 10, PASS, SERVER },
253 {6, UNLOCK, 30, 10, PASS, SERVER },
254 {6, UNLOCK, 32, 8, PASS, SERVER },
255 {6, UNLOCK, 50, 10, PASS, SERVER },
257 /* Add new lock which starts before any old lock in the list - 2,9 */
258 {7, WRLOCK, 10, 10, PASS, SERVER },
259 {7, WRLOCK, 30, 10, PASS, SERVER },
260 {7, WRLOCK, 50, 10, PASS, SERVER },
261 /* Here is the new lock */
262 {7, WRLOCK, 27, 10, PASS, SERVER },
263 /* Go again with the end of the new lock matching the start of old lock */
264 {7, WRLOCK, 25, 2, PASS, SERVER },
266 {7, UNLOCK, 10, 10, PASS, SERVER },
267 {7, UNLOCK, 25, 15, PASS, SERVER },
268 {7, UNLOCK, 50, 10, PASS, SERVER },
270 /* Add new lock which starts in the middle of any old lock in the list and ends after - 11,12 */
271 {8, WRLOCK, 10, 10, PASS, SERVER },
272 {8, WRLOCK, 30, 10, PASS, SERVER },
273 {8, WRLOCK, 50, 10, PASS, SERVER },
274 /* Here is the new lock */
275 {8, WRLOCK, 35, 10, PASS, SERVER },
276 /* Go again with the end of the new lock matching the start of old lock */
277 {8, WRLOCK, 45, 2, PASS, SERVER },
279 {8, UNLOCK, 10, 10, PASS, SERVER },
280 {8, UNLOCK, 30, 17, PASS, SERVER },
281 {8, UNLOCK, 50, 10, PASS, SERVER },
282 /* SECTION 2: Overlapping READ and WRITE and UNLOCK with the same process (SERVER) */
283 /* Add different new lock types which completely overlaps any old lock in the list - 4,5,6 */
284 {9, WRLOCK, 10, 10, PASS, SERVER },
285 {9, WRLOCK, 30, 10, PASS, SERVER },
286 {9, WRLOCK, 50, 10, PASS, SERVER },
287 /* The start is the same, end overlaps */
288 {9, RDLOCK, 30, 15, PASS, SERVER },
289 /* The start is before, end is the same */
290 {9, WRLOCK, 25, 20, PASS, SERVER },
291 /* Both start and end overlap */
292 {9, RDLOCK, 22, 26, PASS, SERVER },
294 {9, UNLOCK, 10, 10, PASS, SERVER },
295 {9, UNLOCK, 22, 26, PASS, SERVER },
296 {9, UNLOCK, 50, 10, PASS, SERVER },
298 /* Add different new locks which are completely overlaped by an old lock in the list - 7,8,10 */
299 {10, WRLOCK, 10, 10, PASS, SERVER },
300 {10, WRLOCK, 30, 10, PASS, SERVER },
301 {10, WRLOCK, 50, 10, PASS, SERVER },
302 /* The start is the same, end is in the middle of old lock */
303 {10, RDLOCK, 30, 5, PASS, SERVER },
304 /* The start and end are in the middle of a lock */
305 {10, WRLOCK, 32, 2, PASS, SERVER },
306 /* Start in the middle and end is the same */
307 {10, RDLOCK, 36, 5, PASS, SERVER },
309 {10, UNLOCK, 10, 10, PASS, SERVER },
310 {10, UNLOCK, 30, 11, PASS, SERVER },
311 {10, UNLOCK, 50, 10, PASS, SERVER },
313 /* Add different new lock types which start before the old lock in the list - 2,9 */
314 {11, WRLOCK, 10, 10, PASS, SERVER },
315 {11, WRLOCK, 30, 10, PASS, SERVER },
316 {11, WRLOCK, 50, 10, PASS, SERVER },
317 /* Here is the new lock */
318 {11, RDLOCK, 27, 10, PASS, SERVER },
319 /* Go again with the end of the new lock matching the start of lock */
320 {11, WRLOCK, 25, 3, PASS, SERVER },
322 {11, UNLOCK, 10, 10, PASS, SERVER },
323 {11, UNLOCK, 25, 15, PASS, SERVER },
324 {11, UNLOCK, 50, 10, PASS, SERVER },
326 /* Add different new lock types which start in the middle of an old lock in the list and end after - 11,12 */
327 {12, WRLOCK, 10, 10, PASS, SERVER },
328 {12, WRLOCK, 30, 10, PASS, SERVER },
329 {12, WRLOCK, 50, 10, PASS, SERVER },
330 /* Here is the new lock */
331 {12, RDLOCK, 35, 10, PASS, SERVER },
332 /* Go again with the end of the new lock matching the start of old lock */
333 {12, WRLOCK, 44, 3, PASS, SERVER },
335 {12, UNLOCK, 10, 10, PASS, SERVER },
336 {12, UNLOCK, 30, 18, PASS, SERVER },
337 {12, UNLOCK, 50, 10, PASS, SERVER },
339 /* SECTION 3: Overlapping READ and WRITE and UNLOCK with the different processes (CLIENT/SERVER) */
340 /* Add new lock, differing types and processes, to middle of the list - exact overlap match - 3 */
341 {13, WRLOCK, 10, 10, PASS, SERVER },
342 {13, WRLOCK, 30, 10, PASS, SERVER },
343 {13, RDLOCK, 50, 10, PASS, SERVER },
344 /* Same lock type, different process */
345 {13, WRLOCK, 30, 10, FAIL, CLIENT },
346 {13, RDLOCK, 50, 10, PASS, CLIENT },
347 /* Exact match - different lock type, different process */
348 {13, RDLOCK, 30, 10, FAIL, CLIENT },
349 /* Exact match - unlock */
350 {13, UNLOCK, 30, 10, PASS, CLIENT },
351 /* New lock - as above, inserting in the list again */
352 {13, UNLOCK, 30, 10, PASS, SERVER },
353 /* Exact match - same lock type, different process */
354 {13, WRLOCK, 30, 10, PASS, CLIENT },
356 {13, UNLOCK, 10, 10, PASS, SERVER },
357 {13, UNLOCK, 30, 10, PASS, CLIENT },
358 {13, UNLOCK, 50, 10, PASS, SERVER },
360 /* Add new lock, differing types and processes, which completely overlap any of the locks in the list - 4,5,6 */
361 {14, WRLOCK, 10, 10, PASS, SERVER },
362 {14, WRLOCK, 30, 10, PASS, SERVER },
363 {14, RDLOCK, 50, 10, PASS, SERVER },
364 /* The start is the same, end overlaps */
365 {14, RDLOCK, 30, 15, FAIL, CLIENT },
366 {14, WRLOCK, 30, 15, FAIL, CLIENT },
367 /* The start is before, end is the same */
368 {14, RDLOCK, 25, 20, FAIL, CLIENT },
369 {14, WRLOCK, 25, 20, FAIL, CLIENT },
370 /* Both start and end overlap */
371 {14, RDLOCK, 22, 26, FAIL, CLIENT },
372 {14, WRLOCK, 22, 26, FAIL, CLIENT },
374 /* The start is the same, end overlaps */
375 {14, RDLOCK, 50, 15, PASS, CLIENT },
376 {14, WRLOCK, 50, 17, FAIL, CLIENT },
377 /* The start is before, end is the same */
378 {14, RDLOCK, 45, 20, PASS, CLIENT },
379 {14, WRLOCK, 43, 22, FAIL, CLIENT },
380 /* Both start and end overlap */
381 {14, RDLOCK, 42, 26, PASS, CLIENT },
382 {14, WRLOCK, 41, 28, FAIL, CLIENT },
384 {14, UNLOCK, 10, 10, PASS, SERVER },
385 {14, UNLOCK, 22, 26, PASS, SERVER },
386 {14, UNLOCK, 42, 26, PASS, CLIENT },
388 /* Add new lock, differing types and processes, which are completely overlaped by an old lock in the list - 7,8,10 */
389 {15, WRLOCK, 10, 10, PASS, SERVER },
390 {15, RDLOCK, 30, 10, PASS, SERVER },
391 {15, WRLOCK, 50, 10, PASS, SERVER },
392 /* The start is the same, end is in the middle of old lock */
393 {15, RDLOCK, 50, 5, FAIL, CLIENT },
394 {15, WRLOCK, 50, 5, FAIL, CLIENT },
395 /* The start and end are in the middle of old lock */
396 {15, RDLOCK, 52, 6, FAIL, CLIENT },
397 {15, WRLOCK, 52, 6, FAIL, CLIENT },
398 /* Start in the middle and end is the same */
399 {15, RDLOCK, 52, 8, FAIL, CLIENT },
400 {15, WRLOCK, 52, 8, FAIL, CLIENT },
401 /* The start is the same, end is in the middle of old lock */
402 {15, RDLOCK, 30, 5, PASS, CLIENT },
403 {15, WRLOCK, 30, 5, FAIL, CLIENT },
404 /* The start and end are in the middle of old lock */
405 {15, RDLOCK, 32, 6, PASS, CLIENT },
406 {15, WRLOCK, 32, 6, FAIL, CLIENT },
407 /* Start in the middle and end is the same */
408 {15, RDLOCK, 32, 8, PASS, CLIENT },
409 {15, WRLOCK, 32, 8, FAIL, CLIENT },
411 {15, UNLOCK, 10, 10, PASS, SERVER },
412 {15, UNLOCK, 30, 10, PASS, SERVER },
413 {15, UNLOCK, 50, 10, PASS, SERVER },
414 /* Add new lock, differing types and processes, which start before a lock in the list - 2,9 */
415 {16, RDLOCK, 10, 10, PASS, SERVER },
416 {16, WRLOCK, 50, 10, PASS, SERVER },
417 /* Start is before, end is the start of the old lock in list */
418 {16, RDLOCK, 5, 6, PASS, CLIENT },
419 {16, WRLOCK, 5, 6, FAIL, CLIENT },
420 /* Start is before, end is in the middle of the old lock */
421 {16, RDLOCK, 5, 10, PASS, CLIENT },
422 {16, WRLOCK, 5, 10, FAIL, CLIENT },
423 /* Start is before, end is the start of the old lock in list */
424 {16, RDLOCK, 45, 6, FAIL, CLIENT },
425 {16, WRLOCK, 45, 6, FAIL, CLIENT },
426 /* Start is before, end is in the middle of the old lock */
427 {16, RDLOCK, 45, 10, FAIL, CLIENT },
428 {16, WRLOCK, 45, 10, FAIL, CLIENT },
430 {16, UNLOCK, 5, 15, PASS, CLIENT },
431 {16, UNLOCK, 30, 10, PASS, SERVER },
432 {16, UNLOCK, 50, 10, PASS, SERVER },
434 /* Add new lock, differing types and processes, which starts in the middle of a lock, and ends after - 11,12 */
435 {17, WRLOCK, 10, 10, PASS, SERVER },
436 {17, RDLOCK, 30, 10, PASS, SERVER },
437 {17, WRLOCK, 50, 10, PASS, SERVER },
438 /* Start in the middle, end after lock in list */
439 {17, WRLOCK, 35, 10, FAIL, CLIENT },
440 /* Start matches end of lock in list */
441 {17, RDLOCK, 35, 10, PASS, CLIENT },
442 {17, RDLOCK, 44, 2, PASS, CLIENT },
443 /* Start in the middle, end after lock in list */
444 {17, RDLOCK, 55, 10, FAIL, CLIENT },
445 {17, WRLOCK, 55, 10, FAIL, CLIENT },
446 /* Start matches end of lock in list */
447 {17, RDLOCK, 59, 5, FAIL, CLIENT },
448 {17, WRLOCK, 59, 5, FAIL, CLIENT },
450 {17, UNLOCK, 10, 10, PASS, SERVER },
451 {17, UNLOCK, 30, 16, PASS, CLIENT },
452 {17, UNLOCK, 50, 10, PASS, SERVER },
454 /* SECTION 4: overlapping and EOF tests */
455 /* Acquire overlapping ranges */
456 {18, WRLOCK, 11, 7, PASS, SERVER },
457 {18, WRLOCK, 13, 8, FAIL, CLIENT },
458 {18, UNLOCK, 11, 7, PASS, SERVER },
459 /* Acquire different ranges beyond EOF */
460 {19, WRLOCK, 10, FILE_SIZE, PASS, SERVER },
461 {19, WRLOCK, FILE_SIZE + 10, 10, PASS, CLIENT },
462 {19, UNLOCK, 10, FILE_SIZE, PASS, SERVER },
463 {19, UNLOCK, FILE_SIZE + 10, 10, PASS, CLIENT },
464 /* Acquire same range beyong EOF */
465 {20, WRLOCK, 10, FILE_SIZE, PASS, SERVER, },
466 {20, WRLOCK, 10, FILE_SIZE, FAIL, CLIENT, },
467 {20, UNLOCK, 10, FILE_SIZE, PASS, SERVER, },
468 /* Acquire whole file lock */
469 {21, WRLOCK, 0, 0, PASS, SERVER, },
470 {21, WRLOCK, 0, 0, FAIL, CLIENT, },
471 {21, UNLOCK, 0, 0, PASS, SERVER, },
472 /* Acquire whole file lock, then range */
473 {22, WRLOCK, 0, 0, PASS, SERVER, },
474 {22, WRLOCK, 1, 5, FAIL, CLIENT, },
475 {22, UNLOCK, 0, 0, PASS, SERVER, },
476 /* Acquire non-overlapping read locks */
477 {23, RDLOCK, 1, 5, PASS, SERVER, },
478 {23, RDLOCK, 7, 6, PASS, CLIENT, },
479 {23, UNLOCK, 1, 5, PASS, SERVER, },
480 {23, UNLOCK, 7, 6, PASS, CLIENT, },
481 /* Acquire overlapping read locks */
482 {24, RDLOCK, 1, 5, PASS, SERVER, },
483 {24, RDLOCK, 2, 6, PASS, CLIENT, },
484 {24, UNLOCK, 1, 5, PASS, SERVER, },
485 {24, UNLOCK, 1, 7, PASS, CLIENT, },
486 /* Acquire non-overlapping read and write locks */
487 {25, RDLOCK, 1, 5, PASS, SERVER, },
488 {25, WRLOCK, 7, 6, PASS, CLIENT, },
489 {25, UNLOCK, 1, 5, PASS, SERVER, },
490 {25, UNLOCK, 7, 6, PASS, CLIENT, },
491 /* Acquire overlapping read and write locks */
492 {26, RDLOCK, 1, 5, PASS, SERVER, },
493 {26, WRLOCK, 2, 6, FAIL, CLIENT, },
494 {26, UNLOCK, 1, 5, PASS, SERVER, },
495 /* Acquire whole file lock, then close (without unlocking) */
496 {27, WRLOCK, 0, 0, PASS, SERVER, },
497 {27, WRLOCK, 1, 5, FAIL, CLIENT, },
498 {27, F_CLOSE,0, 0, PASS, SERVER, },
499 {27, WRLOCK, 1, 5, PASS, CLIENT, },
500 {27, F_OPEN, O_RDWR, 0, PASS, SERVER, },
501 {27, UNLOCK, 1, 5, PASS, CLIENT, },
502 /* Acquire two read locks, close one file and then reopen to check that first lock still exists */
503 {28, RDLOCK, 1, 5, PASS, SERVER, },
504 {28, RDLOCK, 1, 5, PASS, CLIENT, },
505 {28, F_CLOSE,0, 0, PASS, SERVER, },
506 {28, F_OPEN, O_RDWR, 0, PASS, SERVER, },
507 {28, WRLOCK, 0, 0, FAIL, SERVER, },
508 {28, UNLOCK, 1, 5, PASS, SERVER, },
509 /* Verify that F_GETLK for F_WRLCK doesn't require that file be opened for write */
510 {29, F_CLOSE, 0, 0, PASS, SERVER, },
511 {29, F_OPEN, O_RDONLY, 0, PASS, SERVER, },
512 {29, WRTEST, 0, 0, PASS, SERVER, },
513 {29, F_CLOSE,0, 0, PASS, SERVER, },
514 {29, F_OPEN, O_RDWR, 0, PASS, SERVER, },
516 /* Close the opened file and open the file with SHLOCK, other client will try to open with SHLOCK too */
517 {30, F_CLOSE,0, 0, PASS, SERVER, },
518 {30, F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK, 0, PASS, SERVER, },
519 {30, F_CLOSE,0, 0, PASS, CLIENT, },
520 {30, F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK, 0, PASS, CLIENT, },
521 /* Close the opened file and open the file with SHLOCK, other client will try to open with EXLOCK */
522 {31, F_CLOSE,0, 0, PASS, SERVER, },
523 {31, F_CLOSE,0, 0, PASS, CLIENT, },
524 {31, F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK, 0, PASS, SERVER, },
525 {31, F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK, 0, FAIL, CLIENT, },
526 /* Close the opened file and open the file with EXLOCK, other client will try to open with EXLOCK too */
527 {32, F_CLOSE,0, 0, PASS, SERVER, },
528 {32, F_CLOSE,0, 0, FAIL, CLIENT, },
529 {32, F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK, 0, PASS, SERVER, },
530 {32, F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK, 0, FAIL, CLIENT, },
531 {32, F_CLOSE,0, 0, PASS, SERVER, },
532 {32, F_CLOSE,0, 0, FAIL, CLIENT, },
533 {32, F_OPEN, O_RDWR, 0, PASS, SERVER, },
534 {32, F_OPEN, O_RDWR, 0, PASS, CLIENT, },
536 /* indicate end of array */
549 int32_t padding; /* So mac and irix have the same size struct (bloody alignment) */
556 fprintf(stderr, "Usage: %s [options] sharedfile\n\
559 -p port TCP/IP port number for client-server communication\n\
560 -d enable debug tracing\n\
561 -n # test number to run\n\
562 -h host run as client and connect to server on remote host\n\
563 [default run as server]\n", prog);
567 #define INIT_BUFSZ 512
570 initialize(HANDLE fd)
576 int togo = FILE_SIZE;
578 ibuf = (char*)malloc(INIT_BUFSZ);
579 memset(ibuf, ':', INIT_BUFSZ);
584 j = togo > INIT_BUFSZ ? INIT_BUFSZ : togo;
586 if ((nwrite = WRITE(fd, ibuf, j)) != j) {
588 perror("initialize write:");
590 fprintf(stderr, "initialize: write() returns %d, not %d as expected\n",
600 int do_open(int flag)
602 if ((f_fd = OPEN(filename, flag)) == INVALID_HANDLE) {
603 perror("shared file create");
611 static int do_lock(int cmd, int type, int start, int length)
618 fprintf(stderr, "do_lock: cmd=%d type=%d start=%d, length=%d\n", cmd, type, start, length);
626 fl.l_whence = SEEK_SET;
632 ret = fcntl(filedes, cmd, &fl);
636 fprintf(stderr, "do_lock: ret = %d, errno = %d (%s)\n", ret, errno, strerror(errno));
638 return(ret==0?PASS:FAIL);
644 fprintf(stderr, "do_close\n");
649 f_fd = INVALID_HANDLE;
664 fprintf(stderr, "send_ctl: test=%d, command=%d offset=%"LL"d, length=%"LL"d, result=%d, error=%d\n",
665 ctl.test, ctl.command, (long long)ctl.offset, (long long)ctl.length,ctl.result, ctl.error);
668 ctl.test= bswap_uint32(ctl.test);
669 ctl.command = bswap_uint32(ctl.command);
670 ctl.offset = bswap_uint64(ctl.offset);
671 ctl.length = bswap_uint64(ctl.length);
672 ctl.result = bswap_uint32(ctl.result);
673 ctl.index= bswap_uint32(ctl.index);
674 ctl.error = bswap_uint32(ctl.error);
675 nwrite = SOCKET_WRITE(c_fd, (char*)&ctl, sizeof(ctl));
677 ctl.test= bswap_uint32(ctl.test);
678 ctl.command = bswap_uint32(ctl.command);
679 ctl.offset = bswap_uint64(ctl.offset);
680 ctl.length = bswap_uint64(ctl.length);
681 ctl.result = bswap_uint32(ctl.result);
682 ctl.index= bswap_uint32(ctl.index);
683 ctl.error= bswap_uint32(ctl.error);
684 if (nwrite != sizeof(ctl)) {
686 perror("send_ctl: write");
688 fprintf(stderr, "send_ctl[%d]: write() returns %d, not %zu as expected\n",
689 ctl.test, nwrite, sizeof(ctl));
699 if ((nread = SOCKET_READ(c_fd, (char*)&ctl, sizeof(ctl))) != sizeof(ctl)) {
701 perror("recv_ctl: read");
703 fprintf(stderr, "recv_ctl[%d]: read() returns %d, not %zu as expected\n",
704 ctl.test, nread, sizeof(ctl));
705 fprintf(stderr, "socket might has been closed by other locktest\n");
710 ctl.test= bswap_uint32(ctl.test);
711 ctl.command = bswap_uint32(ctl.command);
712 ctl.offset = bswap_uint64(ctl.offset);
713 ctl.length = bswap_uint64(ctl.length);
714 ctl.result = bswap_uint32(ctl.result);
715 ctl.index= bswap_uint32(ctl.index);
716 ctl.error= bswap_uint32(ctl.error);
719 fprintf(stderr, "recv_ctl: test=%d, command=%d offset=%"LL"d, length=%"LL"d, result=%d, error=%d\n",
720 ctl.test, ctl.command, (long long)ctl.offset, (long long)ctl.length, ctl.result, ctl.error);
740 main(int argc, char *argv[])
744 struct sockaddr_in myAddr;
745 struct linger noLinger = {1, 0};
758 /* trim command name of leading directory components */
760 for (p = prog; *p; p++) {
765 while ((c = getopt(argc, argv, "dn:h:p:?")) != EOF) {
768 case 'd': /* debug flag */
772 case 'h': /* (server) hostname */
778 testnumber = atoi(optarg);
781 case 'p': /* TCP/IP port */
782 port = (int)strtol(optarg, &endnum, 10);
783 if (*endnum != '\0') {
784 fprintf(stderr, "%s: -p argument must be a numeric\n",
798 if (errflag || optind != argc-1) {
803 filename=argv[optind];
804 if (do_open(O_RDWR) == FAIL)
807 setbuf(stderr, NULL);
812 s_fd = socket(AF_INET, SOCK_STREAM, 0);
813 if (s_fd == INVALID_SOCKET) {
818 if (setsockopt(s_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one)) < 0) {
819 perror("setsockopt(nodelay)");
823 if (setsockopt(s_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one))<0) {
824 perror("setsockopt(reuseaddr)");
829 if (setsockopt(s_fd, SOL_SOCKET, SO_REUSEPORT, (char*)&one, sizeof(one))<0) {
830 perror("setsockopt(reuseport)");
836 memset(&myAddr, 0, sizeof(myAddr));
837 myAddr.sin_family = AF_INET;
838 myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
839 myAddr.sin_port = htons((short)port);
840 sts = bind(s_fd, (struct sockaddr*)&myAddr, sizeof(myAddr));
847 sts = listen(s_fd, 5); /* Max. of 5 pending connection requests */
855 socklen_t addr_len = sizeof(myAddr);
857 if (getsockname(s_fd, &myAddr, &addr_len)) {
858 perror("getsockname");
862 port = ntohs(myAddr.sin_port);
865 printf("server port: %d\n", port);
868 c_fd = accept(s_fd, NULL, NULL);
869 if (c_fd == INVALID_SOCKET) {
875 if (debug) fprintf(stderr, "Client accepted\n");
879 struct hostent *servInfo;
881 if ((servInfo = gethostbyname(host)) == NULL) {
882 printf("Couldn't get hostbyname for %s", host);
883 if (h_errno == HOST_NOT_FOUND)
884 printf(": host not found");
890 c_fd = socket(AF_INET, SOCK_STREAM, 0);
891 if (c_fd == INVALID_SOCKET) {
896 /* avoid 200 ms delay */
897 if (setsockopt(c_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, sizeof(i)) < 0) {
898 perror("setsockopt(nodelay)");
902 /* Don't linger on close */
903 if (setsockopt(c_fd, SOL_SOCKET, SO_LINGER, (char *)&noLinger, sizeof(noLinger)) < 0) {
904 perror("setsockopt(nolinger)");
909 memset(&myAddr, 0, sizeof(myAddr)); /* Arrgh! &myAddr, not myAddr */
910 myAddr.sin_family = AF_INET;
911 memcpy(&myAddr.sin_addr, servInfo->h_addr, servInfo->h_length);
912 myAddr.sin_port = htons((short)port);
914 if (connect(c_fd, (struct sockaddr*)&myAddr, sizeof(myAddr)) < 0) {
915 perror("unable to connect");
916 fprintf(stderr, "Server might still initializing the shared file\n ");
921 if (debug) fprintf(stderr, "Connected to server\n");
926 /* only server need do shared file */
930 * TCP/IP connection to be established, safe to proceed.
932 * real work is in here ...
945 last_test = testnumber - 1;
946 while(tests[index][TEST_NUM] != testnumber && tests[index][TEST_NUM] != 0) {
950 /* If we have a server command, deal with it */
951 if(tests[index][WHO] == SERVER) {
953 fprintf(stderr, "Got a server command (%d)\n", index);
954 if(tests[index][TEST_NUM] == 0) {
958 memset(&ctl, 0, sizeof(ctl));
959 ctl.test = tests[index][TEST_NUM];
961 if(tests[index][TEST_NUM] != 0) {
962 switch(tests[index][COMMAND]) {
964 result = do_lock(F_SETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
967 result = do_lock(F_SETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
970 result = do_lock(F_SETLK, F_UNLCK, tests[index][OFFSET], tests[index][LENGTH]);
976 result = do_open(tests[index][FLAGS]);
979 result = do_lock(F_GETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
982 result = do_lock(F_GETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
985 if( result != tests[index][RESULT]) {
987 /* We have a failure */
989 fprintf(stderr, "Server failure in test %d, while %sing using offset %lld, length %lld - err = %d:%s\n",
990 ctl.test, tests[index][COMMAND]==WRLOCK?"write lock":
991 tests[index][COMMAND]==RDLOCK?"read lock":
992 tests[index][COMMAND]==UNLOCK?"unlock":
993 tests[index][COMMAND]==F_OPEN?"open":"clos",
994 (long long)tests[index][OFFSET],
995 (long long)tests[index][LENGTH],
996 saved_errno, strerror(saved_errno));
997 fprintf(stderr, "Server failure in %lld:%s\n",
998 (long long)tests[index][TEST_NUM],
999 descriptions[tests[index][TEST_NUM] - 1]);
1002 /* else send it off to the client */
1003 } else if (tests[index][WHO] == CLIENT) {
1004 if(tests[index][TEST_NUM] == 0) {
1009 fprintf(stderr, "Sending command to client (%d) - %s - %lld:%lld\n",
1010 index, tests[index][COMMAND]==WRLOCK?"write lock":
1011 tests[index][COMMAND]==RDLOCK?"read lock":
1012 tests[index][COMMAND]==UNLOCK?"unlock":
1013 tests[index][COMMAND]==F_OPEN?"open":"clos",
1014 (long long)tests[index][OFFSET],
1015 (long long)tests[index][LENGTH]);
1016 /* get the client to do something */
1020 /* Get the clients response */
1022 /* this is the whether the test passed or failed,
1023 * not what the command returned */
1024 if( ctl.result == FAIL ) {
1027 fprintf(stderr, "Client failure in test %d, while %sing using offset %lld, length %lld - err = %d:%s\n",
1028 ctl.test, ctl.command==WRLOCK?"write lock":
1029 ctl.command==RDLOCK?"read lock":
1030 ctl.command==UNLOCK?"unlock":
1031 ctl.command==F_OPEN?"open":"clos",
1032 (long long)ctl.offset, (long long)ctl.length,
1033 ctl.error, strerror(ctl.error));
1034 fprintf(stderr, "Client failure in %lld:%s\n",
1035 (long long)tests[index][TEST_NUM],
1036 descriptions[tests[index][TEST_NUM] - 1]);
1041 fprintf(stderr, "server sleeping ...\n");
1044 if(tests[index][TEST_NUM] != 0) {
1045 if(last_test != tests[index][TEST_NUM]) {
1052 last_test = tests[index][TEST_NUM];
1056 } else { /* CLIENT */
1058 fprintf(stderr,"client: waiting...\n");
1059 /* wait for the server to do something */
1062 /* check for a client command */
1064 if (tests[index][WHO] != CLIENT) {
1065 fprintf(stderr, "not a client command index (%d)\n", index);
1075 ctl.command = tests[index][COMMAND];
1076 ctl.offset = tests[index][OFFSET];
1077 ctl.length = tests[index][LENGTH];
1078 switch(tests[index][COMMAND]) {
1080 result = do_lock(F_SETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
1083 result = do_lock(F_SETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
1086 result = do_lock(F_SETLK, F_UNLCK, tests[index][OFFSET], tests[index][LENGTH]);
1089 result = do_close();
1092 result = do_open(tests[index][FLAGS]);
1095 result = do_lock(F_GETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
1098 result = do_lock(F_GETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
1101 if( result != tests[index][RESULT] ) {
1103 fprintf(stderr,"Got %d, wanted %lld\n", result,
1104 (long long)tests[index][RESULT]);
1106 ctl.error = saved_errno;
1112 fprintf(stderr,"client: sending result to server (%d)\n", ctl.index);
1113 /* Send result to the server */
1115 if(tests[index][TEST_NUM] != 0) {
1116 if(last_test != tests[index][TEST_NUM])
1118 last_test = tests[index][TEST_NUM];
1123 printf("%d tests run, %d failed\n", test_count, fail_count);