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