xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe11.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  * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker  * Check if many children can read what is written to a pipe by the parent.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
12*49cdfc7eSAndroid Build Coastguard Worker  *   For a different nchilds number:
13*49cdfc7eSAndroid Build Coastguard Worker  *	1. Open a pipe and write nchilds * (PIPE_BUF/nchilds) bytes into it
14*49cdfc7eSAndroid Build Coastguard Worker  *	2. Fork nchilds children
15*49cdfc7eSAndroid Build Coastguard Worker  *	3. Each child reads PIPE_BUF/nchilds characters and checks that the
16*49cdfc7eSAndroid Build Coastguard Worker  *	   bytes read are correct
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
22*49cdfc7eSAndroid Build Coastguard Worker static unsigned char buf[PIPE_BUF];
23*49cdfc7eSAndroid Build Coastguard Worker static size_t read_per_child;
24*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void)25*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	size_t nread;
28*49cdfc7eSAndroid Build Coastguard Worker 	unsigned char rbuf[read_per_child];
29*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	nread = SAFE_READ(0, fds[0], rbuf, sizeof(rbuf));
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	if (nread != read_per_child) {
36*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Invalid read size child %i size %zu",
37*49cdfc7eSAndroid Build Coastguard Worker 		        getpid(), nread);
38*49cdfc7eSAndroid Build Coastguard Worker 		return;
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < read_per_child; i++) {
42*49cdfc7eSAndroid Build Coastguard Worker 		if (rbuf[i] != (i % 256)) {
43*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
44*49cdfc7eSAndroid Build Coastguard Worker 			        "Invalid byte read child %i byte %i have %i expected %i",
45*49cdfc7eSAndroid Build Coastguard Worker 				getpid(), i, rbuf[i], i % 256);
46*49cdfc7eSAndroid Build Coastguard Worker 			return;
47*49cdfc7eSAndroid Build Coastguard Worker 		}
48*49cdfc7eSAndroid Build Coastguard Worker 	}
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Child %i read pipe buffer correctly", getpid());
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static unsigned int childs[] = {
54*49cdfc7eSAndroid Build Coastguard Worker 	1,
55*49cdfc7eSAndroid Build Coastguard Worker 	2,
56*49cdfc7eSAndroid Build Coastguard Worker 	3,
57*49cdfc7eSAndroid Build Coastguard Worker 	4,
58*49cdfc7eSAndroid Build Coastguard Worker 	10,
59*49cdfc7eSAndroid Build Coastguard Worker 	50
60*49cdfc7eSAndroid Build Coastguard Worker };
61*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int tcase)62*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int tcase)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
65*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int nchilds = childs[tcase];
66*49cdfc7eSAndroid Build Coastguard Worker 	read_per_child = PIPE_BUF/nchilds;
67*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i, j;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Reading %zu per each of %u children",
70*49cdfc7eSAndroid Build Coastguard Worker 	        read_per_child, nchilds);
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nchilds; i++) {
73*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < read_per_child; j++) {
74*49cdfc7eSAndroid Build Coastguard Worker 			buf[i * read_per_child + j] = j % 256;
75*49cdfc7eSAndroid Build Coastguard Worker 		}
76*49cdfc7eSAndroid Build Coastguard Worker 	}
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fds[1], buf, read_per_child * nchilds);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nchilds; i++) {
83*49cdfc7eSAndroid Build Coastguard Worker 		pid = SAFE_FORK();
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 		if (!pid) {
86*49cdfc7eSAndroid Build Coastguard Worker 			do_child();
87*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
88*49cdfc7eSAndroid Build Coastguard Worker 		}
89*49cdfc7eSAndroid Build Coastguard Worker 	}
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[0]);
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
98*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
99*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
100*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(childs),
101*49cdfc7eSAndroid Build Coastguard Worker };
102