blob: bcbe85874f9ca06d9da2a1d39539fbe19beaeff1 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * write05.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Check the return value, and errnos of write(2)
subrata_modak56207ce2009-03-23 13:35:39 +000026 * - when the file descriptor is invalid - EBADF
plars865695b2001-08-27 22:15:12 +000027 * - 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_modak56207ce2009-03-23 13:35:39 +000031 * Attempt to write on a file with negative file descriptor, check for -1
plars865695b2001-08-27 22:15:12 +000032 * Attempt to write on a file with invalid buffer, check for -1
subrata_modak56207ce2009-03-23 13:35:39 +000033 * Open a pipe and close the read end, attempt to write to the write
34 * end, check for -1.
plars865695b2001-08-27 22:15:12 +000035 *
36 * USAGE: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000037 * write05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000038 * 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
robbiew4644c7e2002-04-26 14:33:32 +000048 * 04/2002 wjhuie sigset cleanups
subrata_modak0206d282007-08-06 09:32:49 +000049 * 08/2007 Ricardo Salveti de Araujo <rsalveti@linux.vnet.ibm.com>
subrata_modak56207ce2009-03-23 13:35:39 +000050 * - Closing the fd before removing the file
plars865695b2001-08-27 22:15:12 +000051 *
52 * Restrictions
subrata_modak56207ce2009-03-23 13:35:39 +000053 * None
plars865695b2001-08-27 22:15:12 +000054 */
robbiew6ee509b2003-04-01 20:28:41 +000055#include <sys/types.h>
56#include <sys/stat.h>
57#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000058#include <errno.h>
59#include <stdio.h>
Steven Jackson249f4052016-12-13 16:16:00 +000060#include <sys/wait.h>
robbiew37d19bd2003-11-14 16:57:49 +000061#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000062#include "test.h"
plars865695b2001-08-27 22:15:12 +000063
64void setup(void);
65void cleanup(void);
66
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020067char *TCID = "write05";
68int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000069char filename[100];
subrata_modak0206d282007-08-06 09:32:49 +000070int fd;
plars865695b2001-08-27 22:15:12 +000071
subrata_modak56207ce2009-03-23 13:35:39 +000072char *bad_addr = 0;
robbiew37d19bd2003-11-14 16:57:49 +000073
robbiew6ee509b2003-04-01 20:28:41 +000074int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000075{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
plars865695b2001-08-27 22:15:12 +000077
78 char pbuf[BUFSIZ];
79 int pipefildes[2];
subrata_modak0206d282007-08-06 09:32:49 +000080 int status, pid;
plars865695b2001-08-27 22:15:12 +000081
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010082 tst_parse_opts(argc, argv, NULL, NULL);
plars865695b2001-08-27 22:15:12 +000083
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 Zhangd59a6592013-03-07 14:59:12 +080090 /* reset tst_count in case we are looping */
91 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000092
robbiew6ee509b2003-04-01 20:28:41 +000093//block1:
plars865695b2001-08-27 22:15:12 +000094 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 {
plars865695b2001-08-27 22:15:12 +000098 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
robbiew6ee509b2003-04-01 20:28:41 +0000105//block2:
plars865695b2001-08-27 22:15:12 +0000106 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 Gao354ebb42012-12-07 10:10:04 +0800111 }
robbiew37d19bd2003-11-14 16:57:49 +0000112 if (write(fd, bad_addr, 10) != -1) {
plars865695b2001-08-27 22:15:12 +0000113 tst_resm(TFAIL, "write() on an invalid buffer "
114 "succeeded, but should have failed");
115 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 } else {
plars865695b2001-08-27 22:15:12 +0000117 if (errno != EFAULT) {
118 tst_resm(TFAIL, "write() returned illegal "
119 "errno: expected EFAULT, got %d",
120 errno);
121 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 }
plars865695b2001-08-27 22:15:12 +0000123 tst_resm(TPASS, "received EFAULT as expected.");
124 }
125 tst_resm(TINFO, "Exit Block 2");
126
robbiew6ee509b2003-04-01 20:28:41 +0000127//block3:
plars865695b2001-08-27 22:15:12 +0000128 tst_resm(TINFO, "Enter Block 3: test with invalid pipe");
robbiewd34d5812005-07-11 22:28:09 +0000129 if ((pid = FORK_OR_VFORK()) == 0) { /* child */
robbiew4644c7e2002-04-26 14:33:32 +0000130 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
131 tst_resm(TINFO, "signal failed");
plars865695b2001-08-27 22:15:12 +0000132 }
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 Stancek2f474a82013-04-19 11:49:47 +0200156 if (WIFSIGNALED(status) &&
157 WTERMSIG(status) == SIGPIPE) {
plars865695b2001-08-27 22:15:12 +0000158 tst_resm(TFAIL, "child set SIGPIPE in exit");
159 } else if (WEXITSTATUS(status) != 0) {
plars865695b2001-08-27 22:15:12 +0000160 tst_resm(TFAIL, "exit status from child "
161 "expected 0 got %d", status >> 8);
162 } else {
plars865695b2001-08-27 22:15:12 +0000163 tst_resm(TPASS, "received EPIPE as expected.");
164 }
165 tst_resm(TINFO, "Exit Block 3");
166 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000167 close(fd);
plars865695b2001-08-27 22:15:12 +0000168 }
169 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800170 tst_exit();
plars865695b2001-08-27 22:15:12 +0000171}
172
173/*
174 * setup()
175 * performs all ONE TIME setup for this test
176 */
subrata_modak56207ce2009-03-23 13:35:39 +0000177void setup(void)
plars865695b2001-08-27 22:15:12 +0000178{
Garrett Cooper2c282152010-12-16 00:55:50 -0800179
plars865695b2001-08-27 22:15:12 +0000180 tst_sig(FORK, DEF_HANDLER, cleanup);
181
plars865695b2001-08-27 22:15:12 +0000182 /* 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
nstrazfa31d552002-05-14 16:50:06 +0000192 sprintf(filename, "write05.%d", getpid());
robbiew37d19bd2003-11-14 16:57:49 +0000193
subrata_modak56207ce2009-03-23 13:35:39 +0000194 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 }
robbiew37d19bd2003-11-14 16:57:49 +0000199
plars865695b2001-08-27 22:15:12 +0000200}
201
202/*
203 * cleanup()
204 * performs all ONE TIME cleanup for this test at
subrata_modak56207ce2009-03-23 13:35:39 +0000205 * completion or premature exit
plars865695b2001-08-27 22:15:12 +0000206 */
subrata_modak56207ce2009-03-23 13:35:39 +0000207void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000208{
plars865695b2001-08-27 22:15:12 +0000209
subrata_modak0206d282007-08-06 09:32:49 +0000210 /* Close the file descriptor befor removing the file */
211 close(fd);
212
plars865695b2001-08-27 22:15:12 +0000213 unlink(filename);
214 tst_rmdir();
215
Wanlong Gao354ebb42012-12-07 10:10:04 +0800216}