Add DOH Exit() and SetExitHandler()

Exit() is a wrapper for exit() by default, but SetExitHandler() allows
specifying a function to call instead.

This means that failures within DOH (e.g. Malloc() failing due to lack
of memory) will now perform cleanup such as removing output files.

This commit also cleans up exit statuses so SWIG should now reliably
exit with status 0 if the run was successful and status 1 if there was
an error (or a warning and -Werror was in effect).

Previously in some situations SWIG would try to exit with the status set
to the number of errors encountered, but that's problematic - for
example if there were 256 errors this would result in exit status 0 on
most platforms.  Also some error statuses have special meanings e.g.
those defined by <sysexits.h>.

Also SWIG/Javascript tried to exit with status -1 in a few places (which
typically results in exit status 255).
This commit is contained in:
Olly Betts 2022-03-06 12:33:54 +13:00
commit 55377bdc08
33 changed files with 243 additions and 173 deletions

View file

@ -235,6 +235,26 @@ void DohMemoryDebug(void) {
}
/* Function to call instead of exit(). */
static void (*doh_exit_handler)(int) = NULL;
void DohSetExitHandler(void (*new_handler)(int)) {
doh_exit_handler = new_handler;
}
void DohExit(int status) {
if (doh_exit_handler) {
void (*handler)(int) = doh_exit_handler;
/* Unset the handler to avoid infinite loops if it tries to do something
* which calls DohExit() (e.g. calling Malloc() and that failing).
*/
doh_exit_handler = NULL;
handler(status);
} else {
exit(status);
}
}
static void allocation_failed(size_t n, size_t size) {
/* Report and exit as directly as possible to try to avoid further issues due
* to lack of memory. */
@ -251,7 +271,7 @@ static void allocation_failed(size_t n, size_t size) {
fprintf(stderr, "Failed to allocate %lu*%lu bytes\n", (unsigned long)n, (unsigned long)size);
#endif
}
exit(EXIT_FAILURE);
DohExit(EXIT_FAILURE);
}
void *DohMalloc(size_t size) {