xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe03.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., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2003-2023 Linux Test Project
5*49cdfc7eSAndroid Build Coastguard Worker  */
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  * Verify that, an attempt to write to the read end of a pipe fails with EBADF
11*49cdfc7eSAndroid Build Coastguard Worker  * and an attempt to read from the write end of a pipe also fails with EBADF.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static int fd[2];
17*49cdfc7eSAndroid Build Coastguard Worker 
verify_pipe(void)18*49cdfc7eSAndroid Build Coastguard Worker static void verify_pipe(void)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	char buf[] = "abcdef";
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fd);
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(write(fd[0], "A", 1), EBADF);
25*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(read(fd[1], buf, 1), EBADF);
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd[0]);
28*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd[1]);
29*49cdfc7eSAndroid Build Coastguard Worker }
30*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)31*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
32*49cdfc7eSAndroid Build Coastguard Worker {
33*49cdfc7eSAndroid Build Coastguard Worker 	if (fd[0] > 0)
34*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd[0]);
35*49cdfc7eSAndroid Build Coastguard Worker 	if (fd[1] > 0)
36*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd[1]);
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
40*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_pipe,
41*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
42*49cdfc7eSAndroid Build Coastguard Worker };
43