| 1 |
/* |
| 2 |
* Error Handling |
| 3 |
* |
| 4 |
* This file implements a simple and fast error handling infrastructure. We use |
| 5 |
* C-style integers to signal errors via return values. That is, we use |
| 6 |
* negative error codes to signal fatal errors (e.g., -ENOMEM) just like the |
| 7 |
* linux kernel does. We use 0 to signal success, and positive error codes to |
| 8 |
* signal API-dependent conditions. |
| 9 |
* |
| 10 |
* Our general rule is to never catch negative error codes returned by our own |
| 11 |
* API. That is, we never match on -EINVAL, -EFAULT, etc., but return them |
| 12 |
* unchanged. We only ever match on those codes if returned by external calls. |
| 13 |
* Once our own calls return them, they're considered fatal, and forwarded |
| 14 |
* through the call-chain. |
| 15 |
* |
| 16 |
* Positive error codes, however, signal well-defined API conditions. Those |
| 17 |
* must be caught by the caller of the respective call. That is, those error |
| 18 |
* codes have clear meanings on a given function, and thus can (and must) be |
| 19 |
* handled by the caller. Callers must not forward those codes, since the codes |
| 20 |
* are only well-defined for a single function. The exception is |
| 21 |
* function-groups that share a set of error codes. Those might forward error |
| 22 |
* codes, if, and only if, they call into each other. |
| 23 |
* |
| 24 |
* In other words, our call chains will always propagate fatal errors up to the |
| 25 |
* application entry point. Well-defined API errors are never propagated, but |
| 26 |
* always caught and handled by the caller (which itself might raise its own |
| 27 |
* errors based on them). |
| 28 |
* |
| 29 |
* For debugging purposes, we want to be able to trace those error codes. |
| 30 |
* Therefore, we provide three simple macros, which are a no-op in the |
| 31 |
* success-path, but provide hooks in the case of errors: |
| 32 |
* |
| 33 |
* error_origin(): This raises a new fatal error. That is, rather than |
| 34 |
* returning -ENOMEM directly on allocation failure, you |
| 35 |
* can use: |
| 36 |
* |
| 37 |
* return error_origin(-ENOMEM); |
| 38 |
* |
| 39 |
* Similarly, you can return 'errno' in case it is |
| 40 |
* unhandled: |
| 41 |
* |
| 42 |
* return error_origin(-errno); |
| 43 |
* |
| 44 |
* The default behavior of error_origin() is to print a |
| 45 |
* error trace message in case of failure. |
| 46 |
* |
| 47 |
* error_trace(): This traces negative errors. That is, when another |
| 48 |
* function returns a negative error and you want to |
| 49 |
* propagate it, wrap it in error_trace() to tell the tracer |
| 50 |
* about the path the error took: |
| 51 |
* |
| 52 |
* return error_trace(r); |
| 53 |
* |
| 54 |
* On success or positive error codes, this is a no-op and |
| 55 |
* returns the code unchanged. That is, use it to propagate |
| 56 |
* an error of another function unchanged, in case the set |
| 57 |
* of error codes for both functions is the same. |
| 58 |
* |
| 59 |
* error_fold(): This folds positive errors and traces fatal errors. That |
| 60 |
* is, whenever another function returns a negative *or* |
| 61 |
* positive error that you didn't handle, you can fold and |
| 62 |
* propagate them via error_fold(). This function turns |
| 63 |
* positive errors into fatal ones, and traces them. On |
| 64 |
* success, this function is a no-op: |
| 65 |
* |
| 66 |
* return error_fold(r); |
| 67 |
* |
| 68 |
* The combination of those three functions allows us to get nice error-traces |
| 69 |
* on fatal, unexpected errors. That is, imagine a syscall returns an error |
| 70 |
* that we did not handle, we will end up with a nice back-trace that a |
| 71 |
* developer can use to debug and fix this problem, like: |
| 72 |
* |
| 73 |
* ERROR manager_listen_path @ ../src/launch/main.c +234: Address already in use |
| 74 |
* run @ ../src/launch/main.c +938 |
| 75 |
* main @ ../src/launch/main.c +983 |
| 76 |
* |
| 77 |
* At the same time, we avoid fatal exceptions that abort the application |
| 78 |
* unexpectedly. That is, we can still gracefully shutdown and release |
| 79 |
* resources that are not auto-managed. |
| 80 |
* We still recommend fatal exceptions (e.g., assertions) for non-recoverable |
| 81 |
* errors (like a detected memory corruption). However, any error that can be |
| 82 |
* gracefully handled will be suitable for this. |
| 83 |
*/ |
| 84 |
|
| 85 |
#include <c-stdaux.h> |
| 86 |
#include <stdlib.h> |
| 87 |
#include "util/error.h" |
| 88 |
|
| 89 |
/** |
| 90 |
* error_slow_origin() - raise an error |
| 91 |
* @r: error code |
| 92 |
* @function: calling function |
| 93 |
* @file: source file |
| 94 |
* @line: source line |
| 95 |
* |
| 96 |
* Instead of returning a literal error code, wrap the literal in this function |
| 97 |
* to integrate with the tracing infrastructure. That is, whenever you want to |
| 98 |
* raise a fatal error (negative error code), pass it through error_origin(). |
| 99 |
* |
| 100 |
* This function will detect 3 cases: |
| 101 |
* |
| 102 |
* 1) The error code is 0. In this case the function is a no-op and 0 is |
| 103 |
* returned. The fast-path macro error_origin() shortcuts this. |
| 104 |
* |
| 105 |
* 2) The error code is negative. In this case the function will start a new |
| 106 |
* error trace and return the error code unchanged. |
| 107 |
* |
| 108 |
* 3) The error code is positive. In this case the function will start a new |
| 109 |
* error trace, but return -ENOTRECOVERABLE. |
| 110 |
* |
| 111 |
* The error-handling is up to the application that links to this code. This |
| 112 |
* macro just provides the hook for the application to start an error trace. |
| 113 |
* |
| 114 |
* Right now, the default behavior is to print a suitable message to stderr. |
| 115 |
* |
| 116 |
* Return: 0 or negative error code, depending on @r. |
| 117 |
*/ |
| 118 |
int error_slow_origin(int r, const char *function, const char *file, int line) { |
| 119 |
int tmp_errno; |
| 120 |
|
| 121 |
if (r < 0) { |
| 122 |
tmp_errno = errno; |
| 123 |
errno = -r; |
| 124 |
fprintf(stderr, "ERROR %s @ %s +%d: %m\n", function, file, line); |
| 125 |
errno = tmp_errno; |
| 126 |
} else if (r > 0) { |
| 127 |
fprintf(stderr, "ERROR %s @ %s +%d: Return code %d\n", function, file, line, r); |
| 128 |
r = -ENOTRECOVERABLE; |
| 129 |
} |
| 130 |
|
| 131 |
return r; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* error_slow_trace() - trace a fatal error |
| 136 |
* @r: error code |
| 137 |
* @function: calling function |
| 138 |
* @file: source file |
| 139 |
* @line: source line |
| 140 |
* |
| 141 |
* Whenever you want to forward an error code unchanged from another function |
| 142 |
* that shares your set of error codes, wrap it in error_trace() to let the |
| 143 |
* tracer know about it. In case @r is 0, or a positive error code, this |
| 144 |
* function is a no-op. |
| 145 |
* |
| 146 |
* In case @r is negative, this function returns @r unchanged, but calls the |
| 147 |
* trace-hook. |
| 148 |
* |
| 149 |
* Right now, the default behavior is to print a suitable message to stderr. |
| 150 |
* |
| 151 |
* Return: @r is returned. |
| 152 |
*/ |
| 153 |
int error_slow_trace(int r, const char *function, const char *file, int line) { |
| 154 |
if (r < 0) |
| 155 |
fprintf(stderr, " %s @ %s +%d\n", function, file, line); |
| 156 |
|
| 157 |
return r; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* error_slow_fold() - fold non-fatal errors |
| 162 |
* @r: error code |
| 163 |
* @function: calling function |
| 164 |
* @file: source file |
| 165 |
* @line: source line |
| 166 |
* |
| 167 |
* This folds any non-fatal error into a fatal one. That is, whenever a |
| 168 |
* function you called failed, but you did not handle the error code (for |
| 169 |
* whatever reason), call error_fold() to let the tracer know and turn it into |
| 170 |
* a fatal error. |
| 171 |
* |
| 172 |
* If @r is 0, this is a no-op. This calls into error_trace() if @r is |
| 173 |
* negative, and error_origin() if @r is positive. |
| 174 |
* |
| 175 |
* Return: 0 or negative error code, depending on @r. |
| 176 |
*/ |
| 177 |
int error_slow_fold(int r, const char *function, const char *file, int line) { |
| 178 |
if (r < 0) |
| 179 |
r = error_slow_trace(r, function, file, line); |
| 180 |
else if (r > 0) |
| 181 |
r = error_slow_origin(r, function, file, line); |
| 182 |
|
| 183 |
return r; |
| 184 |
} |