]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use _exit() in the fatal() function
authorOndřej Surý <ondrej@isc.org>
Wed, 7 Feb 2024 13:44:39 +0000 (14:44 +0100)
committerOndřej Surý <ondrej@isc.org>
Tue, 6 Aug 2024 12:52:24 +0000 (14:52 +0200)
Since the fatal() isn't a correct but rather abrupt termination of the
program, we want to skip the various atexit() calls because not all
memory might be freed during fatal() call, etc.  Using _exit() instead
of exit() has this effect - the program will end, but no destructors or
atexit routines will be called.

(cherry picked from commit 4bec711fe3643f50f525b1879b43eeb9be1a0b7a)

13 files changed:
bin/check/named-checkzone.c
bin/confgen/util.c
bin/delv/delv.c
bin/dig/dighost.c
bin/dnssec/dnssectool.c
bin/named/main.c
bin/named/server.c
bin/nsupdate/nsupdate.c
bin/rndc/util.c
bin/tools/dnstap-read.c
bin/tools/mdig.c
bin/tools/named-rrchecker.c
bin/tools/nsec3hash.c

index 5c74660c0cd105f72ee3d38797fbe2ffc932e15d..6090d24282b1bca9cd28707270b47f0d5dbf6834 100644 (file)
@@ -82,7 +82,7 @@ usage(void) {
                "%s zonename [ (filename|-) ]\n",
                prog_name,
                progmode == progmode_check ? "[-o filename]" : "-o filename");
-       exit(1);
+       exit(EXIT_FAILURE);
 }
 
 static void
@@ -212,7 +212,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -i: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -246,7 +246,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -k: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -257,7 +257,7 @@ main(int argc, char **argv) {
                        if (*endp != '\0') {
                                fprintf(stderr, "source serial number "
                                                "must be numeric");
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -268,7 +268,7 @@ main(int argc, char **argv) {
                        if (*endp != '\0') {
                                fprintf(stderr, "maximum TTL "
                                                "must be numeric");
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -285,7 +285,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -n: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -302,7 +302,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -m: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -327,7 +327,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -r: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -340,7 +340,7 @@ main(int argc, char **argv) {
                                fprintf(stderr,
                                        "unknown or unsupported style: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -350,7 +350,7 @@ main(int argc, char **argv) {
                                fprintf(stderr, "isc_dir_chroot: %s: %s\n",
                                        isc_commandline_argument,
                                        isc_result_totext(result));
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -379,7 +379,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -M: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -396,7 +396,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -S: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -408,7 +408,7 @@ main(int argc, char **argv) {
                        } else {
                                fprintf(stderr, "invalid argument to -T: %s\n",
                                        isc_commandline_argument);
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                        break;
 
@@ -432,7 +432,7 @@ main(int argc, char **argv) {
                default:
                        fprintf(stderr, "%s: unhandled option -%c\n", prog_name,
                                isc_commandline_option);
-                       exit(1);
+                       exit(EXIT_FAILURE);
                }
        }
 
@@ -441,7 +441,7 @@ main(int argc, char **argv) {
                if (result != ISC_R_SUCCESS) {
                        fprintf(stderr, "isc_dir_chdir: %s: %s\n", workdir,
                                isc_result_totext(result));
-                       exit(1);
+                       exit(EXIT_FAILURE);
                }
        }
 
@@ -457,7 +457,7 @@ main(int argc, char **argv) {
                } else {
                        fprintf(stderr, "unknown file format: %s\n",
                                inputformatstr);
-                       exit(1);
+                       exit(EXIT_FAILURE);
                }
        }
 
@@ -475,12 +475,12 @@ main(int argc, char **argv) {
                            rawversion > 1U)
                        {
                                fprintf(stderr, "unknown raw format version\n");
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        }
                } else {
                        fprintf(stderr, "unknown file format: %s\n",
                                outputformatstr);
-                       exit(1);
+                       exit(EXIT_FAILURE);
                }
        }
 
index b0085fe13d557f425b8cb7394f961d3d491b374f..254f13946139285a9bda5505a822763e0dbec46b 100644 (file)
 
 /*! \file */
 
-#include "util.h"
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <isc/print.h>
 
+#include "util.h"
+
 extern bool verbose;
 extern const char *progname;
 
@@ -45,5 +47,5 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
index e1e9e7fe3dafeb379a87453702de7addbf15d36f..d47b99aac04bf9f8e9e464e08f1bdc685eac6952 100644 (file)
@@ -221,7 +221,7 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 static void
index b5e27931e557ecfc55faee9da4d2477d5edfd1b7..4d7e09ef155317655b5b1db42a15ad77d3e88068 100644 (file)
@@ -383,8 +383,6 @@ get_reverse(char *reverse, size_t len, char *value, bool strict) {
        }
 }
 
-void (*dighost_pre_exit_hook)(void) = NULL;
-
 #if TARGET_OS_IPHONE
 void
 warn(const char *format, ...) {
@@ -417,10 +415,7 @@ digexit(void) {
                exitcode = 10;
        }
        if (fatalexit != 0) {
-               exitcode = fatalexit;
-       }
-       if (dighost_pre_exit_hook != NULL) {
-               dighost_pre_exit_hook();
+               _exit(fatalexit);
        }
        exit(exitcode);
 }
@@ -435,6 +430,11 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
+       if (fatalexit == 0 && exitcode != 0) {
+               fatalexit = exitcode;
+       } else if (fatalexit == 0) {
+               fatalexit = EXIT_FAILURE;
+       }
        digexit();
 }
 
index c07e0912662505a8a92f322322864cb1896be89f..8250a127a46cd6a57ccf5689406684991b320754 100644 (file)
@@ -20,6 +20,7 @@
 #include <inttypes.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <isc/base32.h>
 #include <isc/buffer.h>
@@ -81,7 +82,7 @@ fatal(const char *format, ...) {
        if (fatalcallback != NULL) {
                (*fatalcallback)();
        }
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 void
index 154e17efa789204715d287c8cd47b2982fc0dfad..eca30c6ab7d7717418372279a44400a2082644bb 100644 (file)
@@ -184,7 +184,7 @@ named_main_earlyfatal(const char *format, ...) {
        }
        va_end(args);
 
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 noreturn static void
@@ -238,7 +238,7 @@ assertion_failed(const char *file, int line, isc_assertiontype_t type,
        if (named_g_coreok) {
                abort();
        }
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 noreturn static void
@@ -278,7 +278,7 @@ library_fatal_error(const char *file, int line, const char *func,
        if (named_g_coreok) {
                abort();
        }
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 static void
index 6ad090fd5fa8676b71acafab36f7bde9a4349e93..c88a47574ac35409ef7252102ffa964e4c25c1e1 100644 (file)
 #endif /* HAVE_LMDB */
 
 #ifndef SIZE_MAX
-#define SIZE_MAX ((size_t) - 1)
+#define SIZE_MAX ((size_t)-1)
 #endif /* ifndef SIZE_MAX */
 
 #ifndef SIZE_AS_PERCENT
-#define SIZE_AS_PERCENT ((size_t) - 2)
+#define SIZE_AS_PERCENT ((size_t)-2)
 #endif /* ifndef SIZE_AS_PERCENT */
 
 #ifdef TUNE_LARGE
 #define CHECKFATAL(op, msg)                         \
        do {                                        \
                result = (op);                      \
-               if (result != ISC_R_SUCCESS)        \
+               if (result != ISC_R_SUCCESS) {      \
                        fatal(server, msg, result); \
+               }                                   \
        } while (0)
 
 /*%
@@ -10426,7 +10427,7 @@ fatal(named_server_t *server, const char *msg, isc_result_t result) {
                      NAMED_LOGMODULE_SERVER, ISC_LOG_CRITICAL,
                      "exiting (due to fatal error)");
        named_os_shutdown();
-       exit(1);
+       _exit(1);
 }
 
 static void
index 980d54328ce42d267956b860a27c73d47054ac6f..8e068bebefa4d2eb315d065f344024dc0c1b630c 100644 (file)
@@ -266,7 +266,7 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 static void
index b0085fe13d557f425b8cb7394f961d3d491b374f..bc892f0ca10b984102a226574c053012d271940b 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <isc/print.h>
 
@@ -45,5 +46,5 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
index 06dd561406f8a8fc2c450474270b689498d09a13..c55534feb46ce1ad469b6a0d5bc0c658bfaab144 100644 (file)
@@ -32,6 +32,7 @@
 #include <inttypes.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <protobuf-c/protobuf-c.h>
 
@@ -83,7 +84,7 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 static void
index f84f1daed011cc296c0468284ea601a75ac206de..12f289ffb2f847d65e2fc127d8f3f32770b6cca8 100644 (file)
@@ -901,7 +901,7 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(-2);
+       _exit(EXIT_FAILURE);
 }
 
 static isc_result_t
index 4d8a318b5f87efd8bc9ec7728e30b2c140118c46..278a0a267da0b9831b9b5cfbb9496b517b9d2a44 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <stdbool.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <isc/attributes.h>
 #include <isc/buffer.h>
@@ -76,7 +77,7 @@ fatal(const char *format, ...) {
        va_end(args);
        fputc('\n', stderr);
        cleanup();
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 int
index a6a5ec3a714002ddd18313bdec19aca92ce68a7f..e76f7be025bc7c2dba2ec26b1c31a878084a9169 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <isc/attributes.h>
 #include <isc/base32.h>
@@ -47,7 +48,7 @@ fatal(const char *format, ...) {
        vfprintf(stderr, format, args);
        va_end(args);
        fprintf(stderr, "\n");
-       exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 static void