summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/libreboot-utils/include/common.h1
-rw-r--r--util/libreboot-utils/lib/state.c29
-rw-r--r--util/libreboot-utils/lib/string.c66
-rw-r--r--util/libreboot-utils/mkhtemp.c3
-rw-r--r--util/libreboot-utils/nvmutil.c3
5 files changed, 72 insertions, 30 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h
index b26448db..a632b993 100644
--- a/util/libreboot-utils/include/common.h
+++ b/util/libreboot-utils/include/common.h
@@ -547,6 +547,7 @@ int mkdirat_on_eintr(int dirfd,
const char *pathname, mode_t mode);
int if_err(int condition, int errval);
int if_err_sys(int condition);
+char *lbgetprogname(char *argv0);
/* asserts */
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c
index b7701f0d..2fc22d52 100644
--- a/util/libreboot-utils/lib/state.c
+++ b/util/libreboot-utils/lib/state.c
@@ -28,8 +28,9 @@ xstart(int argc, char *argv[])
static int first_run = 1;
static struct xstate us = {
- /* DO NOT MESS THIS UP, OR THERE WILL BE DEMONS */
{
+ /* be careful when modifying xstate. you
+ * must set everything precisely */
{
CMD_DUMP, "dump", cmd_helper_dump, ARGC_3,
ARG_NOPART,
@@ -163,32 +164,6 @@ err(int nvm_errval, const char *msg, ...)
exit(EXIT_FAILURE);
}
-const char *
-getnvmprogname(void)
-{
- struct xstate *x = xstatus();
-
- const char *p;
- static char fallback[] = "nvmutil";
-
- char *rval = fallback;
-
- if (x != NULL) {
-
- if (x->argv0 != NULL && *x->argv0 != '\0')
- rval = x->argv0;
- else
- return fallback;
- }
-
- p = strrchr(rval, '/');
-
- if (p)
- return p + 1;
- else
- return rval;
-}
-
int
exit_cleanup(void)
{
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c
index cb37c1ba..2f2be5f3 100644
--- a/util/libreboot-utils/lib/string.c
+++ b/util/libreboot-utils/lib/string.c
@@ -122,6 +122,8 @@ void
err_no_cleanup(int nvm_errval, const char *msg, ...)
{
va_list args;
+ int saved_errno = errno;
+ const char *p;
#if defined(__OpenBSD__) && defined(OpenBSD)
#if (OpenBSD) >= 509
@@ -129,11 +131,11 @@ err_no_cleanup(int nvm_errval, const char *msg, ...)
fprintf(stderr, "pledge failure during exit");
#endif
#endif
-
if (!errno)
- errno = ECANCELED;
+ saved_errno = errno = ECANCELED;
- fprintf(stderr, "nvmutil: ");
+ if ((p = getnvmprogname()) != NULL)
+ fprintf(stderr, "%s: ", p);
va_start(args, msg);
vfprintf(stderr, msg, args);
@@ -144,3 +146,61 @@ err_no_cleanup(int nvm_errval, const char *msg, ...)
exit(EXIT_FAILURE);
}
+const char *
+getnvmprogname(void)
+{
+ static char *rval = NULL;
+ static char *p;
+ static int setname = 0;
+
+ if (!setname) {
+ if ((rval = lbgetprogname(NULL)) == NULL)
+ return NULL;
+
+ p = strrchr(rval, '/');
+ if (p)
+ rval = p + 1;
+
+ setname = 1;
+ }
+
+ return rval;
+}
+
+/* singleton. if string not null,
+ sets the string. after set,
+ will not set anymore. either
+ way, returns the string
+ */
+char *
+lbgetprogname(char *argv0)
+{
+ static int setname = 0;
+ static char *progname = NULL;
+ size_t len;
+
+ if (!setname) {
+ if (if_err(argv0 == NULL || *argv0 == '\0', EFAULT) ||
+ slen(argv0, 4096, &len) < 0 ||
+ (progname = malloc(len + 1)) == NULL)
+ return NULL;
+
+ memcpy(progname, argv0, len + 1);
+ setname = 1;
+ }
+
+ return progname;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c
index 3e148a4a..b70a1884 100644
--- a/util/libreboot-utils/mkhtemp.c
+++ b/util/libreboot-utils/mkhtemp.c
@@ -80,6 +80,9 @@ main(int argc, char *argv[])
size_t maxlen = 4096;
#endif
+ if (lbgetprogname(argv[0]) == NULL)
+ err_no_cleanup(errno, "could not set progname");
+
/* https://man.openbsd.org/pledge.2 */
#if defined(__OpenBSD__) && defined(OpenBSD)
#if (OpenBSD) >= 509
diff --git a/util/libreboot-utils/nvmutil.c b/util/libreboot-utils/nvmutil.c
index fb45a97c..da7d40ec 100644
--- a/util/libreboot-utils/nvmutil.c
+++ b/util/libreboot-utils/nvmutil.c
@@ -35,6 +35,9 @@ main(int argc, char *argv[])
size_t c;
+ if (lbgetprogname(argv[0]) == NULL)
+ err_no_cleanup(errno, "could not set progname");
+
/* https://man.openbsd.org/pledge.2
https://man.openbsd.org/unveil.2 */
#if defined(__OpenBSD__) && defined(OpenBSD)