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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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"
52
53 char *TCID = "read03";
54 int TST_TOTAL = 1;
55
56 char fifo[100] = "fifo";
57 int rfd, wfd;
58 struct stat buf;
59
60 void alarm_handler();
61 void setup();
62 void cleanup();
63
main(int ac,char ** av)64 int main(int ac, char **av)
65 {
66 int lc;
67
68 int c;
69
70 tst_parse_opts(ac, av, NULL, NULL);
71
72 setup();
73
74 /*
75 * The following loop checks looping state if -i option given
76 */
77 for (lc = 0; TEST_LOOPING(lc); lc++) {
78 /* reset tst_count in case we are looping */
79 tst_count = 0;
80
81 TEST(read(rfd, &c, 1));
82
83 if (TEST_RETURN != -1) {
84 tst_resm(TFAIL, "read() failed to fail when nothing "
85 "is written to a pipe");
86 continue;
87 }
88
89 if (TEST_ERRNO != EAGAIN) {
90 tst_resm(TFAIL, "read set bad errno, expected "
91 "EAGAIN, got %d", TEST_ERRNO);
92 } else {
93 tst_resm(TPASS, "read() succeded in setting errno to "
94 "EAGAIN");
95 }
96 }
97 cleanup();
98 tst_exit();
99
100 }
101
102 /*
103 * setup() - performs all ONE TIME setup for this test
104 */
setup(void)105 void setup(void)
106 {
107
108 tst_sig(NOFORK, DEF_HANDLER, cleanup);
109
110 TEST_PAUSE;
111
112 /* create a temporary filename */
113 sprintf(fifo, "%s.%d", fifo, getpid());
114
115 /* Create a temporary directory and cd to it */
116 tst_tmpdir();
117
118 if (mknod(fifo, S_IFIFO | 0777, 0) < 0) {
119 tst_brkm(TBROK, cleanup, "mknod() failed, errno: %d", errno);
120 }
121 if (stat(fifo, &buf) != 0) {
122 tst_brkm(TBROK, cleanup, "stat() failed, errno: %d", errno);
123 }
124 if ((buf.st_mode & S_IFIFO) == 0) {
125 tst_brkm(TBROK, cleanup, "Mode does not indicate fifo file");
126 }
127
128 rfd = open(fifo, O_RDONLY | O_NONBLOCK);
129 wfd = open(fifo, O_WRONLY | O_NONBLOCK);
130 }
131
132 /*
133 * cleanup() - performs all ONE TIME cleanup for this test at
134 * completion or premature exit.
135 */
cleanup(void)136 void cleanup(void)
137 {
138
139 close(rfd);
140 close(wfd);
141 unlink(fifo);
142
143 /* delete the test directory created in setup() */
144 tst_rmdir();
145
146 }
147