[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / tests / fcntl_lock.c
index c54479434e8ae65f348b1438bf90ed80518eb6a8..3dc12a389739fbeb6cce4d87a2e9b1b961c2f86d 100644 (file)
@@ -1,7 +1,12 @@
 /* test whether fcntl locking works on this system */
 
+#if defined(HAVE_UNISTD_H)
+#include <unistd.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/types.h>
 
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #include <sys/fcntl.h>
 #endif
 
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+
 #include <errno.h>
 
 static int sys_waitpid(pid_t pid,int *status,int options)
@@ -32,13 +41,24 @@ static int sys_waitpid(pid_t pid,int *status,int options)
 int main(int argc, char *argv[])
 {
        struct flock lock;
-       int fd, pid, ret, status=1;
+       int fd, ret, status=1;
+       pid_t pid;
+       char *testdir = NULL;
+
+       testdir = getenv("TESTDIR");
+       if (testdir) chdir(testdir);
+
+       alarm(10);
 
        if (!(pid=fork())) {
                sleep(2);
                fd = open(DATA, O_RDONLY);
 
-               if (fd == -1) exit(1);
+               if (fd == -1) {
+                       fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
+                               DATA, (int)errno);
+                       exit(1);
+               }
 
                lock.l_type = F_WRLCK;
                lock.l_whence = SEEK_SET;
@@ -53,13 +73,21 @@ int main(int argc, char *argv[])
 
                if ((ret == -1) ||
                    (lock.l_type == F_UNLCK)) {
+                       fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
                        exit(1);
                } else {
                        exit(0);
                }
        }
 
-       fd = open(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
+       unlink(DATA);
+       fd = open(DATA, O_RDWR|O_CREAT|O_EXCL, 0600);
+
+       if (fd == -1) {
+               fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
+                       DATA, (int)errno);
+               exit(1);
+       }
 
        lock.l_type = F_WRLCK;
        lock.l_whence = SEEK_SET;
@@ -74,5 +102,20 @@ int main(int argc, char *argv[])
 
        unlink(DATA);
 
+#if defined(WIFEXITED) && defined(WEXITSTATUS)
+    if(WIFEXITED(status)) {
+        status = WEXITSTATUS(status);
+    } else {
+        status = 1;
+    }
+#else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
+       status = (status == 0) ? 0 : 1;
+#endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
+
+       if (status) {
+               fprintf(stderr,"ERROR: lock test failed with status=%d\n", 
+                       status);
+       }
+
        exit(status);
 }