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 | /* |
| 21 | * NAME |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 22 | * write05.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check the return value, and errnos of write(2) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 26 | * - when the file descriptor is invalid - EBADF |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * - when the buf parameter is invalid - EFAULT |
| 28 | * - on an attempt to write to a pipe that is not open for reading - EPIPE |
| 29 | * |
| 30 | * ALGORITHM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 31 | * Attempt to write on a file with negative file descriptor, check for -1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * Attempt to write on a file with invalid buffer, check for -1 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 33 | * Open a pipe and close the read end, attempt to write to the write |
| 34 | * end, check for -1. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * |
| 36 | * USAGE: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 37 | * write05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * where, -c n : Run n copies concurrently. |
| 39 | * -e : Turn on errno logging. |
| 40 | * -i n : Execute test n times. |
| 41 | * -I x : Execute test for x seconds. |
| 42 | * -P x : Pause for x seconds between iterations. |
| 43 | * -t : Turn on syscall timing. |
| 44 | * |
| 45 | * History |
| 46 | * 07/2001 John George |
| 47 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 48 | * 04/2002 wjhuie sigset cleanups |
subrata_modak | 0206d28 | 2007-08-06 09:32:49 +0000 | [diff] [blame] | 49 | * 08/2007 Ricardo Salveti de Araujo <rsalveti@linux.vnet.ibm.com> |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | * - Closing the fd before removing the file |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | * |
| 52 | * Restrictions |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 53 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | */ |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
| 57 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | #include <errno.h> |
| 59 | #include <stdio.h> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 60 | #include <sys/wait.h> |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 61 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
| 64 | void setup(void); |
| 65 | void cleanup(void); |
| 66 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 67 | char *TCID = "write05"; |
| 68 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | char filename[100]; |
subrata_modak | 0206d28 | 2007-08-06 09:32:49 +0000 | [diff] [blame] | 70 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | char *bad_addr = 0; |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 73 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 74 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | char pbuf[BUFSIZ]; |
| 79 | int pipefildes[2]; |
subrata_modak | 0206d28 | 2007-08-06 09:32:49 +0000 | [diff] [blame] | 80 | int status, pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 82 | tst_parse_opts(argc, argv, NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | /* global setup */ |
| 85 | setup(); |
| 86 | |
| 87 | /* The following loop checks looping state if -i option given */ |
| 88 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 89 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 90 | /* reset tst_count in case we are looping */ |
| 91 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 93 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | tst_resm(TINFO, "Enter Block 1: test with bad fd"); |
| 95 | if (write(-1, pbuf, 1) != -1) { |
| 96 | tst_resm(TFAIL, "write of invalid fd passed"); |
| 97 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | if (errno != EBADF) { |
| 99 | tst_resm(TFAIL, "expected EBADF got %d", errno); |
| 100 | } |
| 101 | tst_resm(TPASS, "received EBADF as expected."); |
| 102 | } |
| 103 | tst_resm(TINFO, "Exit Block 1"); |
| 104 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 105 | //block2: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | tst_resm(TINFO, "Enter Block 2: test with a bad address"); |
| 107 | fd = creat(filename, 0644); |
| 108 | if (fd < 0) { |
| 109 | tst_resm(TFAIL, "creating a new file failed"); |
| 110 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | } |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 112 | if (write(fd, bad_addr, 10) != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | tst_resm(TFAIL, "write() on an invalid buffer " |
| 114 | "succeeded, but should have failed"); |
| 115 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | if (errno != EFAULT) { |
| 118 | tst_resm(TFAIL, "write() returned illegal " |
| 119 | "errno: expected EFAULT, got %d", |
| 120 | errno); |
| 121 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | tst_resm(TPASS, "received EFAULT as expected."); |
| 124 | } |
| 125 | tst_resm(TINFO, "Exit Block 2"); |
| 126 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 127 | //block3: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | tst_resm(TINFO, "Enter Block 3: test with invalid pipe"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 129 | if ((pid = FORK_OR_VFORK()) == 0) { /* child */ |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 130 | if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { |
| 131 | tst_resm(TINFO, "signal failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | if (pipe(pipefildes) == -1) { |
| 134 | tst_brkm(TBROK, NULL, "can't open pipe"); |
| 135 | exit(errno); |
| 136 | } |
| 137 | close(pipefildes[0]); |
| 138 | if (write(pipefildes[1], pbuf, 1) != -1) { |
| 139 | tst_resm(TFAIL, "write on read-closed" |
| 140 | "pipe succeeded"); |
| 141 | exit(-1); |
| 142 | } else { |
| 143 | if (errno != EPIPE) { |
| 144 | tst_resm(TFAIL, "write() failed to set" |
| 145 | " errno to EPIPE, got: %d", |
| 146 | errno); |
| 147 | exit(errno); |
| 148 | } |
| 149 | exit(0); |
| 150 | } |
| 151 | } else { |
| 152 | if (pid < 0) { |
| 153 | tst_resm(TFAIL, "Fork failed"); |
| 154 | } |
| 155 | wait(&status); |
Jan Stancek | 2f474a8 | 2013-04-19 11:49:47 +0200 | [diff] [blame] | 156 | if (WIFSIGNALED(status) && |
| 157 | WTERMSIG(status) == SIGPIPE) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_resm(TFAIL, "child set SIGPIPE in exit"); |
| 159 | } else if (WEXITSTATUS(status) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_resm(TFAIL, "exit status from child " |
| 161 | "expected 0 got %d", status >> 8); |
| 162 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | tst_resm(TPASS, "received EPIPE as expected."); |
| 164 | } |
| 165 | tst_resm(TINFO, "Exit Block 3"); |
| 166 | } |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 167 | close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | } |
| 169 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 170 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | * setup() |
| 175 | * performs all ONE TIME setup for this test |
| 176 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 179 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 181 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | /* Pause if that option was specified |
| 183 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 184 | * You want to make sure you do this before you create your temporary |
| 185 | * directory. |
| 186 | */ |
| 187 | TEST_PAUSE; |
| 188 | |
| 189 | /* Create a unique temporary directory and chdir() to it. */ |
| 190 | tst_tmpdir(); |
| 191 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 192 | sprintf(filename, "write05.%d", getpid()); |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 193 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | bad_addr = mmap(0, 1, PROT_NONE, |
| 195 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
| 196 | if (bad_addr == MAP_FAILED) { |
| 197 | printf("mmap failed\n"); |
| 198 | } |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 199 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * cleanup() |
| 204 | * performs all ONE TIME cleanup for this test at |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | * completion or premature exit |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | |
subrata_modak | 0206d28 | 2007-08-06 09:32:49 +0000 | [diff] [blame] | 210 | /* Close the file descriptor befor removing the file */ |
| 211 | close(fd); |
| 212 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | unlink(filename); |
| 214 | tst_rmdir(); |
| 215 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 216 | } |