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