plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * pipe09.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that two processes can use the same pipe at the same time. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * 1. Open a pipe |
| 29 | * 2. Fork a child which writes to the pipe |
| 30 | * 3. Fork another child which writes a different character to the pipe |
| 31 | * 4. Have the parent read from the pipe |
| 32 | * 5. It should get the characters from both children. |
| 33 | * |
| 34 | * USAGE: <for command-line> |
| 35 | * pipe09 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 36 | * where, -c n : Run n copies concurrently. |
| 37 | * -f : Turn off functionality Testing. |
| 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 | * 07/2001 Ported by Wayne Boyer |
| 45 | * |
| 46 | * RESTRICTIONS |
| 47 | * None |
| 48 | */ |
| 49 | #include <unistd.h> |
| 50 | #include <signal.h> |
| 51 | #include <wait.h> |
| 52 | #include <errno.h> |
| 53 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 55 | #define PIPEWRTCNT 100 /* must be an even number */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
| 57 | char *TCID = "pipe09"; |
| 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | void setup(void); |
| 61 | void cleanup(void); |
| 62 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 63 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 64 | { |
| 65 | ssize_t n; |
| 66 | |
| 67 | do { |
| 68 | n = read(fd, buf, count); |
| 69 | } while (n < 0 && errno == EINTR); |
| 70 | |
| 71 | return n; |
| 72 | } |
| 73 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 74 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 77 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 79 | int i, red, wtstatus; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | int pipefd[2]; /* fds for pipe read/write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | char rebuf[BUFSIZ]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | int Acnt = 0, Bcnt = 0; /* count 'A' and 'B' */ |
| 83 | int fork_1, fork_2; /* ret values in parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 85 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 86 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
| 88 | setup(); |
| 89 | |
| 90 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 91 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 92 | /* reset tst_count in case we are looping */ |
| 93 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
| 95 | TEST(pipe(pipefd)); |
| 96 | |
| 97 | if (TEST_RETURN == -1) { |
| 98 | tst_resm(TFAIL, "pipe() call failed"); |
| 99 | continue; |
| 100 | } |
| 101 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 102 | if ((fork_1 = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | tst_brkm(TBROK, cleanup, "fork() #1 failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 104 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
| 106 | if (fork_1 == 0) { /* 1st child */ |
| 107 | if (close(pipefd[0]) != 0) { |
| 108 | tst_resm(TWARN, "pipefd[0] close failed, " |
| 109 | "errno = %d", errno); |
| 110 | exit(1); |
| 111 | } |
| 112 | |
| 113 | for (i = 0; i < PIPEWRTCNT / 2; ++i) { |
| 114 | if (write(pipefd[1], "A", 1) != 1) { |
| 115 | tst_resm(TWARN, "write to pipe failed"); |
| 116 | exit(1); |
| 117 | } |
| 118 | } |
| 119 | exit(0); |
| 120 | } |
| 121 | |
| 122 | /* parent */ |
| 123 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 124 | if (waitpid(fork_1, &wtstatus, 0) == -1) |
| 125 | tst_brkm(TBROK, cleanup, "waitpid failed"); |
Garrett Cooper | c452c25 | 2010-12-19 10:12:42 -0800 | [diff] [blame] | 126 | if (WIFEXITED(wtstatus) && WEXITSTATUS(wtstatus) != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 127 | tst_brkm(TBROK, cleanup, "child exited abnormally"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 130 | if ((fork_2 = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | tst_brkm(TBROK, cleanup, "fork() #2 failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
| 134 | if (fork_2 == 0) { /* 2nd child */ |
| 135 | if (close(pipefd[0]) != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 136 | perror("pipefd[0] close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | exit(1); |
| 138 | } |
| 139 | |
| 140 | for (i = 0; i < PIPEWRTCNT / 2; ++i) { |
| 141 | if (write(pipefd[1], "B", 1) != 1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 142 | perror("write to pipe failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | exit(1); |
| 144 | } |
| 145 | } |
| 146 | exit(0); |
| 147 | } |
| 148 | |
| 149 | /* parent */ |
| 150 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 151 | if (waitpid(fork_2, &wtstatus, 0) == -1) |
| 152 | tst_brkm(TBROK, cleanup, "waitpid failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | if (WEXITSTATUS(wtstatus) != 0) { |
| 154 | tst_brkm(TBROK, cleanup, "problem detected in child, " |
| 155 | "wait status %d, errno = %d", wtstatus, errno); |
| 156 | } |
| 157 | |
| 158 | if (close(pipefd[1]) != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | tst_brkm(TBROK | TERRNO, cleanup, |
| 160 | "pipefd[1] close failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 161 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 163 | while ((red = safe_read(pipefd[0], rebuf, 100)) > 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | for (i = 0; i < red; i++) { |
| 165 | if (rebuf[i] == 'A') { |
| 166 | Acnt++; |
| 167 | continue; |
| 168 | } |
| 169 | if (rebuf[i] == 'B') { |
| 170 | Bcnt++; |
| 171 | continue; |
| 172 | } |
| 173 | tst_resm(TFAIL, "got bogus '%c' character", |
| 174 | rebuf[i]); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (red == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tst_brkm(TBROK | TERRNO, cleanup, |
| 181 | "reading pipefd pipe failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (Bcnt == Acnt && Bcnt == (PIPEWRTCNT / 2)) { |
| 185 | tst_resm(TPASS, "functionality appears to be correct"); |
| 186 | } else { |
| 187 | tst_resm(TFAIL, "functionality is not correct - Acnt " |
| 188 | "= %d, Bcnt = %d", Acnt, Bcnt); |
| 189 | } |
| 190 | |
| 191 | /* clean up things in case we are looping */ |
| 192 | Acnt = Bcnt = 0; |
| 193 | } |
| 194 | cleanup(); |
| 195 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 196 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * setup() - performs all ONE TIME setup for this test. |
| 201 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 202 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 204 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | TEST_PAUSE; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 212 | * completion or premature exit. |
| 213 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 214 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | { |
Garrett Cooper | c452c25 | 2010-12-19 10:12:42 -0800 | [diff] [blame] | 216 | } |