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 | * pipe01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of the pipe(2) syscall: |
| 26 | * Check that both ends of the pipe (both file descriptors) are |
| 27 | * available to a process opening the pipe. |
| 28 | * |
| 29 | * ALGORITHM |
| 30 | * Write a string of characters down a pipe; read the string from the |
| 31 | * other file descriptor. Test passes if both can be done, as reported |
| 32 | * by the number of characters written and read. |
| 33 | * |
| 34 | * USAGE: <for command-line> |
| 35 | * pipe01 [-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 | * RESTRICITONS |
| 44 | * NONE |
| 45 | */ |
| 46 | #include <unistd.h> |
| 47 | #include <errno.h> |
robbiew | bc3c43e | 2003-07-28 15:05:38 +0000 | [diff] [blame] | 48 | #include <string.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
| 51 | char *TCID = "pipe01"; |
| 52 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
| 54 | void setup(void); |
| 55 | void cleanup(void); |
| 56 | |
robbiew | a728d28 | 2003-09-08 21:36:41 +0000 | [diff] [blame] | 57 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 58 | { |
| 59 | ssize_t n; |
| 60 | |
| 61 | do { |
| 62 | n = read(fd, buf, count); |
| 63 | } while (n < 0 && errno == EINTR); |
| 64 | |
| 65 | return n; |
| 66 | } |
| 67 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 68 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 70 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 71 | const char *msg; |
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 fildes[2]; /* fds for pipe read and 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/written to pipe */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | int greater, length; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | setup(); |
| 82 | |
| 83 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 84 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 85 | /* reset tst_count in case we are looping */ |
| 86 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
| 88 | TEST(pipe(fildes)); |
| 89 | |
| 90 | if (TEST_RETURN == -1) { |
| 91 | tst_resm(TFAIL, "pipe() failed unexpectedly - errno %d", |
| 92 | TEST_ERRNO); |
| 93 | continue; |
| 94 | } |
| 95 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 96 | strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz"); |
| 97 | length = strlen(wrbuf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 99 | if ((written = write(fildes[1], wrbuf, length)) == -1) { |
| 100 | tst_brkm(TBROK, cleanup, "write() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 103 | if (written < 0 || written > 26) { |
| 104 | tst_resm(TFAIL, "Condition #1 test failed"); |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | if ((red = safe_read(fildes[0], rebuf, written)) == -1) { |
| 109 | tst_brkm(TBROK | TERRNO, cleanup, |
| 110 | "read() failed"); |
| 111 | } |
| 112 | |
| 113 | if (red < 0 || red > written) { |
| 114 | tst_resm(TFAIL, "Condition #2 test failed"); |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | /* are the strings written and read equal */ |
| 119 | if ((greater = strncmp(rebuf, wrbuf, red)) != 0) { |
| 120 | tst_resm(TFAIL, "Condition #3 test failed"); |
| 121 | continue; |
| 122 | } |
| 123 | tst_resm(TPASS, "pipe() functionality is correct"); |
| 124 | } |
| 125 | |
| 126 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 127 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * setup() - performs all ONE TIME setup for this test. |
| 132 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 133 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | TEST_PAUSE; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 143 | * completion or premature exit. |
| 144 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 145 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 147 | } |