1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Linux Test Project, 2021
4 * Copyright (c) International Business Machines Corp., 2001
5 * 07/2001 Ported by Wayne Boyer
6 */
7
8 /*\
9 * [Description]
10 *
11 * Tests basic error handling of the fcntl syscall.
12 *
13 * - EMFILE when cmd is F_DUPFD and the per-process limit on the number of open
14 * file descriptors has been reached.
15 */
16
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include "tst_test.h"
23
24 static char fname[20] = "testfile";
25 static int fd = -1, max_files;
26
verify_fcntl(void)27 static void verify_fcntl(void)
28 {
29 pid_t pid;
30 int i;
31
32 pid = SAFE_FORK();
33 if (pid == 0) {
34 for (i = 0; i < max_files; i++) {
35 fd = open(fname, O_CREAT | O_RDONLY, 0444);
36 if (fd == -1)
37 break;
38 }
39 TST_EXP_FAIL2(fcntl(1, F_DUPFD, 1), EMFILE,
40 "fcntl(1, F_DUPFD, 1)");
41 }
42
43 tst_reap_children();
44 }
45
setup(void)46 static void setup(void)
47 {
48 max_files = getdtablesize();
49 }
50
cleanup(void)51 static void cleanup(void)
52 {
53 if (fd > -1)
54 SAFE_CLOSE(fd);
55
56 SAFE_UNLINK(fname);
57 }
58
59 static struct tst_test test = {
60 .forks_child = 1,
61 .needs_tmpdir = 1,
62 .setup = setup,
63 .cleanup = cleanup,
64 .test_all = verify_fcntl,
65 };
66