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