usage: allow pluggable die-recursion checks

When any git code calls die or die_errno, we use a counter to detect recursion into the die functions from any of the helper functions. However, such a simple counter is not good enough for threaded programs, which may call die from a sub-thread, killing only the sub-thread (but incrementing the counter for everyone). Rather than try to deal with threads ourselves here, let's just allow callers to plug in their own recursion-detection function. This is similar to how we handle the die routine (the caller plugs in a die routine which may kill only the sub-thread). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 16, 2013 at 15:46 UTC c19a490e37181de8fa94ae1074af4b9f9a518f95
2 files changed +15 -6
git-compat-util.h
+1
@@ -301,6 +301,7 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
301
302 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
303 extern void set_error_routine(void (*routine)(const char *err, va_list params));
304 +extern void set_die_is_recursing_routine(int (*routine)(void));
305
306 extern int prefixcmp(const char *str, const char *prefix);
307 extern int suffixcmp(const char *str, const char *suffix);
usage.c
+14 -6
@@ -6,8 +6,6 @@
6 #include "git-compat-util.h"
7 #include "cache.h"
8
9 -static int dying;
10 -
9 void vreportf(const char *prefix, const char *err, va_list params)
10 {
11 char msg[4096];
@@ -49,12 +47,19 @@ static void warn_builtin(const char *warn, va_list params)
47 vreportf("warning: ", warn, params);
48 }
49
50 +static int die_is_recursing_builtin(void)
51 +{
52 + static int dying;
53 + return dying++;
54 +}
55 +
56 /* If we are in a dlopen()ed .so write to a global variable would segfault
57 * (ugh), so keep things static. */
58 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
59 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
60 static void (*error_routine)(const char *err, va_list params) = error_builtin;
61 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
62 +static int (*die_is_recursing)(void) = die_is_recursing_builtin;
63
64 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
65 {
@@ -66,6 +71,11 @@ void set_error_routine(void (*routine)(const char *err, va_list params))
71 error_routine = routine;
72 }
73
74 +void set_die_is_recursing_routine(int (*routine)(void))
75 +{
76 + die_is_recursing = routine;
77 +}
78 +
79 void NORETURN usagef(const char *err, ...)
80 {
81 va_list params;
@@ -84,11 +94,10 @@ void NORETURN die(const char *err, ...)
94 {
95 va_list params;
96
87 - if (dying) {
97 + if (die_is_recursing()) {
98 fputs("fatal: recursion detected in die handler\n", stderr);
99 exit(128);
100 }
91 - dying = 1;
101
102 va_start(params, err);
103 die_routine(err, params);
@@ -102,12 +111,11 @@ void NORETURN die_errno(const char *fmt, ...)
111 char str_error[256], *err;
112 int i, j;
113
105 - if (dying) {
114 + if (die_is_recursing()) {
115 fputs("fatal: recursion detected in die_errno handler\n",
116 stderr);
117 exit(128);
118 }
110 - dying = 1;
119
120 err = strerror(errno);
121 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {