2 * Check that fault injection works properly.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 static const int expfd = 4;
42 static const int gotfd = 5;
44 #define DEFAULT_ERRNO ENOSYS
46 static const char *errstr;
47 static int is_raw, err, first, step, iter, try;
52 static char buf[sizeof(int) * 3 + 3];
53 const struct iovec io = {
55 .iov_len = sprintf(buf, "%d.", ++try)
60 rc = write(expfd, io.iov_base, io.iov_len);
61 if (rc != (int) io.iov_len)
62 perror_msg_and_fail("write");
66 rc = writev(gotfd, &io, 1);
69 if (!(rc == -1 && errno == err))
70 perror_msg_and_fail("expected errno %d"
71 ", got rc == %d, errno == %d",
75 tprintf("writev(%#x, %p, 0x1) = -1 (errno %d)"
76 " (INJECTED)\n", gotfd, &io, err);
78 tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%d}], 1)"
79 " = -1 %s (%m) (INJECTED)\n",
80 gotfd, buf, (int) io.iov_len, errstr);
82 if (rc != (int) io.iov_len)
83 perror_msg_and_fail("expected %d"
84 ", got rc == %d, errno == %d",
85 (int) io.iov_len, rc, errno);
88 tprintf("writev(%#x, %p, 0x1) = %#x\n", gotfd, &io, rc);
90 tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%d}], 1)"
92 gotfd, buf, (int) io.iov_len, (int) io.iov_len);
97 main(int argc, char *argv[])
101 assert(fstat(expfd, &st) == 0);
102 assert(fstat(gotfd, &st) == 0);
106 is_raw = !strcmp("raw", argv[1]);
115 else if (!strcasecmp(errstr, "EINVAL"))
122 errstr = errno2name();
124 first = atoi(argv[3]);
125 step = atoi(argv[4]);
126 iter = atoi(argv[5]);
134 for (i = 1; i <= iter; ++i) {
146 tprintf("%s\n", "+++ exited with 0 +++");