summaryrefslogtreecommitdiff
path: root/fcntl.c
blob: 83db2c4674cde22c358c29b9b46be7174e254c54 (plain) (blame)
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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>

#define TEST2(c, l, ...) ((c) ? 1 : \
(err++,printf(__FILE__":"#l": "#c" failed: " __VA_ARGS__),putchar('\n'),0))
#define TEST1(c, l, ...) TEST2(c, l, __VA_ARGS__)
#define TEST(c, ...) TEST1(c, __LINE__, __VA_ARGS__)

#define TESTE(c) TEST(c, "errno = %s", strerror(errno))

int test_fcntl(void)
{
	int err = 0;
	struct flock fl = {0};
	FILE *f;
	int fd;
	pid_t pid;
	int status;

	if (!TESTE(f=tmpfile())) return err;
	fd = fileno(f);

	fl.l_type = F_WRLCK;
	fl.l_whence = SEEK_SET;
	fl.l_start = 0;
	fl.l_len = 0;
	TESTE(fcntl(fd, F_SETLK, &fl)==0);

	pid = fork();
	if (!pid) {
		fl.l_type = F_RDLCK;
		_exit(fcntl(fd, F_SETLK, &fl)==0 ||
			(errno!=EAGAIN && errno!=EACCES));
	}
	while (waitpid(pid, &status, 0)<0 && errno==EINTR);
	TEST(status==0, "lock failed to work");

	pid = fork();
	if (!pid) {
		fl.l_type = F_WRLCK;
		_exit(fcntl(fd, F_GETLK, &fl) || fl.l_pid != getppid());
	}
	while (waitpid(pid, &status, 0)<0 && errno==EINTR);
	TEST(status==0, "child failed to detect lock held by parent");

	fclose(f);

	return err;
}