xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe10.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) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that, when a parent process opens a pipe, a child process can
12*49cdfc7eSAndroid Build Coastguard Worker  * read from it.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
19*49cdfc7eSAndroid Build Coastguard Worker 
run(void)20*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	int wr_cnt, rd_cnt;
23*49cdfc7eSAndroid Build Coastguard Worker 	char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
24*49cdfc7eSAndroid Build Coastguard Worker 	char rdbuf[BUFSIZ];
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
27*49cdfc7eSAndroid Build Coastguard Worker 	wr_cnt = SAFE_WRITE(SAFE_WRITE_ALL, fds[1], wrbuf, sizeof(wrbuf));
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_FORK()) {
30*49cdfc7eSAndroid Build Coastguard Worker 		rd_cnt = SAFE_READ(1, fds[0], rdbuf, wr_cnt);
31*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_EQ_LU(wr_cnt, rd_cnt);
32*49cdfc7eSAndroid Build Coastguard Worker 	}
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[0]);
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)39*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
42*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[0]);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[1] > 0)
45*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[1]);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
49*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
50*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
51*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
52*49cdfc7eSAndroid Build Coastguard Worker };
53