aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fcntl_lock_thread.c
blob: e3415146a7f8deb827c0453232a51ef812e0cdde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* test whether fcntl locking works between threads on this Linux system */

#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <fcntl.h>

#include <sys/fcntl.h>

#include <sys/wait.h>

#include <errno.h>
#include <pthread.h>

#define DATA "conftest.fcntl"

#define SEEK_SET 0

static void *test_thread(void *thread_parm)
{
	int *status = thread_parm;
	int fd, ret;
	struct flock lock;
	
	sleep(2);
	fd = open(DATA, O_RDWR);

	if (fd == -1) {
		fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
			DATA, (int)errno);
		pthread_exit(thread_parm);
	}

	lock.l_type = F_WRLCK;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 4;
	lock.l_pid = 0;
		
	/* check if a lock applies */
	ret = fcntl(fd,F_SETLK,&lock);
	if ((ret != -1)) {
		fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
	} else {
		*status = 0;  /* SUCCESS! */
	}
	pthread_exit(thread_parm);
}

/* lock a byte range in a open file */
int main(int argc, char *argv[])
{
	struct flock lock;
	int fd, ret, status=1, rc;
	pid_t pid;
	char *testdir = NULL;
	pthread_t thread_id;
	pthread_attr_t thread_attr;

	testdir = getenv("TESTDIR");
	if (testdir) chdir(testdir);

	alarm(10);

	pthread_attr_init(&thread_attr);
	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
	rc = pthread_create(&thread_id, &thread_attr, &test_thread, &status);
	pthread_attr_destroy(&thread_attr);
	if (rc == 0) {
		fprintf(stderr,"created thread_id=%lu\n", 
			(unsigned long int)thread_id);
	} else {
		fprintf(stderr,"ERROR: thread create failed, rc=%d\n", rc);
	}

	unlink(DATA);
	fd = open(DATA, O_RDWR|O_CREAT|O_RDWR, 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;
	lock.l_start = 0;
	lock.l_len = 4;
	lock.l_pid = getpid();

	/* set a 4 byte write lock */
	fcntl(fd,F_SETLK,&lock);

	sleep(4);  /* allow thread to try getting lock */

	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);
}