1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Linux Test Project, 2021
4 * Copyright (c) International Business Machines Corp., 2001
5 * 07/2001 Ported by Wayne Boyer
6 * MODIFIED: - mridge@us.ibm.com -- changed getpid to syscall(get thread ID)
7 * for unique ID on NPTL threading
8 */
9
10 /*\
11 * [Description]
12 *
13 * Check that file locks are removed when a file descriptor is closed, three
14 * different tests are implemented.
15 *
16 * Parent opens a file and duplicates the file descriptor, places locks using
17 * both file descriptors then closes one descriptor, all locks should be
18 * removed.
19 *
20 * Open same file twice using open, place locks using both descriptors then
21 * close one descriptor, all lock should be removed.
22 *
23 * Open file twice, each in a different process, set the locks and the child
24 * check the locks. Close the first file descriptor and have child check locks
25 * again. Only locks set on first file descriptor should have been removed.
26 */
27
28 #include <signal.h>
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <sys/syscall.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include "tst_test.h"
36 #include "lapi/syscalls.h"
37
38 #define DATA "ABCDEFGHIJ"
39 #define DUP 0
40 #define OPEN 1
41 #define FORK_ 2
42
43 static char tmpname[10] = "testfile";
44 static int fd[2];
45
46 static const struct flock lock_one = {
47 .l_type = F_WRLCK,
48 .l_whence = 0,
49 .l_start = 0L,
50 .l_len = 5L,
51 };
52
53 static const struct flock lock_two = {
54 .l_type = F_WRLCK,
55 .l_whence = 0,
56 .l_start = 5L,
57 .l_len = 5L,
58 };
59
60 static struct tcase {
61 int dup_flag;
62 int test_num;
63 char *dup_flag_name;
64 } tcases[] = {
65 {DUP, 1, "dup"},
66 {OPEN, 2, "open"},
67 {FORK_, 3, "fork"}
68 };
69
lock_region_two(int file_flag,int file_mode)70 static void lock_region_two(int file_flag, int file_mode)
71 {
72 int fd;
73
74 fd = SAFE_OPEN(tmpname, file_flag, file_mode);
75
76 SAFE_FCNTL(fd, F_SETLK, &lock_two);
77
78 TST_CHECKPOINT_WAKE_AND_WAIT(1);
79
80 SAFE_CLOSE(fd);
81 }
82
do_test(int file_flag,int file_mode,int dup_flag)83 static void do_test(int file_flag, int file_mode, int dup_flag)
84 {
85 int ret, fd;
86
87 fd = SAFE_OPEN(tmpname, file_flag, file_mode);
88
89 if (!fcntl(fd, F_SETLK, &lock_one))
90 tst_res(TFAIL, "Succeeded to lock already locked region one");
91 else
92 tst_res(TPASS, "Failed to lock already locked region one");
93
94 if (!fcntl(fd, F_SETLK, &lock_two))
95 tst_res(TFAIL, "Succeeded to lock already locked region two");
96 else
97 tst_res(TPASS, "Failed to lock already locked region two");
98
99 TST_CHECKPOINT_WAKE_AND_WAIT(0);
100
101 if (fcntl(fd, F_SETLK, &lock_one))
102 tst_res(TFAIL | TERRNO, "Failed to lock now unlocked region one");
103 else
104 tst_res(TPASS, "Succeeded to lock now ulocked region two");
105
106 ret = fcntl(fd, F_SETLK, &lock_two);
107
108 if (dup_flag == FORK_) {
109 if (ret)
110 tst_res(TPASS, "Failed to lock already locked region two");
111 else
112 tst_res(TFAIL, "Succeeded to lock already locked region two");
113 } else {
114 if (ret)
115 tst_res(TFAIL, "Failed to lock now ulocked region two");
116 else
117 tst_res(TPASS, "Succeeded to lock now ulocked region two");
118 }
119
120 SAFE_CLOSE(fd);
121 TST_CHECKPOINT_WAKE(0);
122 }
123
run_test(int file_flag,int file_mode,int dup_flag)124 static int run_test(int file_flag, int file_mode, int dup_flag)
125 {
126 fd[0] = SAFE_OPEN(tmpname, file_flag, file_mode);
127 SAFE_WRITE(1, fd[0], DATA, 10);
128
129 switch (dup_flag) {
130 case FORK_:
131 if (!SAFE_FORK()) {
132 lock_region_two(file_flag, file_mode);
133 exit(0);
134 }
135 break;
136 case OPEN:
137 fd[1] = SAFE_OPEN(tmpname, file_flag, file_mode);
138 break;
139 case DUP:
140 fd[1] = SAFE_FCNTL(fd[0], F_DUPFD, 0);
141 break;
142 }
143
144 SAFE_FCNTL(fd[0], F_SETLK, &lock_one);
145
146 // Lock region two or wait until the child locked it
147 if (dup_flag != FORK_)
148 SAFE_FCNTL(fd[1], F_SETLK, &lock_two);
149 else
150 TST_CHECKPOINT_WAIT(1);
151
152 if (!SAFE_FORK()) {
153 do_test(file_flag, file_mode, dup_flag);
154 exit(0);
155 }
156
157 TST_CHECKPOINT_WAIT(0);
158
159 tst_res(TINFO, "Closing a file descriptor in parent");
160
161 SAFE_CLOSE(fd[0]);
162
163 TST_CHECKPOINT_WAKE_AND_WAIT(0);
164
165 if (dup_flag != FORK_)
166 SAFE_CLOSE(fd[1]);
167 else
168 TST_CHECKPOINT_WAKE(1);
169
170 SAFE_UNLINK(tmpname);
171 return 0;
172 }
173
verify_fcntl(unsigned int n)174 static void verify_fcntl(unsigned int n)
175 {
176 struct tcase *tc = &tcases[n];
177
178 tst_res(TINFO, "Running test with %s", tc->dup_flag_name);
179
180 run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, tc->dup_flag);
181 }
182
cleanup(void)183 static void cleanup(void)
184 {
185 size_t i;
186
187 for (i = 0; i < ARRAY_SIZE(fd); i++) {
188 if (fd[i] > 0)
189 SAFE_CLOSE(fd[i]);
190 }
191 }
192
193 static struct tst_test test = {
194 .tcnt = ARRAY_SIZE(tcases),
195 .forks_child = 1,
196 .test = verify_fcntl,
197 .needs_checkpoints = 1,
198 .cleanup = cleanup,
199 };
200