blob: 025557dd918c8d1ac24319b9537b4222b8f94e39 [file] [log] [blame]
Caspar Zhang97105112012-07-13 20:44:01 +08001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Caspar Zhang97105112012-07-13 20:44:01 +080018 */
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
32#include "test.h"
Caspar Zhang97105112012-07-13 20:44:01 +080033#include "safe_macros.h"
34#include "process_vm.h"
35
36char *TCID = "process_vm_readv03";
37int TST_TOTAL = 1;
38
39#define NUM_LOCAL_VECS 4
40
41static int nflag, sflag;
42static char *nr_opt, *sz_opt;
43static option_t options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080044 {"n:", &nflag, &nr_opt},
45 {"s:", &sflag, &sz_opt},
46 {NULL, NULL, NULL}
Caspar Zhang97105112012-07-13 20:44:01 +080047};
48
49static int nr_iovecs;
50static long bufsz;
51static int pipe_fd[2];
52static pid_t pids[2];
53static int semid;
54
55static void gen_random_arr(int *arr, int arr_sz);
56static void child_alloc(int *bufsz_arr);
57static void child_invoke(int *bufsz_arr);
58static long *fetch_remote_addrs(void);
59static void setup(void);
60static void cleanup(void);
61static void help(void);
62
63int main(int argc, char **argv)
64{
65 int lc, status;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
Caspar Zhang97105112012-07-13 20:44:01 +080067 int *bufsz_arr;
68
69 msg = parse_opts(argc, argv, options, &help);
70 if (msg != NULL)
71 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s"
Wanlong Gao354ebb42012-12-07 10:10:04 +080072 "use -help", msg);
Caspar Zhang97105112012-07-13 20:44:01 +080073
74 setup();
75 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080076 tst_count = 0;
Caspar Zhang97105112012-07-13 20:44:01 +080077
78 if (pipe(pipe_fd) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 tst_brkm(TBROK | TERRNO, cleanup, "pipe");
Caspar Zhang97105112012-07-13 20:44:01 +080080
81 bufsz_arr = SAFE_MALLOC(cleanup, nr_iovecs * sizeof(int));
82 gen_random_arr(bufsz_arr, nr_iovecs);
83
84 /* the start of child_alloc and child_invoke is already
85 * synchronized via pipe */
86 pids[0] = fork();
87 switch (pids[0]) {
88 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 tst_brkm(TBROK | TERRNO, cleanup, "fork #0");
Caspar Zhang97105112012-07-13 20:44:01 +080090 case 0:
91 child_alloc(bufsz_arr);
92 exit(0);
93 }
94
95 pids[1] = fork();
96 switch (pids[1]) {
97 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 tst_brkm(TBROK | TERRNO, cleanup, "fork #1");
Caspar Zhang97105112012-07-13 20:44:01 +080099 case 0:
100 child_invoke(bufsz_arr);
101 exit(0);
102 }
103
104 /* wait until child_invoke reads from child_alloc's VM */
105 if (waitpid(pids[1], &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Caspar Zhang97105112012-07-13 20:44:01 +0800107 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
108 tst_resm(TFAIL, "child 1 returns %d", status);
109
110 /* child_alloc is free to exit now */
111 safe_semop(semid, 0, 1);
112
113 if (waitpid(pids[0], &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Caspar Zhang97105112012-07-13 20:44:01 +0800115 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
116 tst_resm(TFAIL, "child 0 returns %d", status);
117
118 free(bufsz_arr);
119 }
120
121 cleanup();
122 tst_exit();
123}
124
125static void gen_random_arr(int *arr, int arr_sz)
126{
127 long bufsz_left, bufsz_single;
128 int i;
129
130 bufsz_left = bufsz;
131 for (i = 0; i < arr_sz - 1; i++) {
132 bufsz_single = rand() % (bufsz_left / 2) + 1;
133 arr[i] = bufsz_single;
134 bufsz_left -= bufsz_single;
135 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 arr[arr_sz - 1] = bufsz_left;
Caspar Zhang97105112012-07-13 20:44:01 +0800137}
138
139static void child_alloc(int *bufsz_arr)
140{
141 char **foo;
142 int i, j;
143 char buf[BUFSIZ];
144 long count;
145
146 foo = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(char *));
147
148 count = 0;
149 for (i = 0; i < nr_iovecs; i++) {
150 foo[i] = SAFE_MALLOC(tst_exit, bufsz_arr[i]);
151 for (j = 0; j < bufsz_arr[i]; j++) {
152 foo[i][j] = count % 256;
153 count++;
154 }
155 }
156 tst_resm(TINFO, "child 0: %d iovecs allocated and initialized.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 nr_iovecs);
Caspar Zhang97105112012-07-13 20:44:01 +0800158
159 /* passing addr via pipe */
160 SAFE_CLOSE(tst_exit, pipe_fd[0]);
161 snprintf(buf, BUFSIZ, "%p", (void *)foo);
162 SAFE_WRITE(tst_exit, 1, pipe_fd[1], buf, strlen(buf));
163 SAFE_CLOSE(tst_exit, pipe_fd[1]);
164
165 /* wait until child_invoke is done reading from our VM */
166 safe_semop(semid, 0, -1);
167}
168
169static long *fetch_remote_addrs(void)
170{
171 long *foo, *bar;
172 char buf[BUFSIZ];
173 long len;
174 struct iovec local, remote;
175
176 /* get addr from pipe */
177 SAFE_CLOSE(tst_exit, pipe_fd[1]);
178 SAFE_READ(tst_exit, 0, pipe_fd[0], buf, BUFSIZ);
179 SAFE_CLOSE(tst_exit, pipe_fd[0]);
180 if (sscanf(buf, "%p", &foo) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 tst_brkm(TBROK | TERRNO, tst_exit, "sscanf");
Caspar Zhang97105112012-07-13 20:44:01 +0800182
183 len = nr_iovecs * sizeof(long);
184 bar = SAFE_MALLOC(tst_exit, len);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 local.iov_base = bar;
186 local.iov_len = len;
Caspar Zhang97105112012-07-13 20:44:01 +0800187 remote.iov_base = foo;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 remote.iov_len = len;
Caspar Zhang97105112012-07-13 20:44:01 +0800189
190 TEST(test_process_vm_readv(pids[0], &local, 1, &remote, 1, 0));
191 if (TEST_RETURN != len)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800192 tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv");
Caspar Zhang97105112012-07-13 20:44:01 +0800193
194 return local.iov_base;
195}
196
197static void child_invoke(int *bufsz_arr)
198{
199 int i, j, count, nr_error;
200 unsigned char expect, actual;
201 long *addrs;
202 struct iovec local[NUM_LOCAL_VECS], *remote;
203 int rcv_arr[NUM_LOCAL_VECS];
204
205 addrs = fetch_remote_addrs();
206
207 remote = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(struct iovec));
208 for (i = 0; i < nr_iovecs; i++) {
209 remote[i].iov_base = (void *)addrs[i];
Wanlong Gao354ebb42012-12-07 10:10:04 +0800210 remote[i].iov_len = bufsz_arr[i];
Caspar Zhang97105112012-07-13 20:44:01 +0800211 }
212 tst_resm(TINFO, "child 1: %d remote iovecs received.", nr_iovecs);
213
214 gen_random_arr(rcv_arr, NUM_LOCAL_VECS);
215 for (i = 0; i < NUM_LOCAL_VECS; i++) {
216 local[i].iov_base = SAFE_MALLOC(tst_exit, rcv_arr[i]);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 local[i].iov_len = rcv_arr[i];
Caspar Zhang97105112012-07-13 20:44:01 +0800218 }
219 tst_resm(TINFO, "child 1: %d local iovecs initialized.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220 NUM_LOCAL_VECS);
Caspar Zhang97105112012-07-13 20:44:01 +0800221
222 TEST(test_process_vm_readv(pids[0], local, NUM_LOCAL_VECS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223 remote, nr_iovecs, 0));
Caspar Zhang97105112012-07-13 20:44:01 +0800224 if (TEST_RETURN != bufsz)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 tst_brkm(TBROK | TERRNO, tst_exit, "process_vm_readv");
Caspar Zhang97105112012-07-13 20:44:01 +0800226
227 /* verify every byte */
228 count = 0;
229 nr_error = 0;
230 for (i = 0; i < NUM_LOCAL_VECS; i++) {
231 for (j = 0; j < local[i].iov_len; j++) {
232 expect = count % 256;
233 actual = ((unsigned char *)local[i].iov_base)[j];
234 if (expect != actual) {
235#if DEBUG
236 tst_resm(TFAIL, "child 1: expected %i, got %i "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 "for byte seq %d",
238 expect, actual, count);
Caspar Zhang97105112012-07-13 20:44:01 +0800239#endif
240 nr_error++;
241 }
242 count++;
243 }
244 }
245 if (nr_error)
246 tst_brkm(TFAIL, tst_exit, "child 1: %d incorrect bytes "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 "received.", nr_error);
Caspar Zhang97105112012-07-13 20:44:01 +0800248 else
249 tst_resm(TPASS, "child 1: all bytes are correctly received.");
250}
251
252static void setup(void)
253{
254 tst_require_root(NULL);
255
256 nr_iovecs = nflag ? SAFE_STRTOL(NULL, nr_opt, 1, IOV_MAX) : 10;
257 bufsz = sflag ? SAFE_STRTOL(NULL, sz_opt, NUM_LOCAL_VECS, LONG_MAX)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800258 : 100000;
Caspar Zhang97105112012-07-13 20:44:01 +0800259
260#if !defined(__NR_process_vm_readv)
261 tst_brkm(TCONF, NULL, "process_vm_readv does not exist "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800262 "on your system");
Caspar Zhang97105112012-07-13 20:44:01 +0800263#endif
264 semid = init_sem(1);
265 srand(time(NULL));
266
267 TEST_PAUSE;
268}
269
270static void cleanup(void)
271{
272 clean_sem(semid);
Caspar Zhang97105112012-07-13 20:44:01 +0800273}
274
275static void help(void)
276{
277 printf(" -n NUM Set the number of iovecs to be allocated.\n");
278 printf(" -s NUM Set the size of total buffer size.\n");
279}