Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Junio C Hamano | 4050c0d | 2005-12-05 11:54:29 -0800 | [diff] [blame] | 6 | #include "git-compat-util.h" |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 7 | #include "cache.h" |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 8 | |
Johannes Sixt | ebaa79f | 2010-03-06 16:40:39 +0100 | [diff] [blame] | 9 | void vreportf(const char *prefix, const char *err, va_list params) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 10 | { |
Jeff King | b5a9e43 | 2017-01-11 09:02:03 -0500 | [diff] [blame] | 11 | char msg[4096]; |
Johannes Schindelin | 116d1fa | 2019-10-30 10:44:36 +0000 | [diff] [blame] | 12 | char *p, *pend = msg + sizeof(msg); |
| 13 | size_t prefix_len = strlen(prefix); |
Jeff King | f4c3edc | 2015-08-11 14:13:59 -0400 | [diff] [blame] | 14 | |
Johannes Schindelin | 116d1fa | 2019-10-30 10:44:36 +0000 | [diff] [blame] | 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++) { |
Jeff King | f290089 | 2017-01-11 09:02:23 -0500 | [diff] [blame] | 25 | if (iscntrl(*p) && *p != '\t' && *p != '\n') |
| 26 | *p = '?'; |
Jeff King | f4c3edc | 2015-08-11 14:13:59 -0400 | [diff] [blame] | 27 | } |
Johannes Schindelin | 116d1fa | 2019-10-30 10:44:36 +0000 | [diff] [blame] | 28 | |
| 29 | *(p++) = '\n'; /* we no longer need a NUL */ |
| 30 | fflush(stderr); |
| 31 | write_in_full(2, msg, p - msg); |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Jonathan Nieder | 64b1cb7 | 2009-11-09 09:05:02 -0600 | [diff] [blame] | 34 | static NORETURN void usage_builtin(const char *err, va_list params) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 35 | { |
Johannes Sixt | ebaa79f | 2010-03-06 16:40:39 +0100 | [diff] [blame] | 36 | vreportf("usage: ", err, params); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 55 | exit(129); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Junio C Hamano | ce88ac5 | 2006-06-23 22:44:33 -0700 | [diff] [blame] | 58 | static NORETURN void die_builtin(const char *err, va_list params) |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 59 | { |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 60 | /* |
| 61 | * We call this trace2 function first and expect it to va_copy 'params' |
| 62 | * before using it (because an 'ap' can only be walked once). |
| 63 | */ |
| 64 | trace2_cmd_error_va(err, params); |
| 65 | |
Johannes Sixt | ebaa79f | 2010-03-06 16:40:39 +0100 | [diff] [blame] | 66 | vreportf("fatal: ", err, params); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 67 | |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 68 | exit(128); |
| 69 | } |
| 70 | |
Junio C Hamano | ce88ac5 | 2006-06-23 22:44:33 -0700 | [diff] [blame] | 71 | static void error_builtin(const char *err, va_list params) |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 72 | { |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 73 | /* |
| 74 | * We call this trace2 function first and expect it to va_copy 'params' |
| 75 | * before using it (because an 'ap' can only be walked once). |
| 76 | */ |
| 77 | trace2_cmd_error_va(err, params); |
| 78 | |
Johannes Sixt | ebaa79f | 2010-03-06 16:40:39 +0100 | [diff] [blame] | 79 | vreportf("error: ", err, params); |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Shawn O. Pearce | fa39b6b | 2006-12-21 19:48:32 -0500 | [diff] [blame] | 82 | static void warn_builtin(const char *warn, va_list params) |
| 83 | { |
Johannes Sixt | ebaa79f | 2010-03-06 16:40:39 +0100 | [diff] [blame] | 84 | vreportf("warning: ", warn, params); |
Shawn O. Pearce | fa39b6b | 2006-12-21 19:48:32 -0500 | [diff] [blame] | 85 | } |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 86 | |
Jeff King | c19a490 | 2013-04-16 15:46:22 -0400 | [diff] [blame] | 87 | static int die_is_recursing_builtin(void) |
| 88 | { |
| 89 | static int dying; |
Ævar Arnfjörð Bjarmason | 2d3c02f | 2017-06-21 20:47:42 +0000 | [diff] [blame] | 90 | /* |
| 91 | * Just an arbitrary number X where "a < x < b" where "a" is |
| 92 | * "maximum number of pthreads we'll ever plausibly spawn" and |
| 93 | * "b" is "something less than Inf", since the point is to |
| 94 | * prevent infinite recursion. |
| 95 | */ |
| 96 | static const int recursion_limit = 1024; |
| 97 | |
| 98 | dying++; |
| 99 | if (dying > recursion_limit) { |
| 100 | return 1; |
| 101 | } else if (dying == 2) { |
| 102 | warning("die() called many times. Recursion error or racy threaded death!"); |
| 103 | return 0; |
| 104 | } else { |
| 105 | return 0; |
| 106 | } |
Jeff King | c19a490 | 2013-04-16 15:46:22 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 109 | /* If we are in a dlopen()ed .so write to a global variable would segfault |
| 110 | * (ugh), so keep things static. */ |
Jonathan Nieder | 64b1cb7 | 2009-11-09 09:05:02 -0600 | [diff] [blame] | 111 | static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin; |
Erik Faye-Lund | 18660bc | 2009-09-30 18:05:50 +0000 | [diff] [blame] | 112 | static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin; |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 113 | static void (*error_routine)(const char *err, va_list params) = error_builtin; |
Shawn O. Pearce | fa39b6b | 2006-12-21 19:48:32 -0500 | [diff] [blame] | 114 | static void (*warn_routine)(const char *err, va_list params) = warn_builtin; |
Jeff King | c19a490 | 2013-04-16 15:46:22 -0400 | [diff] [blame] | 115 | static int (*die_is_recursing)(void) = die_is_recursing_builtin; |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 116 | |
Erik Faye-Lund | 18660bc | 2009-09-30 18:05:50 +0000 | [diff] [blame] | 117 | void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params)) |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 118 | { |
| 119 | die_routine = routine; |
| 120 | } |
| 121 | |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 122 | void set_error_routine(void (*routine)(const char *err, va_list params)) |
| 123 | { |
| 124 | error_routine = routine; |
| 125 | } |
| 126 | |
Christian Couder | 725149b | 2016-09-04 22:18:28 +0200 | [diff] [blame] | 127 | void (*get_error_routine(void))(const char *err, va_list params) |
| 128 | { |
| 129 | return error_routine; |
| 130 | } |
| 131 | |
Christian Couder | b83f108 | 2016-09-04 22:18:27 +0200 | [diff] [blame] | 132 | void set_warn_routine(void (*routine)(const char *warn, va_list params)) |
| 133 | { |
| 134 | warn_routine = routine; |
| 135 | } |
| 136 | |
Christian Couder | 725149b | 2016-09-04 22:18:28 +0200 | [diff] [blame] | 137 | void (*get_warn_routine(void))(const char *warn, va_list params) |
| 138 | { |
| 139 | return warn_routine; |
| 140 | } |
| 141 | |
Jeff King | c19a490 | 2013-04-16 15:46:22 -0400 | [diff] [blame] | 142 | void set_die_is_recursing_routine(int (*routine)(void)) |
| 143 | { |
| 144 | die_is_recursing = routine; |
| 145 | } |
| 146 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 147 | void NORETURN usagef(const char *err, ...) |
Jonathan Nieder | 64b1cb7 | 2009-11-09 09:05:02 -0600 | [diff] [blame] | 148 | { |
| 149 | va_list params; |
| 150 | |
| 151 | va_start(params, err); |
| 152 | usage_routine(err, params); |
| 153 | va_end(params); |
| 154 | } |
| 155 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 156 | void NORETURN usage(const char *err) |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 157 | { |
Jonathan Nieder | 64b1cb7 | 2009-11-09 09:05:02 -0600 | [diff] [blame] | 158 | usagef("%s", err); |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 161 | void NORETURN die(const char *err, ...) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 162 | { |
| 163 | va_list params; |
| 164 | |
Jeff King | c19a490 | 2013-04-16 15:46:22 -0400 | [diff] [blame] | 165 | if (die_is_recursing()) { |
Brandon Casey | cd163d4 | 2012-11-14 17:45:52 -0800 | [diff] [blame] | 166 | fputs("fatal: recursion detected in die handler\n", stderr); |
| 167 | exit(128); |
| 168 | } |
Brandon Casey | cd163d4 | 2012-11-14 17:45:52 -0800 | [diff] [blame] | 169 | |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 170 | va_start(params, err); |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 171 | die_routine(err, params); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 172 | va_end(params); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Nguyễn Thái Ngọc Duy | 58e4e51 | 2016-05-08 16:47:21 +0700 | [diff] [blame] | 175 | static const char *fmt_with_err(char *buf, int n, const char *fmt) |
Thomas Rast | b875036 | 2009-06-27 17:58:44 +0200 | [diff] [blame] | 176 | { |
Junio C Hamano | f8b5a8e | 2009-06-27 17:58:45 +0200 | [diff] [blame] | 177 | char str_error[256], *err; |
| 178 | int i, j; |
Thomas Rast | b875036 | 2009-06-27 17:58:44 +0200 | [diff] [blame] | 179 | |
Junio C Hamano | f8b5a8e | 2009-06-27 17:58:45 +0200 | [diff] [blame] | 180 | err = strerror(errno); |
| 181 | for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) { |
| 182 | if ((str_error[j++] = err[i++]) != '%') |
| 183 | continue; |
| 184 | if (j < sizeof(str_error) - 1) { |
| 185 | str_error[j++] = '%'; |
| 186 | } else { |
| 187 | /* No room to double the '%', so we overwrite it with |
| 188 | * '\0' below */ |
| 189 | j--; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | str_error[j] = 0; |
Jeff King | ac4896f | 2018-05-18 18:58:44 -0700 | [diff] [blame] | 194 | /* Truncation is acceptable here */ |
Nguyễn Thái Ngọc Duy | 58e4e51 | 2016-05-08 16:47:21 +0700 | [diff] [blame] | 195 | snprintf(buf, n, "%s: %s", fmt, str_error); |
| 196 | return buf; |
| 197 | } |
| 198 | |
| 199 | void NORETURN die_errno(const char *fmt, ...) |
| 200 | { |
| 201 | char buf[1024]; |
| 202 | va_list params; |
| 203 | |
| 204 | if (die_is_recursing()) { |
| 205 | fputs("fatal: recursion detected in die_errno handler\n", |
| 206 | stderr); |
| 207 | exit(128); |
| 208 | } |
Thomas Rast | b875036 | 2009-06-27 17:58:44 +0200 | [diff] [blame] | 209 | |
| 210 | va_start(params, fmt); |
Nguyễn Thái Ngọc Duy | 58e4e51 | 2016-05-08 16:47:21 +0700 | [diff] [blame] | 211 | die_routine(fmt_with_err(buf, sizeof(buf), fmt), params); |
Thomas Rast | b875036 | 2009-06-27 17:58:44 +0200 | [diff] [blame] | 212 | va_end(params); |
| 213 | } |
| 214 | |
Jeff King | 4df5e91 | 2016-08-30 23:41:22 -0400 | [diff] [blame] | 215 | #undef error_errno |
Nguyễn Thái Ngọc Duy | fd1d672 | 2016-05-08 16:47:22 +0700 | [diff] [blame] | 216 | int error_errno(const char *fmt, ...) |
| 217 | { |
| 218 | char buf[1024]; |
| 219 | va_list params; |
| 220 | |
| 221 | va_start(params, fmt); |
| 222 | error_routine(fmt_with_err(buf, sizeof(buf), fmt), params); |
| 223 | va_end(params); |
| 224 | return -1; |
| 225 | } |
| 226 | |
Jeff King | e208f9c | 2012-12-15 12:37:36 -0500 | [diff] [blame] | 227 | #undef error |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 228 | int error(const char *err, ...) |
| 229 | { |
| 230 | va_list params; |
| 231 | |
| 232 | va_start(params, err); |
Petr Baudis | 39a3f5e | 2006-06-24 04:34:38 +0200 | [diff] [blame] | 233 | error_routine(err, params); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 234 | va_end(params); |
| 235 | return -1; |
| 236 | } |
Shawn O. Pearce | fa39b6b | 2006-12-21 19:48:32 -0500 | [diff] [blame] | 237 | |
Nguyễn Thái Ngọc Duy | fd1d672 | 2016-05-08 16:47:22 +0700 | [diff] [blame] | 238 | void warning_errno(const char *warn, ...) |
| 239 | { |
| 240 | char buf[1024]; |
| 241 | va_list params; |
| 242 | |
| 243 | va_start(params, warn); |
| 244 | warn_routine(fmt_with_err(buf, sizeof(buf), warn), params); |
| 245 | va_end(params); |
| 246 | } |
| 247 | |
Theodore Ts'o | 46efd2d | 2007-03-30 19:07:05 -0400 | [diff] [blame] | 248 | void warning(const char *warn, ...) |
Shawn O. Pearce | fa39b6b | 2006-12-21 19:48:32 -0500 | [diff] [blame] | 249 | { |
| 250 | va_list params; |
| 251 | |
| 252 | va_start(params, warn); |
| 253 | warn_routine(warn, params); |
| 254 | va_end(params); |
| 255 | } |
Jeff King | d819374 | 2017-05-12 23:28:50 -0400 | [diff] [blame] | 256 | |
Johannes Schindelin | a86303c | 2018-05-02 11:38:28 +0200 | [diff] [blame] | 257 | /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */ |
| 258 | int BUG_exit_code; |
| 259 | |
Jeff King | d819374 | 2017-05-12 23:28:50 -0400 | [diff] [blame] | 260 | static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params) |
| 261 | { |
| 262 | char prefix[256]; |
| 263 | |
| 264 | /* truncation via snprintf is OK here */ |
| 265 | if (file) |
| 266 | snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line); |
| 267 | else |
| 268 | snprintf(prefix, sizeof(prefix), "BUG: "); |
| 269 | |
| 270 | vreportf(prefix, fmt, params); |
Johannes Schindelin | a86303c | 2018-05-02 11:38:28 +0200 | [diff] [blame] | 271 | if (BUG_exit_code) |
| 272 | exit(BUG_exit_code); |
Jeff King | d819374 | 2017-05-12 23:28:50 -0400 | [diff] [blame] | 273 | abort(); |
| 274 | } |
| 275 | |
| 276 | #ifdef HAVE_VARIADIC_MACROS |
Ramsay Jones | 3d7dd2d | 2017-05-21 23:25:39 +0100 | [diff] [blame] | 277 | NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...) |
Jeff King | d819374 | 2017-05-12 23:28:50 -0400 | [diff] [blame] | 278 | { |
| 279 | va_list ap; |
| 280 | va_start(ap, fmt); |
| 281 | BUG_vfl(file, line, fmt, ap); |
| 282 | va_end(ap); |
| 283 | } |
| 284 | #else |
Ramsay Jones | 3d7dd2d | 2017-05-21 23:25:39 +0100 | [diff] [blame] | 285 | NORETURN void BUG(const char *fmt, ...) |
Jeff King | d819374 | 2017-05-12 23:28:50 -0400 | [diff] [blame] | 286 | { |
| 287 | va_list ap; |
| 288 | va_start(ap, fmt); |
| 289 | BUG_vfl(NULL, 0, fmt, ap); |
| 290 | va_end(ap); |
| 291 | } |
| 292 | #endif |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 293 | |
| 294 | #ifdef SUPPRESS_ANNOTATED_LEAKS |
| 295 | void unleak_memory(const void *ptr, size_t len) |
| 296 | { |
| 297 | static struct suppressed_leak_root { |
| 298 | struct suppressed_leak_root *next; |
| 299 | char data[FLEX_ARRAY]; |
| 300 | } *suppressed_leaks; |
| 301 | struct suppressed_leak_root *root; |
| 302 | |
| 303 | FLEX_ALLOC_MEM(root, data, ptr, len); |
| 304 | root->next = suppressed_leaks; |
| 305 | suppressed_leaks = root; |
| 306 | } |
| 307 | #endif |