1 /* test whether fcntl locking works between threads on this Linux system */
11 #include <sys/fcntl.h>
18 #define DATA "conftest.fcntl"
22 static void *test_thread(void *thread_parm
)
24 int *status
= thread_parm
;
29 fd
= open(DATA
, O_RDWR
);
32 fprintf(stderr
,"ERROR: failed to open %s (errno=%d)\n",
34 pthread_exit(thread_parm
);
37 lock
.l_type
= F_WRLCK
;
38 lock
.l_whence
= SEEK_SET
;
43 /* check if a lock applies */
44 ret
= fcntl(fd
,F_SETLK
,&lock
);
46 fprintf(stderr
,"ERROR: lock test failed (ret=%d errno=%d)\n", ret
, (int)errno
);
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
[])
57 int fd
, ret
, status
=1, rc
;
61 pthread_attr_t thread_attr
;
63 testdir
= getenv("TESTDIR");
64 if (testdir
) chdir(testdir
);
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
);
73 fprintf(stderr
,"created thread_id=%lu\n",
74 (unsigned long int)thread_id
);
76 fprintf(stderr
,"ERROR: thread create failed, rc=%d\n", rc
);
80 fd
= open(DATA
, O_RDWR
|O_CREAT
|O_RDWR
, 0600);
83 fprintf(stderr
,"ERROR: failed to open %s (errno=%d)\n",
88 lock
.l_type
= F_WRLCK
;
89 lock
.l_whence
= SEEK_SET
;
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 */
101 #if defined(WIFEXITED) && defined(WEXITSTATUS)
102 if(WIFEXITED(status
)) {
103 status
= WEXITSTATUS(status
);
107 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
108 status
= (status
== 0) ? 0 : 1;
109 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
112 fprintf(stderr
,"ERROR: lock test failed with status=%d\n",