xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * 07/2001 Ported by Wayne Boyer
5  * Copyright (c) Linux Test Project, 2002-2015
6  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Verify that, pipe(2) syscall fails with errno EMFILE when
13  * limit on the number of open file descriptors has been reached.
14  */
15 
16 #include "tst_test.h"
17 #include <stdlib.h>
18 
19 static int fds[2];
20 static int *opened_fds, num_opened_fds;
21 
setup(void)22 static void setup(void)
23 {
24 	int max_fds;
25 
26 	max_fds = getdtablesize();
27 	tst_res(TINFO, "getdtablesize() = %d", max_fds);
28 	opened_fds = SAFE_MALLOC(max_fds * sizeof(int));
29 
30 	do {
31 		SAFE_PIPE(fds);
32 		opened_fds[num_opened_fds++] = fds[0];
33 		opened_fds[num_opened_fds++] = fds[1];
34 	} while (fds[1] < max_fds - 2);
35 
36 	tst_res(TINFO, "Number of fds opened by pipe calls: %d", num_opened_fds);
37 }
38 
run(void)39 static void run(void)
40 {
41 	TST_EXP_FAIL(pipe(fds), EMFILE);
42 }
43 
cleanup(void)44 static void cleanup(void)
45 {
46 	for (int i = 0; i < num_opened_fds; i++)
47 		SAFE_CLOSE(opened_fds[i]);
48 
49 	if (opened_fds)
50 		free(opened_fds);
51 }
52 
53 static struct tst_test test = {
54 	.setup = setup,
55 	.cleanup = cleanup,
56 	.test_all = run
57 };
58