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/mkhtemp.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/mkhtemp.c')
| -rw-r--r-- | util/libreboot-utils/mkhtemp.c | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c index 32a967d1..5be5a38a 100644 --- a/util/libreboot-utils/mkhtemp.c +++ b/util/libreboot-utils/mkhtemp.c @@ -34,6 +34,9 @@ #include "include/common.h" +static void +exit_cleanup(void); + int main(int argc, char *argv[]) { @@ -57,10 +60,11 @@ main(int argc, char *argv[]) int fd = -1; int type = MKHTEMP_FILE; - int stfu = 0; /* -q option */ + + (void) errhook(exit_cleanup); if (lbgetprogname(argv[0]) == NULL) - err_no_cleanup(stfu, errno, "could not set progname"); + err_exit(errno, "could not set progname"); /* https://man.openbsd.org/pledge.2 */ xpledgex("stdio flock rpath wpath cpath", NULL); @@ -79,7 +83,6 @@ main(int argc, char *argv[]) case 'q': /* don't print errors */ /* (exit status unchanged) */ - stfu = 1; break; default: @@ -95,14 +98,14 @@ main(int argc, char *argv[]) /* custom template e.g. foo.XXXXXXXXXXXXXXXXXXXXX */ if (template != NULL) { if (slen(template, maxlen, &tlen) < 0) - err_no_cleanup(stfu, EINVAL, + err_exit(EINVAL, "invalid template"); for (p = template + tlen; p > template && *--p == 'X'; xc++); if (xc < 3) /* the gnu mktemp errs on less than 3 */ - err_no_cleanup(stfu, EINVAL, + err_exit(EINVAL, "template must have 3 X or more on end (12+ advised"); } @@ -116,31 +119,37 @@ main(int argc, char *argv[]) if (tmpdir != NULL) { rp = realpath(tmpdir, resolved); if (rp == NULL) - err_no_cleanup(stfu, errno, "%s", tmpdir); + err_exit(errno, "%s", tmpdir); tmpdir = resolved; } if (new_tmp_common(&fd, &s, type, tmpdir, template) < 0) - err_no_cleanup(stfu, errno, "%s", s); + err_exit(errno, "%s", s); xpledgex("stdio", NULL); if (s == NULL) - err_no_cleanup(stfu, EFAULT, "bad string initialisation"); + err_exit(EFAULT, "bad string initialisation"); if (*s == '\0') - err_no_cleanup(stfu, EFAULT, "empty string initialisation"); + err_exit(EFAULT, "empty string initialisation"); if (slen(s, maxlen, &len) < 0) - err_no_cleanup(stfu, EFAULT, "unterminated string initialisiert"); + err_exit(EFAULT, "unterminated string initialisiert"); printf("%s\n", s); return EXIT_SUCCESS; err_usage: - err_no_cleanup(stfu, EINVAL, + err_exit(EINVAL, "usage: %s [-d] [-p dir] [template]\n", getnvmprogname()); +} + +static void +exit_cleanup(void) +{ + return; }/* ( >:3 ) |
