1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 * Copyright (C) 2024 Andrea Cervesato andrea.cervesato@suse.com
6 */
7
8 /*\
9 * [Description]
10 *
11 * This test is checking fcntl() syscall locking mechanism between two
12 * processes.
13 * The test sets a random starting position on file using lseek(), it randomly
14 * generates fcntl() parameters for parent and child and it verifies that
15 * fcntl() will raise a blocking error on child when it's supposed to.
16 */
17
18 #include <fcntl.h>
19 #include <stdlib.h>
20 #include "tst_test.h"
21
22 #ifndef S_ENFMT
23 # define S_ENFMT S_ISGID
24 #endif
25
26 #define CHECK_FAILURE(VAL_A, VAL_B) do { \
27 TST_EXP_EQ_LI_SILENT(VAL_A, VAL_B); \
28 if (!TST_PASS) \
29 results->last_failed = 1; \
30 } while (0)
31
32 struct file_conf {
33 short type;
34 short whence;
35 long start;
36 long len;
37 };
38
39 struct testcase {
40 struct flock flock;
41 struct file_conf parent; /* parent parameters for fcntl() */
42 struct file_conf child; /* child parameters for fcntl() */
43 short blocking; /* blocking/non-blocking flag */
44 long pos; /* starting file position */
45 };
46
47 struct tc_results {
48 int num_pass;
49 int last_failed;
50 };
51
52 static const char filepath[] = "unlocked.txt";
53 static const char filedata[] = "Here some bytes!";
54 static char *str_op_nums;
55 static int op_nums = 5000;
56 static int file_mode = 0777;
57 static struct tc_results *results;
58
dochild(struct testcase * tc,const int fd,const pid_t parent_pid)59 static void dochild(struct testcase *tc, const int fd, const pid_t parent_pid)
60 {
61 struct flock flock = tc->flock;
62
63 results->last_failed = 0;
64
65 flock.l_type = tc->child.type;
66 flock.l_whence = tc->child.whence;
67 flock.l_start = tc->child.start;
68 flock.l_len = tc->child.len;
69 flock.l_pid = 0;
70
71 SAFE_FCNTL(fd, F_GETLK, &flock);
72
73 if (tc->blocking) {
74 tst_res(TDEBUG, "Child: expecting blocked file by parent");
75
76 CHECK_FAILURE(flock.l_pid, parent_pid);
77 CHECK_FAILURE(flock.l_type, tc->parent.type);
78
79 flock.l_type = tc->child.type;
80 flock.l_whence = tc->child.whence;
81 flock.l_start = tc->child.start;
82 flock.l_len = tc->child.len;
83 flock.l_pid = 0;
84
85 TST_EXP_FAIL_SILENT(fcntl(fd, F_SETLK, &flock), EWOULDBLOCK);
86 } else {
87 tst_res(TDEBUG, "Child: expecting no blocking errors");
88
89 CHECK_FAILURE(flock.l_type, F_UNLCK);
90 CHECK_FAILURE(flock.l_whence, tc->child.whence);
91 CHECK_FAILURE(flock.l_start, tc->child.start);
92 CHECK_FAILURE(flock.l_len, tc->child.len);
93 CHECK_FAILURE(flock.l_pid, 0);
94
95 TST_EXP_PASS_SILENT(fcntl(fd, F_SETLK, &flock));
96 }
97 }
98
run_testcase(struct testcase * tc,const int file_mode)99 static void run_testcase(struct testcase *tc, const int file_mode)
100 {
101 struct flock flock = tc->flock;
102 pid_t parent_pid;
103 pid_t child_pid;
104 int fd;
105
106 tst_res(TDEBUG, "Parent: locking file");
107
108 /* open file and move cursor according with the test */
109 fd = SAFE_OPEN(filepath, O_RDWR, file_mode);
110 SAFE_LSEEK(fd, tc->pos, 0);
111
112 /* set the initial parent lock on the file */
113 flock.l_type = tc->parent.type;
114 flock.l_whence = tc->parent.whence;
115 flock.l_start = tc->parent.start;
116 flock.l_len = tc->parent.len;
117 flock.l_pid = 0;
118
119 SAFE_FCNTL(fd, F_SETLK, &flock);
120
121 /* set the child lock on the file */
122 parent_pid = getpid();
123 child_pid = SAFE_FORK();
124
125 if (!child_pid) {
126 dochild(tc, fd, parent_pid);
127 exit(0);
128 }
129
130 tst_reap_children();
131
132 flock.l_type = F_UNLCK;
133 flock.l_whence = 0;
134 flock.l_start = 0;
135 flock.l_len = 0;
136 flock.l_pid = 0;
137
138 SAFE_CLOSE(fd);
139 }
140
genconf(struct file_conf * conf,const int size,const long pos)141 static void genconf(struct file_conf *conf, const int size, const long pos)
142 {
143 conf->type = rand() % 2 ? F_RDLCK : F_WRLCK;
144 conf->whence = SEEK_CUR;
145 conf->start = rand() % (size - 1);
146 conf->len = rand() % (size - conf->start - 1) + 1;
147 conf->start -= pos;
148 }
149
fcntl_overlap(struct file_conf * parent,struct file_conf * child)150 static short fcntl_overlap(
151 struct file_conf *parent,
152 struct file_conf *child)
153 {
154 short overlap;
155
156 if (child->start < parent->start)
157 overlap = parent->start < (child->start + child->len);
158 else
159 overlap = child->start < (parent->start + parent->len);
160
161 if (overlap)
162 tst_res(TDEBUG, "child/parent fcntl() configurations overlap");
163
164 return overlap;
165 }
166
gentestcase(struct testcase * tc)167 static void gentestcase(struct testcase *tc)
168 {
169 struct file_conf *parent = &tc->parent;
170 struct file_conf *child = &tc->child;
171 int size;
172
173 memset(&tc->flock, 0, sizeof(struct flock));
174
175 size = strlen(filedata);
176 tc->pos = rand() % size;
177
178 genconf(parent, size, tc->pos);
179 genconf(child, size, tc->pos);
180
181 tc->blocking = fcntl_overlap(parent, child);
182
183 if (parent->type == F_RDLCK && child->type == F_RDLCK)
184 tc->blocking = 0;
185 }
186
print_testcase(struct testcase * tc)187 static void print_testcase(struct testcase *tc)
188 {
189 tst_res(TDEBUG, "Starting read/write position: %ld", tc->pos);
190
191 tst_res(TDEBUG,
192 "Parent: type=%s whence=%s start=%ld len=%ld",
193 tc->parent.type == F_RDLCK ? "F_RDLCK" : "F_WRLCK",
194 tc->parent.whence == SEEK_SET ? "SEEK_SET" : "SEEK_CUR",
195 tc->parent.start,
196 tc->parent.len);
197
198 tst_res(TDEBUG,
199 "Child: type=%s whence=%s start=%ld len=%ld",
200 tc->child.type == F_RDLCK ? "F_RDLCK" : "F_WRLCK",
201 tc->child.whence == SEEK_SET ? "SEEK_SET" : "SEEK_CUR",
202 tc->child.start,
203 tc->child.len);
204
205 tst_res(TDEBUG, "Expencted %s test",
206 tc->blocking ? "blocking" : "non-blocking");
207 }
208
run(void)209 static void run(void)
210 {
211 struct testcase tc;
212
213 results->num_pass = 0;
214
215 for (int i = 0; i < op_nums; i++) {
216 gentestcase(&tc);
217 print_testcase(&tc);
218
219 tst_res(TDEBUG, "Running test #%u", i);
220 run_testcase(&tc, file_mode);
221
222 if (results->last_failed)
223 return;
224
225 results->num_pass++;
226 }
227
228 if (results->num_pass == op_nums)
229 tst_res(TPASS, "All %d test file operations passed", op_nums);
230 }
231
setup(void)232 static void setup(void)
233 {
234 int fd;
235
236 if (tst_parse_int(str_op_nums, &op_nums, 1, INT_MAX))
237 tst_brk(TBROK, "Invalid number of operations '%s'", str_op_nums);
238
239 if (tst_variant == 1) {
240 tst_res(TINFO, "Requested mandatory locking");
241
242 file_mode = S_ENFMT | 0600;
243 }
244
245 fd = SAFE_OPEN(filepath, O_CREAT | O_RDWR | O_TRUNC, 0777);
246 SAFE_WRITE(SAFE_WRITE_ALL, fd, filedata, strlen(filedata));
247 SAFE_CLOSE(fd);
248
249 srand(time(0));
250
251 results = SAFE_MMAP(
252 NULL,
253 sizeof(struct tc_results),
254 PROT_READ | PROT_WRITE,
255 MAP_ANONYMOUS | MAP_SHARED,
256 -1, 0);
257 }
258
cleanup(void)259 static void cleanup(void)
260 {
261 if (results)
262 SAFE_MUNMAP(results, sizeof(struct tc_results));
263 }
264
265 static struct tst_test test = {
266 .timeout = 8,
267 .test_all = run,
268 .setup = setup,
269 .cleanup = cleanup,
270 .test_variants = 2,
271 .forks_child = 1,
272 .needs_tmpdir = 1,
273 .options = (struct tst_option[]) {
274 { "n:", &str_op_nums, "Total # operations to do (default 5000)" },
275 {},
276 },
277 .skip_filesystems = (const char *const []) {
278 "nfs",
279 NULL
280 },
281 };
282