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 <avinesh.kumar@suse.com>
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
18static int pipefd[2];
19static volatile int sigpipe_cnt;
20
21static void sighandler(int sig)
22{
23	if (sig == SIGPIPE)
24		sigpipe_cnt++;
25}
26
27static 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
42static void setup(void)
43{
44	SAFE_SIGNAL(SIGPIPE, sighandler);
45}
46
47static 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
55static struct tst_test test = {
56	.setup = setup,
57	.test_all = run,
58	.cleanup = cleanup
59};
60