Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 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" |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 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[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 44 | {"n:", &nflag, &nr_opt}, |
| 45 | {"s:", &sflag, &sz_opt}, |
| 46 | {NULL, NULL, NULL} |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 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 | |
| 63 | int main(int argc, char **argv) |
| 64 | { |
| 65 | int lc, status; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 66 | const char *msg; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 67 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 72 | "use -help", msg); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 73 | |
| 74 | setup(); |
| 75 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 76 | tst_count = 0; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 77 | |
| 78 | if (pipe(pipe_fd) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | tst_brkm(TBROK | TERRNO, cleanup, "pipe"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 80 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | tst_brkm(TBROK | TERRNO, cleanup, "fork #0"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 90 | case 0: |
| 91 | child_alloc(bufsz_arr); |
| 92 | exit(0); |
| 93 | } |
| 94 | |
| 95 | pids[1] = fork(); |
| 96 | switch (pids[1]) { |
| 97 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | tst_brkm(TBROK | TERRNO, cleanup, "fork #1"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 99 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 107 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 115 | 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 | |
| 125 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | arr[arr_sz - 1] = bufsz_left; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | nr_iovecs); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 158 | |
| 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 | |
| 169 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | tst_brkm(TBROK | TERRNO, tst_exit, "sscanf"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 182 | |
| 183 | len = nr_iovecs * sizeof(long); |
| 184 | bar = SAFE_MALLOC(tst_exit, len); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | local.iov_base = bar; |
| 186 | local.iov_len = len; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 187 | remote.iov_base = foo; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | remote.iov_len = len; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 189 | |
| 190 | TEST(test_process_vm_readv(pids[0], &local, 1, &remote, 1, 0)); |
| 191 | if (TEST_RETURN != len) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 193 | |
| 194 | return local.iov_base; |
| 195 | } |
| 196 | |
| 197 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 210 | remote[i].iov_len = bufsz_arr[i]; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 211 | } |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 217 | local[i].iov_len = rcv_arr[i]; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 218 | } |
| 219 | tst_resm(TINFO, "child 1: %d local iovecs initialized.", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | NUM_LOCAL_VECS); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 221 | |
| 222 | TEST(test_process_vm_readv(pids[0], local, NUM_LOCAL_VECS, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 223 | remote, nr_iovecs, 0)); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 224 | if (TEST_RETURN != bufsz) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | tst_brkm(TBROK | TERRNO, tst_exit, "process_vm_readv"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 226 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 237 | "for byte seq %d", |
| 238 | expect, actual, count); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 239 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | "received.", nr_error); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 248 | else |
| 249 | tst_resm(TPASS, "child 1: all bytes are correctly received."); |
| 250 | } |
| 251 | |
| 252 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 258 | : 100000; |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 259 | |
| 260 | #if !defined(__NR_process_vm_readv) |
| 261 | tst_brkm(TCONF, NULL, "process_vm_readv does not exist " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 262 | "on your system"); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 263 | #endif |
| 264 | semid = init_sem(1); |
| 265 | srand(time(NULL)); |
| 266 | |
| 267 | TEST_PAUSE; |
| 268 | } |
| 269 | |
| 270 | static void cleanup(void) |
| 271 | { |
| 272 | clean_sem(semid); |
Caspar Zhang | 9710511 | 2012-07-13 20:44:01 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static 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 | } |