ctdb-scripts: Drop assumption that there are VLANs with no '@'
[Samba.git] / tests / fcntl_lock_thread.c
blobe3415146a7f8deb827c0453232a51ef812e0cdde
1 /* test whether fcntl locking works between threads on this Linux system */
3 #include <unistd.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
9 #include <fcntl.h>
11 #include <sys/fcntl.h>
13 #include <sys/wait.h>
15 #include <errno.h>
16 #include <pthread.h>
18 #define DATA "conftest.fcntl"
20 #define SEEK_SET 0
22 static void *test_thread(void *thread_parm)
24 int *status = thread_parm;
25 int fd, ret;
26 struct flock lock;
28 sleep(2);
29 fd = open(DATA, O_RDWR);
31 if (fd == -1) {
32 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
33 DATA, (int)errno);
34 pthread_exit(thread_parm);
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;
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! */
50 pthread_exit(thread_parm);
53 /* lock a byte range in a open file */
54 int main(int argc, char *argv[])
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;
63 testdir = getenv("TESTDIR");
64 if (testdir) chdir(testdir);
66 alarm(10);
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);
79 unlink(DATA);
80 fd = open(DATA, O_RDWR|O_CREAT|O_RDWR, 0600);
82 if (fd == -1) {
83 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
84 DATA, (int)errno);
85 exit(1);
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();
94 /* set a 4 byte write lock */
95 fcntl(fd,F_SETLK,&lock);
97 sleep(4); /* allow thread to try getting lock */
99 unlink(DATA);
101 #if defined(WIFEXITED) && defined(WEXITSTATUS)
102 if(WIFEXITED(status)) {
103 status = WEXITSTATUS(status);
104 } else {
105 status = 1;
107 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
108 status = (status == 0) ? 0 : 1;
109 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
111 if (status) {
112 fprintf(stderr,"ERROR: lock test failed with status=%d\n",
113 status);
116 exit(status);