1 /*
2 *
3 * Copyright (C) Bull S.A. 2001
4 * Copyright (c) International Business Machines Corp., 2001
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * Test Name: pread03
23 *
24 * Test Description:
25 * Verify that,
26 * 1) pread() fails when fd refers to a directory.
27 *
28 *
29 * Expected Result:
30 * 1) pread() should return -1 and set errno to EISDIR.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Pause for SIGUSR1 if option specified.
36 * Create a temporary directory.
37 * Get the currect directory name
38 * Open temporary directory
39 *
40 * Test:
41 * Loop if the proper options are given.
42 * Execute system call
43 * Check return code, if system call failed (return=-1)
44 * if errno set == expected errno
45 * Issue sys call fails with expected return value and errno.
46 * Otherwise,
47 * Issue sys call fails with unexpected errno.
48 * Otherwise,
49 * Issue sys call returns unexpected value.
50 *
51 * Cleanup:
52 * Print errno log and/or timing stats if options given
53 * Delete the temporary directory(s)/file(s) created.
54 *
55 * Usage: <for command-line>
56 * pread03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
57 * where, -c n : Run n copies concurrently.
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
60 * -P x : Pause for x seconds between iterations.
61 * -t : Turn on syscall timing.
62 *
63 * HISTORY
64 * 04/2002 Ported by Andr� Merlier
65 *
66 * RESTRICTIONS:
67 * None.
68 */
69
70 #define _XOPEN_SOURCE 500
71
72 #include <sys/stat.h>
73 #include <sys/types.h>
74 #include <errno.h>
75 #include <unistd.h>
76 #include <fcntl.h>
77 #include <string.h>
78 #include <stdlib.h>
79 #include <sys/file.h>
80
81 #include "test.h"
82
83 #define PREAD_TEMPDIR "test"
84 #define K1 2048
85 #define NBUFS 1
86
87 char *TCID = "pread03";
88 int TST_TOTAL = 1;
89
90 char *read_buf[NBUFS]; /* buffer to hold data read from file */
91 int fd1;
92
93 void setup(); /* Main setup function of test */
94 void cleanup(); /* cleanup function for the test */
95 void init_buffers(); /* function to initialize/allocate buffers */
96
main(int ac,char ** av)97 int main(int ac, char **av)
98 {
99 int lc;
100 size_t nbytes; /* no. of bytes to be written */
101 off_t offset; /* offset position in the specified file */
102 char *test_desc; /* test specific error message */
103
104 tst_parse_opts(ac, av, NULL, NULL);
105
106 setup();
107
108 /* Check for looping state if -i option is given */
109 for (lc = 0; TEST_LOOPING(lc); lc++) {
110 /* reset tst_count in case we are looping */
111 tst_count = 0;
112
113 test_desc = "EISDIR";
114 nbytes = K1;
115 offset = 20;
116
117 TEST(pread(fd1, read_buf[0], nbytes, offset));
118
119 /* Check for the return code of pread() */
120 if (TEST_RETURN != -1) {
121 tst_brkm(TFAIL, cleanup, "pread() returned "
122 "%ld, expected -1, errno:%d\n",
123 TEST_RETURN, EISDIR);
124 }
125
126 /*
127 * Verify whether expected errno is set.
128 */
129 if (TEST_ERRNO == EISDIR) {
130 tst_resm(TPASS,
131 "pread() fails with expected error EISDIR errno:%d",
132 TEST_ERRNO);
133 } else {
134 tst_resm(TFAIL, "pread() fails, %s, unexpected "
135 "errno:%d, expected:%d\n", test_desc,
136 TEST_ERRNO, EISDIR);
137 }
138 }
139
140 cleanup();
141 tst_exit();
142
143 }
144
145 /*
146 * setup() - performs all ONE TIME setup for this test.
147 * create temporary directory and open it
148 */
setup(void)149 void setup(void)
150 {
151 tst_sig(FORK, DEF_HANDLER, cleanup);
152
153 TEST_PAUSE;
154
155 /* Allocate the read buffer */
156 init_buffers();
157
158 tst_tmpdir();
159
160 /*
161 * create a temporary directory
162 */
163 if (mkdir(PREAD_TEMPDIR, 0777) != 0) {
164 tst_resm(TFAIL, "mkdir() failed to create" " test directory");
165 exit(1);
166
167 }
168
169 /* open temporary directory used for test */
170 if ((fd1 = open(PREAD_TEMPDIR, O_RDONLY)) < 0) {
171 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
172 PREAD_TEMPDIR, errno, strerror(errno));
173 }
174 }
175
176 /*
177 * init_buffers() - allocate/Initialize write_buf array.
178 *
179 * Allocate read buffer.
180 */
init_buffers(void)181 void init_buffers(void)
182 {
183 int count; /* counter variable for loop */
184
185 /* Allocate and Initialize read buffer */
186 for (count = 0; count < NBUFS; count++) {
187 read_buf[count] = malloc(K1);
188
189 if (read_buf[count] == NULL) {
190 tst_brkm(TBROK, NULL,
191 "malloc() failed on read buffers");
192 }
193 }
194 }
195
196 /*
197 * cleanup() - performs all ONE TIME cleanup for this test at
198 * completion or premature exit.
199 *
200 * Close/Remove the temporary directory created.
201 */
cleanup(void)202 void cleanup(void)
203 {
204 int count;
205
206 /* Free the memory allocated for the read buffer */
207 for (count = 0; count < NBUFS; count++) {
208 free(read_buf[count]);
209 }
210
211 /* delete the test directory created in setup() */
212 tst_rmdir();
213
214 }
215