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 char test_dir[100];
92 int fd1;
93
94 void setup(); /* Main setup function of test */
95 void cleanup(); /* cleanup function for the test */
96 void init_buffers(); /* function to initialize/allocate buffers */
97
main(int ac,char ** av)98 int main(int ac, char **av)
99 {
100 int lc;
101 size_t nbytes; /* no. of bytes to be written */
102 off_t offset; /* offset position in the specified file */
103 char *test_desc; /* test specific error message */
104
105 tst_parse_opts(ac, av, NULL, NULL);
106
107 setup();
108
109 /* Check for looping state if -i option is given */
110 for (lc = 0; TEST_LOOPING(lc); lc++) {
111 /* reset tst_count in case we are looping */
112 tst_count = 0;
113
114 test_desc = "EISDIR";
115 nbytes = K1;
116 offset = 20;
117
118 TEST(pread(fd1, read_buf[0], nbytes, offset));
119
120 /* Check for the return code of pread() */
121 if (TEST_RETURN != -1) {
122 tst_brkm(TFAIL, cleanup, "pread() returned "
123 "%ld, expected -1, errno:%d\n",
124 TEST_RETURN, EISDIR);
125 }
126
127 /*
128 * Verify whether expected errno is set.
129 */
130 if (TEST_ERRNO == EISDIR) {
131 tst_resm(TPASS,
132 "pread() fails with expected error EISDIR errno:%d",
133 TEST_ERRNO);
134 } else {
135 tst_resm(TFAIL, "pread() fails, %s, unexpected "
136 "errno:%d, expected:%d\n", test_desc,
137 TEST_ERRNO, EISDIR);
138 }
139 }
140
141 cleanup();
142 tst_exit();
143
144 }
145
146 /*
147 * setup() - performs all ONE TIME setup for this test.
148 * create temporary directory and open it
149 */
setup(void)150 void setup(void)
151 {
152 char *cur_dir = NULL;
153
154 tst_sig(FORK, DEF_HANDLER, cleanup);
155
156 TEST_PAUSE;
157
158 /* Allocate the read buffer */
159 init_buffers();
160
161 tst_tmpdir();
162
163 /* get the currect directory name */
164 if ((cur_dir = getcwd(cur_dir, 0)) == NULL) {
165 tst_brkm(TBROK, cleanup, "Couldn't get current directory name");
166 }
167
168 sprintf(test_dir, "%s.%d", cur_dir, getpid());
169
170 /*
171 * create a temporary directory
172 */
173 if (mkdir(PREAD_TEMPDIR, 0777) != 0) {
174 tst_resm(TFAIL, "mkdir() failed to create" " test directory");
175 exit(1);
176
177 }
178
179 /* open temporary directory used for test */
180 if ((fd1 = open(PREAD_TEMPDIR, O_RDONLY)) < 0) {
181 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
182 PREAD_TEMPDIR, errno, strerror(errno));
183 }
184
185 }
186
187 /*
188 * init_buffers() - allocate/Initialize write_buf array.
189 *
190 * Allocate read buffer.
191 */
init_buffers(void)192 void init_buffers(void)
193 {
194 int count; /* counter variable for loop */
195
196 /* Allocate and Initialize read buffer */
197 for (count = 0; count < NBUFS; count++) {
198 read_buf[count] = malloc(K1);
199
200 if (read_buf[count] == NULL) {
201 tst_brkm(TBROK, NULL,
202 "malloc() failed on read buffers");
203 }
204 }
205 }
206
207 /*
208 * cleanup() - performs all ONE TIME cleanup for this test at
209 * completion or premature exit.
210 *
211 * Close/Remove the temporary directory created.
212 */
cleanup(void)213 void cleanup(void)
214 {
215 int count;
216
217 /* Free the memory allocated for the read buffer */
218 for (count = 0; count < NBUFS; count++) {
219 free(read_buf[count]);
220 }
221
222 /* delete the test directory created in setup() */
223 tst_rmdir();
224
225 }
226