summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils/lib/string.c')
-rw-r--r--util/libreboot-utils/lib/string.c83
1 files changed, 61 insertions, 22 deletions
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c
index 986c7b8e..76141c58 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;
}
@@ -201,14 +201,10 @@ scatn(ssize_t sc, const char **sv,
errno = saved_errno;
return 0;
err:
- if (ct != NULL)
- free(ct);
- if (size != NULL)
- free(size);
- if (errno == saved_errno)
- errno = EFAULT;
+ free_and_set_null(&ct);
+ free_and_set_null((char **)&size);
- return -1;
+ return set_errno(saved_errno, EFAULT);
}
/* strict strcat */
@@ -285,21 +281,32 @@ err:
return -1;
}
+/* on functions that return with errno,
+ * i sometimes have a default fallback,
+ * which is set if errno wasn't changed,
+ * under error condition.
+ */
+int
+set_errno(int saved_errno, int fallback)
+{
+ if (errno == saved_errno)
+ errno = fallback;
+ return -1;
+}
+
/* 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;
-#if defined(__OpenBSD__) && defined(OpenBSD)
-#if (OpenBSD) >= 509
- if (pledge("stdio", NULL) == -1)
- fprintf(stderr, "pledge failure during exit");
-#endif
-#endif
+ func_t err_cleanup = errhook(NULL);
+ err_cleanup();
+ errno = saved_errno;
+
if (!errno)
saved_errno = errno = ECANCELED;
@@ -318,6 +325,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)
{
@@ -363,6 +401,8 @@ lbgetprogname(char *argv0)
return progname;
}
+/* https://man.openbsd.org/pledge.2
+ https://man.openbsd.org/unveil.2 */
int
xpledgex(const char *promises, const char *execpromises)
{
@@ -370,12 +410,11 @@ 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;
}
-
int
xunveilx(const char *path, const char *permissions)
{
@@ -383,7 +422,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;