From d2abde53033d58b6665becd75f854ad87aba33f6 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 31 Mar 2026 15:43:43 +0100 Subject: libreboot-utils: stricter errno handling where possible, try not to clobber sys errno. override it only when relatively safe. also: when a syscall succeeds, it may set errno. this is rare, but permitted (nothing specified against it in specs, and the specs say that errno is undefined on success). i'm not libc, but i'm wrapping around it, so i need to be careful in how i handle the errno value. also: i removed the requirement for directories to be executable, in mkhtemp.c, because this isn't required and will only break certain setups. in world_writeable and sticky, i made the checks stricter: the faccessat check was being skipped on some paths, so i've closed that loophole now. i also generally cleaned up some code, as part of the errno handling refactoring, where it made sense to do so, plus a few other bits of code cleanup. Signed-off-by: Leah Rowe --- util/libreboot-utils/include/common.h | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'util/libreboot-utils/include') diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index bc0eb3b3..48831ea3 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -26,6 +26,24 @@ #include #endif +/* dangerously cool macros: + */ + +#define SUCCESS(x) ((x) >= 0) + +/* syscalls can set errno even on success; this + * is rare, but permitted. in various functions, we + * reset errno on success, to what the caller had, + * but we must still honour what was returned. + * + * lib/file.c is littered with examples + */ +#define reset_caller_errno(return_value) \ + do { \ + if (SUCCESS(return_value) && (!errno)) \ + errno = saved_errno; \ + } while (0) + #define items(x) (sizeof((x)) / sizeof((x)[0])) /* system prototypes @@ -464,7 +482,7 @@ ssize_t rw_gbe_file_exact(int fd, unsigned char *mem, size_t nrw, int fsync_dir(const char *path); ssize_t rw_file_exact(int fd, unsigned char *mem, size_t len, off_t off, int rw_type, size_t max_retries, int off_reset); -ssize_t prw(int fd, void *mem, size_t nrw, +ssize_t rw(int fd, void *mem, size_t nrw, off_t off, int rw_type); int io_args(int fd, void *mem, size_t nrw, off_t off, int rw_type); @@ -489,7 +507,7 @@ int rw_retry(int saved_errno, ssize_t rval); */ void usage(void); -int set_errno(int saved_errno, int fallback); +int with_fallback_errno(int fallback); void err_exit(int nvm_errval, const char *msg, ...); func_t errhook(func_t ptr); /* hook function for cleanup on err */ const char *lbgetprogname(void); @@ -506,7 +524,7 @@ int new_tmpdir(int *fd, char **path, char *tmpdir, int new_tmp_common(int *fd, char **path, int type, char *tmpdir, const char *template); int mkhtemp_try_create(int dirfd, - struct stat *st_dir_initial, + struct stat *st_dir_first, char *fname_copy, char *p, size_t xc, @@ -515,7 +533,7 @@ int mkhtemp_try_create(int dirfd, int type); int mkhtemp_tmpfile_linux(int dirfd, - struct stat *st_dir_initial, + struct stat *st_dir_first, char *fname_copy, char *p, size_t xc, @@ -523,7 +541,7 @@ mkhtemp_tmpfile_linux(int dirfd, struct stat *st); int mkhtemp(int *fd, struct stat *st, char *template, int dirfd, const char *fname, - struct stat *st_dir_initial, int type); + struct stat *st_dir_first, int type); int world_writeable_and_sticky(const char *s, int sticky_allowed, int always_sticky); int same_dir(const char *a, const char *b); -- cgit v1.2.1