1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2002
4 * Ported by Paul Larson
5 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
6 * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Verify that, pipe(2) syscall can open the maximum number of
13 * file descriptors permitted.
14 */
15
16 #include "tst_test.h"
17 #include <stdlib.h>
18
19 static int *opened_fds, *pipe_fds;
20 static int num_pipe_fds, exp_num_pipes;
21
record_open_fds(void)22 static int record_open_fds(void)
23 {
24 DIR *dir;
25 struct dirent *ent;
26 int fd;
27 int num_opened_fds = 0;
28 int arr_size = 0;
29
30 dir = SAFE_OPENDIR("/proc/self/fd");
31
32 while ((ent = SAFE_READDIR(dir))) {
33 if (!strcmp(ent->d_name, ".") ||
34 !strcmp(ent->d_name, ".."))
35 continue;
36 fd = atoi(ent->d_name);
37
38 if (fd == dirfd(dir))
39 continue;
40
41 if (num_opened_fds >= arr_size) {
42 arr_size = MAX(1, arr_size * 2);
43 opened_fds = SAFE_REALLOC(opened_fds, arr_size * sizeof(int));
44 }
45 opened_fds[num_opened_fds++] = fd;
46 }
47
48 return num_opened_fds;
49 }
50
setup(void)51 static void setup(void)
52 {
53 int max_fds;
54
55 max_fds = getdtablesize();
56 tst_res(TINFO, "getdtablesize() = %d", max_fds);
57 pipe_fds = SAFE_MALLOC(max_fds * sizeof(int));
58
59 exp_num_pipes = (max_fds - record_open_fds()) / 2;
60 tst_res(TINFO, "expected max fds to be opened by pipe(): %d", exp_num_pipes * 2);
61 }
62
run(void)63 static void run(void)
64 {
65 int fds[2];
66
67 do {
68 TEST(pipe(fds));
69 if (!TST_RET) {
70 pipe_fds[num_pipe_fds++] = fds[0];
71 pipe_fds[num_pipe_fds++] = fds[1];
72 }
73 } while (!TST_RET);
74
75 TST_EXP_EQ_LI(errno, EMFILE);
76 TST_EXP_EQ_LI(exp_num_pipes * 2, num_pipe_fds);
77
78 for (int i = 0; i < num_pipe_fds; i++)
79 SAFE_CLOSE(pipe_fds[i]);
80
81 num_pipe_fds = 0;
82 }
83
cleanup(void)84 static void cleanup(void)
85 {
86 for (int i = 0; i < num_pipe_fds; i++)
87 if (pipe_fds[i] > 0)
88 SAFE_CLOSE(pipe_fds[i]);
89
90 if (pipe_fds)
91 free(pipe_fds);
92
93 if (opened_fds)
94 free(opened_fds);
95 }
96
97 static struct tst_test test = {
98 .setup = setup,
99 .cleanup = cleanup,
100 .test_all = run
101 };
102