From 18568ee8f8836db5fc1848f2b34e313ac0a86980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 7 Dec 2021 19:26:29 +0100 Subject: usage.c: add a die_message() routine MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have code in various places that would like to call die(), but wants to defer the exit(128) it would invoke, e.g. to print an additional message, or adjust the exit code. Add a die_message() helper routine to bridge this gap in the API. Functionally this behaves just like the error() routine, except it'll print a "fatal: " prefix, and it will return with 128 instead of -1, this is so that caller can pass the return value to "exit()", instead of having to hardcode "exit(128)". Note that as with the other routines the "die_message_builtin" needs to return "void" and otherwise conform to the "report_fn" signature. As we'll see in a subsequent commit callers will want to replace e.g. their default "die_routine" with a "die_message_routine". For now we're just adding the routine and making die_builtin() in usage.c itself use it. In order to do that we need to add a get_die_message_routine() function, which works like the other get_*_routine() functions in usage.c. There is no set_die_message_rotine(), as it hasn't been needed yet. We can add it if we ever need it. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- git-compat-util.h | 2 ++ usage.c | 28 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index c6bd2a84e5..d5e495529c 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -479,6 +479,7 @@ NORETURN void usage(const char *err); NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); +int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); int error(const char *err, ...) __attribute__((format (printf, 1, 2))); int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); @@ -513,6 +514,7 @@ static inline int const_error(void) typedef void (*report_fn)(const char *, va_list params); void set_die_routine(NORETURN_PTR report_fn routine); +report_fn get_die_message_routine(void); void set_error_routine(report_fn routine); report_fn get_error_routine(void); void set_warn_routine(report_fn routine); diff --git a/usage.c b/usage.c index c7d233b0de..76399ba840 100644 --- a/usage.c +++ b/usage.c @@ -55,6 +55,12 @@ static NORETURN void usage_builtin(const char *err, va_list params) exit(129); } +static void die_message_builtin(const char *err, va_list params) +{ + trace2_cmd_error_va(err, params); + vreportf("fatal: ", err, params); +} + /* * We call trace2_cmd_error_va() in the below functions first and * expect it to va_copy 'params' before using it (because an 'ap' can @@ -62,10 +68,9 @@ static NORETURN void usage_builtin(const char *err, va_list params) */ static NORETURN void die_builtin(const char *err, va_list params) { - trace2_cmd_error_va(err, params); - - vreportf("fatal: ", err, params); + report_fn die_message_fn = get_die_message_routine(); + die_message_fn(err, params); exit(128); } @@ -109,6 +114,7 @@ static int die_is_recursing_builtin(void) * (ugh), so keep things static. */ static NORETURN_PTR report_fn usage_routine = usage_builtin; static NORETURN_PTR report_fn die_routine = die_builtin; +static report_fn die_message_routine = die_message_builtin; static report_fn error_routine = error_builtin; static report_fn warn_routine = warn_builtin; static int (*die_is_recursing)(void) = die_is_recursing_builtin; @@ -118,6 +124,11 @@ void set_die_routine(NORETURN_PTR report_fn routine) die_routine = routine; } +report_fn get_die_message_routine(void) +{ + return die_message_routine; +} + void set_error_routine(report_fn routine) { error_routine = routine; @@ -211,6 +222,17 @@ void NORETURN die_errno(const char *fmt, ...) va_end(params); } +#undef die_message +int die_message(const char *err, ...) +{ + va_list params; + + va_start(params, err); + die_message_routine(err, params); + va_end(params); + return 128; +} + #undef error_errno int error_errno(const char *fmt, ...) { -- cgit v1.2.3 From e081a7c3b7290c50c817598e5f2686d068540762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 7 Dec 2021 19:26:30 +0100 Subject: usage.c API users: use die_message() for "fatal :" + exit 128 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change code that printed its own "fatal: " message and exited with a status code of 128 to use the die_message() function added in a preceding commit. This change also demonstrates why the return value of die_message_routine() needed to be that of "report_fn". We have callers such as the run-command.c::child_err_spew() which would like to replace its error routine with the return value of "get_die_message_routine()". Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- builtin/fast-import.c | 12 +++++++----- http-backend.c | 3 ++- parse-options.c | 2 +- run-command.c | 16 +++++----------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 20406f6775..2b2e28bad7 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -401,16 +401,18 @@ static void dump_marks(void); static NORETURN void die_nicely(const char *err, va_list params) { + va_list cp; static int zombie; - char message[2 * PATH_MAX]; + report_fn die_message_fn = get_die_message_routine(); - vsnprintf(message, sizeof(message), err, params); - fputs("fatal: ", stderr); - fputs(message, stderr); - fputc('\n', stderr); + va_copy(cp, params); + die_message_fn(err, params); if (!zombie) { + char message[2 * PATH_MAX]; + zombie = 1; + vsnprintf(message, sizeof(message), err, cp); write_crash_report(message); end_packfile(); unkeep_all_packs(); diff --git a/http-backend.c b/http-backend.c index 3d6e2ff17f..982cb62c7c 100644 --- a/http-backend.c +++ b/http-backend.c @@ -659,8 +659,9 @@ static NORETURN void die_webcgi(const char *err, va_list params) { if (dead <= 1) { struct strbuf hdr = STRBUF_INIT; + report_fn die_message_fn = get_die_message_routine(); - vreportf("fatal: ", err, params); + die_message_fn(err, params); http_status(&hdr, 500, "Internal Server Error"); hdr_nocache(&hdr); diff --git a/parse-options.c b/parse-options.c index fc5b43ff0b..8bc0a21f1d 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1075,6 +1075,6 @@ void NORETURN usage_msg_opt(const char *msg, const char * const *usagestr, const struct option *options) { - fprintf(stderr, "fatal: %s\n\n", msg); + die_message("%s\n", msg); /* The extra \n is intentional */ usage_with_options(usagestr, options); } diff --git a/run-command.c b/run-command.c index f40df01c77..a790fe9799 100644 --- a/run-command.c +++ b/run-command.c @@ -340,15 +340,6 @@ static void child_close_pair(int fd[2]) child_close(fd[1]); } -/* - * parent will make it look like the child spewed a fatal error and died - * this is needed to prevent changes to t0061. - */ -static void fake_fatal(const char *err, va_list params) -{ - vreportf("fatal: ", err, params); -} - static void child_error_fn(const char *err, va_list params) { const char msg[] = "error() should not be called in child\n"; @@ -372,9 +363,10 @@ static void NORETURN child_die_fn(const char *err, va_list params) static void child_err_spew(struct child_process *cmd, struct child_err *cerr) { static void (*old_errfn)(const char *err, va_list params); + report_fn die_message_routine = get_die_message_routine(); old_errfn = get_error_routine(); - set_error_routine(fake_fatal); + set_error_routine(die_message_routine); errno = cerr->syserr; switch (cerr->err) { @@ -1082,7 +1074,9 @@ static void *run_thread(void *data) static NORETURN void die_async(const char *err, va_list params) { - vreportf("fatal: ", err, params); + report_fn die_message_fn = get_die_message_routine(); + + die_message_fn(err, params); if (in_async()) { struct async *async = pthread_getspecific(async_key); -- cgit v1.2.3 From adcd4d4c6f9352bef53058161db36bb1ccd5cd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 7 Dec 2021 19:26:31 +0100 Subject: usage.c API users: use die_message() for error() + exit 128 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Continue the migration of code that printed a message and exited with 128. In this case the caller used "error()", so we'll be changing the output from "error: " to "fatal: ". This change is intentional and desired. This code is dying, so it should emit "fatal", the only reason it didn't do so was because before the existence of "die_message()" it would have needed to craft its own "fatal: " message. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- builtin/notes.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 71c59583a1..2812d1eac4 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -201,11 +201,12 @@ static void prepare_note_data(const struct object_id *object, struct note_data * static void write_note_data(struct note_data *d, struct object_id *oid) { if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) { - error(_("unable to write note object")); + int status = die_message(_("unable to write note object")); + if (d->edit_path) - error(_("the note contents have been left in %s"), - d->edit_path); - exit(128); + die_message(_("the note contents have been left in %s"), + d->edit_path); + exit(status); } } -- cgit v1.2.3 From 0faf84d97d21d5d29132c8c021b16afe72d9fbf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 7 Dec 2021 19:26:32 +0100 Subject: gc: return from cmd_gc(), don't call exit() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A minor code cleanup. Let's "return" from cmd_gc() instead of calling exit(). See 338abb0f045 (builtins + test helpers: use return instead of exit() in cmd_*, 2021-06-08) for other such cases. While we're at it add a \n to separate the variable declaration from the rest of the code in this block. Both of these changes make a subsequent change smaller and easier to read. This change isn't really needed for that subsequent change, but now someone viewing that future behavior change won't need to wonder why we're either still calling exit() here, or fixing it while we're at it. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- builtin/gc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/gc.c b/builtin/gc.c index bcef6a4c8d..900ccfb8d4 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -611,9 +611,10 @@ int cmd_gc(int argc, const char **argv, const char *prefix) } if (detach_auto) { int ret = report_last_gc_error(); + if (ret < 0) /* an I/O error occurred, already reported */ - exit(128); + return 128; if (ret == 1) /* Last gc --auto failed. Skip this one. */ return 0; -- cgit v1.2.3 From 24f6e6d626b0125f741880918fb3b9b85f607a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 7 Dec 2021 19:26:33 +0100 Subject: usage.c + gc: add and use a die_message_errno() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the "error: " output when we exit with 128 due to gc.log errors to use a "fatal: " prefix instead. To do this add a die_message_errno() a sibling function to the die_errno() added in a preceding commit. Before this we'd expect report_last_gc_error() to return -1 from error_errno() in this case. It already treated a status of 0 and 1 specially. Let's just document that anything that's not 0 or 1 should be returned. We could also retain the "ret < 0" behavior here without hardcoding 128 by returning -128, and having the caller do a "return -ret", but I think this makes more sense, and preserves the path from die_message*()'s return value to the "return" without hardcoding "128". Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- builtin/gc.c | 13 +++++++------ git-compat-util.h | 1 + usage.c | 12 ++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/builtin/gc.c b/builtin/gc.c index 900ccfb8d4..8e60ef1eab 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -470,7 +470,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid) /* * Returns 0 if there was no previous error and gc can proceed, 1 if * gc should not proceed due to an error in the last run. Prints a - * message and returns -1 if an error occurred while reading gc.log + * message and returns with a non-[01] status code if an error occurred + * while reading gc.log */ static int report_last_gc_error(void) { @@ -484,7 +485,7 @@ static int report_last_gc_error(void) if (errno == ENOENT) goto done; - ret = error_errno(_("cannot stat '%s'"), gc_log_path); + ret = die_message_errno(_("cannot stat '%s'"), gc_log_path); goto done; } @@ -493,7 +494,7 @@ static int report_last_gc_error(void) len = strbuf_read_file(&sb, gc_log_path, 0); if (len < 0) - ret = error_errno(_("cannot read '%s'"), gc_log_path); + ret = die_message_errno(_("cannot read '%s'"), gc_log_path); else if (len > 0) { /* * A previous gc failed. Report the error, and don't @@ -612,12 +613,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix) if (detach_auto) { int ret = report_last_gc_error(); - if (ret < 0) - /* an I/O error occurred, already reported */ - return 128; if (ret == 1) /* Last gc --auto failed. Skip this one. */ return 0; + else if (ret) + /* an I/O error occurred, already reported */ + return ret; if (lock_repo_for_gc(force, &pid)) return 0; diff --git a/git-compat-util.h b/git-compat-util.h index d5e495529c..c6c6f7d6b5 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -480,6 +480,7 @@ NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)) NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); +int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); int error(const char *err, ...) __attribute__((format (printf, 1, 2))); int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index 76399ba840..3d09e8eea4 100644 --- a/usage.c +++ b/usage.c @@ -233,6 +233,18 @@ int die_message(const char *err, ...) return 128; } +#undef die_message_errno +int die_message_errno(const char *fmt, ...) +{ + char buf[1024]; + va_list params; + + va_start(params, fmt); + die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params); + va_end(params); + return 128; +} + #undef error_errno int error_errno(const char *fmt, ...) { -- cgit v1.2.3 From f5c39c3268107e1f3def70709d509fd24282832c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 7 Dec 2021 19:26:34 +0100 Subject: config API: use get_error_routine(), not vreportf() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the git_die_config() function added in 5a80e97c827 (config: add `git_die_config()` to the config-set API, 2014-08-07) to use the public callbacks in the usage.[ch] API instead of the the underlying vreportf() function. In preceding commits the rest of the vreportf() users outside of usage.c was migrated to die_message(), so we can now make it "static". Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- config.c | 3 ++- git-compat-util.h | 1 - usage.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config.c b/config.c index c5873f3a70..e96b8fdb62 100644 --- a/config.c +++ b/config.c @@ -2555,11 +2555,12 @@ void git_die_config(const char *key, const char *err, ...) { const struct string_list *values; struct key_value_info *kv_info; + report_fn error_fn = get_error_routine(); if (err) { va_list params; va_start(params, err); - vreportf("error: ", err, params); + error_fn(err, params); va_end(params); } values = git_config_get_value_multi(key); diff --git a/git-compat-util.h b/git-compat-util.h index c6c6f7d6b5..bdb3977b9e 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -474,7 +474,6 @@ static inline int git_has_dir_sep(const char *path) struct strbuf; /* General helper functions */ -void vreportf(const char *prefix, const char *err, va_list params); NORETURN void usage(const char *err); NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index 3d09e8eea4..9943dd8742 100644 --- a/usage.c +++ b/usage.c @@ -6,7 +6,7 @@ #include "git-compat-util.h" #include "cache.h" -void vreportf(const char *prefix, const char *err, va_list params) +static void vreportf(const char *prefix, const char *err, va_list params) { char msg[4096]; char *p, *pend = msg + sizeof(msg); -- cgit v1.2.3