1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker * Ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2013 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2003-2024
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * Test for dup(2) syscall with max open file descriptors.
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker static int *pfildes;
19*49cdfc7eSAndroid Build Coastguard Worker static int minfd, maxfd, freefds;
20*49cdfc7eSAndroid Build Coastguard Worker static char pfilname[40];
21*49cdfc7eSAndroid Build Coastguard Worker
cnt_free_fds(int maxfd)22*49cdfc7eSAndroid Build Coastguard Worker static int cnt_free_fds(int maxfd)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker int freefds = 0;
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker for (maxfd--; maxfd >= 0; maxfd--)
27*49cdfc7eSAndroid Build Coastguard Worker if (fcntl(maxfd, F_GETFD) == -1 && errno == EBADF)
28*49cdfc7eSAndroid Build Coastguard Worker freefds++;
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker return freefds;
31*49cdfc7eSAndroid Build Coastguard Worker }
32*49cdfc7eSAndroid Build Coastguard Worker
setup(void)33*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker minfd = getdtablesize(); /* get number of files allowed open */
36*49cdfc7eSAndroid Build Coastguard Worker maxfd = minfd + 5;
37*49cdfc7eSAndroid Build Coastguard Worker freefds = cnt_free_fds(minfd);
38*49cdfc7eSAndroid Build Coastguard Worker pfildes = SAFE_MALLOC(maxfd * sizeof(int));
39*49cdfc7eSAndroid Build Coastguard Worker memset(pfildes, -1, maxfd * sizeof(int));
40*49cdfc7eSAndroid Build Coastguard Worker sprintf(pfilname, "./dup06.%d\n", getpid());
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)43*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker if (pfildes != NULL)
46*49cdfc7eSAndroid Build Coastguard Worker free(pfildes);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker
run(void)49*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker int i;
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker pfildes[0] = SAFE_CREAT(pfilname, 0666);
54*49cdfc7eSAndroid Build Coastguard Worker for (i = 1; i < maxfd; i++) {
55*49cdfc7eSAndroid Build Coastguard Worker pfildes[i] = dup(pfildes[i - 1]);
56*49cdfc7eSAndroid Build Coastguard Worker if (pfildes[i] == -1)
57*49cdfc7eSAndroid Build Coastguard Worker break;
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker if (i < freefds)
60*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Not enough files duped");
61*49cdfc7eSAndroid Build Coastguard Worker else if (i > freefds)
62*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Too many files duped");
63*49cdfc7eSAndroid Build Coastguard Worker else
64*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Test passed");
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(pfilname);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < maxfd; i++) {
69*49cdfc7eSAndroid Build Coastguard Worker if (pfildes[i] != 0 && pfildes[i] != -1)
70*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pfildes[i]);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
75*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
76*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
77*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
78*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
79*49cdfc7eSAndroid Build Coastguard Worker };
80