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> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 48 | #include <sys/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 | |
Cyril Hrubis | f467e7b | 2016-02-08 18:50:38 +0100 | [diff] [blame] | 58 | ssize_t do_read(int fd, void *buf, size_t count) |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | int fd[2]; /* fds for pipe read/write */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | char wrbuf[BUFSIZ], rebuf[BUFSIZ]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | int red, written; /* no of chars read and */ |
| 76 | /* written to pipe */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | int length, greater, forkstat; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | int retval = 0, status, e_code; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 80 | tst_parse_opts(ac, av, NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | setup(); |
| 83 | |
| 84 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 85 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 86 | /* reset tst_count in case we are looping */ |
| 87 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | TEST(pipe(fd)); |
| 90 | |
| 91 | if (TEST_RETURN == -1) { |
plars | 40b56e8 | 2001-09-14 20:00:04 +0000 | [diff] [blame] | 92 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | tst_resm(TFAIL, "pipe creation failed"); |
| 94 | continue; |
| 95 | } |
| 96 | |
subrata_modak | da8fc61 | 2007-04-13 11:11:20 +0000 | [diff] [blame] | 97 | strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | length = strlen(wrbuf) + 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
| 100 | written = write(fd[1], wrbuf, length); |
| 101 | |
| 102 | /* did write write at least some chars */ |
| 103 | if ((written < 0) || (written > length)) { |
| 104 | tst_brkm(TBROK, cleanup, "write to pipe failed"); |
| 105 | } |
| 106 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 107 | forkstat = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | if (forkstat == -1) { |
| 110 | tst_brkm(TBROK, cleanup, "fork() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | if (forkstat == 0) { /* child */ |
Cyril Hrubis | f467e7b | 2016-02-08 18:50:38 +0100 | [diff] [blame] | 114 | red = do_read(fd[0], rebuf, written); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
| 116 | /* did read , get at least some chars */ |
| 117 | if ((red < 0) || (red > written)) { |
| 118 | tst_brkm(TBROK, cleanup, "read pipe failed"); |
| 119 | } |
| 120 | |
| 121 | greater = strcmp(rebuf, wrbuf); |
| 122 | |
| 123 | /* are the strings written and read equal */ |
| 124 | if (greater == 0) { |
| 125 | tst_resm(TPASS, "functionality is correct"); |
| 126 | } else { |
plars | 40b56e8 | 2001-09-14 20:00:04 +0000 | [diff] [blame] | 127 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | tst_resm(TFAIL, "read & write strings do " |
| 129 | "not match"); |
| 130 | } |
plars | 40b56e8 | 2001-09-14 20:00:04 +0000 | [diff] [blame] | 131 | exit(retval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } else { /* parent */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | /* wait for the child to finish */ |
| 134 | wait(&status); |
| 135 | /* make sure the child returned a good exit status */ |
| 136 | e_code = status >> 8; |
| 137 | if (e_code != 0) { |
| 138 | tst_resm(TFAIL, "Failures reported above"); |
| 139 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | cleanup(); |
| 143 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 144 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | * setup() - performs all ONE TIME setup for this test. |
| 149 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 150 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | TEST_PAUSE; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 160 | * completion or premature exit. |
| 161 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 162 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 164 | } |