robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Test Name: pread03 |
| 23 | * |
| 24 | * Test Description: |
| 25 | * Verify that, |
| 26 | * 1) pread() fails when fd refers to a directory. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 27 | * |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 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. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 37 | * Get the currect directory name |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 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. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 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. |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 62 | * |
| 63 | * HISTORY |
| 64 | * 04/2002 Ported by André Merlier |
| 65 | * |
| 66 | * RESTRICTIONS: |
| 67 | * None. |
| 68 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 69 | |
| 70 | #define _XOPEN_SOURCE 500 |
| 71 | |
| 72 | #include <sys/stat.h> |
| 73 | #include <sys/types.h> |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 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" |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 82 | |
plars | dff1ddc | 2002-06-12 17:38:50 +0000 | [diff] [blame] | 83 | #define PREAD_TEMPDIR "test" |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 84 | #define K1 2048 |
| 85 | #define NBUFS 1 |
| 86 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 87 | char *TCID = "pread03"; |
| 88 | int TST_TOTAL = 1; |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 89 | |
| 90 | char *read_buf[NBUFS]; /* buffer to hold data read from file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | char test_dir[100]; |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 92 | int fd1; |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 93 | |
| 94 | void setup(); /* Main setup function of test */ |
| 95 | void cleanup(); /* cleanup function for the test */ |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 96 | void init_buffers(); /* function to initialize/allocate buffers */ |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | int main(int ac, char **av) |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 99 | { |
| 100 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 101 | const char *msg; |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 102 | 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 */ |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 105 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 106 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 107 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 108 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 109 | setup(); |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 110 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 111 | /* Check for looping state if -i option is given */ |
| 112 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 113 | /* reset tst_count in case we are looping */ |
| 114 | tst_count = 0; |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 115 | |
| 116 | test_desc = "EISDIR"; |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 117 | nbytes = K1; |
| 118 | offset = 20; |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 119 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 120 | TEST(pread(fd1, read_buf[0], nbytes, offset)); |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 121 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 122 | /* Check for the return code of pread() */ |
| 123 | if (TEST_RETURN != -1) { |
| 124 | tst_brkm(TFAIL, cleanup, "pread() returned " |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 125 | "%ld, expected -1, errno:%d\n", |
| 126 | TEST_RETURN, EISDIR); |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 127 | } |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 128 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Verify whether expected errno is set. |
| 131 | */ |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 132 | if (TEST_ERRNO == EISDIR) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | tst_resm(TPASS, |
| 134 | "pread() fails with expected error EISDIR errno:%d", |
| 135 | TEST_ERRNO); |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 136 | } else { |
| 137 | tst_resm(TFAIL, "pread() fails, %s, unexpected " |
| 138 | "errno:%d, expected:%d\n", test_desc, |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 139 | TEST_ERRNO, EISDIR); |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 142 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 143 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 144 | tst_exit(); |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 145 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * setup() - performs all ONE TIME setup for this test. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 150 | * create temporary directory and open it |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 151 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 152 | void setup(void) |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 153 | { |
| 154 | char *cur_dir = NULL; |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 155 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 156 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 157 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 158 | TEST_PAUSE; |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 159 | |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 160 | /* Allocate the read buffer */ |
| 161 | init_buffers(); |
| 162 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 163 | tst_tmpdir(); |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 164 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 165 | /* get the currect directory name */ |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 166 | if ((cur_dir = getcwd(cur_dir, 0)) == NULL) { |
| 167 | tst_brkm(TBROK, cleanup, "Couldn't get current directory name"); |
| 168 | } |
| 169 | |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 170 | sprintf(test_dir, "%s.%d", cur_dir, getpid()); |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 171 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 172 | /* |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 173 | * create a temporary directory |
| 174 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | if (mkdir(PREAD_TEMPDIR, 0777) != 0) { |
| 176 | tst_resm(TFAIL, "mkdir() failed to create" " test directory"); |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 177 | exit(1); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 179 | } |
| 180 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 181 | /* open temporary directory used for test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | if ((fd1 = open(PREAD_TEMPDIR, O_RDONLY)) < 0) { |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 183 | tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s", |
plars | dff1ddc | 2002-06-12 17:38:50 +0000 | [diff] [blame] | 184 | PREAD_TEMPDIR, errno, strerror(errno)); |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | } |
| 188 | |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 189 | /* |
| 190 | * init_buffers() - allocate/Initialize write_buf array. |
| 191 | * |
| 192 | * Allocate read buffer. |
| 193 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 194 | void init_buffers(void) |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 195 | { |
| 196 | int count; /* counter variable for loop */ |
| 197 | |
| 198 | /* Allocate and Initialize read buffer */ |
| 199 | for (count = 0; count < NBUFS; count++) { |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 200 | read_buf[count] = malloc(K1); |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 201 | |
| 202 | if (read_buf[count] == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 203 | tst_brkm(TBROK, NULL, |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 204 | "malloc() failed on read buffers"); |
| 205 | } |
| 206 | } |
| 207 | } |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 208 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 215 | void cleanup(void) |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 216 | { |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 217 | int count; |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 218 | |
vapier | 2642d3a | 2006-04-10 23:58:12 +0000 | [diff] [blame] | 219 | /* Free the memory allocated for the read buffer */ |
| 220 | for (count = 0; count < NBUFS; count++) { |
| 221 | free(read_buf[count]); |
| 222 | } |
| 223 | |
robbiew | 62b4a99 | 2002-04-29 18:30:36 +0000 | [diff] [blame] | 224 | /* delete the test directory created in setup() */ |
| 225 | tst_rmdir(); |
| 226 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 227 | } |