diff options
Diffstat (limited to 'util/libreboot-utils/lib/state.c')
| -rw-r--r-- | util/libreboot-utils/lib/state.c | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c index c2040144..e3cb0890 100644 --- a/util/libreboot-utils/lib/state.c +++ b/util/libreboot-utils/lib/state.c @@ -26,6 +26,11 @@ struct xstate * xstart(int argc, char *argv[]) { static int first_run = 1; + static char *dir = NULL; + static char *base = NULL; + char *realdir = NULL; + char *tmpdir = NULL; + char *tmpbase_local = NULL; static struct xstate us = { { @@ -107,9 +112,35 @@ xstart(int argc, char *argv[]) us.f.tmp_fd = -1; us.f.tname = NULL; - if (new_tmpfile(&us.f.tmp_fd, &us.f.tname) < 0) + if ((realdir = realpath(us.f.fname, NULL)) == NULL) + err_no_cleanup(errno, "xstart: can't get realpath of %s", + us.f.fname); + + if (fs_dirname_basename(realdir, &dir, &base, 0) < 0) + err_no_cleanup(errno, "xstart: don't know CWD of %s", + us.f.fname); + + if ((us.f.base = strdup(base)) == NULL) + err_no_cleanup(errno, "strdup base"); + + us.f.dirfd = fs_open(dir, + O_RDONLY | O_DIRECTORY); + if (us.f.dirfd < 0) + err_no_cleanup(errno, "%s: open dir", dir); + + if (new_tmpfile(&us.f.tmp_fd, &us.f.tname, dir) < 0) err_no_cleanup(errno, "%s", us.f.tname); + if (fs_dirname_basename(us.f.tname, + &tmpdir, &tmpbase_local, 0) < 0) + err_no_cleanup(errno, "tmp basename"); + + us.f.tmpbase = strdup(tmpbase_local); + if (us.f.tmpbase == NULL) + err_no_cleanup(errno, "strdup tmpbase"); + + free_if_null(&tmpdir); + if (us.f.tname == NULL) err_no_cleanup(errno, "x->f.tname null"); if (*us.f.tname == '\0') @@ -164,32 +195,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) { @@ -212,6 +217,10 @@ exit_cleanup(void) if (f->tname != NULL) if (unlink(f->tname) == -1) close_err = 1; + + close_no_err(&f->dirfd); + free_if_null(&f->base); + free_if_null(&f->tmpbase); } if (saved_errno) |
