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 * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check that creat(2) system call returns EMFILE.
9*49cdfc7eSAndroid Build Coastguard Worker */
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <linux/limits.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker static int *opened_fds, num_opened_fds;
24*49cdfc7eSAndroid Build Coastguard Worker
verify_creat(void)25*49cdfc7eSAndroid Build Coastguard Worker static void verify_creat(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker TEST(creat("filename", 0666));
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
30*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "call succeeded unexpectedly");
31*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET);
32*49cdfc7eSAndroid Build Coastguard Worker return;
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EMFILE)
36*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "creat() failed with EMFILE");
37*49cdfc7eSAndroid Build Coastguard Worker else
38*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "Expected EMFILE");
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker
setup(void)41*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker int max_open;
44*49cdfc7eSAndroid Build Coastguard Worker int fd;
45*49cdfc7eSAndroid Build Coastguard Worker char fname[PATH_MAX];
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker /* get the maximum number of files that we can open */
48*49cdfc7eSAndroid Build Coastguard Worker max_open = getdtablesize();
49*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "getdtablesize() = %d", max_open);
50*49cdfc7eSAndroid Build Coastguard Worker opened_fds = SAFE_MALLOC(max_open * sizeof(int));
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker /* now open as many files as we can up to max_open */
53*49cdfc7eSAndroid Build Coastguard Worker do {
54*49cdfc7eSAndroid Build Coastguard Worker snprintf(fname, sizeof(fname), "creat05_%d", num_opened_fds);
55*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(fname, 0666);
56*49cdfc7eSAndroid Build Coastguard Worker opened_fds[num_opened_fds++] = fd;
57*49cdfc7eSAndroid Build Coastguard Worker } while (fd < max_open - 1);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Opened additional #%d fds", num_opened_fds);
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)62*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker int i;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < num_opened_fds; i++) {
67*49cdfc7eSAndroid Build Coastguard Worker if (close(opened_fds[i])) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "close(%i) failed",
69*49cdfc7eSAndroid Build Coastguard Worker opened_fds[i]);
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker free(opened_fds);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
77*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_creat,
78*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
79*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
80*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
81*49cdfc7eSAndroid Build Coastguard Worker };
82