blob: 45198605552c969380a2fa5a52fb7cb511f8788f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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>
robbiewbc3c43e2003-07-28 15:05:38 +000053#include <string.h>
plars865695b2001-08-27 22:15:12 +000054#include "test.h"
plars865695b2001-08-27 22:15:12 +000055
56char *TCID = "pipe08";
57int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000058
59void setup(void);
60void cleanup(void);
61void sighandler(int);
62
plars74948ad2002-11-14 16:16:14 +000063int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000064{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020065 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
plars865695b2001-08-27 22:15:12 +000067
subrata_modak56207ce2009-03-23 13:35:39 +000068 int pipefd[2]; /* fds for pipe read/write */
plars865695b2001-08-27 22:15:12 +000069 char wrbuf[BUFSIZ];
70 int written, length;
subrata_modak56207ce2009-03-23 13:35:39 +000071 int close_stat; /* exit status of close(read fd) */
plars865695b2001-08-27 22:15:12 +000072
Garrett Cooper53740502010-12-16 00:04:01 -080073 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080074 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000075
plars865695b2001-08-27 22:15:12 +000076 setup();
77
78 for (lc = 0; TEST_LOOPING(lc); lc++) {
79
Caspar Zhangd59a6592013-03-07 14:59:12 +080080 /* reset tst_count in case we are looping */
81 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000082
83 TEST(pipe(pipefd));
84
85 if (TEST_RETURN != 0) {
86 tst_resm(TFAIL, "call failed unexpectedly");
87 continue;
88 }
89
90 if ((close_stat = close(pipefd[0])) == -1) {
91 tst_brkm(TBROK, cleanup, "close of read side failed");
92 }
93
94 strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz\0");
95 length = strlen(wrbuf);
96
97 /*
98 * the SIGPIPE signal will be caught here or else
99 * the program will dump core when the signal is
100 * sent
101 */
102 written = write(pipefd[1], wrbuf, length);
103 }
104 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800105 tst_exit();
plars865695b2001-08-27 22:15:12 +0000106
plars865695b2001-08-27 22:15:12 +0000107}
108
109/*
110 * sighandler - catch signals and look for SIGPIPE
111 */
subrata_modak56207ce2009-03-23 13:35:39 +0000112void sighandler(int sig)
plars865695b2001-08-27 22:15:12 +0000113{
Garrett Cooper52626672010-12-20 15:03:21 -0800114 if (sig != SIGPIPE)
plars865695b2001-08-27 22:15:12 +0000115 tst_resm(TFAIL, "expected SIGPIPE, got %d", sig);
Garrett Cooper52626672010-12-20 15:03:21 -0800116 else
plars865695b2001-08-27 22:15:12 +0000117 tst_resm(TPASS, "got expected SIGPIPE signal");
plars865695b2001-08-27 22:15:12 +0000118}
119
120/*
121 * setup() - performs all ONE TIME setup for this test.
122 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400123void setup(void)
plars865695b2001-08-27 22:15:12 +0000124{
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
plars865695b2001-08-27 22:15:12 +0000126 tst_sig(NOFORK, sighandler, cleanup);
127
plars865695b2001-08-27 22:15:12 +0000128 TEST_PAUSE;
129}
130
131/*
132 * cleanup() - performs all ONE TIME cleanup for this test at
133 * completion or premature exit.
134 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400135void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000136{
Garrett Cooper52626672010-12-20 15:03:21 -0800137}