summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/filelock/ofdlocks.c
blob: 68bac28b234b7db3d7754d359201b997e54292ee (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
118
119
120
// SPDX-License-Identifier: GPL-2.0

#define _GNU_SOURCE
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "kselftest.h"

static int lock_set(int fd, struct flock *fl)
{
	int ret;

	fl->l_pid = 0;		// needed for OFD locks
	fl->l_whence = SEEK_SET;
	ret = fcntl(fd, F_OFD_SETLK, fl);
	if (ret)
		ksft_perror("fcntl()");
	return ret;
}

static int lock_get(int fd, struct flock *fl)
{
	int ret;

	fl->l_pid = 0;		// needed for OFD locks
	fl->l_whence = SEEK_SET;
	ret = fcntl(fd, F_OFD_GETLK, fl);
	if (ret)
		ksft_perror("fcntl()");
	return ret;
}

int main(void)
{
	int rc;
	struct flock fl, fl2;
	int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
	int fd2 = open("/tmp/aa", O_RDONLY);

	ksft_print_header();
	ksft_set_plan(4);

	unlink("/tmp/aa");
	assert(fd != -1);
	assert(fd2 != -1);
	ksft_print_msg("opened fds %i %i\n", fd, fd2);

	/* Set some read lock */
	fl.l_type = F_RDLCK;
	fl.l_start = 5;
	fl.l_len = 3;
	rc = lock_set(fd, &fl);
	ksft_test_result(rc == 0, "set OFD read lock on first fd\n");
	if (rc != 0)
		ksft_finished();

	/* Make sure read locks do not conflict on different fds. */
	fl.l_type = F_RDLCK;
	fl.l_start = 5;
	fl.l_len = 1;
	rc = lock_get(fd2, &fl);
	if (rc != 0)
		ksft_finished();
	if (fl.l_type != F_UNLCK)
		ksft_exit_fail_msg("read locks conflicted\n");

	/* Make sure read/write locks do conflict on different fds. */
	fl.l_type = F_WRLCK;
	fl.l_start = 5;
	fl.l_len = 1;
	rc = lock_get(fd2, &fl);
	if (rc != 0)
		ksft_finished();
	ksft_test_result(fl.l_type != F_UNLCK,
			 "read and write locks conflicted\n");
	if (fl.l_type == F_UNLCK)
		ksft_finished();

	/* Get info about the lock on first fd. */
	fl.l_type = F_UNLCK;
	fl.l_start = 5;
	fl.l_len = 1;
	rc = lock_get(fd, &fl);
	if (rc != 0)
		ksft_exit_fail_msg("F_OFD_GETLK with F_UNLCK not supported\n");
	ksft_test_result(fl.l_type != F_UNLCK,
			 "F_OFD_GETLK with F_UNLCK returned lock info\n");
	if (fl.l_type == F_UNLCK)
		ksft_exit_fail();
	ksft_print_msg("F_UNLCK test returns: locked, type %i pid %i len %zi\n",
		       fl.l_type, fl.l_pid, fl.l_len);

	/* Try the same but by locking everything by len==0. */
	fl2.l_type = F_UNLCK;
	fl2.l_start = 0;
	fl2.l_len = 0;
	rc = lock_get(fd, &fl2);
	if (rc != 0)
		ksft_exit_fail_msg
		    ("F_OFD_GETLK with F_UNLCK not supported\n");
	ksft_test_result(memcmp(&fl, &fl2, sizeof(fl)) == 0,
			 "F_UNLCK with len==0 returned the same\n");
	if (memcmp(&fl, &fl2, sizeof(fl))) {
		ksft_exit_fail_msg
		    ("F_UNLCK test returns: locked, type %i pid %i len %zi\n",
		     fl.l_type, fl.l_pid, fl.l_len);
	}

	/* Get info about the lock on second fd - no locks on it. */
	fl.l_type = F_UNLCK;
	fl.l_start = 0;
	fl.l_len = 0;
	lock_get(fd2, &fl);
	ksft_test_result(fl.l_type == F_UNLCK,
			 "F_OFD_GETLK with F_UNLCK return lock info from another fd\n");

	ksft_finished();
}