robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 18 | */ |
| 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> |
robbiew | bc3c43e | 2003-07-28 15:05:38 +0000 | [diff] [blame] | 52 | #include <string.h> |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 53 | #include <sys/wait.h> |
| 54 | #include <sys/types.h> |
| 55 | #include "test.h" |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 56 | |
| 57 | char *TCID = "pipe02"; |
| 58 | int TST_TOTAL = 1; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 59 | |
| 60 | int usrsig; |
| 61 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 62 | void do_child(void); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 63 | void setup(void); |
| 64 | void cleanup(void); |
| 65 | void catch_usr2(int); |
| 66 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 67 | ssize_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 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 78 | int pp[2]; /* pipe descriptor */ |
| 79 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 80 | int main(int ac, char **av) |
| 81 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 82 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 83 | const char *msg; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 84 | char rbuf[BUFSIZ], wbuf[BUFSIZ]; |
| 85 | int pid, ret, len, rlen, status; |
| 86 | int sig = 0; |
| 87 | |
| 88 | usrsig = 0; |
| 89 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 90 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 93 | #ifdef UCLINUX |
| 94 | maybe_run_child(&do_child, "dd", &pp[0], &pp[1]); |
| 95 | #endif |
| 96 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 97 | setup(); |
| 98 | |
| 99 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 100 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 101 | /* reset tst_count in case we are looping */ |
| 102 | tst_count = 0; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 103 | |
| 104 | ret = pipe(pp); |
| 105 | if (ret == -1) |
| 106 | tst_brkm(TFAIL, cleanup, "pipe() failed, errno %d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | errno); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 108 | |
| 109 | strcpy(wbuf, "abcd\0"); |
| 110 | len = strlen(wbuf); |
| 111 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 112 | pid = FORK_OR_VFORK(); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 113 | if (pid < 0) |
| 114 | tst_brkm(TFAIL, cleanup, "fork() failed, errno %d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | errno); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 116 | if (pid == 0) { |
| 117 | /* CHILD */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 118 | #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 |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 125 | } else { |
| 126 | /* PARENT */ |
| 127 | close(pp[1]); /* close write end of pipe */ |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 128 | memset(rbuf, 0, sizeof(rbuf)); |
| 129 | rlen = safe_read(pp[0], rbuf, len); |
| 130 | if (memcmp(wbuf, rbuf, len) != 0) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 131 | tst_resm(TFAIL, "pipe read data and pipe " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | "write data didn't match"); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 133 | close(pp[0]); /* close read end of pipe */ |
| 134 | kill(pid, SIGUSR2); |
| 135 | wait(&status); |
| 136 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 137 | if (WIFSIGNALED(status)) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 138 | sig = WTERMSIG(status); |
| 139 | if (sig != SIGPIPE) |
| 140 | tst_resm(TFAIL, "SIGPIPE not returned by " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | "child process"); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 142 | else |
| 143 | tst_resm(TPASS, "child process returned " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | "expected SIGPIPE"); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | cleanup(); |
| 148 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | tst_exit(); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 150 | } |
| 151 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | void catch_usr2(int sig) |
| 153 | { |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 154 | usrsig = 1; |
| 155 | } |
| 156 | |
| 157 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 158 | * do_child() |
| 159 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 160 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 161 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | tst_resm(TWARN, "signal setup for SIGUSR2 " "failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 170 | if (signal(SIGPIPE, SIG_DFL) == SIG_ERR) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | tst_resm(TWARN, "signal setup for SIGPIPE " "failed"); |
| 172 | close(pp[0]); /* close read end of pipe */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 173 | write(pp[1], wbuf, len); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | while (!usrsig) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 175 | sleep(1); |
| 176 | write(pp[1], wbuf, len); |
| 177 | exit(1); |
| 178 | } |
| 179 | |
| 180 | /* |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 181 | * setup() - performs all ONE TIME setup for this test. |
| 182 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 183 | void setup(void) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 184 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 185 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 186 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 187 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 188 | TEST_PAUSE; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 194 | * completion or premature exit. |
| 195 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 196 | void cleanup(void) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 197 | { |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 198 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 199 | } |