s3: VFS: Add a synchronous smb_vfs_fsync_sync() call, built from async primitives.
[samba.git] / tests / fcntl_lock_thread.c
1 /* test whether fcntl locking works between threads on this Linux system */
2
3 #include <unistd.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8
9 #include <fcntl.h>
10
11 #include <sys/fcntl.h>
12
13 #include <sys/wait.h>
14
15 #include <errno.h>
16 #include <pthread.h>
17
18 #define DATA "conftest.fcntl"
19
20 #define SEEK_SET 0
21
22 static void *test_thread(void *thread_parm)
23 {
24         int *status = thread_parm;
25         int fd, ret;
26         struct flock lock;
27         
28         sleep(2);
29         fd = open(DATA, O_RDWR);
30
31         if (fd == -1) {
32                 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
33                         DATA, (int)errno);
34                 pthread_exit(thread_parm);
35         }
36
37         lock.l_type = F_WRLCK;
38         lock.l_whence = SEEK_SET;
39         lock.l_start = 0;
40         lock.l_len = 4;
41         lock.l_pid = 0;
42                 
43         /* check if a lock applies */
44         ret = fcntl(fd,F_SETLK,&lock);
45         if ((ret != -1)) {
46                 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
47         } else {
48                 *status = 0;  /* SUCCESS! */
49         }
50         pthread_exit(thread_parm);
51 }
52
53 /* lock a byte range in a open file */
54 int main(int argc, char *argv[])
55 {
56         struct flock lock;
57         int fd, ret, status=1, rc;
58         pid_t pid;
59         char *testdir = NULL;
60         pthread_t thread_id;
61         pthread_attr_t thread_attr;
62
63         testdir = getenv("TESTDIR");
64         if (testdir) chdir(testdir);
65
66         alarm(10);
67
68         pthread_attr_init(&thread_attr);
69         pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
70         rc = pthread_create(&thread_id, &thread_attr, &test_thread, &status);
71         pthread_attr_destroy(&thread_attr);
72         if (rc == 0) {
73                 fprintf(stderr,"created thread_id=%lu\n", 
74                         (unsigned long int)thread_id);
75         } else {
76                 fprintf(stderr,"ERROR: thread create failed, rc=%d\n", rc);
77         }
78
79         unlink(DATA);
80         fd = open(DATA, O_RDWR|O_CREAT|O_RDWR, 0600);
81
82         if (fd == -1) {
83                 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
84                         DATA, (int)errno);
85                 exit(1);
86         }
87
88         lock.l_type = F_WRLCK;
89         lock.l_whence = SEEK_SET;
90         lock.l_start = 0;
91         lock.l_len = 4;
92         lock.l_pid = getpid();
93
94         /* set a 4 byte write lock */
95         fcntl(fd,F_SETLK,&lock);
96
97         sleep(4);  /* allow thread to try getting lock */
98
99         unlink(DATA);
100
101 #if defined(WIFEXITED) && defined(WEXITSTATUS)
102     if(WIFEXITED(status)) {
103         status = WEXITSTATUS(status);
104     } else {
105         status = 1;
106     }
107 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
108         status = (status == 0) ? 0 : 1;
109 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
110
111         if (status) {
112                 fprintf(stderr,"ERROR: lock test failed with status=%d\n", 
113                         status);
114         }
115
116         exit(status);
117 }