1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * Copyright (c) 2020 SUSE LLC
5 *
6 */
7 /*\
8 * [Description]
9 *
10 * Verify that dup(2) syscall fails with errno EMFILE when the per-process
11 * limit on the number of open file descriptors has been reached.
12 */
13
14 #include <stdlib.h>
15 #include "tst_test.h"
16
17 static int *fd;
18 static int nfds;
19
run(void)20 static void run(void)
21 {
22 TST_EXP_FAIL2(dup(fd[0]), EMFILE, "dup(%d)", fd[0]);
23
24 if (TST_RET != -1)
25 SAFE_CLOSE(TST_RET);
26 }
27
setup(void)28 static void setup(void)
29 {
30 long maxfds;
31
32 maxfds = SAFE_SYSCONF(_SC_OPEN_MAX);
33 fd = SAFE_MALLOC(maxfds * sizeof(int));
34
35 fd[0] = SAFE_OPEN("dupfile", O_RDWR | O_CREAT, 0700);
36
37 for (nfds = 1; nfds < maxfds; nfds++) {
38 fd[nfds] = SAFE_DUP(fd[0]);
39 if (fd[nfds] >= maxfds - 1)
40 break;
41 }
42 }
43
cleanup(void)44 static void cleanup(void)
45 {
46 int i;
47
48 for (i = 0; i < nfds; i++)
49 SAFE_CLOSE(fd[i]);
50 }
51
52 static struct tst_test test = {
53 .test_all = run,
54 .setup = setup,
55 .cleanup = cleanup,
56 .needs_tmpdir = 1,
57 };
58