• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2012
3  * Copyright (c) Linux Test Project, 2012
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
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #define _GNU_SOURCE
21 #include <sys/types.h>
22 #include <sys/uio.h>
23 #include <sys/wait.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <limits.h>
32 
33 #include "test.h"
34 #include "safe_macros.h"
35 #include "lapi/syscalls.h"
36 
37 char *TCID = "process_vm_readv03";
38 int TST_TOTAL = 1;
39 
40 #define NUM_LOCAL_VECS 4
41 
42 static int nflag, sflag;
43 static char *nr_opt, *sz_opt;
44 static option_t options[] = {
45 	{"n:", &nflag, &nr_opt},
46 	{"s:", &sflag, &sz_opt},
47 	{NULL, NULL, NULL}
48 };
49 
50 static int nr_iovecs;
51 static long bufsz;
52 static int pipe_fd[2];
53 static pid_t pids[2];
54 
55 static void gen_random_arr(int *arr, int arr_sz);
56 static void child_alloc(int *bufsz_arr);
57 static void child_invoke(int *bufsz_arr);
58 static long *fetch_remote_addrs(void);
59 static void setup(void);
60 static void cleanup(void);
61 static void help(void);
62 
main(int argc,char ** argv)63 int main(int argc, char **argv)
64 {
65 	int lc, status;
66 	int *bufsz_arr;
67 
68 	tst_parse_opts(argc, argv, options, &help);
69 
70 	setup();
71 	for (lc = 0; TEST_LOOPING(lc); lc++) {
72 		tst_count = 0;
73 
74 		SAFE_PIPE(cleanup, pipe_fd);
75 
76 		bufsz_arr = SAFE_MALLOC(cleanup, nr_iovecs * sizeof(int));
77 		gen_random_arr(bufsz_arr, nr_iovecs);
78 
79 		/* the start of child_alloc and child_invoke is already
80 		 * synchronized via pipe */
81 		pids[0] = fork();
82 		switch (pids[0]) {
83 		case -1:
84 			tst_brkm(TBROK | TERRNO, cleanup, "fork #0");
85 		case 0:
86 			child_alloc(bufsz_arr);
87 			exit(0);
88 		}
89 
90 		pids[1] = fork();
91 		switch (pids[1]) {
92 		case -1:
93 			tst_brkm(TBROK | TERRNO, cleanup, "fork #1");
94 		case 0:
95 			child_invoke(bufsz_arr);
96 			exit(0);
97 		}
98 
99 		/* wait until child_invoke reads from child_alloc's VM */
100 		SAFE_WAITPID(cleanup, pids[1], &status, 0);
101 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
102 			tst_resm(TFAIL, "child 1 returns %d", status);
103 
104 		/* child_alloc is free to exit now */
105 		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
106 
107 		SAFE_WAITPID(cleanup, pids[0], &status, 0);
108 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
109 			tst_resm(TFAIL, "child 0 returns %d", status);
110 
111 		free(bufsz_arr);
112 	}
113 
114 	cleanup();
115 	tst_exit();
116 }
117 
gen_random_arr(int * arr,int arr_sz)118 static void gen_random_arr(int *arr, int arr_sz)
119 {
120 	long bufsz_left, bufsz_single;
121 	int i;
122 
123 	bufsz_left = bufsz;
124 	for (i = 0; i < arr_sz - 1; i++) {
125 		bufsz_single = rand() % (bufsz_left / 2) + 1;
126 		arr[i] = bufsz_single;
127 		bufsz_left -= bufsz_single;
128 	}
129 	arr[arr_sz - 1] = bufsz_left;
130 }
131 
child_alloc(int * bufsz_arr)132 static void child_alloc(int *bufsz_arr)
133 {
134 	char **foo;
135 	int i, j;
136 	char buf[BUFSIZ];
137 	long count;
138 
139 	foo = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(char *));
140 
141 	count = 0;
142 	for (i = 0; i < nr_iovecs; i++) {
143 		foo[i] = SAFE_MALLOC(tst_exit, bufsz_arr[i]);
144 		for (j = 0; j < bufsz_arr[i]; j++) {
145 			foo[i][j] = count % 256;
146 			count++;
147 		}
148 	}
149 	tst_resm(TINFO, "child 0: %d iovecs allocated and initialized.",
150 		 nr_iovecs);
151 
152 	/* passing addr via pipe */
153 	SAFE_CLOSE(tst_exit, pipe_fd[0]);
154 	snprintf(buf, BUFSIZ, "%p", (void *)foo);
155 	SAFE_WRITE(tst_exit, 1, pipe_fd[1], buf, strlen(buf) + 1);
156 	SAFE_CLOSE(tst_exit, pipe_fd[1]);
157 
158 	/* wait until child_invoke is done reading from our VM */
159 	TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
160 }
161 
fetch_remote_addrs(void)162 static long *fetch_remote_addrs(void)
163 {
164 	long *foo, *bar;
165 	char buf[BUFSIZ];
166 	long len;
167 	struct iovec local, remote;
168 
169 	/* get addr from pipe */
170 	SAFE_CLOSE(tst_exit, pipe_fd[1]);
171 	SAFE_READ(tst_exit, 0, pipe_fd[0], buf, BUFSIZ);
172 	SAFE_CLOSE(tst_exit, pipe_fd[0]);
173 	if (sscanf(buf, "%p", &foo) != 1)
174 		tst_brkm(TBROK | TERRNO, tst_exit, "sscanf");
175 
176 	len = nr_iovecs * sizeof(long);
177 	bar = SAFE_MALLOC(tst_exit, len);
178 	local.iov_base = bar;
179 	local.iov_len = len;
180 	remote.iov_base = foo;
181 	remote.iov_len = len;
182 
183 	TEST(ltp_syscall(__NR_process_vm_readv, pids[0], &local,
184 			 1, &remote, 1, 0));
185 	if (TEST_RETURN != len)
186 		tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv");
187 
188 	return local.iov_base;
189 }
190 
child_invoke(int * bufsz_arr)191 static void child_invoke(int *bufsz_arr)
192 {
193 	int i, j, count, nr_error;
194 	unsigned char expect, actual;
195 	long *addrs;
196 	struct iovec local[NUM_LOCAL_VECS], *remote;
197 	int rcv_arr[NUM_LOCAL_VECS];
198 
199 	addrs = fetch_remote_addrs();
200 
201 	remote = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(struct iovec));
202 	for (i = 0; i < nr_iovecs; i++) {
203 		remote[i].iov_base = (void *)addrs[i];
204 		remote[i].iov_len = bufsz_arr[i];
205 	}
206 	tst_resm(TINFO, "child 1: %d remote iovecs received.", nr_iovecs);
207 
208 	gen_random_arr(rcv_arr, NUM_LOCAL_VECS);
209 	for (i = 0; i < NUM_LOCAL_VECS; i++) {
210 		local[i].iov_base = SAFE_MALLOC(tst_exit, rcv_arr[i]);
211 		local[i].iov_len = rcv_arr[i];
212 	}
213 	tst_resm(TINFO, "child 1: %d local iovecs initialized.",
214 		 NUM_LOCAL_VECS);
215 
216 	TEST(ltp_syscall(__NR_process_vm_readv, pids[0], local, NUM_LOCAL_VECS,
217 				   remote, nr_iovecs, 0));
218 	if (TEST_RETURN != bufsz)
219 		tst_brkm(TBROK | TERRNO, tst_exit, "process_vm_readv");
220 
221 	/* verify every byte */
222 	count = 0;
223 	nr_error = 0;
224 	for (i = 0; i < NUM_LOCAL_VECS; i++) {
225 		for (j = 0; j < local[i].iov_len; j++) {
226 			expect = count % 256;
227 			actual = ((unsigned char *)local[i].iov_base)[j];
228 			if (expect != actual) {
229 #if DEBUG
230 				tst_resm(TFAIL, "child 1: expected %i, got %i "
231 					 "for byte seq %d",
232 					 expect, actual, count);
233 #endif
234 				nr_error++;
235 			}
236 			count++;
237 		}
238 	}
239 	if (nr_error)
240 		tst_brkm(TFAIL, tst_exit, "child 1: %d incorrect bytes "
241 			 "received.", nr_error);
242 	else
243 		tst_resm(TPASS, "child 1: all bytes are correctly received.");
244 }
245 
setup(void)246 static void setup(void)
247 {
248 	tst_require_root();
249 
250 	/* Just a sanity check of the existence of syscall */
251 	ltp_syscall(__NR_process_vm_readv, getpid(), NULL, 0, NULL, 0, 0);
252 
253 	nr_iovecs = nflag ? SAFE_STRTOL(NULL, nr_opt, 1, IOV_MAX) : 10;
254 	bufsz = sflag ? SAFE_STRTOL(NULL, sz_opt, NUM_LOCAL_VECS, LONG_MAX)
255 	    : 100000;
256 
257 	tst_tmpdir();
258 	TST_CHECKPOINT_INIT(cleanup);
259 	srand(time(NULL));
260 
261 	TEST_PAUSE;
262 }
263 
cleanup(void)264 static void cleanup(void)
265 {
266 	tst_rmdir();
267 }
268 
help(void)269 static void help(void)
270 {
271 	printf("    -n NUM  Set the number of iovecs to be allocated.\n");
272 	printf("    -s NUM  Set the size of total buffer size.\n");
273 }
274