xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/dup2/dup204.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Test whether the inode number are the same for both file descriptors.
10  */
11 
12 #include <unistd.h>
13 #include "tst_test.h"
14 #include "tst_safe_macros.h"
15 
16 static int fd[2] = {-1, -1};
17 static int nfd[2] = {10, 20};
18 
setup(void)19 static void setup(void)
20 {
21 	SAFE_PIPE(fd);
22 }
23 
cleanup(void)24 static void cleanup(void)
25 {
26 	unsigned int i;
27 
28 	for (i = 0; i < ARRAY_SIZE(fd); i++) {
29 		close(fd[i]);
30 		close(nfd[i]);
31 	}
32 }
33 
run(unsigned int i)34 static void run(unsigned int i)
35 {
36 	struct stat oldbuf, newbuf;
37 
38 	TST_EXP_VAL(dup2(fd[i], nfd[i]), nfd[i]);
39 	if (TST_RET == -1)
40 		return;
41 
42 	SAFE_FSTAT(fd[i], &oldbuf);
43 	SAFE_FSTAT(nfd[i], &newbuf);
44 
45 	TST_EXP_EQ_LU(oldbuf.st_ino, newbuf.st_ino);
46 
47 	SAFE_CLOSE(TST_RET);
48 }
49 
50 static struct tst_test test = {
51 	.tcnt = ARRAY_SIZE(fd),
52 	.test = run,
53 	.setup = setup,
54 	.cleanup = cleanup,
55 };
56