1*bfe2e8f5SStas Sergeev // SPDX-License-Identifier: GPL-2.0
2*bfe2e8f5SStas Sergeev
3*bfe2e8f5SStas Sergeev #define _GNU_SOURCE
4*bfe2e8f5SStas Sergeev #include <fcntl.h>
5*bfe2e8f5SStas Sergeev #include <assert.h>
6*bfe2e8f5SStas Sergeev #include <stdio.h>
7*bfe2e8f5SStas Sergeev #include <unistd.h>
8*bfe2e8f5SStas Sergeev #include <string.h>
9*bfe2e8f5SStas Sergeev #include "../kselftest.h"
10*bfe2e8f5SStas Sergeev
lock_set(int fd,struct flock * fl)11*bfe2e8f5SStas Sergeev static int lock_set(int fd, struct flock *fl)
12*bfe2e8f5SStas Sergeev {
13*bfe2e8f5SStas Sergeev int ret;
14*bfe2e8f5SStas Sergeev
15*bfe2e8f5SStas Sergeev fl->l_pid = 0; // needed for OFD locks
16*bfe2e8f5SStas Sergeev fl->l_whence = SEEK_SET;
17*bfe2e8f5SStas Sergeev ret = fcntl(fd, F_OFD_SETLK, fl);
18*bfe2e8f5SStas Sergeev if (ret)
19*bfe2e8f5SStas Sergeev perror("fcntl()");
20*bfe2e8f5SStas Sergeev return ret;
21*bfe2e8f5SStas Sergeev }
22*bfe2e8f5SStas Sergeev
lock_get(int fd,struct flock * fl)23*bfe2e8f5SStas Sergeev static int lock_get(int fd, struct flock *fl)
24*bfe2e8f5SStas Sergeev {
25*bfe2e8f5SStas Sergeev int ret;
26*bfe2e8f5SStas Sergeev
27*bfe2e8f5SStas Sergeev fl->l_pid = 0; // needed for OFD locks
28*bfe2e8f5SStas Sergeev fl->l_whence = SEEK_SET;
29*bfe2e8f5SStas Sergeev ret = fcntl(fd, F_OFD_GETLK, fl);
30*bfe2e8f5SStas Sergeev if (ret)
31*bfe2e8f5SStas Sergeev perror("fcntl()");
32*bfe2e8f5SStas Sergeev return ret;
33*bfe2e8f5SStas Sergeev }
34*bfe2e8f5SStas Sergeev
main(void)35*bfe2e8f5SStas Sergeev int main(void)
36*bfe2e8f5SStas Sergeev {
37*bfe2e8f5SStas Sergeev int rc;
38*bfe2e8f5SStas Sergeev struct flock fl, fl2;
39*bfe2e8f5SStas Sergeev int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
40*bfe2e8f5SStas Sergeev int fd2 = open("/tmp/aa", O_RDONLY);
41*bfe2e8f5SStas Sergeev
42*bfe2e8f5SStas Sergeev unlink("/tmp/aa");
43*bfe2e8f5SStas Sergeev assert(fd != -1);
44*bfe2e8f5SStas Sergeev assert(fd2 != -1);
45*bfe2e8f5SStas Sergeev ksft_print_msg("[INFO] opened fds %i %i\n", fd, fd2);
46*bfe2e8f5SStas Sergeev
47*bfe2e8f5SStas Sergeev /* Set some read lock */
48*bfe2e8f5SStas Sergeev fl.l_type = F_RDLCK;
49*bfe2e8f5SStas Sergeev fl.l_start = 5;
50*bfe2e8f5SStas Sergeev fl.l_len = 3;
51*bfe2e8f5SStas Sergeev rc = lock_set(fd, &fl);
52*bfe2e8f5SStas Sergeev if (rc == 0) {
53*bfe2e8f5SStas Sergeev ksft_print_msg
54*bfe2e8f5SStas Sergeev ("[SUCCESS] set OFD read lock on first fd\n");
55*bfe2e8f5SStas Sergeev } else {
56*bfe2e8f5SStas Sergeev ksft_print_msg("[FAIL] to set OFD read lock on first fd\n");
57*bfe2e8f5SStas Sergeev return -1;
58*bfe2e8f5SStas Sergeev }
59*bfe2e8f5SStas Sergeev /* Make sure read locks do not conflict on different fds. */
60*bfe2e8f5SStas Sergeev fl.l_type = F_RDLCK;
61*bfe2e8f5SStas Sergeev fl.l_start = 5;
62*bfe2e8f5SStas Sergeev fl.l_len = 1;
63*bfe2e8f5SStas Sergeev rc = lock_get(fd2, &fl);
64*bfe2e8f5SStas Sergeev if (rc != 0)
65*bfe2e8f5SStas Sergeev return -1;
66*bfe2e8f5SStas Sergeev if (fl.l_type != F_UNLCK) {
67*bfe2e8f5SStas Sergeev ksft_print_msg("[FAIL] read locks conflicted\n");
68*bfe2e8f5SStas Sergeev return -1;
69*bfe2e8f5SStas Sergeev }
70*bfe2e8f5SStas Sergeev /* Make sure read/write locks do conflict on different fds. */
71*bfe2e8f5SStas Sergeev fl.l_type = F_WRLCK;
72*bfe2e8f5SStas Sergeev fl.l_start = 5;
73*bfe2e8f5SStas Sergeev fl.l_len = 1;
74*bfe2e8f5SStas Sergeev rc = lock_get(fd2, &fl);
75*bfe2e8f5SStas Sergeev if (rc != 0)
76*bfe2e8f5SStas Sergeev return -1;
77*bfe2e8f5SStas Sergeev if (fl.l_type != F_UNLCK) {
78*bfe2e8f5SStas Sergeev ksft_print_msg
79*bfe2e8f5SStas Sergeev ("[SUCCESS] read and write locks conflicted\n");
80*bfe2e8f5SStas Sergeev } else {
81*bfe2e8f5SStas Sergeev ksft_print_msg
82*bfe2e8f5SStas Sergeev ("[SUCCESS] read and write locks not conflicted\n");
83*bfe2e8f5SStas Sergeev return -1;
84*bfe2e8f5SStas Sergeev }
85*bfe2e8f5SStas Sergeev /* Get info about the lock on first fd. */
86*bfe2e8f5SStas Sergeev fl.l_type = F_UNLCK;
87*bfe2e8f5SStas Sergeev fl.l_start = 5;
88*bfe2e8f5SStas Sergeev fl.l_len = 1;
89*bfe2e8f5SStas Sergeev rc = lock_get(fd, &fl);
90*bfe2e8f5SStas Sergeev if (rc != 0) {
91*bfe2e8f5SStas Sergeev ksft_print_msg
92*bfe2e8f5SStas Sergeev ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
93*bfe2e8f5SStas Sergeev return -1;
94*bfe2e8f5SStas Sergeev }
95*bfe2e8f5SStas Sergeev if (fl.l_type != F_UNLCK) {
96*bfe2e8f5SStas Sergeev ksft_print_msg
97*bfe2e8f5SStas Sergeev ("[SUCCESS] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
98*bfe2e8f5SStas Sergeev fl.l_type, fl.l_pid, fl.l_len);
99*bfe2e8f5SStas Sergeev } else {
100*bfe2e8f5SStas Sergeev ksft_print_msg
101*bfe2e8f5SStas Sergeev ("[FAIL] F_OFD_GETLK with F_UNLCK did not return lock info\n");
102*bfe2e8f5SStas Sergeev return -1;
103*bfe2e8f5SStas Sergeev }
104*bfe2e8f5SStas Sergeev /* Try the same but by locking everything by len==0. */
105*bfe2e8f5SStas Sergeev fl2.l_type = F_UNLCK;
106*bfe2e8f5SStas Sergeev fl2.l_start = 0;
107*bfe2e8f5SStas Sergeev fl2.l_len = 0;
108*bfe2e8f5SStas Sergeev rc = lock_get(fd, &fl2);
109*bfe2e8f5SStas Sergeev if (rc != 0) {
110*bfe2e8f5SStas Sergeev ksft_print_msg
111*bfe2e8f5SStas Sergeev ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
112*bfe2e8f5SStas Sergeev return -1;
113*bfe2e8f5SStas Sergeev }
114*bfe2e8f5SStas Sergeev if (memcmp(&fl, &fl2, sizeof(fl))) {
115*bfe2e8f5SStas Sergeev ksft_print_msg
116*bfe2e8f5SStas Sergeev ("[FAIL] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
117*bfe2e8f5SStas Sergeev fl.l_type, fl.l_pid, fl.l_len);
118*bfe2e8f5SStas Sergeev return -1;
119*bfe2e8f5SStas Sergeev }
120*bfe2e8f5SStas Sergeev ksft_print_msg("[SUCCESS] F_UNLCK with len==0 returned the same\n");
121*bfe2e8f5SStas Sergeev /* Get info about the lock on second fd - no locks on it. */
122*bfe2e8f5SStas Sergeev fl.l_type = F_UNLCK;
123*bfe2e8f5SStas Sergeev fl.l_start = 0;
124*bfe2e8f5SStas Sergeev fl.l_len = 0;
125*bfe2e8f5SStas Sergeev lock_get(fd2, &fl);
126*bfe2e8f5SStas Sergeev if (fl.l_type != F_UNLCK) {
127*bfe2e8f5SStas Sergeev ksft_print_msg
128*bfe2e8f5SStas Sergeev ("[FAIL] F_OFD_GETLK with F_UNLCK return lock info from another fd\n");
129*bfe2e8f5SStas Sergeev return -1;
130*bfe2e8f5SStas Sergeev }
131*bfe2e8f5SStas Sergeev return 0;
132*bfe2e8f5SStas Sergeev }
133