xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/dup2/dup201.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  * 01/2002 Removed EMFILE test - Paul Larson
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Negative tests for dup2() with bad fd (EBADF).
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - First fd argument is less than 0
13*49cdfc7eSAndroid Build Coastguard Worker  * - First fd argument is getdtablesize()
14*49cdfc7eSAndroid Build Coastguard Worker  * - Second fd argument is less than 0
15*49cdfc7eSAndroid Build Coastguard Worker  * - Second fd argument is getdtablesize()
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static int maxfd, mystdout;
24*49cdfc7eSAndroid Build Coastguard Worker static int goodfd = 5;
25*49cdfc7eSAndroid Build Coastguard Worker static int badfd = -1;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
28*49cdfc7eSAndroid Build Coastguard Worker 	int *ofd;
29*49cdfc7eSAndroid Build Coastguard Worker 	int *nfd;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{&badfd, &goodfd},
32*49cdfc7eSAndroid Build Coastguard Worker 	{&maxfd, &goodfd},
33*49cdfc7eSAndroid Build Coastguard Worker 	{&mystdout, &badfd},
34*49cdfc7eSAndroid Build Coastguard Worker 	{&mystdout, &maxfd},
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)37*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	/* get some test specific values */
40*49cdfc7eSAndroid Build Coastguard Worker 	maxfd = getdtablesize();
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)43*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = tcases + i;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(dup2(*tc->ofd, *tc->nfd), EBADF,
48*49cdfc7eSAndroid Build Coastguard Worker 			"dup2(%d, %d)", *tc->ofd, *tc->nfd);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
52*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
53*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
54*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
55*49cdfc7eSAndroid Build Coastguard Worker };
56