blob: 21916c80b01ec940b830a93891adfd940be63c0c [file] [log] [blame]
robbiew62b4a992002-04-29 18:30:36 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew62b4a992002-04-29 18:30:36 +000019 */
20
21/*
22 * Test Name: pread03
23 *
24 * Test Description:
25 * Verify that,
26 * 1) pread() fails when fd refers to a directory.
vapier22cf2042006-04-10 23:53:29 +000027 *
robbiew62b4a992002-04-29 18:30:36 +000028 *
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.
vapier22cf2042006-04-10 23:53:29 +000037 * Get the currect directory name
robbiew62b4a992002-04-29 18:30:36 +000038 * 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.
vapier22cf2042006-04-10 23:53:29 +000058 * -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.
robbiew62b4a992002-04-29 18:30:36 +000062 *
63 * HISTORY
64 * 04/2002 Ported by André Merlier
65 *
66 * RESTRICTIONS:
67 * None.
68 */
plars74948ad2002-11-14 16:16:14 +000069
70#define _XOPEN_SOURCE 500
71
72#include <sys/stat.h>
73#include <sys/types.h>
robbiew62b4a992002-04-29 18:30:36 +000074#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"
robbiew62b4a992002-04-29 18:30:36 +000082
plarsdff1ddc2002-06-12 17:38:50 +000083#define PREAD_TEMPDIR "test"
robbiew62b4a992002-04-29 18:30:36 +000084#define K1 2048
85#define NBUFS 1
86
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020087char *TCID = "pread03";
88int TST_TOTAL = 1;
robbiew62b4a992002-04-29 18:30:36 +000089
90char *read_buf[NBUFS]; /* buffer to hold data read from file */
subrata_modak56207ce2009-03-23 13:35:39 +000091char test_dir[100];
Cyril Hrubis605fa332015-02-04 13:11:20 +010092int fd1;
robbiew62b4a992002-04-29 18:30:36 +000093
94void setup(); /* Main setup function of test */
95void cleanup(); /* cleanup function for the test */
vapier2642d3a2006-04-10 23:58:12 +000096void init_buffers(); /* function to initialize/allocate buffers */
robbiew62b4a992002-04-29 18:30:36 +000097
subrata_modak56207ce2009-03-23 13:35:39 +000098int main(int ac, char **av)
robbiew62b4a992002-04-29 18:30:36 +000099{
100 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200101 const char *msg;
robbiew62b4a992002-04-29 18:30:36 +0000102 size_t nbytes; /* no. of bytes to be written */
103 off_t offset; /* offset position in the specified file */
104 char *test_desc; /* test specific error message */
vapier22cf2042006-04-10 23:53:29 +0000105
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800106 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800107 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew62b4a992002-04-29 18:30:36 +0000108
robbiew62b4a992002-04-29 18:30:36 +0000109 setup();
vapier22cf2042006-04-10 23:53:29 +0000110
robbiew62b4a992002-04-29 18:30:36 +0000111 /* Check for looping state if -i option is given */
112 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800113 /* reset tst_count in case we are looping */
114 tst_count = 0;
vapier22cf2042006-04-10 23:53:29 +0000115
116 test_desc = "EISDIR";
robbiew62b4a992002-04-29 18:30:36 +0000117 nbytes = K1;
118 offset = 20;
vapier22cf2042006-04-10 23:53:29 +0000119
robbiew62b4a992002-04-29 18:30:36 +0000120 TEST(pread(fd1, read_buf[0], nbytes, offset));
vapier22cf2042006-04-10 23:53:29 +0000121
robbiew62b4a992002-04-29 18:30:36 +0000122 /* Check for the return code of pread() */
123 if (TEST_RETURN != -1) {
124 tst_brkm(TFAIL, cleanup, "pread() returned "
subrata_modak923b23f2009-11-02 13:57:16 +0000125 "%ld, expected -1, errno:%d\n",
126 TEST_RETURN, EISDIR);
robbiew62b4a992002-04-29 18:30:36 +0000127 }
vapier22cf2042006-04-10 23:53:29 +0000128
robbiew62b4a992002-04-29 18:30:36 +0000129 /*
130 * Verify whether expected errno is set.
131 */
Cyril Hrubis605fa332015-02-04 13:11:20 +0100132 if (TEST_ERRNO == EISDIR) {
subrata_modak56207ce2009-03-23 13:35:39 +0000133 tst_resm(TPASS,
134 "pread() fails with expected error EISDIR errno:%d",
135 TEST_ERRNO);
robbiew62b4a992002-04-29 18:30:36 +0000136 } else {
137 tst_resm(TFAIL, "pread() fails, %s, unexpected "
138 "errno:%d, expected:%d\n", test_desc,
Cyril Hrubis605fa332015-02-04 13:11:20 +0100139 TEST_ERRNO, EISDIR);
robbiew62b4a992002-04-29 18:30:36 +0000140 }
141 }
vapier22cf2042006-04-10 23:53:29 +0000142
robbiew62b4a992002-04-29 18:30:36 +0000143 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800144 tst_exit();
robbiew62b4a992002-04-29 18:30:36 +0000145
robbiew62b4a992002-04-29 18:30:36 +0000146}
147
148/*
149 * setup() - performs all ONE TIME setup for this test.
vapier22cf2042006-04-10 23:53:29 +0000150 * create temporary directory and open it
robbiew62b4a992002-04-29 18:30:36 +0000151 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400152void setup(void)
robbiew62b4a992002-04-29 18:30:36 +0000153{
154 char *cur_dir = NULL;
vapier22cf2042006-04-10 23:53:29 +0000155
robbiew62b4a992002-04-29 18:30:36 +0000156 tst_sig(FORK, DEF_HANDLER, cleanup);
157
robbiew62b4a992002-04-29 18:30:36 +0000158 TEST_PAUSE;
vapier22cf2042006-04-10 23:53:29 +0000159
vapier2642d3a2006-04-10 23:58:12 +0000160 /* Allocate the read buffer */
161 init_buffers();
162
robbiew62b4a992002-04-29 18:30:36 +0000163 tst_tmpdir();
vapier22cf2042006-04-10 23:53:29 +0000164
robbiew62b4a992002-04-29 18:30:36 +0000165 /* get the currect directory name */
vapier22cf2042006-04-10 23:53:29 +0000166 if ((cur_dir = getcwd(cur_dir, 0)) == NULL) {
167 tst_brkm(TBROK, cleanup, "Couldn't get current directory name");
168 }
169
vapier2642d3a2006-04-10 23:58:12 +0000170 sprintf(test_dir, "%s.%d", cur_dir, getpid());
vapier22cf2042006-04-10 23:53:29 +0000171
robbiew62b4a992002-04-29 18:30:36 +0000172 /*
vapier22cf2042006-04-10 23:53:29 +0000173 * create a temporary directory
174 */
subrata_modak56207ce2009-03-23 13:35:39 +0000175 if (mkdir(PREAD_TEMPDIR, 0777) != 0) {
176 tst_resm(TFAIL, "mkdir() failed to create" " test directory");
vapier22cf2042006-04-10 23:53:29 +0000177 exit(1);
Garrett Cooper2c282152010-12-16 00:55:50 -0800178
vapier22cf2042006-04-10 23:53:29 +0000179 }
180
robbiew62b4a992002-04-29 18:30:36 +0000181 /* open temporary directory used for test */
subrata_modak56207ce2009-03-23 13:35:39 +0000182 if ((fd1 = open(PREAD_TEMPDIR, O_RDONLY)) < 0) {
robbiew62b4a992002-04-29 18:30:36 +0000183 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
plarsdff1ddc2002-06-12 17:38:50 +0000184 PREAD_TEMPDIR, errno, strerror(errno));
robbiew62b4a992002-04-29 18:30:36 +0000185 }
186
187}
188
vapier2642d3a2006-04-10 23:58:12 +0000189/*
190 * init_buffers() - allocate/Initialize write_buf array.
191 *
192 * Allocate read buffer.
193 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400194void init_buffers(void)
vapier2642d3a2006-04-10 23:58:12 +0000195{
196 int count; /* counter variable for loop */
197
198 /* Allocate and Initialize read buffer */
199 for (count = 0; count < NBUFS; count++) {
Cyril Hrubisd218f342014-09-23 13:14:56 +0200200 read_buf[count] = malloc(K1);
vapier2642d3a2006-04-10 23:58:12 +0000201
202 if (read_buf[count] == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800203 tst_brkm(TBROK, NULL,
vapier2642d3a2006-04-10 23:58:12 +0000204 "malloc() failed on read buffers");
205 }
206 }
207}
robbiew62b4a992002-04-29 18:30:36 +0000208
209/*
210 * cleanup() - performs all ONE TIME cleanup for this test at
211 * completion or premature exit.
212 *
213 * Close/Remove the temporary directory created.
214 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400215void cleanup(void)
robbiew62b4a992002-04-29 18:30:36 +0000216{
Cyril Hrubis605fa332015-02-04 13:11:20 +0100217 int count;
robbiew62b4a992002-04-29 18:30:36 +0000218
vapier2642d3a2006-04-10 23:58:12 +0000219 /* Free the memory allocated for the read buffer */
220 for (count = 0; count < NBUFS; count++) {
221 free(read_buf[count]);
222 }
223
robbiew62b4a992002-04-29 18:30:36 +0000224 /* delete the test directory created in setup() */
225 tst_rmdir();
226
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800227}