1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "git-compat-util.h"
7#include "cache.h"
8
9void vreportf(const char *prefix, const char *err, va_list params)
10{
11 char msg[4096];
12 char *p;
13
14 vsnprintf(msg, sizeof(msg), err, params);
15 for (p = msg; *p; p++) {
16 if (iscntrl(*p) && *p != '\t' && *p != '\n')
17 *p = '?';
18 }
19 fprintf(stderr, "%s%s\n", prefix, msg);
20}
21
22static NORETURN void usage_builtin(const char *err, va_list params)
23{
24 vreportf("usage: ", err, params);
25
26 /*
27 * When we detect a usage error *before* the command dispatch in
28 * cmd_main(), we don't know what verb to report. Force it to this
29 * to facilitate post-processing.
30 */
31 trace2_cmd_name("_usage_");
32
33 /*
34 * Currently, the (err, params) are usually just the static usage
35 * string which isn't very useful here. Usually, the call site
36 * manually calls fprintf(stderr,...) with the actual detailed
37 * syntax error before calling usage().
38 *
39 * TODO It would be nice to update the call sites to pass both
40 * the static usage string and the detailed error message.
41 */
42
43 exit(129);
44}
45
46static NORETURN void die_builtin(const char *err, va_list params)
47{
48 /*
49 * We call this trace2 function first and expect it to va_copy 'params'
50 * before using it (because an 'ap' can only be walked once).
51 */
52 trace2_cmd_error_va(err, params);
53
54 vreportf("fatal: ", err, params);
55
56 exit(128);
57}
58
59static void error_builtin(const char *err, va_list params)
60{
61 /*
62 * We call this trace2 function first and expect it to va_copy 'params'
63 * before using it (because an 'ap' can only be walked once).
64 */
65 trace2_cmd_error_va(err, params);
66
67 vreportf("error: ", err, params);
68}
69
70static void warn_builtin(const char *warn, va_list params)
71{
72 vreportf("warning: ", warn, params);
73}
74
75static int die_is_recursing_builtin(void)
76{
77 static int dying;
78 /*
79 * Just an arbitrary number X where "a < x < b" where "a" is
80 * "maximum number of pthreads we'll ever plausibly spawn" and
81 * "b" is "something less than Inf", since the point is to
82 * prevent infinite recursion.
83 */
84 static const int recursion_limit = 1024;
85
86 dying++;
87 if (dying > recursion_limit) {
88 return 1;
89 } else if (dying == 2) {
90 warning("die() called many times. Recursion error or racy threaded death!");
91 return 0;
92 } else {
93 return 0;
94 }
95}
96
97/* If we are in a dlopen()ed .so write to a global variable would segfault
98 * (ugh), so keep things static. */
99static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
100static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
101static void (*error_routine)(const char *err, va_list params) = error_builtin;
102static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
103static int (*die_is_recursing)(void) = die_is_recursing_builtin;
104
105void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
106{
107 die_routine = routine;
108}
109
110void set_error_routine(void (*routine)(const char *err, va_list params))
111{
112 error_routine = routine;
113}
114
115void (*get_error_routine(void))(const char *err, va_list params)
116{
117 return error_routine;
118}
119
120void set_warn_routine(void (*routine)(const char *warn, va_list params))
121{
122 warn_routine = routine;
123}
124
125void (*get_warn_routine(void))(const char *warn, va_list params)
126{
127 return warn_routine;
128}
129
130void set_die_is_recursing_routine(int (*routine)(void))
131{
132 die_is_recursing = routine;
133}
134
135void NORETURN usagef(const char *err, ...)
136{
137 va_list params;
138
139 va_start(params, err);
140 usage_routine(err, params);
141 va_end(params);
142}
143
144void NORETURN usage(const char *err)
145{
146 usagef("%s", err);
147}
148
149void NORETURN die(const char *err, ...)
150{
151 va_list params;
152
153 if (die_is_recursing()) {
154 fputs("fatal: recursion detected in die handler\n", stderr);
155 exit(128);
156 }
157
158 va_start(params, err);
159 die_routine(err, params);
160 va_end(params);
161}
162
163static const char *fmt_with_err(char *buf, int n, const char *fmt)
164{
165 char str_error[256], *err;
166 int i, j;
167
168 err = strerror(errno);
169 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
170 if ((str_error[j++] = err[i++]) != '%')
171 continue;
172 if (j < sizeof(str_error) - 1) {
173 str_error[j++] = '%';
174 } else {
175 /* No room to double the '%', so we overwrite it with
176 * '\0' below */
177 j--;
178 break;
179 }
180 }
181 str_error[j] = 0;
182 /* Truncation is acceptable here */
183 snprintf(buf, n, "%s: %s", fmt, str_error);
184 return buf;
185}
186
187void NORETURN die_errno(const char *fmt, ...)
188{
189 char buf[1024];
190 va_list params;
191
192 if (die_is_recursing()) {
193 fputs("fatal: recursion detected in die_errno handler\n",
194 stderr);
195 exit(128);
196 }
197
198 va_start(params, fmt);
199 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
200 va_end(params);
201}
202
203#undef error_errno
204int error_errno(const char *fmt, ...)
205{
206 char buf[1024];
207 va_list params;
208
209 va_start(params, fmt);
210 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
211 va_end(params);
212 return -1;
213}
214
215#undef error
216int error(const char *err, ...)
217{
218 va_list params;
219
220 va_start(params, err);
221 error_routine(err, params);
222 va_end(params);
223 return -1;
224}
225
226void warning_errno(const char *warn, ...)
227{
228 char buf[1024];
229 va_list params;
230
231 va_start(params, warn);
232 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
233 va_end(params);
234}
235
236void warning(const char *warn, ...)
237{
238 va_list params;
239
240 va_start(params, warn);
241 warn_routine(warn, params);
242 va_end(params);
243}
244
245/* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
246int BUG_exit_code;
247
248static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
249{
250 char prefix[256];
251
252 /* truncation via snprintf is OK here */
253 if (file)
254 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
255 else
256 snprintf(prefix, sizeof(prefix), "BUG: ");
257
258 vreportf(prefix, fmt, params);
259 if (BUG_exit_code)
260 exit(BUG_exit_code);
261 abort();
262}
263
264#ifdef HAVE_VARIADIC_MACROS
265NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
266{
267 va_list ap;
268 va_start(ap, fmt);
269 BUG_vfl(file, line, fmt, ap);
270 va_end(ap);
271}
272#else
273NORETURN void BUG(const char *fmt, ...)
274{
275 va_list ap;
276 va_start(ap, fmt);
277 BUG_vfl(NULL, 0, fmt, ap);
278 va_end(ap);
279}
280#endif
281
282#ifdef SUPPRESS_ANNOTATED_LEAKS
283void unleak_memory(const void *ptr, size_t len)
284{
285 static struct suppressed_leak_root {
286 struct suppressed_leak_root *next;
287 char data[FLEX_ARRAY];
288 } *suppressed_leaks;
289 struct suppressed_leak_root *root;
290
291 FLEX_ALLOC_MEM(root, data, ptr, len);
292 root->next = suppressed_leaks;
293 suppressed_leaks = root;
294}
295#endif