1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * If oldfd is a valid file descriptor, and newfd has the same value as oldfd,
11 * then dup2() does nothing, and returns newfd.
12 */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include "tst_test.h"
17
18 static int fd = -1;
19
verify_dup2(void)20 static void verify_dup2(void)
21 {
22 TST_EXP_FD_SILENT(dup2(fd, fd), "dup2(%d, %d)", fd, fd);
23
24 if (TST_RET != fd) {
25 tst_res(TFAIL, "dup2(%d, %d) returns wrong newfd(%ld)", fd, fd, TST_RET);
26 SAFE_CLOSE(TST_RET);
27 return;
28 }
29 tst_res(TPASS, "dup2(%d, %d) returns newfd(%d)", fd, fd, fd);
30 }
31
setup(void)32 static void setup(void)
33 {
34 fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0666);
35 }
36
cleanup(void)37 static void cleanup(void)
38 {
39 if (fd > -1)
40 SAFE_CLOSE(fd);
41 }
42
43 static struct tst_test test = {
44 .needs_tmpdir = 1,
45 .setup = setup,
46 .cleanup = cleanup,
47 .test_all = verify_dup2,
48 };
49