xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/dup/dup01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * Copyright (c) 2020 SUSE LLC
5  *
6  * 03/30/1992 AUTHOR: William Roske CO-PILOT: Dave Fenner
7  *
8  */
9 
10 /*\
11  * [Description]
12  *
13  * Verify that dup(2) syscall executes successfully and allocates
14  * a new file descriptor which refers to the same open file as oldfd.
15  */
16 
17 #include "tst_test.h"
18 
19 static int fd;
20 static struct stat buf1, buf2;
21 
verify_dup(void)22 static void verify_dup(void)
23 {
24 	TST_EXP_FD(dup(fd));
25 
26 	SAFE_FSTAT(TST_RET, &buf2);
27 	TST_EXP_EQ_LU(buf1.st_ino, buf2.st_ino);
28 
29 	SAFE_CLOSE(TST_RET);
30 }
31 
setup(void)32 static void setup(void)
33 {
34 	fd = SAFE_OPEN("dupfile", O_RDWR | O_CREAT, 0700);
35 	SAFE_FSTAT(fd, &buf1);
36 }
37 
cleanup(void)38 static void cleanup(void)
39 {
40 	if (fd > 0)
41 		SAFE_CLOSE(fd);
42 }
43 
44 static struct tst_test test = {
45 	.test_all = verify_dup,
46 	.setup = setup,
47 	.cleanup = cleanup,
48 	.needs_tmpdir = 1,
49 };
50