1570af302Sopenharmony_ci#include <stdio.h>
2570af302Sopenharmony_ci#include <fcntl.h>
3570af302Sopenharmony_ci#include <unistd.h>
4570af302Sopenharmony_ci#include <errno.h>
5570af302Sopenharmony_ci#include <string.h>
6570af302Sopenharmony_ci#include <sys/wait.h>
7570af302Sopenharmony_ci#include "test.h"
8570af302Sopenharmony_ci
9570af302Sopenharmony_ci#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
10570af302Sopenharmony_ci#define TESTE(c) (errno=0, TEST(c, "errno = %s\n", strerror(errno)))
11570af302Sopenharmony_ci
12570af302Sopenharmony_ciint main(void)
13570af302Sopenharmony_ci{
14570af302Sopenharmony_ci	struct flock fl = {0};
15570af302Sopenharmony_ci	FILE *f;
16570af302Sopenharmony_ci	int fd;
17570af302Sopenharmony_ci	pid_t pid;
18570af302Sopenharmony_ci	int status;
19570af302Sopenharmony_ci
20570af302Sopenharmony_ci	if (!TESTE(f=tmpfile())) return t_status;
21570af302Sopenharmony_ci	fd = fileno(f);
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci	fl.l_type = F_WRLCK;
24570af302Sopenharmony_ci	fl.l_whence = SEEK_SET;
25570af302Sopenharmony_ci	fl.l_start = 0;
26570af302Sopenharmony_ci	fl.l_len = 0;
27570af302Sopenharmony_ci	TESTE(fcntl(fd, F_SETLK, &fl)==0);
28570af302Sopenharmony_ci
29570af302Sopenharmony_ci	pid = fork();
30570af302Sopenharmony_ci	if (!pid) {
31570af302Sopenharmony_ci		fl.l_type = F_RDLCK;
32570af302Sopenharmony_ci		_exit(fcntl(fd, F_SETLK, &fl)==0 ||
33570af302Sopenharmony_ci			(errno!=EAGAIN && errno!=EACCES));
34570af302Sopenharmony_ci	}
35570af302Sopenharmony_ci	while (waitpid(pid, &status, 0)<0 && errno==EINTR);
36570af302Sopenharmony_ci	TEST(status==0, "lock failed to work\n");
37570af302Sopenharmony_ci
38570af302Sopenharmony_ci	pid = fork();
39570af302Sopenharmony_ci	if (!pid) {
40570af302Sopenharmony_ci		fl.l_type = F_WRLCK;
41570af302Sopenharmony_ci		_exit(fcntl(fd, F_GETLK, &fl) || fl.l_pid != getppid());
42570af302Sopenharmony_ci	}
43570af302Sopenharmony_ci	while (waitpid(pid, &status, 0)<0 && errno==EINTR);
44570af302Sopenharmony_ci	TEST(status==0, "child failed to detect lock held by parent\n");
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci	fclose(f);
47570af302Sopenharmony_ci
48570af302Sopenharmony_ci	return t_status;
49570af302Sopenharmony_ci}
50