8e27e922668ee243ffe971e5c6208c49048f5acf
[xfstests-dev.git] / src / locktest.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 /*
8  * Synchronized byte range lock exerciser
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <malloc.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/tcp.h>
21
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <netdb.h>
25 #include <endian.h>
26 #include <byteswap.h>
27 #include <errno.h>
28 #include <string.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*/
33 #define LL                  "ll"
34
35 extern int h_errno;
36
37 #define inet_aton(STRING, INADDRP) \
38     (((INADDRP)->s_addr = inet_addr(STRING)) == -1 ? 0 : 1)
39
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))
43
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)
48 #else
49 #define bswap_uint16(x)         x
50 #define bswap_uint32(x)         x
51 #define bswap_uint64(x)         x
52 #endif
53
54 #define SOCKET              int
55 #define SOCKET_READ         read
56 #define SOCKET_WRITE        write
57 #define SOCKET_CLOSE(S)     (close(S))
58 #define INVALID_SOCKET      -1
59
60 #define O_BINARY            0
61        
62 #define HANDLE              int
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))
69
70 #define RAND()              (rand())
71 #define SRAND(s)            (srand(s))
72 #define SLEEP(s)            (sleep(s))
73
74 #define MIN(A,B)            (((A)<(B))?(A):(B))
75 #define MAX(A,B)            (((A)>(B))?(A):(B))
76
77 #define ALLOC_ALIGNED(S)    (memalign(65536, S)) 
78 #define FREE_ALIGNED(P)     (free(P)) 
79
80 static char     *prog;
81 static char     *filename = 0;
82 static int      debug = 0;
83 static int      server = 1;
84 static int      port = 0;
85 static int      testnumber = -1;
86 static int      saved_errno = 0;
87
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      */
91
92 #define         WRLOCK  0
93 #define         RDLOCK  1
94 #define         UNLOCK  2
95 #define         F_CLOSE 3
96 #define         F_OPEN  4
97 #define         WRTEST  5
98 #define         RDTEST  6
99
100 #define         PASS    1
101 #define         FAIL    0
102
103 #define         SERVER  0
104 #define         CLIENT  1
105
106 #define         TEST_NUM        0
107 #define         COMMAND         1
108 #define         OFFSET          2
109 #define         LENGTH          3
110 #define         RESULT          4
111 #define         WHO             5
112 #define         FLAGS           2 /* index 2 is also used for do_open() flag, see below */
113
114 /* 
115  * flags for Mac OS X do_open() 
116  * O_RDONLY     0x0000
117  * O_WRONLY     0x0001  
118  * O_RDWR       0x0002
119  * O_NONBLOCK   0x0004  
120  * O_APPEND     0x0008
121  * O_SHLOCK     0x0010  
122  * O_EXLOCK     0x0020
123  * O_ASYNC      0x0040  
124  * O_FSYNC      0x0080
125  * O_NOFOLLOW   0x0100  
126  * O_CREAT      0x0200
127  * O_TRUNC      0x0400
128  * O_EXCL       0x0800
129  */
130
131 /*
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
135  * (or vice versa)
136  */
137
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",
168     #if defined(macosx)
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"
172     #endif
173 };
174
175 static int64_t tests[][6] =
176         /*      test #  Action  [offset|flags]  length          expected        server/client */
177         {       
178         /* Various simple tests exercising the list */
179
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  }, 
184                 
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  }, 
191                 
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  }, 
197                 
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  }, 
203                 
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  }, 
208                 
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  }, 
221                 
222                 {4,     UNLOCK, 10,             10,             PASS,           SERVER  }, 
223                 {4,     UNLOCK, 30,             10,             PASS,           SERVER  }, 
224                 {4,     UNLOCK, 50,             10,             PASS,           SERVER  }, 
225                 
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  }, 
236                 
237                 {5,     UNLOCK, 10,             10,             PASS,           SERVER  }, 
238                 {5,     UNLOCK, 22,             26,             PASS,           SERVER  }, 
239                 {5,     UNLOCK, 50,             10,             PASS,           SERVER  }, 
240                 
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  }, 
251                 
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  }, 
256                 
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  }, 
265                 
266                 {7,     UNLOCK, 10,             10,             PASS,           SERVER  }, 
267                 {7,     UNLOCK, 25,             15,             PASS,           SERVER  }, 
268                 {7,     UNLOCK, 50,             10,             PASS,           SERVER  }, 
269         
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  }, 
278                 
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  }, 
293                 
294                 {9,     UNLOCK, 10,             10,             PASS,           SERVER  }, 
295                 {9,     UNLOCK, 22,             26,             PASS,           SERVER  }, 
296                 {9,     UNLOCK, 50,             10,             PASS,           SERVER  }, 
297                 
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  }, 
308                 
309                 {10,    UNLOCK, 10,             10,             PASS,           SERVER  }, 
310                 {10,    UNLOCK, 30,             11,             PASS,           SERVER  }, 
311                 {10,    UNLOCK, 50,             10,             PASS,           SERVER  }, 
312                 
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  }, 
321                 
322                 {11,    UNLOCK, 10,             10,             PASS,           SERVER  }, 
323                 {11,    UNLOCK, 25,             15,             PASS,           SERVER  }, 
324                 {11,    UNLOCK, 50,             10,             PASS,           SERVER  }, 
325         
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  }, 
334                 
335                 {12,    UNLOCK, 10,             10,             PASS,           SERVER  }, 
336                 {12,    UNLOCK, 30,             18,             PASS,           SERVER  }, 
337                 {12,    UNLOCK, 50,             10,             PASS,           SERVER  }, 
338
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  }, 
355                 
356                 {13,    UNLOCK, 10,             10,             PASS,           SERVER  }, 
357                 {13,    UNLOCK, 30,             10,             PASS,           CLIENT  }, 
358                 {13,    UNLOCK, 50,             10,             PASS,           SERVER  }, 
359                 
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  }, 
373                 
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  }, 
383                 
384                 {14,    UNLOCK, 10,             10,             PASS,           SERVER  }, 
385                 {14,    UNLOCK, 22,             26,             PASS,           SERVER  }, 
386                 {14,    UNLOCK, 42,             26,             PASS,           CLIENT  }, 
387
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  }, 
410                 
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  }, 
429                 
430                 {16,    UNLOCK, 5,              15,             PASS,           CLIENT  }, 
431                 {16,    UNLOCK, 30,             10,             PASS,           SERVER  }, 
432                 {16,    UNLOCK, 50,             10,             PASS,           SERVER  }, 
433
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  }, 
449                 
450                 {17,    UNLOCK, 10,             10,             PASS,           SERVER  }, 
451                 {17,    UNLOCK, 30,             16,             PASS,           CLIENT  }, 
452                 {17,    UNLOCK, 50,             10,             PASS,           SERVER  }, 
453
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, },
515 #ifdef macosx
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, },
535 #endif /* macosx */
536         /* indicate end of array */
537                 {0,0,0,0,0,SERVER},
538                 {0,0,0,0,0,CLIENT}
539         };
540
541 static struct {
542     int32_t             test;
543     int32_t             command;
544     int64_t             offset;
545     int64_t             length;
546     int32_t             result;
547     int32_t             index;
548     int32_t             error;
549     int32_t             padding; /* So mac and irix have the same size struct (bloody alignment) */
550 } ctl;
551
552
553 void
554 usage(void)
555 {
556     fprintf(stderr, "Usage: %s [options] sharedfile\n\
557 \n\
558 options:\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);
564     exit(1);
565 }
566
567 #define INIT_BUFSZ 512 
568
569 void
570 initialize(HANDLE fd)
571 {
572     char*       ibuf;
573     int         j=0;
574     int         nwrite;
575     int         offset = 0;
576     int         togo = FILE_SIZE;
577
578     ibuf = (char*)malloc(INIT_BUFSZ);
579     memset(ibuf, ':', INIT_BUFSZ);
580
581     SEEK(fd, 0L);
582     while (togo) {
583         offset+=j;
584         j = togo > INIT_BUFSZ ? INIT_BUFSZ : togo;
585
586         if ((nwrite = WRITE(fd, ibuf, j)) != j) {
587             if (nwrite < 0)
588                 perror("initialize write:");
589             else
590                 fprintf(stderr, "initialize: write() returns %d, not %d as expected\n", 
591                         nwrite, j);
592             exit(1);
593             /*NOTREACHED*/
594         }
595         togo -= j;
596     }
597 }
598
599
600 int do_open(int flag)
601 {
602     if ((f_fd = OPEN(filename, flag)) == INVALID_HANDLE) {
603         perror("shared file create");
604         return FAIL;
605         /*NOTREACHED*/
606     }
607
608     return PASS;
609 }
610
611 static int do_lock(int cmd, int type, int start, int length)
612 {
613     int ret;
614     int filedes = f_fd;
615     struct flock fl;
616
617     if(debug > 1) {
618         fprintf(stderr, "do_lock: cmd=%d type=%d start=%d, length=%d\n", cmd, type, start, length);
619     }
620
621     if (f_fd < 0)
622         return f_fd;
623     
624     fl.l_start = start;
625     fl.l_len = length;
626     fl.l_whence = SEEK_SET;
627     fl.l_pid = getpid();
628     fl.l_type = type;
629
630     errno = 0;
631
632     ret = fcntl(filedes, cmd, &fl);
633     saved_errno = errno;            
634
635     if(debug > 1 && ret)
636         fprintf(stderr, "do_lock: ret = %d, errno = %d (%s)\n", ret, errno, strerror(errno));
637
638     return(ret==0?PASS:FAIL);
639 }
640
641 int do_close(void)
642 {       
643     if(debug > 1) {
644         fprintf(stderr, "do_close\n");
645     }
646
647     errno =0;
648     CLOSE(f_fd);
649     f_fd = INVALID_HANDLE;
650
651     saved_errno = errno;            
652         
653     if (errno)
654         return FAIL;
655     return PASS;
656 }
657
658 void
659 send_ctl(void)
660 {
661     int         nwrite;
662
663     if (debug > 1) {
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);
666     }
667
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));
676
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)) {
685         if (nwrite < 0)
686             perror("send_ctl: write");
687         else
688             fprintf(stderr, "send_ctl[%d]: write() returns %d, not %zu as expected\n", 
689                     ctl.test, nwrite, sizeof(ctl));
690         exit(1);
691         /*NOTREACHED*/
692     }
693 }
694
695 void recv_ctl(void)
696 {
697     int         nread;
698
699     if ((nread = SOCKET_READ(c_fd, (char*)&ctl, sizeof(ctl))) != sizeof(ctl)) {
700         if (nread < 0)
701             perror("recv_ctl: read");
702         else {
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");
706         } 
707         exit(1);
708         /*NOTREACHED*/
709     }
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);
717
718     if (debug > 1) {
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);
721     }
722 }
723
724 void
725 cleanup(void)
726 {
727     if (f_fd>=0)
728         CLOSE(f_fd);
729     
730     if (c_fd>=0)
731         SOCKET_CLOSE(c_fd);
732     
733     if (s_fd>=0)
734         SOCKET_CLOSE(s_fd);
735     
736     PLATFORM_CLEANUP();
737 }
738
739 int
740 main(int argc, char *argv[])
741 {
742     int         i, sts;
743     int         c;
744     struct sockaddr_in  myAddr;
745     struct linger       noLinger = {1, 0};
746     char        *host = NULL;
747     char        *endnum;
748     int         errflag = 0;
749     char        *p;
750     extern char *optarg;
751     extern int  optind;
752     int fail_count = 0;; 
753     
754     atexit(cleanup);
755     
756     PLATFORM_INIT();
757
758     /* trim command name of leading directory components */
759     prog = argv[0];
760     for (p = prog; *p; p++) {
761         if (*p == '/')
762             prog = p+1;
763     }
764
765     while ((c = getopt(argc, argv, "dn:h:p:?")) != EOF) {
766         switch (c) {
767
768         case 'd':       /* debug flag */
769             debug++;
770             break;
771
772         case 'h':       /* (server) hostname */
773             server = 0;
774             host = optarg;
775             break;
776
777         case 'n':
778             testnumber = atoi(optarg);
779             break;
780
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", 
785                         prog);
786                 exit(1);
787                 /*NOTREACHED*/
788             }
789             break;
790
791         case '?':
792         default:
793             errflag++;
794             break;
795         }
796     }
797
798     if (errflag || optind != argc-1) {
799         usage();
800         /*NOTREACHED*/
801     }
802
803     filename=argv[optind];
804     if (do_open(O_RDWR) == FAIL)
805         exit(1);
806
807     setbuf(stderr, NULL);
808
809     if (server) {
810         int one = 1;
811         
812         s_fd = socket(AF_INET, SOCK_STREAM, 0);
813         if (s_fd == INVALID_SOCKET) {
814             perror("socket");
815             exit(1);
816             /*NOTREACHED*/
817         }
818         if (setsockopt(s_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one)) < 0) {
819             perror("setsockopt(nodelay)");
820             exit(1);
821             /*NOTREACHED*/
822         }
823         if (setsockopt(s_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one))<0) {
824             perror("setsockopt(reuseaddr)");
825             exit(1);
826             /*NOTREACHED*/
827         }
828 #ifdef SO_REUSEPORT
829         if (setsockopt(s_fd, SOL_SOCKET, SO_REUSEPORT, (char*)&one, sizeof(one))<0) {
830             perror("setsockopt(reuseport)");
831             exit(1);
832             /*NOTREACHED*/
833         }
834 #endif
835
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));
841         if (sts < 0) {
842             perror("bind");
843             exit(1);
844             /*NOTREACHED*/
845         }
846
847         sts = listen(s_fd, 5);  /* Max. of 5 pending connection requests */
848         if (sts == -1) {
849             perror("listen");
850             exit(1);
851             /*NOTREACHED*/
852         }
853
854         if (port == 0) {
855                 socklen_t addr_len = sizeof(myAddr);
856
857                 if (getsockname(s_fd, &myAddr, &addr_len)) {
858                     perror("getsockname");
859                     exit(1);
860                 }
861
862                 port = ntohs(myAddr.sin_port);
863         }
864
865         printf("server port: %d\n", port);
866         fflush(stdout);
867
868         c_fd = accept(s_fd, NULL, NULL);
869         if (c_fd == INVALID_SOCKET) {
870             perror("accept");
871             exit(1);
872             /*NOTREACHED*/
873         }
874
875         if (debug) fprintf(stderr, "Client accepted\n");
876         SRAND(12345L);
877     }
878     else {
879         struct hostent  *servInfo;
880
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");
885             printf("\n");
886             exit(1);
887             /*NOTREACHED*/
888         }
889
890         c_fd = socket(AF_INET, SOCK_STREAM, 0);
891         if (c_fd == INVALID_SOCKET) {
892             perror("socket");
893             exit(1);
894             /*NOTREACHED*/
895         }
896         /* avoid 200 ms delay */
897         if (setsockopt(c_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, sizeof(i)) < 0) {
898             perror("setsockopt(nodelay)");
899             exit(1);
900             /*NOTREACHED*/
901         }
902         /* Don't linger on close */
903         if (setsockopt(c_fd, SOL_SOCKET, SO_LINGER, (char *)&noLinger, sizeof(noLinger)) < 0) {
904             perror("setsockopt(nolinger)");
905             exit(1);
906             /*NOTREACHED*/
907         }
908
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);
913
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 ");
917             exit(1);
918             /*NOTREACHED*/
919         }
920
921         if (debug) fprintf(stderr, "Connected to server\n");
922         SRAND(6789L);
923     }
924
925     if (server)
926         /* only server need do shared file */
927         initialize(f_fd);
928
929     /*
930      * TCP/IP connection to be established, safe to proceed.
931      *
932      * real work is in here ...
933      */
934     i = 0;
935 {
936     int index = 0;
937     int end = 0;
938     int result = 0;
939     int last_test = 0;
940     int test_count = 0;
941     int fail_flag = 0;
942     while(!end) {
943         if (server) {
944             if(testnumber > 0) {
945                 last_test = testnumber - 1;
946                 while(tests[index][TEST_NUM] != testnumber && tests[index][TEST_NUM] != 0) {
947                     index++;
948                 }
949             }
950             /* If we have a server command, deal with it */
951             if(tests[index][WHO] == SERVER) {
952                 if(debug>1)
953                     fprintf(stderr, "Got a server command (%d)\n", index);
954                 if(tests[index][TEST_NUM] == 0) {
955                     index++;
956                     continue;
957                 } 
958                 memset(&ctl, 0, sizeof(ctl));
959                 ctl.test = tests[index][TEST_NUM];
960
961                 if(tests[index][TEST_NUM] != 0) {
962                     switch(tests[index][COMMAND]) {
963                         case WRLOCK:
964                             result = do_lock(F_SETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
965                             break;
966                         case RDLOCK:
967                             result = do_lock(F_SETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
968                             break;
969                         case UNLOCK:
970                             result = do_lock(F_SETLK, F_UNLCK, tests[index][OFFSET], tests[index][LENGTH]);
971                             break;
972                         case F_CLOSE:
973                             result = do_close();
974                             break;
975                         case F_OPEN:
976                             result = do_open(tests[index][FLAGS]);
977                             break;
978                         case WRTEST:
979                             result = do_lock(F_GETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
980                             break;
981                         case RDTEST:
982                             result = do_lock(F_GETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
983                             break;
984                     }
985                     if( result != tests[index][RESULT]) {
986                         fail_flag++;
987                         /* We have a failure */
988                         if(debug)
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]);
1000                     }
1001                 }
1002             /* else send it off to the client */
1003             } else if (tests[index][WHO] == CLIENT) {
1004                 if(tests[index][TEST_NUM] == 0) {
1005                     ctl.test = 0;
1006                     end=1;
1007                 } 
1008                 if(debug > 1)
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 */
1017                 ctl.index = index;
1018                 send_ctl();
1019                 if(ctl.test != 0) {
1020                     /* Get the clients response */
1021                     recv_ctl();
1022                     /* this is the whether the test passed or failed,
1023                      * not what the command returned */
1024                     if( ctl.result == FAIL ) {
1025                         fail_flag++;
1026                         if(debug)
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]);
1037                     }
1038                 }
1039             }
1040             if (debug > 1) {
1041                 fprintf(stderr, "server sleeping ...\n");
1042                 SLEEP(1);
1043             }
1044             if(tests[index][TEST_NUM] != 0) {
1045                 if(last_test != tests[index][TEST_NUM]) {
1046                     test_count++;
1047                     if(fail_flag)
1048                         fail_count++;
1049                     fail_flag = 0;
1050
1051                 }
1052                 last_test = tests[index][TEST_NUM];
1053             }
1054                 
1055             index++;
1056         } else { /* CLIENT */
1057             if(debug > 2)
1058                 fprintf(stderr,"client: waiting...\n");
1059             /* wait for the server to do something */
1060             recv_ctl();
1061
1062             /* check for a client command */
1063             index = ctl.index;
1064             if (tests[index][WHO] != CLIENT) { 
1065                 fprintf(stderr, "not a client command index (%d)\n", index);
1066                 exit(1);
1067             }
1068                 
1069             if(ctl.test == 0) {
1070                 end = 1;
1071                 break;
1072             }
1073                 
1074
1075             ctl.command = tests[index][COMMAND];
1076             ctl.offset = tests[index][OFFSET];
1077             ctl.length = tests[index][LENGTH];
1078             switch(tests[index][COMMAND]) {
1079                 case WRLOCK:
1080                     result = do_lock(F_SETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
1081                     break;
1082                 case RDLOCK:
1083                     result = do_lock(F_SETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
1084                     break;
1085                 case UNLOCK:
1086                     result = do_lock(F_SETLK, F_UNLCK, tests[index][OFFSET], tests[index][LENGTH]);
1087                     break;
1088                 case F_CLOSE:
1089                     result = do_close();
1090                     break;
1091                 case F_OPEN:
1092                     result = do_open(tests[index][FLAGS]);
1093                     break;
1094                 case WRTEST:
1095                     result = do_lock(F_GETLK, F_WRLCK, tests[index][OFFSET], tests[index][LENGTH]);
1096                     break;
1097                 case RDTEST:
1098                     result = do_lock(F_GETLK, F_RDLCK, tests[index][OFFSET], tests[index][LENGTH]);
1099                     break;
1100             }
1101             if( result != tests[index][RESULT] ) {
1102                 if(debug)
1103                     fprintf(stderr,"Got %d, wanted %lld\n", result,
1104                                         (long long)tests[index][RESULT]);
1105                 ctl.result = FAIL;
1106                 ctl.error = saved_errno;
1107                 fail_count++;
1108             } else {
1109                 ctl.result = PASS;
1110             }
1111             if(debug > 2)
1112                 fprintf(stderr,"client: sending result to server (%d)\n", ctl.index);
1113             /* Send result to the server */
1114             send_ctl();
1115             if(tests[index][TEST_NUM] != 0) {
1116                 if(last_test != tests[index][TEST_NUM])
1117                     test_count++;
1118                 last_test = tests[index][TEST_NUM];
1119             }
1120         }
1121     }
1122     if(server)
1123         printf("%d tests run, %d failed\n", test_count, fail_count);
1124 }   
1125     
1126     exit(fail_count);
1127     /*NOTREACHED*/
1128 }
1129
1130