blob: e2582f2bc5bc715abd378984b3d7f83139043cc5 [file] [log] [blame]
robbiew906d4682003-01-06 17:23:07 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
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
robbiew906d4682003-01-06 17:23:07 +000018 */
19
20/*
21 * NAME
22 * pipe02.c
23 *
24 * DESCRIPTION
25 * Check that if a child has a "broken pipe", this information
26 * is transmitted to the waiting parent.
27 *
28 * ALGORITHM
29 * 1. Create a pipe, fork child
30 * 2. Child writes to pipe, parent reads to verify the pipe is working
31 * 3. Parent closes the read end of the pipe, child writes to it
32 * 4. Parent checks to see that child was terminated with SIGPIPE
33 *
34 * USAGE: <for command-line>
35 * pipe02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -e : Turn on errno logging.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * HISTORY
44 * 12/2002 Ported by Paul Larson
45 *
46 * RESTRICTIONS
47 * None
48 */
49#include <signal.h>
50#include <fcntl.h>
51#include <errno.h>
robbiewbc3c43e2003-07-28 15:05:38 +000052#include <string.h>
robbiew906d4682003-01-06 17:23:07 +000053#include <sys/wait.h>
54#include <sys/types.h>
55#include "test.h"
robbiew906d4682003-01-06 17:23:07 +000056
57char *TCID = "pipe02";
58int TST_TOTAL = 1;
robbiew906d4682003-01-06 17:23:07 +000059
60int usrsig;
61
robbiewd34d5812005-07-11 22:28:09 +000062void do_child(void);
robbiew906d4682003-01-06 17:23:07 +000063void setup(void);
64void cleanup(void);
65void catch_usr2(int);
66
robbiewa728d282003-09-08 21:36:41 +000067ssize_t safe_read(int fd, void *buf, size_t count)
68{
69 ssize_t n;
70
71 do {
72 n = read(fd, buf, count);
73 } while (n < 0 && errno == EINTR);
74
75 return n;
76}
77
robbiewd34d5812005-07-11 22:28:09 +000078int pp[2]; /* pipe descriptor */
79
robbiew906d4682003-01-06 17:23:07 +000080int main(int ac, char **av)
81{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020082 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020083 const char *msg;
robbiew906d4682003-01-06 17:23:07 +000084 char rbuf[BUFSIZ], wbuf[BUFSIZ];
85 int pid, ret, len, rlen, status;
86 int sig = 0;
87
88 usrsig = 0;
89
Garrett Cooper45e285d2010-11-22 12:19:25 -080090 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 }
robbiewd34d5812005-07-11 22:28:09 +000093#ifdef UCLINUX
94 maybe_run_child(&do_child, "dd", &pp[0], &pp[1]);
95#endif
96
robbiew906d4682003-01-06 17:23:07 +000097 setup();
98
99 for (lc = 0; TEST_LOOPING(lc); lc++) {
100
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 /* reset tst_count in case we are looping */
102 tst_count = 0;
robbiew906d4682003-01-06 17:23:07 +0000103
104 ret = pipe(pp);
105 if (ret == -1)
106 tst_brkm(TFAIL, cleanup, "pipe() failed, errno %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000107 errno);
robbiew906d4682003-01-06 17:23:07 +0000108
109 strcpy(wbuf, "abcd\0");
110 len = strlen(wbuf);
111
robbiewd34d5812005-07-11 22:28:09 +0000112 pid = FORK_OR_VFORK();
robbiew906d4682003-01-06 17:23:07 +0000113 if (pid < 0)
114 tst_brkm(TFAIL, cleanup, "fork() failed, errno %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000115 errno);
robbiew906d4682003-01-06 17:23:07 +0000116 if (pid == 0) {
117 /* CHILD */
robbiewd34d5812005-07-11 22:28:09 +0000118#ifdef UCLINUX
119 if (self_exec(av[0], "dd", pp[0], pp[1]) < 0) {
120 tst_brkm(TBROK, cleanup, "self_exec failed");
121 }
122#else
123 do_child();
124#endif
robbiew906d4682003-01-06 17:23:07 +0000125 } else {
126 /* PARENT */
127 close(pp[1]); /* close write end of pipe */
robbiewa728d282003-09-08 21:36:41 +0000128 memset(rbuf, 0, sizeof(rbuf));
129 rlen = safe_read(pp[0], rbuf, len);
130 if (memcmp(wbuf, rbuf, len) != 0)
robbiew906d4682003-01-06 17:23:07 +0000131 tst_resm(TFAIL, "pipe read data and pipe "
subrata_modak56207ce2009-03-23 13:35:39 +0000132 "write data didn't match");
robbiew906d4682003-01-06 17:23:07 +0000133 close(pp[0]); /* close read end of pipe */
134 kill(pid, SIGUSR2);
135 wait(&status);
136
subrata_modak4bb656a2009-02-26 12:02:09 +0000137 if (WIFSIGNALED(status))
robbiew906d4682003-01-06 17:23:07 +0000138 sig = WTERMSIG(status);
139 if (sig != SIGPIPE)
140 tst_resm(TFAIL, "SIGPIPE not returned by "
subrata_modak56207ce2009-03-23 13:35:39 +0000141 "child process");
robbiew906d4682003-01-06 17:23:07 +0000142 else
143 tst_resm(TPASS, "child process returned "
subrata_modak56207ce2009-03-23 13:35:39 +0000144 "expected SIGPIPE");
robbiew906d4682003-01-06 17:23:07 +0000145 }
146 }
147 cleanup();
148
Garrett Cooper2c282152010-12-16 00:55:50 -0800149 tst_exit();
robbiew906d4682003-01-06 17:23:07 +0000150}
151
subrata_modak56207ce2009-03-23 13:35:39 +0000152void catch_usr2(int sig)
153{
robbiew906d4682003-01-06 17:23:07 +0000154 usrsig = 1;
155}
156
157/*
robbiewd34d5812005-07-11 22:28:09 +0000158 * do_child()
159 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400160void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000161{
162 char wbuf[BUFSIZ];
163 int len;
164
165 strcpy(wbuf, "abcd\0");
166 len = strlen(wbuf);
167
168 if (signal(SIGUSR2, catch_usr2) == SIG_ERR)
subrata_modak56207ce2009-03-23 13:35:39 +0000169 tst_resm(TWARN, "signal setup for SIGUSR2 " "failed");
robbiewd34d5812005-07-11 22:28:09 +0000170 if (signal(SIGPIPE, SIG_DFL) == SIG_ERR)
subrata_modak56207ce2009-03-23 13:35:39 +0000171 tst_resm(TWARN, "signal setup for SIGPIPE " "failed");
172 close(pp[0]); /* close read end of pipe */
robbiewd34d5812005-07-11 22:28:09 +0000173 write(pp[1], wbuf, len);
subrata_modak56207ce2009-03-23 13:35:39 +0000174 while (!usrsig)
robbiewd34d5812005-07-11 22:28:09 +0000175 sleep(1);
176 write(pp[1], wbuf, len);
177 exit(1);
178}
179
180/*
robbiew906d4682003-01-06 17:23:07 +0000181 * setup() - performs all ONE TIME setup for this test.
182 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400183void setup(void)
robbiew906d4682003-01-06 17:23:07 +0000184{
Garrett Cooper2c282152010-12-16 00:55:50 -0800185
robbiew906d4682003-01-06 17:23:07 +0000186 tst_sig(FORK, DEF_HANDLER, cleanup);
187
robbiew906d4682003-01-06 17:23:07 +0000188 TEST_PAUSE;
189
190}
191
192/*
193 * cleanup() - performs all ONE TIME cleanup for this test at
194 * completion or premature exit.
195 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400196void cleanup(void)
robbiew906d4682003-01-06 17:23:07 +0000197{
robbiew906d4682003-01-06 17:23:07 +0000198
Chris Dearmanec6edca2012-10-17 19:54:01 -0700199}