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., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Verify that open(2) fails with EMFILE when per-process limit on the number
11*49cdfc7eSAndroid Build Coastguard Worker * of open file descriptors has been reached.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
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 #define FNAME "open04"
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker static int fds_limit, first, i;
21*49cdfc7eSAndroid Build Coastguard Worker static int *fds;
22*49cdfc7eSAndroid Build Coastguard Worker static char fname[20];
23*49cdfc7eSAndroid Build Coastguard Worker
setup(void)24*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker int fd;
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker fds_limit = getdtablesize();
29*49cdfc7eSAndroid Build Coastguard Worker first = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0777);
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first));
32*49cdfc7eSAndroid Build Coastguard Worker fds[0] = first;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker for (i = first + 1; i < fds_limit; i++) {
35*49cdfc7eSAndroid Build Coastguard Worker sprintf(fname, FNAME ".%d", i);
36*49cdfc7eSAndroid Build Coastguard Worker fd = open(fname, O_RDWR | O_CREAT, 0777);
37*49cdfc7eSAndroid Build Coastguard Worker if (fd == -1) {
38*49cdfc7eSAndroid Build Coastguard Worker if (errno != EMFILE)
39*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Expected EMFILE but got %d", errno);
40*49cdfc7eSAndroid Build Coastguard Worker fds_limit = i;
41*49cdfc7eSAndroid Build Coastguard Worker break;
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker fds[i - first] = fd;
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
run(void)47*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker sprintf(fname, FNAME ".%d", fds_limit);
50*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777), EMFILE);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)53*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker if (!first || !fds)
56*49cdfc7eSAndroid Build Coastguard Worker return;
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker for (i = first; i < fds_limit; i++)
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fds[i - first]);
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker if (fds)
62*49cdfc7eSAndroid Build Coastguard Worker free(fds);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
66*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
67*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
68*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
69*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1
70*49cdfc7eSAndroid Build Coastguard Worker };
71