Skip to content

Commit c19a490

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 04a74b6 commit c19a490

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
301301

302302
extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
303303
extern void set_error_routine(void (*routine)(const char *err, va_list params));
304+
extern void set_die_is_recursing_routine(int (*routine)(void));
304305

305306
extern int prefixcmp(const char *str, const char *prefix);
306307
extern int suffixcmp(const char *str, const char *suffix);

usage.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include "git-compat-util.h"
77
#include "cache.h"
88

9-
static int dying;
10-
119
void vreportf(const char *prefix, const char *err, va_list params)
1210
{
1311
char msg[4096];
@@ -49,12 +47,19 @@ static void warn_builtin(const char *warn, va_list params)
4947
vreportf("warning: ", warn, params);
5048
}
5149

50+
static int die_is_recursing_builtin(void)
51+
{
52+
static int dying;
53+
return dying++;
54+
}
55+
5256
/* If we are in a dlopen()ed .so write to a global variable would segfault
5357
* (ugh), so keep things static. */
5458
static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
5559
static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
5660
static void (*error_routine)(const char *err, va_list params) = error_builtin;
5761
static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
62+
static int (*die_is_recursing)(void) = die_is_recursing_builtin;
5863

5964
void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
6065
{
@@ -66,6 +71,11 @@ void set_error_routine(void (*routine)(const char *err, va_list params))
6671
error_routine = routine;
6772
}
6873

74+
void set_die_is_recursing_routine(int (*routine)(void))
75+
{
76+
die_is_recursing = routine;
77+
}
78+
6979
void NORETURN usagef(const char *err, ...)
7080
{
7181
va_list params;
@@ -84,11 +94,10 @@ void NORETURN die(const char *err, ...)
8494
{
8595
va_list params;
8696

87-
if (dying) {
97+
if (die_is_recursing()) {
8898
fputs("fatal: recursion detected in die handler\n", stderr);
8999
exit(128);
90100
}
91-
dying = 1;
92101

93102
va_start(params, err);
94103
die_routine(err, params);
@@ -102,12 +111,11 @@ void NORETURN die_errno(const char *fmt, ...)
102111
char str_error[256], *err;
103112
int i, j;
104113

105-
if (dying) {
114+
if (die_is_recursing()) {
106115
fputs("fatal: recursion detected in die_errno handler\n",
107116
stderr);
108117
exit(128);
109118
}
110-
dying = 1;
111119

112120
err = strerror(errno);
113121
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {

0 commit comments

Comments
 (0)