• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
56 char *TCID = "pipe08";
57 int TST_TOTAL = 1;
58 
59 void setup(void);
60 void cleanup(void);
61 void sighandler(int);
62 
main(int ac,char ** av)63 int 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 		if (written > 0)
102 			tst_brkm(TBROK, cleanup, "write succeeded unexpectedly");
103 	}
104 	cleanup();
105 	tst_exit();
106 
107 }
108 
109 /*
110  * sighandler - catch signals and look for SIGPIPE
111  */
sighandler(int sig)112 void sighandler(int sig)
113 {
114 	if (sig != SIGPIPE)
115 		tst_resm(TFAIL, "expected SIGPIPE, got %d", sig);
116 	else
117 		tst_resm(TPASS, "got expected SIGPIPE signal");
118 }
119 
120 /*
121  * setup() - performs all ONE TIME setup for this test.
122  */
setup(void)123 void setup(void)
124 {
125 
126 	tst_sig(NOFORK, sighandler, cleanup);
127 
128 	TEST_PAUSE;
129 }
130 
131 /*
132  * cleanup() - performs all ONE TIME cleanup for this test at
133  *	       completion or premature exit.
134  */
cleanup(void)135 void cleanup(void)
136 {
137 }
138