diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-14 07:20:50 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-14 07:20:50 +0000 |
| commit | 90ed69474c77aa282c345b7709afd4dde17d9513 (patch) | |
| tree | bb05ac03e4eaf8421a3e04668fe2d96334a7623d | |
| parent | 1a8896b82d9032d0944bd8a5ea0a58c9b1cd3456 (diff) | |
util/nvmutil: don't recurse err/close_files
Signed-off-by: Leah Rowe <leah@libreboot.org>
| -rw-r--r-- | util/nvmutil/nvmutil.c | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 8e8f41cd..6d7cb916 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -355,8 +355,8 @@ static int try_err(int loop_err, int errval); /* * Error handling and cleanup */ +static int close_files(void); static void err(int nvm_errval, const char *msg, ...); -static void close_files(void); static const char *getnvmprogname(void); static void usage(int usage_exit); @@ -705,7 +705,8 @@ main(int argc, char *argv[]) err(EIO, "%s: bad write", fname); } - close_files(); + if (close_files() == -1) + err(EIO, "%s: close", fname); return EXIT_SUCCESS; } @@ -1999,17 +2000,42 @@ try_err(int loop_err, int errval) return -1; } +static int +close_files(void) +{ + int close_err_gbe = 0; + int close_err_rand = 0; + int saved_errno = errno; + + if (gbe_fd > -1) { + if (close(gbe_fd) == -1) + close_err_gbe = errno; + gbe_fd = -1; + } + + if (urandom_fd > -1) { + if (close(urandom_fd) == -1) + close_err_rand = errno; + urandom_fd = -1; + } + + if (saved_errno) + errno = saved_errno; + + return -(close_err_gbe | close_err_rand); +} + static void err(int nvm_errval, const char *msg, ...) { va_list args; - if (nvm_errval >= 0) { - close_files(); - errno = nvm_errval; - } if (errno <= 0) errno = ECANCELED; + if (!errno) + errno = nvm_errval; + + (void)close_files(); fprintf(stderr, "%s: ", getnvmprogname()); @@ -2023,22 +2049,6 @@ err(int nvm_errval, const char *msg, ...) exit(EXIT_FAILURE); } -static void -close_files(void) -{ - if (gbe_fd > -1) { - if (close(gbe_fd) == -1) - err(-1, "%s: close failed", fname); - gbe_fd = -1; - } - - if (urandom_fd > -1) { - if (close(urandom_fd) == -1) - err(-1, "%s: close failed", rname); - urandom_fd = -1; - } -} - static const char * getnvmprogname(void) { |
