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
32 #include "test.h"
33 #include "safe_macros.h"
34 #include "process_vm.h"
35
36 char *TCID = "process_vm_readv03";
37 int TST_TOTAL = 1;
38
39 #define NUM_LOCAL_VECS 4
40
41 static int nflag, sflag;
42 static char *nr_opt, *sz_opt;
43 static option_t options[] = {
44 {"n:", &nflag, &nr_opt},
45 {"s:", &sflag, &sz_opt},
46 {NULL, NULL, NULL}
47 };
48
49 static int nr_iovecs;
50 static long bufsz;
51 static int pipe_fd[2];
52 static pid_t pids[2];
53 static int semid;
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 if (pipe(pipe_fd) < 0)
75 tst_brkm(TBROK | TERRNO, cleanup, "pipe");
76
77 bufsz_arr = SAFE_MALLOC(cleanup, nr_iovecs * sizeof(int));
78 gen_random_arr(bufsz_arr, nr_iovecs);
79
80 /* the start of child_alloc and child_invoke is already
81 * synchronized via pipe */
82 pids[0] = fork();
83 switch (pids[0]) {
84 case -1:
85 tst_brkm(TBROK | TERRNO, cleanup, "fork #0");
86 case 0:
87 child_alloc(bufsz_arr);
88 exit(0);
89 }
90
91 pids[1] = fork();
92 switch (pids[1]) {
93 case -1:
94 tst_brkm(TBROK | TERRNO, cleanup, "fork #1");
95 case 0:
96 child_invoke(bufsz_arr);
97 exit(0);
98 }
99
100 /* wait until child_invoke reads from child_alloc's VM */
101 if (waitpid(pids[1], &status, 0) == -1)
102 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
103 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
104 tst_resm(TFAIL, "child 1 returns %d", status);
105
106 /* child_alloc is free to exit now */
107 safe_semop(semid, 0, 1);
108
109 if (waitpid(pids[0], &status, 0) == -1)
110 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
111 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
112 tst_resm(TFAIL, "child 0 returns %d", status);
113
114 free(bufsz_arr);
115 }
116
117 cleanup();
118 tst_exit();
119 }
120
gen_random_arr(int * arr,int arr_sz)121 static void gen_random_arr(int *arr, int arr_sz)
122 {
123 long bufsz_left, bufsz_single;
124 int i;
125
126 bufsz_left = bufsz;
127 for (i = 0; i < arr_sz - 1; i++) {
128 bufsz_single = rand() % (bufsz_left / 2) + 1;
129 arr[i] = bufsz_single;
130 bufsz_left -= bufsz_single;
131 }
132 arr[arr_sz - 1] = bufsz_left;
133 }
134
child_alloc(int * bufsz_arr)135 static void child_alloc(int *bufsz_arr)
136 {
137 char **foo;
138 int i, j;
139 char buf[BUFSIZ];
140 long count;
141
142 foo = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(char *));
143
144 count = 0;
145 for (i = 0; i < nr_iovecs; i++) {
146 foo[i] = SAFE_MALLOC(tst_exit, bufsz_arr[i]);
147 for (j = 0; j < bufsz_arr[i]; j++) {
148 foo[i][j] = count % 256;
149 count++;
150 }
151 }
152 tst_resm(TINFO, "child 0: %d iovecs allocated and initialized.",
153 nr_iovecs);
154
155 /* passing addr via pipe */
156 SAFE_CLOSE(tst_exit, pipe_fd[0]);
157 snprintf(buf, BUFSIZ, "%p", (void *)foo);
158 SAFE_WRITE(tst_exit, 1, pipe_fd[1], buf, strlen(buf));
159 SAFE_CLOSE(tst_exit, pipe_fd[1]);
160
161 /* wait until child_invoke is done reading from our VM */
162 safe_semop(semid, 0, -1);
163 }
164
fetch_remote_addrs(void)165 static long *fetch_remote_addrs(void)
166 {
167 long *foo, *bar;
168 char buf[BUFSIZ];
169 long len;
170 struct iovec local, remote;
171
172 /* get addr from pipe */
173 SAFE_CLOSE(tst_exit, pipe_fd[1]);
174 SAFE_READ(tst_exit, 0, pipe_fd[0], buf, BUFSIZ);
175 SAFE_CLOSE(tst_exit, pipe_fd[0]);
176 if (sscanf(buf, "%p", &foo) != 1)
177 tst_brkm(TBROK | TERRNO, tst_exit, "sscanf");
178
179 len = nr_iovecs * sizeof(long);
180 bar = SAFE_MALLOC(tst_exit, len);
181 local.iov_base = bar;
182 local.iov_len = len;
183 remote.iov_base = foo;
184 remote.iov_len = len;
185
186 TEST(test_process_vm_readv(pids[0], &local, 1, &remote, 1, 0));
187 if (TEST_RETURN != len)
188 tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv");
189
190 return local.iov_base;
191 }
192
child_invoke(int * bufsz_arr)193 static void child_invoke(int *bufsz_arr)
194 {
195 int i, j, count, nr_error;
196 unsigned char expect, actual;
197 long *addrs;
198 struct iovec local[NUM_LOCAL_VECS], *remote;
199 int rcv_arr[NUM_LOCAL_VECS];
200
201 addrs = fetch_remote_addrs();
202
203 remote = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(struct iovec));
204 for (i = 0; i < nr_iovecs; i++) {
205 remote[i].iov_base = (void *)addrs[i];
206 remote[i].iov_len = bufsz_arr[i];
207 }
208 tst_resm(TINFO, "child 1: %d remote iovecs received.", nr_iovecs);
209
210 gen_random_arr(rcv_arr, NUM_LOCAL_VECS);
211 for (i = 0; i < NUM_LOCAL_VECS; i++) {
212 local[i].iov_base = SAFE_MALLOC(tst_exit, rcv_arr[i]);
213 local[i].iov_len = rcv_arr[i];
214 }
215 tst_resm(TINFO, "child 1: %d local iovecs initialized.",
216 NUM_LOCAL_VECS);
217
218 TEST(test_process_vm_readv(pids[0], local, NUM_LOCAL_VECS,
219 remote, nr_iovecs, 0));
220 if (TEST_RETURN != bufsz)
221 tst_brkm(TBROK | TERRNO, tst_exit, "process_vm_readv");
222
223 /* verify every byte */
224 count = 0;
225 nr_error = 0;
226 for (i = 0; i < NUM_LOCAL_VECS; i++) {
227 for (j = 0; j < local[i].iov_len; j++) {
228 expect = count % 256;
229 actual = ((unsigned char *)local[i].iov_base)[j];
230 if (expect != actual) {
231 #if DEBUG
232 tst_resm(TFAIL, "child 1: expected %i, got %i "
233 "for byte seq %d",
234 expect, actual, count);
235 #endif
236 nr_error++;
237 }
238 count++;
239 }
240 }
241 if (nr_error)
242 tst_brkm(TFAIL, tst_exit, "child 1: %d incorrect bytes "
243 "received.", nr_error);
244 else
245 tst_resm(TPASS, "child 1: all bytes are correctly received.");
246 }
247
setup(void)248 static void setup(void)
249 {
250 tst_require_root();
251
252 nr_iovecs = nflag ? SAFE_STRTOL(NULL, nr_opt, 1, IOV_MAX) : 10;
253 bufsz = sflag ? SAFE_STRTOL(NULL, sz_opt, NUM_LOCAL_VECS, LONG_MAX)
254 : 100000;
255
256 #if !defined(__NR_process_vm_readv)
257 tst_brkm(TCONF, NULL, "process_vm_readv does not exist "
258 "on your system");
259 #endif
260 semid = init_sem(1);
261 srand(time(NULL));
262
263 TEST_PAUSE;
264 }
265
cleanup(void)266 static void cleanup(void)
267 {
268 clean_sem(semid);
269 }
270
help(void)271 static void help(void)
272 {
273 printf(" -n NUM Set the number of iovecs to be allocated.\n");
274 printf(" -s NUM Set the size of total buffer size.\n");
275 }
276