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