xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
6  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
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 	SAFE_CLOSEDIR(dir);
49 
50 	return num_opened_fds;
51 }
52 
setup(void)53 static void setup(void)
54 {
55 	int max_fds;
56 
57 	max_fds = getdtablesize();
58 	tst_res(TINFO, "getdtablesize() = %d", max_fds);
59 	pipe_fds = SAFE_MALLOC(max_fds * sizeof(int));
60 
61 	exp_num_pipes = (max_fds - record_open_fds()) / 2 * 2;
62 	tst_res(TINFO, "expected max fds to be opened by pipe(): %d", exp_num_pipes);
63 }
64 
run(void)65 static void run(void)
66 {
67 	int fds[2];
68 
69 	do {
70 		TEST(pipe(fds));
71 		if (!TST_RET) {
72 			pipe_fds[num_pipe_fds++] = fds[0];
73 			pipe_fds[num_pipe_fds++] = fds[1];
74 		}
75 	} while (!TST_RET);
76 
77 	TST_EXP_EQ_LI(errno, EMFILE);
78 	TST_EXP_EQ_LI(exp_num_pipes, num_pipe_fds);
79 
80 	for (int i = 0; i < num_pipe_fds; i++)
81 		SAFE_CLOSE(pipe_fds[i]);
82 
83 	num_pipe_fds = 0;
84 }
85 
cleanup(void)86 static void cleanup(void)
87 {
88 	for (int i = 0; i < num_pipe_fds; i++)
89 		if (pipe_fds[i] > 0)
90 			SAFE_CLOSE(pipe_fds[i]);
91 
92 	if (pipe_fds)
93 		free(pipe_fds);
94 
95 	if (opened_fds)
96 		free(opened_fds);
97 }
98 
99 static struct tst_test test = {
100 	.setup = setup,
101 	.cleanup = cleanup,
102 	.test_all = run
103 };
104