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 | * pipe03.c |
| 23 | * |
| 24 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * Make sure that writing to the read end of a pipe and reading from |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 26 | * the write end of a pipe both fail. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * 1. Open a pipe |
| 30 | * 2. Attempt to write to the [0] descriptor (expect -1) |
| 31 | * 3. Attempt to read from the [1] descriptor (expect -1) |
| 32 | * |
| 33 | * USAGE: <for command-line> |
| 34 | * pipe03 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 35 | * where, -c n : Run n copies concurrently. |
| 36 | * -f : Turn off functionality Testing. |
| 37 | * -i n : Execute test n times. |
| 38 | * -I x : Execute test for x seconds. |
| 39 | * -P x : Pause for x seconds between iterations. |
| 40 | * -t : Turn on syscall timing. |
| 41 | * |
| 42 | * HISTORY |
| 43 | * 11/2002 Ported by Paul Larson |
| 44 | */ |
| 45 | #include <unistd.h> |
| 46 | #include <errno.h> |
| 47 | #include "test.h" |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 48 | |
| 49 | char *TCID = "pipe03"; |
| 50 | int TST_TOTAL = 1; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 51 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 52 | void setup(void); |
| 53 | void cleanup(void); |
| 54 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 55 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 56 | { |
| 57 | ssize_t n; |
| 58 | |
| 59 | do { |
| 60 | n = read(fd, buf, count); |
| 61 | } while (n < 0 && errno == EINTR); |
| 62 | |
| 63 | return n; |
| 64 | } |
| 65 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 66 | int main(int ac, char **av) |
| 67 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 68 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int fildes[2]; /* fds for pipe read and write */ |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 72 | char rbuf[BUFSIZ]; |
| 73 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 74 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 75 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 76 | |
| 77 | setup(); |
| 78 | |
| 79 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 80 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 81 | /* reset tst_count in case we are looping */ |
| 82 | tst_count = 0; |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 83 | |
| 84 | TEST(pipe(fildes)); |
| 85 | |
| 86 | if (TEST_RETURN == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | tst_brkm(TBROK | TTERRNO, cleanup, |
| 88 | "pipe() failed unexpectedly"); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 89 | |
| 90 | TEST(write(fildes[0], "A", 1)); |
| 91 | if (TEST_RETURN == -1 && TEST_ERRNO == EBADF) |
| 92 | tst_resm(TPASS, "expected failure writing to " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | "read end of pipe"); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 94 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | tst_resm(TFAIL | TTERRNO, |
| 96 | "success when writing to read " |
| 97 | "end of pipe ret=%ld", TEST_RETURN); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 98 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 99 | TEST(safe_read(fildes[1], rbuf, 1)); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 100 | if (TEST_RETURN == -1 && TEST_ERRNO == EBADF) |
| 101 | tst_resm(TPASS, "expected failure reading from " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | "write end of pipe"); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 103 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | tst_resm(TFAIL | TTERRNO, "success when reading from " |
Garrett Cooper | c452c25 | 2010-12-19 10:12:42 -0800 | [diff] [blame] | 105 | "write end of pipe ret=%ld", TEST_RETURN); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 106 | } |
| 107 | cleanup(); |
| 108 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 109 | tst_exit(); |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * setup() - performs all ONE TIME setup for this test. |
| 114 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 115 | void setup(void) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 116 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 117 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 118 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 119 | |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 120 | TEST_PAUSE; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 125 | * completion or premature exit. |
| 126 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 127 | void cleanup(void) |
robbiew | 906d468 | 2003-01-06 17:23:07 +0000 | [diff] [blame] | 128 | { |
Garrett Cooper | c452c25 | 2010-12-19 10:12:42 -0800 | [diff] [blame] | 129 | } |