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 | * pipe10.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that parent can open a pipe and have a child read from it |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Parent opens pipe, child reads. Passes if child can read all the |
| 29 | * characters written by the parent. |
| 30 | * |
| 31 | * USAGE: <for command-line> |
| 32 | * pipe10 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 33 | * where, -c n : Run n copies concurrently. |
| 34 | * -f : Turn off functionality Testing. |
| 35 | * -i n : Execute test n times. |
| 36 | * -I x : Execute test for x seconds. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * HISTORY |
| 41 | * 07/2001 Ported by Wayne Boyer |
| 42 | * |
| 43 | * RESTRICTIONS |
| 44 | * None |
| 45 | */ |
| 46 | #include <errno.h> |
| 47 | #include <unistd.h> |
| 48 | #include <wait.h> |
robbiew | bc3c43e | 2003-07-28 15:05:38 +0000 | [diff] [blame] | 49 | #include <string.h> |
| 50 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
| 52 | char *TCID = "pipe10"; |
| 53 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | void setup(void); |
| 56 | void cleanup(void); |
| 57 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 58 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 59 | { |
| 60 | ssize_t n; |
| 61 | |
| 62 | do { |
| 63 | n = read(fd, buf, count); |
| 64 | } while (n < 0 && errno == EINTR); |
| 65 | |
| 66 | return n; |
| 67 | } |
| 68 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 69 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | int fd[2]; /* fds for pipe read/write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | char wrbuf[BUFSIZ], rebuf[BUFSIZ]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | int red, written; /* no of chars read and */ |
| 77 | /* written to pipe */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | int length, greater, forkstat; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | int retval = 0, status, e_code; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 81 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 82 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | setup(); |
| 85 | |
| 86 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 87 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 88 | /* reset tst_count in case we are looping */ |
| 89 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | TEST(pipe(fd)); |
| 92 | |
| 93 | if (TEST_RETURN == -1) { |
plars | 40b56e8 | 2001-09-14 20:00:04 +0000 | [diff] [blame] | 94 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | tst_resm(TFAIL, "pipe creation failed"); |
| 96 | continue; |
| 97 | } |
| 98 | |
subrata_modak | da8fc61 | 2007-04-13 11:11:20 +0000 | [diff] [blame] | 99 | strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | length = strlen(wrbuf) + 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | written = write(fd[1], wrbuf, length); |
| 103 | |
| 104 | /* did write write at least some chars */ |
| 105 | if ((written < 0) || (written > length)) { |
| 106 | tst_brkm(TBROK, cleanup, "write to pipe failed"); |
| 107 | } |
| 108 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 109 | forkstat = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
| 111 | if (forkstat == -1) { |
| 112 | tst_brkm(TBROK, cleanup, "fork() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | if (forkstat == 0) { /* child */ |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 116 | red = safe_read(fd[0], rebuf, written); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
| 118 | /* did read , get at least some chars */ |
| 119 | if ((red < 0) || (red > written)) { |
| 120 | tst_brkm(TBROK, cleanup, "read pipe failed"); |
| 121 | } |
| 122 | |
| 123 | greater = strcmp(rebuf, wrbuf); |
| 124 | |
| 125 | /* are the strings written and read equal */ |
| 126 | if (greater == 0) { |
| 127 | tst_resm(TPASS, "functionality is correct"); |
| 128 | } else { |
plars | 40b56e8 | 2001-09-14 20:00:04 +0000 | [diff] [blame] | 129 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_resm(TFAIL, "read & write strings do " |
| 131 | "not match"); |
| 132 | } |
plars | 40b56e8 | 2001-09-14 20:00:04 +0000 | [diff] [blame] | 133 | exit(retval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } else { /* parent */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | /* wait for the child to finish */ |
| 136 | wait(&status); |
| 137 | /* make sure the child returned a good exit status */ |
| 138 | e_code = status >> 8; |
| 139 | if (e_code != 0) { |
| 140 | tst_resm(TFAIL, "Failures reported above"); |
| 141 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | cleanup(); |
| 145 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 146 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * setup() - performs all ONE TIME setup for this test. |
| 151 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 152 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 162 | * completion or premature exit. |
| 163 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 164 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 166 | } |