xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe13.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) 2020 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
7*49cdfc7eSAndroid Build Coastguard Worker  * This case is designed to test whether pipe can wakeup all readers
8*49cdfc7eSAndroid Build Coastguard Worker  * when last writer closes.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This is also a regression test for commit 6551d5c56eb0
11*49cdfc7eSAndroid Build Coastguard Worker  * ("pipe: make sure to wake up everybody when the last reader/writer closes").
12*49cdfc7eSAndroid Build Coastguard Worker  * This bug was introduced by commit 0ddad21d3e99 ("pipe: use exclusive
13*49cdfc7eSAndroid Build Coastguard Worker  * waits when reading or writing").
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
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 unsigned int tcases[] = {
22*49cdfc7eSAndroid Build Coastguard Worker 	2,
23*49cdfc7eSAndroid Build Coastguard Worker 	10,
24*49cdfc7eSAndroid Build Coastguard Worker 	27,
25*49cdfc7eSAndroid Build Coastguard Worker 	100
26*49cdfc7eSAndroid Build Coastguard Worker };
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
29*49cdfc7eSAndroid Build Coastguard Worker 
do_child(unsigned int i)30*49cdfc7eSAndroid Build Coastguard Worker static void do_child(unsigned int i)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	char buf;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
35*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(i);
36*49cdfc7eSAndroid Build Coastguard Worker 	int ret = SAFE_READ(0, fds[0], &buf, 1);
37*49cdfc7eSAndroid Build Coastguard Worker 	if (ret != 0)
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Wrong return from read %i", ret);
39*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
verify_pipe(unsigned int n)42*49cdfc7eSAndroid Build Coastguard Worker static void verify_pipe(unsigned int n)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
45*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i, cnt = 0, sleep_us = 1, fail = 0;
46*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int child_num = tcases[n];
47*49cdfc7eSAndroid Build Coastguard Worker 	int pid[child_num];
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
50*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Creating %d child processes", child_num);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < child_num; i++) {
53*49cdfc7eSAndroid Build Coastguard Worker 		pid[i] = SAFE_FORK();
54*49cdfc7eSAndroid Build Coastguard Worker 		if (pid[i] == 0)
55*49cdfc7eSAndroid Build Coastguard Worker 			do_child(i);
56*49cdfc7eSAndroid Build Coastguard Worker 		TST_CHECKPOINT_WAIT(i);
57*49cdfc7eSAndroid Build Coastguard Worker 		TST_PROCESS_STATE_WAIT(pid[i], 'S', 0);
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[0]);
61*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	while (cnt < child_num && sleep_us < 1000000) {
64*49cdfc7eSAndroid Build Coastguard Worker 		ret = waitpid(-1, NULL, WNOHANG);
65*49cdfc7eSAndroid Build Coastguard Worker 		if (ret < 0)
66*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TERRNO, "waitpid()");
67*49cdfc7eSAndroid Build Coastguard Worker 		if (ret > 0) {
68*49cdfc7eSAndroid Build Coastguard Worker 			cnt++;
69*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < child_num; i++) {
70*49cdfc7eSAndroid Build Coastguard Worker 				if (pid[i] == ret)
71*49cdfc7eSAndroid Build Coastguard Worker 					pid[i] = 0;
72*49cdfc7eSAndroid Build Coastguard Worker 			}
73*49cdfc7eSAndroid Build Coastguard Worker 			continue;
74*49cdfc7eSAndroid Build Coastguard Worker 		}
75*49cdfc7eSAndroid Build Coastguard Worker 		usleep(sleep_us);
76*49cdfc7eSAndroid Build Coastguard Worker 		sleep_us *= 2;
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < child_num; i++) {
80*49cdfc7eSAndroid Build Coastguard Worker 		if (pid[i]) {
81*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "pid %i still sleeps", pid[i]);
82*49cdfc7eSAndroid Build Coastguard Worker 			fail = 1;
83*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_KILL(pid[i], SIGKILL);
84*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_WAIT(NULL);
85*49cdfc7eSAndroid Build Coastguard Worker 		}
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	if (fail)
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Closed pipe didn't wake up everyone");
90*49cdfc7eSAndroid Build Coastguard Worker 	else
91*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Closed pipe waked up everyone");
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
95*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_pipe,
96*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
97*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
98*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
99*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
100*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "6551d5c56eb"},
101*49cdfc7eSAndroid Build Coastguard Worker 		{}
102*49cdfc7eSAndroid Build Coastguard Worker 	}
103*49cdfc7eSAndroid Build Coastguard Worker };
104