1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * NAME
22 *	pipe08.c
23 *
24 * DESCRIPTION
25 *	Check that a SIGPIPE signal is generated when a write is
26 *	attempted on an empty pipe.
27 *
28 * ALGORITHM
29 *	1. Write to a pipe after closing the read side.
30 *	2. Check for the signal SIGPIPE to be received.
31 *
32 * USAGE:  <for command-line>
33 *  pipe08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
34 *     where,  -c n : Run n copies concurrently.
35 *             -f   : Turn off functionality Testing.
36 *             -i n : Execute test n times.
37 *             -I x : Execute test for x seconds.
38 *             -P x : Pause for x seconds between iterations.
39 *             -t   : Turn on syscall timing.
40 *
41 * USAGE
42 *	pipe08
43 *
44 * HISTORY
45 *	07/2001 Ported by Wayne Boyer
46 *
47 * RESTRICTIONS
48 *	None
49 */
50#include <errno.h>
51#include <unistd.h>
52#include <signal.h>
53#include <string.h>
54#include "test.h"
55
56char *TCID = "pipe08";
57int TST_TOTAL = 1;
58
59void setup(void);
60void cleanup(void);
61void sighandler(int);
62
63int main(int ac, char **av)
64{
65	int lc;
66
67	int pipefd[2];		/* fds for pipe read/write */
68	char wrbuf[BUFSIZ];
69	int written, length;
70	int close_stat;		/*  exit status of close(read fd) */
71
72	tst_parse_opts(ac, av, NULL, NULL);
73
74	setup();
75
76	for (lc = 0; TEST_LOOPING(lc); lc++) {
77
78		/* reset tst_count in case we are looping */
79		tst_count = 0;
80
81		TEST(pipe(pipefd));
82
83		if (TEST_RETURN != 0) {
84			tst_resm(TFAIL, "call failed unexpectedly");
85			continue;
86		}
87
88		if ((close_stat = close(pipefd[0])) == -1) {
89			tst_brkm(TBROK, cleanup, "close of read side failed");
90		}
91
92		strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz\0");
93		length = strlen(wrbuf);
94
95		/*
96		 * the SIGPIPE signal will be caught here or else
97		 * the program will dump core when the signal is
98		 * sent
99		 */
100		written = write(pipefd[1], wrbuf, length);
101	}
102	cleanup();
103	tst_exit();
104
105}
106
107/*
108 * sighandler - catch signals and look for SIGPIPE
109 */
110void sighandler(int sig)
111{
112	if (sig != SIGPIPE)
113		tst_resm(TFAIL, "expected SIGPIPE, got %d", sig);
114	else
115		tst_resm(TPASS, "got expected SIGPIPE signal");
116}
117
118/*
119 * setup() - performs all ONE TIME setup for this test.
120 */
121void setup(void)
122{
123
124	tst_sig(NOFORK, sighandler, cleanup);
125
126	TEST_PAUSE;
127}
128
129/*
130 * cleanup() - performs all ONE TIME cleanup for this test at
131 *	       completion or premature exit.
132 */
133void cleanup(void)
134{
135}
136