diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-28 06:53:37 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-28 06:53:37 +0000 |
| commit | 0f1a22174fc7c6a0767617974640d521074174d5 (patch) | |
| tree | 5d13587d08a95332518b3191b7440424f0f190c6 /util/libreboot-utils/lib/string.c | |
| parent | 55f0e6ac8e540cea24af64070bfc49a032729511 (diff) | |
libreboot-utils: unified error handling
i now use a singleton hook function per program:
nvmutil, mkhtemp and lottery
call this at the startup of your program:
(void) errhook(exit_cleanup);
then provide that function. make it static,
so that each program has its own version.
if you're writing a program that handles lots
of files for example, and you want to do certain
cleanup on exit (including error exit), this can
be quite useful.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/string.c')
| -rw-r--r-- | util/libreboot-utils/lib/string.c | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c index dd11c039..9e38a9e9 100644 --- a/util/libreboot-utils/lib/string.c +++ b/util/libreboot-utils/lib/string.c @@ -40,9 +40,9 @@ vmalloc(void **buf, size_t size) void *rval = NULL; if (size >= SIZE_MAX - 1) - err_no_cleanup(0, EOVERFLOW, "integer overflow in vmalloc"); + err_exit(EOVERFLOW, "integer overflow in vmalloc"); if (buf == NULL) - err_no_cleanup(0, EFAULT, "Bad pointer passed to vmalloc"); + err_exit(EFAULT, "Bad pointer passed to vmalloc"); /* lots of programs will * re-initialise a buffer @@ -52,14 +52,14 @@ vmalloc(void **buf, size_t size) * force the programmer to behave */ if (*buf != NULL) - err_no_cleanup(0, EFAULT, "Non-null pointer given to vmalloc"); + err_exit(EFAULT, "Non-null pointer given to vmalloc"); if (!size) - err_no_cleanup(0, EFAULT, + err_exit(EFAULT, "Tried to vmalloc(0) and that is very bad. Fix it now"); if ((rval = malloc(size)) == NULL) - err_no_cleanup(0, errno, "malloc fail in vmalloc"); + err_exit(errno, "malloc fail in vmalloc"); return *buf = rval; } @@ -288,12 +288,16 @@ err: /* the one for nvmutil state is in state.c */ /* this one just exits */ void -err_no_cleanup(int stfu, int nvm_errval, const char *msg, ...) +err_exit(int nvm_errval, const char *msg, ...) { va_list args; int saved_errno = errno; const char *p; + func_t err_cleanup = errhook(NULL); + err_cleanup(); + errno = saved_errno; + if (!errno) saved_errno = errno = ECANCELED; @@ -312,6 +316,37 @@ err_no_cleanup(int stfu, int nvm_errval, const char *msg, ...) exit(EXIT_FAILURE); } +/* the err function will + * call this upon exit, and + * cleanup will be performed + * e.g. you might want to + * close some files, depending + * on your program. + * see: err_exit() + */ +func_t errhook(func_t ptr) +{ + static int set = 0; + static func_t hook = NULL; + + if (!set) { + set = 1; + + if (ptr == NULL) + hook = no_op; + else + hook = ptr; + } + + return hook; +} + +void +no_op(void) +{ + return; +} + const char * getnvmprogname(void) { @@ -366,7 +401,7 @@ xpledgex(const char *promises, const char *execpromises) (void) promises, (void) execpromises, (void) saved_errno; #ifdef __OpenBSD__ if (pledge(promises, execpromises) == -1) - err_no_cleanup(0, errno, "pledge"); + err_exit(errno, "pledge"); #endif errno = saved_errno; return 0; @@ -378,7 +413,7 @@ xunveilx(const char *path, const char *permissions) (void) path, (void) permissions, (void) saved_errno; #ifdef __OpenBSD__ if (pledge(promises, execpromises) == -1) - err_no_cleanup(0, errno, "pledge"); + err_exit(errno, "pledge"); #endif errno = saved_errno; return 0; |
