xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe08.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, on any attempt to write to a pipe which is closed for
12*49cdfc7eSAndroid Build Coastguard Worker  * reading will generate a SIGPIPE signal and write will fail with
13*49cdfc7eSAndroid Build Coastguard Worker  * EPIPE errno.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static int pipefd[2];
19*49cdfc7eSAndroid Build Coastguard Worker static volatile int sigpipe_cnt;
20*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig)21*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	if (sig == SIGPIPE)
24*49cdfc7eSAndroid Build Coastguard Worker 		sigpipe_cnt++;
25*49cdfc7eSAndroid Build Coastguard Worker }
26*49cdfc7eSAndroid Build Coastguard Worker 
run(void)27*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	sigpipe_cnt = 0;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pipefd);
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipefd[0]);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2_SILENT(write(pipefd[1], wrbuf, sizeof(wrbuf)), EPIPE);
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(sigpipe_cnt, 1);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipefd[1]);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)42*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGPIPE, sighandler);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	if (pipefd[0] > 0)
50*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(pipefd[0]);
51*49cdfc7eSAndroid Build Coastguard Worker 	if (pipefd[1] > 0)
52*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(pipefd[1]);
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
56*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
57*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
58*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
59*49cdfc7eSAndroid Build Coastguard Worker };
60