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