[a42d7d8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup bithenge
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Fake system call errors for testing.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <fcntl.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 | #include <sys/stat.h>
|
---|
| 42 | #include <sys/types.h>
|
---|
| 43 | #include <sys/wait.h>
|
---|
| 44 | #include <unistd.h>
|
---|
| 45 | #define BITHENGE_FAILURE_DECLS_ONLY 1
|
---|
| 46 | #include "failure.h"
|
---|
| 47 |
|
---|
| 48 | /* This file raises fake errors from system calls, to test that Bithenge
|
---|
| 49 | * handles the errors correctly. It has two primary modes of operation,
|
---|
| 50 | * depending on an environment variable:
|
---|
| 51 | *
|
---|
| 52 | * BITHENGE_FAILURE_INDEX not set: when a system call is made, a child process
|
---|
| 53 | * returns a fake error from that call. If the child process handles the error
|
---|
| 54 | * correctly (exit code is 1), the main process continues without errors. If
|
---|
| 55 | * the child process has a problem, the main process raises the fake error
|
---|
| 56 | * again and shows all stdout and stderr output. For speed, failures only occur
|
---|
| 57 | * for some system calls after the first 128.
|
---|
| 58 | *
|
---|
| 59 | * BITHENGE_FAILURE_INDEX set: the program runs normally until system call
|
---|
| 60 | * number BITHENGE_FAILURE_INDEX is made; a fake error is returned from this
|
---|
| 61 | * call. */
|
---|
| 62 |
|
---|
| 63 | static int g_failure_index = -1;
|
---|
| 64 | static int g_failure_index_selected = -2;
|
---|
| 65 |
|
---|
| 66 | static int should_fail(void)
|
---|
| 67 | {
|
---|
| 68 | g_failure_index++;
|
---|
| 69 |
|
---|
| 70 | if (g_failure_index_selected == -2) {
|
---|
| 71 | char *sel_str = getenv("BITHENGE_FAILURE_INDEX");
|
---|
| 72 | if (sel_str) {
|
---|
| 73 | g_failure_index_selected = strtol(sel_str, NULL, 10);
|
---|
| 74 | } else {
|
---|
| 75 | g_failure_index_selected = -1;
|
---|
| 76 | }
|
---|
| 77 | } else if (g_failure_index_selected != -1) {
|
---|
| 78 | if (g_failure_index == g_failure_index_selected)
|
---|
| 79 | return 1; /* breakpoint here */
|
---|
| 80 | return 0;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /* Only fail half the time after 128 failures, 1/4 the time after 256
|
---|
| 84 | * failures, 1/8 the time after 512 failures... */
|
---|
| 85 | int index = g_failure_index;
|
---|
| 86 | while (index >= 128) {
|
---|
| 87 | int test = (index & (64 | 1));
|
---|
| 88 | if (test == (64 | 1) || test == 0)
|
---|
| 89 | return 0;
|
---|
| 90 | index >>= 1;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (!fork()) {
|
---|
| 94 | /* Child */
|
---|
| 95 | int null = open("/dev/null", O_WRONLY);
|
---|
| 96 | if (null == -1)
|
---|
| 97 | exit(127);
|
---|
| 98 | dup2(null, STDOUT_FILENO);
|
---|
| 99 | dup2(null, STDERR_FILENO);
|
---|
| 100 | close(null);
|
---|
| 101 | return 1;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /* Parent */
|
---|
| 105 | int status;
|
---|
| 106 | wait(&status);
|
---|
| 107 | if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
|
---|
| 108 | return 0;
|
---|
| 109 |
|
---|
| 110 | /* The child had an error! We couldn't see it because stdout and stderr
|
---|
| 111 | * were redirected, and we couldn't debug it easily because it was a
|
---|
| 112 | * separate process. Do it again without redirecting or forking. */
|
---|
| 113 | fprintf(stderr, "** Fake error raised here (BITHENGE_FAILURE_INDEX=%d)\n",
|
---|
| 114 | g_failure_index);
|
---|
| 115 | return 1;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void *bithenge_failure_malloc(size_t size)
|
---|
| 119 | {
|
---|
| 120 | if (should_fail())
|
---|
| 121 | return NULL;
|
---|
| 122 | return malloc(size);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void *bithenge_failure_realloc(void *ptr, size_t size)
|
---|
| 126 | {
|
---|
| 127 | if (should_fail())
|
---|
| 128 | return NULL;
|
---|
| 129 | return realloc(ptr, size);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
|
---|
| 133 | {
|
---|
| 134 | if (should_fail()) {
|
---|
| 135 | errno = EIO;
|
---|
| 136 | return -1;
|
---|
| 137 | }
|
---|
| 138 | return read(fd, buf, count);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
|
---|
| 142 | {
|
---|
| 143 | if (should_fail()) {
|
---|
| 144 | errno = EINVAL;
|
---|
| 145 | return (off_t) -1;
|
---|
| 146 | }
|
---|
| 147 | return lseek(fd, offset, whither);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** @}
|
---|
| 151 | */
|
---|