Exit code and logging invariants for log_fatal and hardware log_error
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-arm32 (push) Waiting to run
Build / build-win (push) Waiting to run
Build / build-freebsd (push) Waiting to run
Build / build-musl (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run

This commit is contained in:
Andrea Mazzoleni 2026-09-20 15:56:14 +02:00
parent c2d46f4039
commit 51b5020b8e
14 changed files with 120 additions and 24 deletions

View file

@ -181,6 +181,8 @@ uint64_t random_u64(void)
int is_hw(int err)
{
/* LCOV_EXCL_START */
if (err == EDATA)
return 1;
if (err == EIO)
return 1;
#ifdef EFSCORRUPTED
@ -211,6 +213,19 @@ int is_hw(int err)
return 0;
}
/**
* Return if the error is a hardware error represented by a system errno.
*
* Unlike is_hw(), this excludes synthetic errors such as EDATA, which
* may be reported while still allowing a zero process exit code.
*
* All other hardware errors must prevent a successful process exit.
*/
int is_hw_errno(int err)
{
return err > 0 && is_hw(err);
}
void log_fatal_errno(int err, const char* name)
{
/* LCOV_EXCL_START */
@ -297,6 +312,18 @@ int msg_fatal_stderr = 1;
FILE* stdlog = 0;
int msg_line_prev = 0; /**< Previous line width on stdout */
int msg_line_curr = 0; /**< Current line width on stdout */
static unsigned log_hardware_error_count = 0;
unsigned log_hardware_errors(void)
{
unsigned count;
lock_msg();
count = log_hardware_error_count;
unlock_msg();
return count;
}
/*
* Note that in the following functions we always flush both
@ -379,12 +406,14 @@ void vlog_fatal(int err, const char* format, va_list ap, const char* post)
lock_msg();
if (is_hw(err))
++log_hardware_error_count;
if (stdlog) {
if (is_hw(err))
log_failed = fprintf(stdlog, "msg:fatal_hardware: ") < 0;
else
log_failed = fprintf(stdlog, "msg:fatal: ") < 0;
va_list ap_copy;
va_copy(ap_copy, ap);
if (vfprintf(stdlog, format, ap_copy) < 0)
@ -427,6 +456,9 @@ void vlog_error(int err, const char* format, va_list ap, const char* post)
{
lock_msg();
if (is_hw(err))
++log_hardware_error_count;
if (stdlog) {
if (is_hw(err))
fprintf(stdlog, "msg:error_hardware: ");
@ -470,6 +502,9 @@ void vlog_info(int err, const char* format, va_list ap, const char* post)
{
lock_msg();
if (is_hw(err))
++log_hardware_error_count;
if (stdlog) {
if (is_hw(err))
fprintf(stdlog, "msg:error_hardware: "); /* we never expect an hardware error */
@ -536,6 +571,9 @@ void log_expected(int err, const char* format, ...)
lock_msg();
if (is_hw(err))
++log_hardware_error_count;
if (stdlog) {
if (is_hw(err))
fprintf(stdlog, "msg:error_hardware: "); /* we never expect an hardware error */