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