plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Description: |
| 22 | * Verify that, |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 23 | * 1) pwrite() fails when attempted to write to an unnamed pipe, |
| 24 | * returns ESPIPE. |
| 25 | * 2) pwrite() fails if the specified offset position was invalid, |
| 26 | * returns EINVAL. |
| 27 | * 3) pwrite() fails if fd is not a valid file descriptor, |
| 28 | * returns EBADF. |
Cyril Hrubis | 0b791f4 | 2014-05-28 18:22:46 +0200 | [diff] [blame] | 29 | * 4) pwrite() fails if fd is not open for writing, return EBADF. |
| 30 | * 5) pwrite() fails when attempted to write with buf outside |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 31 | * accessible address space, returns EFAULT. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 33 | |
| 34 | #define _XOPEN_SOURCE 500 |
| 35 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | #include <errno.h> |
| 37 | #include <unistd.h> |
| 38 | #include <fcntl.h> |
| 39 | |
| 40 | #include "test.h" |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 41 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
| 43 | #define TEMPFILE "pwrite_file" |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 44 | #define K1 1024 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 46 | TCID_DEFINE(pwrite02); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 48 | static char write_buf[K1]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 50 | static void setup(void); |
| 51 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 53 | static void test_espipe(void); |
| 54 | static void test_einval(void); |
Cyril Hrubis | 0b791f4 | 2014-05-28 18:22:46 +0200 | [diff] [blame] | 55 | static void test_ebadf1(void); |
| 56 | static void test_ebadf2(void); |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 57 | |
| 58 | #if !defined(UCLINUX) |
| 59 | static void test_efault(void); |
| 60 | #endif |
| 61 | |
| 62 | static void (*testfunc[])(void) = { |
Cyril Hrubis | 0b791f4 | 2014-05-28 18:22:46 +0200 | [diff] [blame] | 63 | test_espipe, test_einval, test_ebadf1, test_ebadf2, |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 64 | #if !defined(UCLINUX) |
| 65 | test_efault |
| 66 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 69 | int TST_TOTAL = ARRAY_SIZE(testfunc); |
| 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | { |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 73 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 74 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 75 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 76 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | setup(); |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 82 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 84 | for (i = 0; i < TST_TOTAL; i++) |
| 85 | (*testfunc[i])(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 86 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 89 | tst_exit(); |
| 90 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * sighandler - handle SIGXFSZ |
| 94 | * |
| 95 | * This is here to start looking at a failure in test case #2. This |
| 96 | * test case passes on a machine running RedHat 6.2 but it will fail |
| 97 | * on a machine running RedHat 7.1. |
| 98 | */ |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 99 | static void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | { |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 101 | int ret; |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | if (sig != SIGXFSZ) { |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 104 | ret = write(STDOUT_FILENO, "get wrong signal\n", |
| 105 | sizeof("get wrong signal\n")); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | } else { |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 107 | ret = write(STDOUT_FILENO, "caught SIGXFSZ\n", |
| 108 | sizeof("caught SIGXFSZ\n")); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | } |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 110 | |
| 111 | (void)ret; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 114 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 117 | |
| 118 | /* see the comment in the sighandler() function */ |
| 119 | /* call signal() to trap the signal generated */ |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 120 | if (signal(SIGXFSZ, sighandler) == SIG_ERR) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_brkm(TBROK, cleanup, "signal() failed"); |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | TEST_PAUSE; |
| 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_tmpdir(); |
| 126 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 127 | memset(write_buf, 'a', K1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 130 | static void print_test_result(int err, int exp_errno) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 132 | if (err == 0) { |
| 133 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 134 | return; |
| 135 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 137 | if (err == exp_errno) { |
| 138 | tst_resm(TPASS, "pwrite failed as expected: %d - %s", |
| 139 | err, strerror(err)); |
| 140 | } else { |
| 141 | tst_resm(TFAIL, "pwrite failed unexpectedly; expected: %d - %s" |
| 142 | "return: %d - %s", exp_errno, strerror(exp_errno), |
| 143 | err, strerror(err)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 147 | static void test_espipe(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | { |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 149 | int pipe_fds[2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 151 | SAFE_PIPE(cleanup, pipe_fds); |
| 152 | |
| 153 | TEST(pwrite(pipe_fds[1], write_buf, K1, 0)); |
| 154 | |
| 155 | print_test_result(errno, ESPIPE); |
| 156 | |
| 157 | SAFE_CLOSE(cleanup, pipe_fds[0]); |
| 158 | SAFE_CLOSE(cleanup, pipe_fds[1]); |
| 159 | } |
| 160 | |
| 161 | static void test_einval(void) |
| 162 | { |
| 163 | int fd; |
| 164 | |
| 165 | fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666); |
| 166 | |
| 167 | /* the specified offset was invalid */ |
| 168 | TEST(pwrite(fd, write_buf, K1, -1)); |
| 169 | |
| 170 | print_test_result(errno, EINVAL); |
| 171 | |
| 172 | SAFE_CLOSE(cleanup, fd); |
| 173 | } |
| 174 | |
Cyril Hrubis | 0b791f4 | 2014-05-28 18:22:46 +0200 | [diff] [blame] | 175 | static void test_ebadf1(void) |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 176 | { |
| 177 | int fd = -1; |
| 178 | |
| 179 | TEST(pwrite(fd, write_buf, K1, 0)); |
| 180 | |
| 181 | print_test_result(errno, EBADF); |
| 182 | } |
| 183 | |
Cyril Hrubis | 0b791f4 | 2014-05-28 18:22:46 +0200 | [diff] [blame] | 184 | static void test_ebadf2(void) |
| 185 | { |
| 186 | int fd; |
| 187 | |
| 188 | fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDONLY | O_CREAT, 0666); |
| 189 | |
| 190 | TEST(pwrite(fd, write_buf, K1, 0)); |
| 191 | |
| 192 | print_test_result(errno, EBADF); |
| 193 | |
| 194 | SAFE_CLOSE(cleanup, fd); |
| 195 | } |
| 196 | |
Cyril Hrubis | 435448c | 2014-05-28 18:10:46 +0200 | [diff] [blame] | 197 | #if !defined(UCLINUX) |
| 198 | static void test_efault(void) |
| 199 | { |
| 200 | int fd; |
| 201 | char *buf = sbrk(0); |
| 202 | |
| 203 | fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666); |
| 204 | |
| 205 | TEST(pwrite(fd, buf, K1, 0)); |
| 206 | |
| 207 | print_test_result(errno, EFAULT); |
| 208 | |
| 209 | SAFE_CLOSE(cleanup, fd); |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | static void cleanup(void) |
| 214 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 216 | } |