src/locktest: Remove D_flag
[xfstests-dev.git] / src / locktest.c
index c9cd04ca4a5917e8d7c93b8e346822f24698f181..089951e2d992d559fa6045915cb79445f3553072 100644 (file)
@@ -1,19 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
  * All Rights Reserved.
- *
- * 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.
- *
- * This program is distributed in the hope that it would 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, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 /*
@@ -73,8 +61,7 @@ extern int h_errno;
        
 #define HANDLE              int
 #define INVALID_HANDLE      -1
-#define OPEN(N,F)           (open(N, F|O_CREAT|O_BINARY| \
-                            (D_flag ? O_DIRECT : 0), 0644))
+#define OPEN(N,F)           (open(N, F|O_CREAT|O_BINARY, 0644))
 #define SEEK(H, O)          (lseek(H, O, SEEK_SET))
 #define READ(H, B, L)       (read(H, B, L))
 #define WRITE(H, B, L)      (write(H, B, L))
@@ -95,7 +82,7 @@ static char   *filename = 0;
 static int     debug = 0;
 static int     server = 1;
 static int     maxio = 8192;
-static int     port = 7890;
+static int     port = 0;
 static int     testnumber = -1;
 static int     saved_errno = 0;
 
@@ -103,7 +90,6 @@ static SOCKET        s_fd = -1;              /* listen socket    */
 static SOCKET  c_fd = -1;              /* IPC socket       */
 static HANDLE  f_fd = INVALID_HANDLE;  /* shared file      */
 static char    *buf;                   /* I/O buffer       */
-static int     D_flag = 0;
 
 #define        WRLOCK  0
 #define        RDLOCK  1
@@ -180,10 +166,11 @@ char *descriptions[] = {
     /* 26 */"Acquire read and write locks with overlapping ranges",
     /* 27 */"Acquire whole file write lock and then close without unlocking (and attempt a lock)",
     /* 28 */"Acquire two read locks, close and reopen the file, and test if the inital lock is still there",
+    /* 29 */"Verify that F_GETLK for F_WRLCK doesn't require that file be opened for write",
     #if defined(macosx)
-    /* 29 */"Close the opened file and open the file with SHLOCK, other client will try to open with SHLOCK too",
-    /* 30 */"Close the opened file and open the file with SHLOCK, other client will try to open with EXLOCK",
-    /* 31 */"Close the opened file and open the file with EXLOCK, other client will try to open with EXLOCK too"
+    /* 30 */"Close the opened file and open the file with SHLOCK, other client will try to open with SHLOCK too",
+    /* 31 */"Close the opened file and open the file with SHLOCK, other client will try to open with EXLOCK",
+    /* 32 */"Close the opened file and open the file with EXLOCK, other client will try to open with EXLOCK too"
     #endif
 };
 
@@ -521,26 +508,32 @@ static int64_t tests[][6] =
                {28,    F_OPEN, O_RDWR,         0,              PASS,           SERVER, },
                {28,    WRLOCK, 0,              0,              FAIL,           SERVER, },
                {28,    UNLOCK, 1,              5,              PASS,           SERVER, },
+       /* Verify that F_GETLK for F_WRLCK doesn't require that file be opened for write */
+               {29,    F_CLOSE, 0,             0,              PASS,           SERVER, },
+               {29,    F_OPEN, O_RDONLY,       0,              PASS,           SERVER, },
+               {29,    WRTEST, 0,              0,              PASS,           SERVER, },
+               {29,    F_CLOSE,0,              0,              PASS,           SERVER, },
+               {29,    F_OPEN, O_RDWR,         0,              PASS,           SERVER, },
 #ifdef macosx
        /* Close the opened file and open the file with SHLOCK, other client will try to open with SHLOCK too */
-               {29,    F_CLOSE,0,              0,              PASS,           SERVER, },
-               {29,    F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK,     0,      PASS,           SERVER, },
-               {29,    F_CLOSE,0,              0,              PASS,           CLIENT, },
-               {29,    F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK,     0,      PASS,           CLIENT, },
-       /* Close the opened file and open the file with SHLOCK, other client will try to open with EXLOCK */
                {30,    F_CLOSE,0,              0,              PASS,           SERVER, },
-               {30,    F_CLOSE,0,              0,              PASS,           CLIENT, },
                {30,    F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK,     0,      PASS,           SERVER, },
-               {30,    F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK,     0,      FAIL,           CLIENT, },
-       /* Close the opened file and open the file with EXLOCK, other client will try to open with EXLOCK too */
+               {30,    F_CLOSE,0,              0,              PASS,           CLIENT, },
+               {30,    F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK,     0,      PASS,           CLIENT, },
+       /* Close the opened file and open the file with SHLOCK, other client will try to open with EXLOCK */
                {31,    F_CLOSE,0,              0,              PASS,           SERVER, },
-               {31,    F_CLOSE,0,              0,              FAIL,           CLIENT, },
-               {31,    F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK,     0,      PASS,           SERVER, },
+               {31,    F_CLOSE,0,              0,              PASS,           CLIENT, },
+               {31,    F_OPEN, O_RDWR|O_SHLOCK|O_NONBLOCK,     0,      PASS,           SERVER, },
                {31,    F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK,     0,      FAIL,           CLIENT, },
-               {31,    F_CLOSE,0,              0,              PASS,           SERVER, },
-               {31,    F_CLOSE,0,              0,              FAIL,           CLIENT, },
-               {31,    F_OPEN, O_RDWR,         0,              PASS,           SERVER, },
-               {31,    F_OPEN, O_RDWR,         0,              PASS,           CLIENT, },
+       /* Close the opened file and open the file with EXLOCK, other client will try to open with EXLOCK too */
+               {32,    F_CLOSE,0,              0,              PASS,           SERVER, },
+               {32,    F_CLOSE,0,              0,              FAIL,           CLIENT, },
+               {32,    F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK,     0,      PASS,           SERVER, },
+               {32,    F_OPEN, O_RDWR|O_EXLOCK|O_NONBLOCK,     0,      FAIL,           CLIENT, },
+               {32,    F_CLOSE,0,              0,              PASS,           SERVER, },
+               {32,    F_CLOSE,0,              0,              FAIL,           CLIENT, },
+               {32,    F_OPEN, O_RDWR,         0,              PASS,           SERVER, },
+               {32,    F_OPEN, O_RDWR,         0,              PASS,           CLIENT, },
 #endif /* macosx */
        /* indicate end of array */
                {0,0,0,0,0,SERVER},
@@ -584,12 +577,7 @@ initialize(HANDLE fd)
     int        offset = 0;
     int        togo = FILE_SIZE;
 
-    if (D_flag) {
-        ibuf = (char *)ALLOC_ALIGNED(INIT_BUFSZ);
-    }
-    else {
-        ibuf = (char*)malloc(INIT_BUFSZ);
-    }
+    ibuf = (char*)malloc(INIT_BUFSZ);
     memset(ibuf, ':', INIT_BUFSZ);
 
     SEEK(fd, 0L);
@@ -619,15 +607,6 @@ int do_open(int flag)
        /*NOTREACHED*/
     }
 
-#ifdef __sun
-    if (D_flag) {
-        directio(f_fd, DIRECTIO_ON);
-    }
-#elif defined(__APPLE__)
-    if (D_flag) {
-       fcntl(f_fd, F_NOCACHE, 1);
-    }
-#endif
     return PASS;
 }
 
@@ -772,7 +751,6 @@ main(int argc, char *argv[])
     char       *p;
     extern char        *optarg;
     extern int optind;
-    extern int errno;
     int fail_count = 0;; 
     
     atexit(cleanup);
@@ -832,18 +810,10 @@ main(int argc, char *argv[])
      * +10 is slop for the iteration number if do_write() ... never
      * needed unless maxio is very small
      */
-    if (D_flag) {
-        if ((buf = (char *)ALLOC_ALIGNED(maxio + 10)) == NULL) {
-           perror("aligned alloc buf");
-           exit(1);
-           /*NOTREACHED*/
-        }
-    } else {
-        if ((buf = (char *)malloc(maxio + 10)) == NULL) {
-           perror("malloc buf");
-           exit(1);
-           /*NOTREACHED*/
-        }
+    if ((buf = (char *)malloc(maxio + 10)) == NULL) {
+        perror("malloc buf");
+        exit(1);
+        /*NOTREACHED*/
     }
 
     setbuf(stderr, NULL);
@@ -893,6 +863,20 @@ main(int argc, char *argv[])
            /*NOTREACHED*/
        }
 
+       if (port == 0) {
+               socklen_t addr_len = sizeof(myAddr);
+
+               if (getsockname(s_fd, &myAddr, &addr_len)) {
+                   perror("getsockname");
+                   exit(1);
+               }
+
+               port = ntohs(myAddr.sin_port);
+       }
+
+       printf("server port: %d\n", port);
+       fflush(stdout);
+
        c_fd = accept(s_fd, NULL, NULL);
        if (c_fd == INVALID_SOCKET) {
            perror("accept");
@@ -1151,10 +1135,7 @@ main(int argc, char *argv[])
        printf("%d tests run, %d failed\n", test_count, fail_count);
 }   
     if (buf) {
-        if (D_flag)
-            FREE_ALIGNED(buf);
-        else
-            free(buf);
+        free(buf);
     }