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