mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-19 09:32:27 +02:00
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>
This commit is contained in:
@@ -12,39 +12,44 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "include/common.h"
|
||||
|
||||
static void
|
||||
exit_cleanup(void);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct xstate *x;
|
||||
|
||||
struct commands *cmd;
|
||||
struct xfile *f;
|
||||
|
||||
size_t c;
|
||||
|
||||
(void) errhook(exit_cleanup);
|
||||
|
||||
if (lbgetprogname(argv[0]) == NULL)
|
||||
err_no_cleanup(0, errno, "could not set progname");
|
||||
err_exit(errno, "could not set progname");
|
||||
|
||||
xpledgex("stdio flock rpath wpath cpath unveil", NULL);
|
||||
xunveilx("/dev/urandom", "r");
|
||||
|
||||
#ifndef S_ISREG
|
||||
err_no_cleanup(0, ECANCELED,
|
||||
err_exit(ECANCELED,
|
||||
"Can't determine file types (S_ISREG undefined)");
|
||||
#endif
|
||||
#if ((CHAR_BIT) != 8)
|
||||
err_no_cleanup(0, ECANCELED, "Unsupported char size");
|
||||
err_exit(ECANCELED, "Unsupported char size");
|
||||
#endif
|
||||
|
||||
if ((x = xstart(argc, argv)) == NULL)
|
||||
err_no_cleanup(0, ECANCELED, "NULL state on init");
|
||||
err_exit(ECANCELED, "NULL state on init");
|
||||
|
||||
/* parse user command */
|
||||
/* TODO: CHECK ACCESSES VIA xstatus() */
|
||||
@@ -64,15 +69,12 @@ main(int argc, char *argv[])
|
||||
xpledgex("stdio flock rpath wpath cpath", NULL);
|
||||
|
||||
if (cmd->run == NULL)
|
||||
b0rk(errno, "Command not set");
|
||||
err_exit(errno, "Command not set");
|
||||
|
||||
sanitize_command_list();
|
||||
|
||||
open_gbe_file();
|
||||
|
||||
copy_gbe();
|
||||
read_checksums();
|
||||
|
||||
cmd->run();
|
||||
|
||||
for (c = 0; c < items(x->cmd); c++)
|
||||
@@ -81,13 +83,17 @@ main(int argc, char *argv[])
|
||||
if ((cmd->flags & O_ACCMODE) == O_RDWR)
|
||||
write_to_gbe_bin();
|
||||
|
||||
if (exit_cleanup() == -1)
|
||||
b0rk(EIO, "%s: close", f->fname);
|
||||
|
||||
exit_cleanup();
|
||||
if (f->io_err_gbe_bin)
|
||||
b0rk(EIO, "%s: error writing final file");
|
||||
err_exit(EIO, "%s: error writing final file");
|
||||
|
||||
free_and_set_null(&f->tname);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static void
|
||||
exit_cleanup(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user