]>
Commit | Line | Data |
---|---|---|
0fcfd160 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
4050c0df | 6 | #include "git-compat-util.h" |
3bc4181f | 7 | #include "cache.h" |
0fcfd160 | 8 | |
f5c39c32 | 9 | static void vreportf(const char *prefix, const char *err, va_list params) |
0fcfd160 | 10 | { |
b5a9e435 | 11 | char msg[4096]; |
116d1fa6 JS |
12 | char *p, *pend = msg + sizeof(msg); |
13 | size_t prefix_len = strlen(prefix); | |
f4c3edc0 | 14 | |
116d1fa6 JS |
15 | if (sizeof(msg) <= prefix_len) { |
16 | fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix); | |
17 | abort(); | |
18 | } | |
19 | memcpy(msg, prefix, prefix_len); | |
20 | p = msg + prefix_len; | |
21 | if (vsnprintf(p, pend - p, err, params) < 0) | |
22 | *p = '\0'; /* vsnprintf() failed, clip at prefix */ | |
23 | ||
24 | for (; p != pend - 1 && *p; p++) { | |
f2900898 JK |
25 | if (iscntrl(*p) && *p != '\t' && *p != '\n') |
26 | *p = '?'; | |
f4c3edc0 | 27 | } |
116d1fa6 JS |
28 | |
29 | *(p++) = '\n'; /* we no longer need a NUL */ | |
30 | fflush(stderr); | |
31 | write_in_full(2, msg, p - msg); | |
3bc4181f CB |
32 | } |
33 | ||
64b1cb74 | 34 | static NORETURN void usage_builtin(const char *err, va_list params) |
0fcfd160 | 35 | { |
a1fd2cf8 | 36 | vreportf(_("usage: "), err, params); |
ee4512ed JH |
37 | |
38 | /* | |
39 | * When we detect a usage error *before* the command dispatch in | |
40 | * cmd_main(), we don't know what verb to report. Force it to this | |
41 | * to facilitate post-processing. | |
42 | */ | |
43 | trace2_cmd_name("_usage_"); | |
44 | ||
45 | /* | |
46 | * Currently, the (err, params) are usually just the static usage | |
47 | * string which isn't very useful here. Usually, the call site | |
48 | * manually calls fprintf(stderr,...) with the actual detailed | |
49 | * syntax error before calling usage(). | |
50 | * | |
51 | * TODO It would be nice to update the call sites to pass both | |
52 | * the static usage string and the detailed error message. | |
53 | */ | |
54 | ||
5d1a5c02 | 55 | exit(129); |
0fcfd160 LT |
56 | } |
57 | ||
18568ee8 ÆAB |
58 | static void die_message_builtin(const char *err, va_list params) |
59 | { | |
60 | trace2_cmd_error_va(err, params); | |
a1fd2cf8 | 61 | vreportf(_("fatal: "), err, params); |
18568ee8 ÆAB |
62 | } |
63 | ||
c00c7382 ÆAB |
64 | /* |
65 | * We call trace2_cmd_error_va() in the below functions first and | |
66 | * expect it to va_copy 'params' before using it (because an 'ap' can | |
67 | * only be walked once). | |
68 | */ | |
ce88ac5b | 69 | static NORETURN void die_builtin(const char *err, va_list params) |
39a3f5ea | 70 | { |
18568ee8 | 71 | report_fn die_message_fn = get_die_message_routine(); |
ee4512ed | 72 | |
18568ee8 | 73 | die_message_fn(err, params); |
39a3f5ea PB |
74 | exit(128); |
75 | } | |
76 | ||
ce88ac5b | 77 | static void error_builtin(const char *err, va_list params) |
39a3f5ea | 78 | { |
ee4512ed JH |
79 | trace2_cmd_error_va(err, params); |
80 | ||
a1fd2cf8 | 81 | vreportf(_("error: "), err, params); |
39a3f5ea PB |
82 | } |
83 | ||
fa39b6b5 SP |
84 | static void warn_builtin(const char *warn, va_list params) |
85 | { | |
0ee10fd1 JT |
86 | trace2_cmd_error_va(warn, params); |
87 | ||
a1fd2cf8 | 88 | vreportf(_("warning: "), warn, params); |
fa39b6b5 | 89 | } |
39a3f5ea | 90 | |
c19a490e JK |
91 | static int die_is_recursing_builtin(void) |
92 | { | |
93 | static int dying; | |
2d3c02f5 ÆAB |
94 | /* |
95 | * Just an arbitrary number X where "a < x < b" where "a" is | |
96 | * "maximum number of pthreads we'll ever plausibly spawn" and | |
97 | * "b" is "something less than Inf", since the point is to | |
98 | * prevent infinite recursion. | |
99 | */ | |
100 | static const int recursion_limit = 1024; | |
101 | ||
102 | dying++; | |
103 | if (dying > recursion_limit) { | |
104 | return 1; | |
105 | } else if (dying == 2) { | |
106 | warning("die() called many times. Recursion error or racy threaded death!"); | |
107 | return 0; | |
108 | } else { | |
109 | return 0; | |
110 | } | |
c19a490e JK |
111 | } |
112 | ||
39a3f5ea PB |
113 | /* If we are in a dlopen()ed .so write to a global variable would segfault |
114 | * (ugh), so keep things static. */ | |
5710dcce JK |
115 | static NORETURN_PTR report_fn usage_routine = usage_builtin; |
116 | static NORETURN_PTR report_fn die_routine = die_builtin; | |
18568ee8 | 117 | static report_fn die_message_routine = die_message_builtin; |
5710dcce JK |
118 | static report_fn error_routine = error_builtin; |
119 | static report_fn warn_routine = warn_builtin; | |
c19a490e | 120 | static int (*die_is_recursing)(void) = die_is_recursing_builtin; |
39a3f5ea | 121 | |
5710dcce | 122 | void set_die_routine(NORETURN_PTR report_fn routine) |
39a3f5ea PB |
123 | { |
124 | die_routine = routine; | |
125 | } | |
126 | ||
18568ee8 ÆAB |
127 | report_fn get_die_message_routine(void) |
128 | { | |
129 | return die_message_routine; | |
130 | } | |
131 | ||
5710dcce | 132 | void set_error_routine(report_fn routine) |
3bc4181f CB |
133 | { |
134 | error_routine = routine; | |
135 | } | |
136 | ||
5710dcce | 137 | report_fn get_error_routine(void) |
725149be CC |
138 | { |
139 | return error_routine; | |
140 | } | |
141 | ||
5710dcce | 142 | void set_warn_routine(report_fn routine) |
b83f108b CC |
143 | { |
144 | warn_routine = routine; | |
145 | } | |
146 | ||
5710dcce | 147 | report_fn get_warn_routine(void) |
725149be CC |
148 | { |
149 | return warn_routine; | |
150 | } | |
151 | ||
c19a490e JK |
152 | void set_die_is_recursing_routine(int (*routine)(void)) |
153 | { | |
154 | die_is_recursing = routine; | |
155 | } | |
156 | ||
c2e86add | 157 | void NORETURN usagef(const char *err, ...) |
64b1cb74 JN |
158 | { |
159 | va_list params; | |
160 | ||
161 | va_start(params, err); | |
162 | usage_routine(err, params); | |
163 | va_end(params); | |
164 | } | |
165 | ||
c2e86add | 166 | void NORETURN usage(const char *err) |
39a3f5ea | 167 | { |
64b1cb74 | 168 | usagef("%s", err); |
39a3f5ea PB |
169 | } |
170 | ||
c2e86add | 171 | void NORETURN die(const char *err, ...) |
0fcfd160 LT |
172 | { |
173 | va_list params; | |
174 | ||
c19a490e | 175 | if (die_is_recursing()) { |
cd163d4b BC |
176 | fputs("fatal: recursion detected in die handler\n", stderr); |
177 | exit(128); | |
178 | } | |
cd163d4b | 179 | |
0fcfd160 | 180 | va_start(params, err); |
39a3f5ea | 181 | die_routine(err, params); |
0fcfd160 | 182 | va_end(params); |
0fcfd160 LT |
183 | } |
184 | ||
58e4e511 | 185 | static const char *fmt_with_err(char *buf, int n, const char *fmt) |
b875036e | 186 | { |
f8b5a8e1 JH |
187 | char str_error[256], *err; |
188 | int i, j; | |
189 | ||
190 | err = strerror(errno); | |
191 | for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) { | |
192 | if ((str_error[j++] = err[i++]) != '%') | |
193 | continue; | |
194 | if (j < sizeof(str_error) - 1) { | |
195 | str_error[j++] = '%'; | |
196 | } else { | |
197 | /* No room to double the '%', so we overwrite it with | |
198 | * '\0' below */ | |
199 | j--; | |
200 | break; | |
201 | } | |
202 | } | |
203 | str_error[j] = 0; | |
ac4896f0 | 204 | /* Truncation is acceptable here */ |
58e4e511 NTND |
205 | snprintf(buf, n, "%s: %s", fmt, str_error); |
206 | return buf; | |
207 | } | |
208 | ||
209 | void NORETURN die_errno(const char *fmt, ...) | |
210 | { | |
211 | char buf[1024]; | |
212 | va_list params; | |
213 | ||
214 | if (die_is_recursing()) { | |
215 | fputs("fatal: recursion detected in die_errno handler\n", | |
216 | stderr); | |
217 | exit(128); | |
218 | } | |
b875036e TR |
219 | |
220 | va_start(params, fmt); | |
58e4e511 | 221 | die_routine(fmt_with_err(buf, sizeof(buf), fmt), params); |
b875036e TR |
222 | va_end(params); |
223 | } | |
224 | ||
18568ee8 ÆAB |
225 | #undef die_message |
226 | int die_message(const char *err, ...) | |
227 | { | |
228 | va_list params; | |
229 | ||
230 | va_start(params, err); | |
231 | die_message_routine(err, params); | |
232 | va_end(params); | |
233 | return 128; | |
234 | } | |
235 | ||
24f6e6d6 ÆAB |
236 | #undef die_message_errno |
237 | int die_message_errno(const char *fmt, ...) | |
238 | { | |
239 | char buf[1024]; | |
240 | va_list params; | |
241 | ||
242 | va_start(params, fmt); | |
243 | die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params); | |
244 | va_end(params); | |
245 | return 128; | |
246 | } | |
247 | ||
4df5e918 | 248 | #undef error_errno |
fd1d6723 NTND |
249 | int error_errno(const char *fmt, ...) |
250 | { | |
251 | char buf[1024]; | |
252 | va_list params; | |
253 | ||
254 | va_start(params, fmt); | |
255 | error_routine(fmt_with_err(buf, sizeof(buf), fmt), params); | |
256 | va_end(params); | |
257 | return -1; | |
258 | } | |
259 | ||
e208f9cc | 260 | #undef error |
0fcfd160 LT |
261 | int error(const char *err, ...) |
262 | { | |
263 | va_list params; | |
264 | ||
265 | va_start(params, err); | |
39a3f5ea | 266 | error_routine(err, params); |
0fcfd160 LT |
267 | va_end(params); |
268 | return -1; | |
269 | } | |
fa39b6b5 | 270 | |
fd1d6723 NTND |
271 | void warning_errno(const char *warn, ...) |
272 | { | |
273 | char buf[1024]; | |
274 | va_list params; | |
275 | ||
276 | va_start(params, warn); | |
277 | warn_routine(fmt_with_err(buf, sizeof(buf), warn), params); | |
278 | va_end(params); | |
279 | } | |
280 | ||
46efd2d9 | 281 | void warning(const char *warn, ...) |
fa39b6b5 SP |
282 | { |
283 | va_list params; | |
284 | ||
285 | va_start(params, warn); | |
286 | warn_routine(warn, params); | |
287 | va_end(params); | |
288 | } | |
d8193743 | 289 | |
a86303cb JS |
290 | /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */ |
291 | int BUG_exit_code; | |
292 | ||
0cc05b04 ÆAB |
293 | static void BUG_vfl_common(const char *file, int line, const char *fmt, |
294 | va_list params) | |
d8193743 JK |
295 | { |
296 | char prefix[256]; | |
297 | ||
298 | /* truncation via snprintf is OK here */ | |
56a29d2c | 299 | snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line); |
d8193743 JK |
300 | |
301 | vreportf(prefix, fmt, params); | |
0cc05b04 ÆAB |
302 | } |
303 | ||
304 | static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params) | |
305 | { | |
306 | va_list params_copy; | |
307 | static int in_bug; | |
308 | ||
309 | va_copy(params_copy, params); | |
310 | BUG_vfl_common(file, line, fmt, params); | |
0a9dde4a JT |
311 | |
312 | if (in_bug) | |
313 | abort(); | |
314 | in_bug = 1; | |
315 | ||
316 | trace2_cmd_error_va(fmt, params_copy); | |
317 | ||
a86303cb JS |
318 | if (BUG_exit_code) |
319 | exit(BUG_exit_code); | |
d8193743 JK |
320 | abort(); |
321 | } | |
322 | ||
3d7dd2d3 | 323 | NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...) |
d8193743 JK |
324 | { |
325 | va_list ap; | |
0cc05b04 ÆAB |
326 | |
327 | bug_called_must_BUG = 0; | |
328 | ||
d8193743 JK |
329 | va_start(ap, fmt); |
330 | BUG_vfl(file, line, fmt, ap); | |
331 | va_end(ap); | |
332 | } | |
0e5bba53 | 333 | |
0cc05b04 ÆAB |
334 | int bug_called_must_BUG; |
335 | void bug_fl(const char *file, int line, const char *fmt, ...) | |
336 | { | |
f8535596 | 337 | va_list ap; |
0cc05b04 ÆAB |
338 | |
339 | bug_called_must_BUG = 1; | |
340 | ||
0cc05b04 ÆAB |
341 | va_start(ap, fmt); |
342 | BUG_vfl_common(file, line, fmt, ap); | |
343 | va_end(ap); | |
f8535596 JK |
344 | |
345 | va_start(ap, fmt); | |
346 | trace2_cmd_error_va(fmt, ap); | |
347 | va_end(ap); | |
0cc05b04 ÆAB |
348 | } |
349 | ||
0e5bba53 JK |
350 | #ifdef SUPPRESS_ANNOTATED_LEAKS |
351 | void unleak_memory(const void *ptr, size_t len) | |
352 | { | |
353 | static struct suppressed_leak_root { | |
354 | struct suppressed_leak_root *next; | |
355 | char data[FLEX_ARRAY]; | |
356 | } *suppressed_leaks; | |
357 | struct suppressed_leak_root *root; | |
358 | ||
359 | FLEX_ALLOC_MEM(root, data, ptr, len); | |
360 | root->next = suppressed_leaks; | |
361 | suppressed_leaks = root; | |
362 | } | |
363 | #endif |