mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-03-25 13:29:03 +02:00
mkhtemp: show progname on error
i have my own getprogname implementation, because not every libc is good enough to include one. Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
@@ -547,6 +547,7 @@ int mkdirat_on_eintr(int dirfd,
|
|||||||
const char *pathname, mode_t mode);
|
const char *pathname, mode_t mode);
|
||||||
int if_err(int condition, int errval);
|
int if_err(int condition, int errval);
|
||||||
int if_err_sys(int condition);
|
int if_err_sys(int condition);
|
||||||
|
char *lbgetprogname(char *argv0);
|
||||||
|
|
||||||
/* asserts */
|
/* asserts */
|
||||||
|
|
||||||
|
|||||||
@@ -164,32 +164,6 @@ err(int nvm_errval, const char *msg, ...)
|
|||||||
exit(EXIT_FAILURE);
|
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
|
int
|
||||||
exit_cleanup(void)
|
exit_cleanup(void)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -122,6 +122,8 @@ void
|
|||||||
err_no_cleanup(int nvm_errval, const char *msg, ...)
|
err_no_cleanup(int nvm_errval, const char *msg, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
int saved_errno = errno;
|
||||||
|
const char *p;
|
||||||
|
|
||||||
#if defined(__OpenBSD__) && defined(OpenBSD)
|
#if defined(__OpenBSD__) && defined(OpenBSD)
|
||||||
#if (OpenBSD) >= 509
|
#if (OpenBSD) >= 509
|
||||||
@@ -129,11 +131,11 @@ err_no_cleanup(int nvm_errval, const char *msg, ...)
|
|||||||
fprintf(stderr, "pledge failure during exit");
|
fprintf(stderr, "pledge failure during exit");
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!errno)
|
if (!errno)
|
||||||
errno = ECANCELED;
|
saved_errno = errno = ECANCELED;
|
||||||
|
|
||||||
fprintf(stderr, "nvmutil: ");
|
if ((p = getnvmprogname()) != NULL)
|
||||||
|
fprintf(stderr, "%s: ", p);
|
||||||
|
|
||||||
va_start(args, msg);
|
va_start(args, msg);
|
||||||
vfprintf(stderr, msg, args);
|
vfprintf(stderr, msg, args);
|
||||||
@@ -144,3 +146,61 @@ err_no_cleanup(int nvm_errval, const char *msg, ...)
|
|||||||
exit(EXIT_FAILURE);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -80,6 +80,9 @@ main(int argc, char *argv[])
|
|||||||
size_t maxlen = 4096;
|
size_t maxlen = 4096;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (lbgetprogname(argv[0]) == NULL)
|
||||||
|
err_no_cleanup(errno, "could not set progname");
|
||||||
|
|
||||||
/* https://man.openbsd.org/pledge.2 */
|
/* https://man.openbsd.org/pledge.2 */
|
||||||
#if defined(__OpenBSD__) && defined(OpenBSD)
|
#if defined(__OpenBSD__) && defined(OpenBSD)
|
||||||
#if (OpenBSD) >= 509
|
#if (OpenBSD) >= 509
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
size_t c;
|
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/pledge.2
|
||||||
https://man.openbsd.org/unveil.2 */
|
https://man.openbsd.org/unveil.2 */
|
||||||
#if defined(__OpenBSD__) && defined(OpenBSD)
|
#if defined(__OpenBSD__) && defined(OpenBSD)
|
||||||
|
|||||||
Reference in New Issue
Block a user