1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2002
4f08c3bdfSopenharmony_ci * Ported from SPIE, section2/iosuite/dup6.c, by Airong Zhang
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Negative test for dup2() with max open file descriptors.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <stdlib.h>
14f08c3bdfSopenharmony_ci#include <stdio.h>
15f08c3bdfSopenharmony_ci#include <unistd.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic int *fildes;
20f08c3bdfSopenharmony_cistatic int min;
21f08c3bdfSopenharmony_cistatic char pfilname[40];
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic void setup(void)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	min = getdtablesize();	/* get number of files allowed open */
26f08c3bdfSopenharmony_ci	fildes = SAFE_MALLOC((min + 10) * sizeof(int));
27f08c3bdfSopenharmony_ci	memset(fildes, -1, (min + 10) * sizeof(int));
28f08c3bdfSopenharmony_ci	sprintf(pfilname, "./dup205.%d\n", getpid());
29f08c3bdfSopenharmony_ci}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void cleanup(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	if (fildes != NULL)
34f08c3bdfSopenharmony_ci		free(fildes);
35f08c3bdfSopenharmony_ci}
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic void run(void)
38f08c3bdfSopenharmony_ci{
39f08c3bdfSopenharmony_ci	int ifile = -1, rc = 0;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	fildes[0] = SAFE_CREAT(pfilname, 0666);
42f08c3bdfSopenharmony_ci	fildes[fildes[0]] = fildes[0];
43f08c3bdfSopenharmony_ci	for (ifile = fildes[0] + 1; ifile < min + 10; ifile++) {
44f08c3bdfSopenharmony_ci		TEST(dup2(fildes[ifile - 1], ifile));
45f08c3bdfSopenharmony_ci		fildes[ifile] = TST_RET;
46f08c3bdfSopenharmony_ci		if (fildes[ifile] == -1)
47f08c3bdfSopenharmony_ci			break;
48f08c3bdfSopenharmony_ci		if (fildes[ifile] != ifile)
49f08c3bdfSopenharmony_ci			tst_brk(TFAIL,
50f08c3bdfSopenharmony_ci				"got wrong descriptor number back (%d != %d)",
51f08c3bdfSopenharmony_ci				fildes[ifile], ifile);
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	if (ifile < min) {
55f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Not enough files duped");
56f08c3bdfSopenharmony_ci		rc++;
57f08c3bdfSopenharmony_ci	} else if (ifile > min) {
58f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Too many files duped");
59f08c3bdfSopenharmony_ci		rc++;
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci	if (TST_ERR != EBADF && TST_ERR != EMFILE && TST_ERR != EINVAL) {
62f08c3bdfSopenharmony_ci		tst_res(TFAIL, "bad errno on dup2 failure");
63f08c3bdfSopenharmony_ci		rc++;
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if (rc)
67f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Test failed");
68f08c3bdfSopenharmony_ci	else
69f08c3bdfSopenharmony_ci		tst_res(TPASS, "Test passed");
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	SAFE_UNLINK(pfilname);
72f08c3bdfSopenharmony_ci	for (ifile = fildes[0]; ifile < min + 10; ifile++) {
73f08c3bdfSopenharmony_ci		if (fildes[ifile] > 0)
74f08c3bdfSopenharmony_ci			SAFE_CLOSE(fildes[ifile]);
75f08c3bdfSopenharmony_ci	}
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic struct tst_test test = {
79f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
80f08c3bdfSopenharmony_ci	.test_all = run,
81f08c3bdfSopenharmony_ci	.setup = setup,
82f08c3bdfSopenharmony_ci	.cleanup = cleanup,
83f08c3bdfSopenharmony_ci};
84