1 /* 2 * Check that fault injection works properly. 3 * 4 * Copyright (c) 2016 Dmitry V. Levin <ldv (at) altlinux.org> 5 * Copyright (c) 2016-2017 The strace developers. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "tests.h" 32 33 #include <assert.h> 34 #include <errno.h> 35 #include <fcntl.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 #include <sys/uio.h> 43 #include <sys/wait.h> 44 45 static int exp_fd; 46 static int got_fd; 47 static int out_fd; 48 49 #define DEFAULT_ERRNO ENOSYS 50 51 static const char *errstr; 52 static int is_raw, err, first, step, iter, try; 53 54 static void 55 invoke(int fail) 56 { 57 static char buf[sizeof(int) * 3 + 3]; 58 const struct iovec io = { 59 .iov_base = buf, 60 .iov_len = sprintf(buf, "%d.", ++try) 61 }; 62 int rc; 63 64 if (!fail) { 65 rc = write(exp_fd, io.iov_base, io.iov_len); 66 if (rc != (int) io.iov_len) 67 perror_msg_and_fail("write"); 68 } 69 70 errno = 0; 71 rc = writev(got_fd, &io, 1); 72 73 if (fail) { 74 if (!(rc == -1 && errno == err)) 75 perror_msg_and_fail("expected errno %d" 76 ", got rc == %d, errno == %d", 77 err, rc, errno); 78 79 if (is_raw) 80 tprintf("writev(%#x, %p, 0x1) = -1 (errno %d)" 81 " (INJECTED)\n", got_fd, &io, err); 82 else 83 tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%d}], 1)" 84 " = -1 %s (%m) (INJECTED)\n", 85 got_fd, buf, (int) io.iov_len, errstr); 86 } else { 87 if (rc != (int) io.iov_len) 88 perror_msg_and_fail("expected %d" 89 ", got rc == %d, errno == %d", 90 (int) io.iov_len, rc, errno); 91 92 if (is_raw) 93 tprintf("writev(%#x, %p, 0x1) = %#x\n", 94 got_fd, &io, rc); 95 else 96 tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%d}], 1)" 97 " = %d\n", 98 got_fd, buf, (int) io.iov_len, 99 (int) io.iov_len); 100 } 101 } 102 103 static int 104 open_file(const char *prefix, int proc) 105 { 106 static const int open_flags = O_WRONLY | O_TRUNC | O_CREAT; 107 static char path[PATH_MAX + 1]; 108 109 snprintf(path, sizeof(path), "%s.%d", prefix, proc); 110 111 int fd = open(path, open_flags, 0600); 112 if (fd < 0) 113 perror_msg_and_fail("open: %s", path); 114 115 return fd; 116 } 117 118 int 119 main(int argc, char *argv[]) 120 { 121 assert(argc == 11); 122 123 is_raw = !strcmp("raw", argv[1]); 124 125 errstr = argv[2]; 126 err = atoi(errstr); 127 assert(err >= 0); 128 129 if (!err) { 130 if (!*errstr) 131 err = DEFAULT_ERRNO; 132 else if (!strcasecmp(errstr, "EINVAL")) 133 err = EINVAL; 134 else 135 err = ENOSYS; 136 } 137 138 errno = err; 139 errstr = errno2name(); 140 141 first = atoi(argv[3]); 142 step = atoi(argv[4]); 143 iter = atoi(argv[5]); 144 int num_procs = atoi(argv[6]); 145 char *exp_prefix = argv[7]; 146 char *got_prefix = argv[8]; 147 char *out_prefix = argv[9]; 148 char *pid_prefix = argv[10]; 149 150 assert(first > 0); 151 assert(step >= 0); 152 assert(num_procs > 0); 153 154 int proc; 155 for (proc = 0; proc < num_procs; ++proc) { 156 int ret = fork(); 157 158 if (ret < 0) 159 perror_msg_and_fail("fork"); 160 161 if (ret > 0) { 162 int pidfd = open_file(pid_prefix, proc); 163 164 char pidstr[sizeof(ret) * 3]; 165 int len = snprintf(pidstr, sizeof(pidstr), "%d", ret); 166 assert(len > 0 && len < (int) sizeof(pidstr)); 167 assert(write(pidfd, pidstr, len) == len); 168 169 close(pidfd); 170 171 continue; 172 } 173 174 tprintf("%s", ""); 175 176 exp_fd = open_file(exp_prefix, proc); 177 got_fd = open_file(got_prefix, proc); 178 out_fd = open_file(out_prefix, proc); 179 180 /* This magic forces tprintf to write where we want it. */ 181 dup2(out_fd, 3); 182 183 int i; 184 for (i = 1; i <= iter; ++i) { 185 int fail = 0; 186 if (first > 0) { 187 --first; 188 if (first == 0) { 189 fail = 1; 190 first = step; 191 } 192 } 193 invoke(fail); 194 } 195 196 tprintf("%s\n", "+++ exited with 0 +++"); 197 return 0; 198 } 199 200 for (proc = 0; proc < num_procs; ++proc) { 201 int status; 202 int ret = wait(&status); 203 if (ret <= 0) 204 perror_msg_and_fail("wait %d", proc); 205 if (status) 206 error_msg_and_fail("wait: pid=%d status=%d", 207 ret, status); 208 } 209 210 return 0; 211 } 212