xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/writev/writev05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * NAME
22*49cdfc7eSAndroid Build Coastguard Worker  *	writev05.c
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker  *	These testcases are written to test writev() on sparse files. This
26*49cdfc7eSAndroid Build Coastguard Worker  *	is same as writev02.c. But the initial write() with valid data is
27*49cdfc7eSAndroid Build Coastguard Worker  *	done at the beginning of the file.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * USAGE:  <for command-line>
30*49cdfc7eSAndroid Build Coastguard Worker  *      writev05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
31*49cdfc7eSAndroid Build Coastguard Worker  *      where,  -c n : Run n copies concurrently.
32*49cdfc7eSAndroid Build Coastguard Worker  *              -e   : Turn on errno logging.
33*49cdfc7eSAndroid Build Coastguard Worker  *              -i n : Execute test n times.
34*49cdfc7eSAndroid Build Coastguard Worker  *              -I x : Execute test for x seconds.
35*49cdfc7eSAndroid Build Coastguard Worker  *              -P x : Pause for x seconds between iterations.
36*49cdfc7eSAndroid Build Coastguard Worker  *              -t   : Turn on syscall timing.
37*49cdfc7eSAndroid Build Coastguard Worker  *
38*49cdfc7eSAndroid Build Coastguard Worker  * History
39*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 John George
40*49cdfc7eSAndroid Build Coastguard Worker  *		-Ported
41*49cdfc7eSAndroid Build Coastguard Worker  *      04/2002 wjhuie sigset cleanups
42*49cdfc7eSAndroid Build Coastguard Worker  *
43*49cdfc7eSAndroid Build Coastguard Worker  * Restrictions
44*49cdfc7eSAndroid Build Coastguard Worker  *	NONE
45*49cdfc7eSAndroid Build Coastguard Worker  */
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <memory.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
54*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker #define	K_1	8192
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker #define	NBUFS		2
59*49cdfc7eSAndroid Build Coastguard Worker #define	CHUNK		K_1	/* single chunk */
60*49cdfc7eSAndroid Build Coastguard Worker #define	MAX_IOVEC	2
61*49cdfc7eSAndroid Build Coastguard Worker #define	DATA_FILE	"writev_data_file"
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker char buf1[K_1];
64*49cdfc7eSAndroid Build Coastguard Worker char buf2[K_1];
65*49cdfc7eSAndroid Build Coastguard Worker char buf3[K_1];
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker char *bad_addr = 0;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker struct iovec wr_iovec[MAX_IOVEC] = {
70*49cdfc7eSAndroid Build Coastguard Worker 	{(caddr_t) - 1, CHUNK},
71*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, 0}
72*49cdfc7eSAndroid Build Coastguard Worker };
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker char name[K_1], f_name[K_1];
75*49cdfc7eSAndroid Build Coastguard Worker int fd[2], in_sighandler;
76*49cdfc7eSAndroid Build Coastguard Worker char *buf_list[NBUFS];
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "writev05";
79*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker void sighandler(int);
82*49cdfc7eSAndroid Build Coastguard Worker long l_seek(int, long, int);
83*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
84*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
85*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char ** argv)86*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	int nbytes;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	setup();		/* set "tstdir", and "testfile" vars */
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	/* The following loop checks looping state if -i option given */
97*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		/* reset tst_count in case we are looping */
100*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 		buf_list[0] = buf1;
103*49cdfc7eSAndroid Build Coastguard Worker 		buf_list[1] = buf2;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 		fd[1] = -1;	/* Invalid file descriptor */
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 		if (signal(SIGTERM, sighandler) == SIG_ERR) {
108*49cdfc7eSAndroid Build Coastguard Worker 			perror("signal");
109*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "signal() SIGTERM FAILED");
110*49cdfc7eSAndroid Build Coastguard Worker 			cleanup();
111*49cdfc7eSAndroid Build Coastguard Worker 		}
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 		if (signal(SIGPIPE, sighandler) == SIG_ERR) {
114*49cdfc7eSAndroid Build Coastguard Worker 			perror("signal");
115*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "signal() SIGPIPE FAILED");
116*49cdfc7eSAndroid Build Coastguard Worker 			cleanup();
117*49cdfc7eSAndroid Build Coastguard Worker 		}
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 		/* Fill the buf_list[0] and buf_list[1] with 0 zeros */
120*49cdfc7eSAndroid Build Coastguard Worker 		memset(buf_list[0], 0, K_1);
121*49cdfc7eSAndroid Build Coastguard Worker 		memset(buf_list[1], 0, K_1);
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 		if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) {
124*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "open(2) failed: fname = %s, "
125*49cdfc7eSAndroid Build Coastguard Worker 				 "errno = %d", f_name, errno);
126*49cdfc7eSAndroid Build Coastguard Worker 			cleanup();
127*49cdfc7eSAndroid Build Coastguard Worker 		} else {
128*49cdfc7eSAndroid Build Coastguard Worker 			if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) {
129*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "write(2) failed: nbytes "
130*49cdfc7eSAndroid Build Coastguard Worker 					 "= %d, errno = %d", nbytes, errno);
131*49cdfc7eSAndroid Build Coastguard Worker 				cleanup();
132*49cdfc7eSAndroid Build Coastguard Worker 			}
133*49cdfc7eSAndroid Build Coastguard Worker 		}
134*49cdfc7eSAndroid Build Coastguard Worker 
135*49cdfc7eSAndroid Build Coastguard Worker 		if (close(fd[0]) < 0) {
136*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "close failed: errno = %d", errno);
137*49cdfc7eSAndroid Build Coastguard Worker 			cleanup();
138*49cdfc7eSAndroid Build Coastguard Worker 		}
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 		if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) {
141*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "open failed: fname = %s, errno = %d",
142*49cdfc7eSAndroid Build Coastguard Worker 				 f_name, errno);
143*49cdfc7eSAndroid Build Coastguard Worker 			cleanup();
144*49cdfc7eSAndroid Build Coastguard Worker 		}
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 		/*
147*49cdfc7eSAndroid Build Coastguard Worker 		 * In this block we are trying to call writev() with invalid
148*49cdfc7eSAndroid Build Coastguard Worker 		 * vector to be written in a sparse file. This will return
149*49cdfc7eSAndroid Build Coastguard Worker 		 * EFAULT. At the same time, check should be made whether
150*49cdfc7eSAndroid Build Coastguard Worker 		 * the scheduled write() with valid data is done correctly
151*49cdfc7eSAndroid Build Coastguard Worker 		 * or not.
152*49cdfc7eSAndroid Build Coastguard Worker 		 */
153*49cdfc7eSAndroid Build Coastguard Worker 		l_seek(fd[0], 0, 0);
154*49cdfc7eSAndroid Build Coastguard Worker 		TEST(writev(fd[0], wr_iovec, 2));
155*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
156*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_ERRNO == EFAULT) {
157*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "Received EFAULT as expected");
158*49cdfc7eSAndroid Build Coastguard Worker 			} else {
159*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Expected EFAULT, got %d",
160*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_ERRNO);
161*49cdfc7eSAndroid Build Coastguard Worker 			}
162*49cdfc7eSAndroid Build Coastguard Worker 			l_seek(fd[0], K_1, 0);
163*49cdfc7eSAndroid Build Coastguard Worker 			if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != 0) {
164*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Expected nbytes = 0, got "
165*49cdfc7eSAndroid Build Coastguard Worker 					 "%d", nbytes);
166*49cdfc7eSAndroid Build Coastguard Worker 			}
167*49cdfc7eSAndroid Build Coastguard Worker 		} else {
168*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Error writev returned a positive "
169*49cdfc7eSAndroid Build Coastguard Worker 				 "value");
170*49cdfc7eSAndroid Build Coastguard Worker 		}
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker 	close(fd[0]);
173*49cdfc7eSAndroid Build Coastguard Worker 	close(fd[1]);
174*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
175*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker }
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker /*
180*49cdfc7eSAndroid Build Coastguard Worker  * setup()
181*49cdfc7eSAndroid Build Coastguard Worker  *	performs all ONE TIME setup for this test
182*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)183*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
184*49cdfc7eSAndroid Build Coastguard Worker {
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 	/* Pause if that option was specified.
189*49cdfc7eSAndroid Build Coastguard Worker 	 * TEST_PAUSE contains the code to fork the test with the -i option.
190*49cdfc7eSAndroid Build Coastguard Worker 	 * You want to make sure you do this before you create your temporary
191*49cdfc7eSAndroid Build Coastguard Worker 	 * directory.
192*49cdfc7eSAndroid Build Coastguard Worker 	 */
193*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker 	/* Create a unique temporary directory and chdir() to it. */
196*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(name, DATA_FILE);
199*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(f_name, "%s.%d", name, getpid());
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
202*49cdfc7eSAndroid Build Coastguard Worker 	if (bad_addr == MAP_FAILED) {
203*49cdfc7eSAndroid Build Coastguard Worker 		printf("mmap failed\n");
204*49cdfc7eSAndroid Build Coastguard Worker 	}
205*49cdfc7eSAndroid Build Coastguard Worker 	wr_iovec[0].iov_base = bad_addr;
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker }
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker /*
210*49cdfc7eSAndroid Build Coastguard Worker  * cleanup()
211*49cdfc7eSAndroid Build Coastguard Worker  *	performs all ONE TIME cleanup for this test at
212*49cdfc7eSAndroid Build Coastguard Worker  *	completion or premature exit
213*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)214*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
215*49cdfc7eSAndroid Build Coastguard Worker {
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	if (unlink(f_name) < 0) {
218*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d",
219*49cdfc7eSAndroid Build Coastguard Worker 			 f_name, errno);
220*49cdfc7eSAndroid Build Coastguard Worker 	}
221*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
222*49cdfc7eSAndroid Build Coastguard Worker 
223*49cdfc7eSAndroid Build Coastguard Worker }
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker /*
226*49cdfc7eSAndroid Build Coastguard Worker  * sighandler()
227*49cdfc7eSAndroid Build Coastguard Worker  *	Signal handler for SIGTERM and SIGPIPE
228*49cdfc7eSAndroid Build Coastguard Worker  */
sighandler(int sig)229*49cdfc7eSAndroid Build Coastguard Worker void sighandler(int sig)
230*49cdfc7eSAndroid Build Coastguard Worker {
231*49cdfc7eSAndroid Build Coastguard Worker 	switch (sig) {
232*49cdfc7eSAndroid Build Coastguard Worker 	case SIGTERM:
233*49cdfc7eSAndroid Build Coastguard Worker 		break;
234*49cdfc7eSAndroid Build Coastguard Worker 	case SIGPIPE:
235*49cdfc7eSAndroid Build Coastguard Worker 		++in_sighandler;
236*49cdfc7eSAndroid Build Coastguard Worker 		return;
237*49cdfc7eSAndroid Build Coastguard Worker 	default:
238*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "sighandler() received invalid signal "
239*49cdfc7eSAndroid Build Coastguard Worker 			 ": %d", sig);
240*49cdfc7eSAndroid Build Coastguard Worker 		break;
241*49cdfc7eSAndroid Build Coastguard Worker 	}
242*49cdfc7eSAndroid Build Coastguard Worker 
243*49cdfc7eSAndroid Build Coastguard Worker 	if ((unlink(f_name) < 0) && (errno != ENOENT)) {
244*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d",
245*49cdfc7eSAndroid Build Coastguard Worker 			 f_name, errno);
246*49cdfc7eSAndroid Build Coastguard Worker 		cleanup();
247*49cdfc7eSAndroid Build Coastguard Worker 	}
248*49cdfc7eSAndroid Build Coastguard Worker 	exit(sig);
249*49cdfc7eSAndroid Build Coastguard Worker }
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker /*
252*49cdfc7eSAndroid Build Coastguard Worker  * l_seek()
253*49cdfc7eSAndroid Build Coastguard Worker  *	Wrap around for regular lseek() to give error message on failure
254*49cdfc7eSAndroid Build Coastguard Worker  */
l_seek(int fdesc,long offset,int whence)255*49cdfc7eSAndroid Build Coastguard Worker long l_seek(int fdesc, long offset, int whence)
256*49cdfc7eSAndroid Build Coastguard Worker {
257*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fdesc, offset, whence) < 0) {
258*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "lseek Failed : errno = %d", errno);
259*49cdfc7eSAndroid Build Coastguard Worker 	}
260*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
261*49cdfc7eSAndroid Build Coastguard Worker }
262