xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe08.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *  07/2001 Ported by Wayne Boyer
5  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that, on any attempt to write to a pipe which is closed for
12  * reading will generate a SIGPIPE signal and write will fail with
13  * EPIPE errno.
14  */
15 
16 #include "tst_test.h"
17 
18 static int pipefd[2];
19 static volatile int sigpipe_cnt;
20 
sighandler(int sig)21 static void sighandler(int sig)
22 {
23 	if (sig == SIGPIPE)
24 		sigpipe_cnt++;
25 }
26 
run(void)27 static void run(void)
28 {
29 	char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
30 
31 	sigpipe_cnt = 0;
32 
33 	SAFE_PIPE(pipefd);
34 	SAFE_CLOSE(pipefd[0]);
35 
36 	TST_EXP_FAIL2_SILENT(write(pipefd[1], wrbuf, sizeof(wrbuf)), EPIPE);
37 	TST_EXP_EQ_LI(sigpipe_cnt, 1);
38 
39 	SAFE_CLOSE(pipefd[1]);
40 }
41 
setup(void)42 static void setup(void)
43 {
44 	SAFE_SIGNAL(SIGPIPE, sighandler);
45 }
46 
cleanup(void)47 static void cleanup(void)
48 {
49 	if (pipefd[0] > 0)
50 		SAFE_CLOSE(pipefd[0]);
51 	if (pipefd[1] > 0)
52 		SAFE_CLOSE(pipefd[1]);
53 }
54 
55 static struct tst_test test = {
56 	.setup = setup,
57 	.test_all = run,
58 	.cleanup = cleanup
59 };
60