xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe14.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) 2023 SUSE LLC Avinesh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Verify that, if the write end of a pipe is closed, then a process reading
10*49cdfc7eSAndroid Build Coastguard Worker  * from the pipe will see end-of-file (i.e., read() returns 0) once it has
11*49cdfc7eSAndroid Build Coastguard Worker  * read all remaining data in the pipe.
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 fds[2];
17*49cdfc7eSAndroid Build Coastguard Worker 
run(void)18*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
21*49cdfc7eSAndroid Build Coastguard Worker 	char rdbuf[30];
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker 	memset(rdbuf, 0, sizeof(rdbuf));
24*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fds[1], wrbuf, sizeof(wrbuf));
27*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(0, fds[0], rdbuf, sizeof(wrbuf));
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_VAL(SAFE_READ(0, fds[0], rdbuf, 1), 0);
32*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[0]);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
38*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[0]);
39*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[1] > 0)
40*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[1]);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
44*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
45*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
46*49cdfc7eSAndroid Build Coastguard Worker };
47