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