1 /* test whether fcntl locking works between threads on this Linux system */
11 #include <sys/fcntl.h>
18 static int sys_waitpid(pid_t pid,int *status,int options)
20 return waitpid(pid,status,options);
23 #define DATA "conftest.fcntl"
27 static void *test_thread(void *thread_parm)
29 int *status = thread_parm;
34 fd = open(DATA, O_RDWR);
37 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
39 pthread_exit(thread_parm);
42 lock.l_type = F_WRLCK;
43 lock.l_whence = SEEK_SET;
48 /* check if a lock applies */
49 ret = fcntl(fd,F_SETLK,&lock);
51 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
53 *status = 0; /* SUCCESS! */
55 pthread_exit(thread_parm);
58 /* lock a byte range in a open file */
59 int main(int argc, char *argv[])
62 int fd, ret, status=1, rc;
66 pthread_attr_t thread_attr;
68 testdir = getenv("TESTDIR");
69 if (testdir) chdir(testdir);
73 pthread_attr_init(&thread_attr);
74 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
75 rc = pthread_create(&thread_id, &thread_attr, &test_thread, &status);
76 pthread_attr_destroy(&thread_attr);
78 fprintf(stderr,"created thread_id=%lu\n",
79 (unsigned long int)thread_id);
81 fprintf(stderr,"ERROR: thread create failed, rc=%d\n", rc);
85 fd = open(DATA, O_RDWR|O_CREAT|O_RDWR, 0600);
88 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
93 lock.l_type = F_WRLCK;
94 lock.l_whence = SEEK_SET;
97 lock.l_pid = getpid();
99 /* set a 4 byte write lock */
100 fcntl(fd,F_SETLK,&lock);
102 sleep(4); /* allow thread to try getting lock */
106 #if defined(WIFEXITED) && defined(WEXITSTATUS)
107 if(WIFEXITED(status)) {
108 status = WEXITSTATUS(status);
112 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
113 status = (status == 0) ? 0 : 1;
114 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
117 fprintf(stderr,"ERROR: lock test failed with status=%d\n",