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 | * read03.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check that read() sets errno to EAGAIN |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Create a named pipe (fifo), open it in O_NONBLOCK mode, and |
| 29 | * attempt to read from it, without writing to it. read() should fail |
| 30 | * with EAGAIN. |
| 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * read03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -e : Turn on errno logging. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
| 41 | * HISTORY |
| 42 | * 07/2001 Ported by Wayne Boyer |
| 43 | * |
| 44 | * RESTRICTIONS |
| 45 | * NONE |
| 46 | */ |
| 47 | #include <sys/stat.h> |
| 48 | #include <fcntl.h> |
| 49 | #include <signal.h> |
| 50 | #include <errno.h> |
| 51 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | char *TCID = "read03"; |
| 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | char fifo[100] = "fifo"; |
| 57 | int rfd, wfd; |
| 58 | struct stat buf; |
| 59 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | void alarm_handler(); |
| 61 | void setup(); |
| 62 | void cleanup(); |
| 63 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 64 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | { |
| 66 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 67 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 69 | int c; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 71 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | setup(); |
| 76 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | /* |
| 78 | * The following loop checks looping state if -i option given |
| 79 | */ |
| 80 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | TEST(read(rfd, &c, 1)); |
| 85 | |
| 86 | if (TEST_RETURN != -1) { |
| 87 | tst_resm(TFAIL, "read() failed to fail when nothing " |
| 88 | "is written to a pipe"); |
| 89 | continue; |
| 90 | } |
| 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | if (TEST_ERRNO != EAGAIN) { |
| 93 | tst_resm(TFAIL, "read set bad errno, expected " |
| 94 | "EAGAIN, got %d", TEST_ERRNO); |
| 95 | } else { |
| 96 | tst_resm(TINFO, "read() succeded in setting errno to " |
| 97 | "EAGAIN"); |
| 98 | } |
| 99 | } |
| 100 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 101 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * setup() - performs all ONE TIME setup for this test |
| 107 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | TEST_PAUSE; |
| 114 | |
| 115 | /* create a temporary filename */ |
| 116 | sprintf(fifo, "%s.%d", fifo, getpid()); |
| 117 | |
| 118 | /* Create a temporary directory and cd to it */ |
| 119 | tst_tmpdir(); |
| 120 | |
| 121 | if (mknod(fifo, S_IFIFO | 0777, 0) < 0) { |
| 122 | tst_brkm(TBROK, cleanup, "mknod() failed, errno: %d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | if (stat(fifo, &buf) != 0) { |
| 125 | tst_brkm(TBROK, cleanup, "stat() failed, errno: %d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | if ((buf.st_mode & S_IFIFO) == 0) { |
| 128 | tst_brkm(TBROK, cleanup, "Mode does not indicate fifo file"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
| 131 | rfd = open(fifo, O_RDONLY | O_NONBLOCK); |
| 132 | wfd = open(fifo, O_WRONLY | O_NONBLOCK); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 137 | * completion or premature exit. |
| 138 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 139 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
| 142 | close(rfd); |
| 143 | close(wfd); |
| 144 | unlink(fifo); |
| 145 | |
| 146 | /* delete the test directory created in setup() */ |
| 147 | tst_rmdir(); |
| 148 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 149 | } |