xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/dup/dup07.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) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  * Ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2006-2024
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Verify that the file descriptor created by dup(2) syscall has the same
13*49cdfc7eSAndroid Build Coastguard Worker  * access mode as the old one.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static const char *testfile = "dup07";
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
21*49cdfc7eSAndroid Build Coastguard Worker 	char *mode_desc;
22*49cdfc7eSAndroid Build Coastguard Worker 	int mode;
23*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
24*49cdfc7eSAndroid Build Coastguard Worker 	{"read only", 0444},
25*49cdfc7eSAndroid Build Coastguard Worker 	{"write only", 0222},
26*49cdfc7eSAndroid Build Coastguard Worker 	{"read/write", 0666},
27*49cdfc7eSAndroid Build Coastguard Worker };
28*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)29*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	int oldfd, dupfd;
32*49cdfc7eSAndroid Build Coastguard Worker 	struct stat oldbuf, dupbuf;
33*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	oldfd = SAFE_CREAT(testfile, tc->mode);
36*49cdfc7eSAndroid Build Coastguard Worker 	dupfd = TST_EXP_FD_SILENT(dup(oldfd), "dup() %s file", tc->mode_desc);
37*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_PASS) {
38*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FSTAT(oldfd, &oldbuf);
39*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FSTAT(dupfd, &dupbuf);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 		if (oldbuf.st_mode != dupbuf.st_mode)
42*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "%s and dup do not match", tc->mode_desc);
43*49cdfc7eSAndroid Build Coastguard Worker 		else
44*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS, "Passed in %s mode", tc->mode_desc);
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dupfd);
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(oldfd);
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(testfile);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
54*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
55*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
56*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
57*49cdfc7eSAndroid Build Coastguard Worker };
58