summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils/lib')
-rw-r--r--util/libreboot-utils/lib/checksum.c4
-rw-r--r--util/libreboot-utils/lib/command.c54
-rw-r--r--util/libreboot-utils/lib/file.c44
-rw-r--r--util/libreboot-utils/lib/io.c78
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c36
-rw-r--r--util/libreboot-utils/lib/num.c5
-rw-r--r--util/libreboot-utils/lib/rand.c16
-rw-r--r--util/libreboot-utils/lib/state.c26
-rw-r--r--util/libreboot-utils/lib/string.c41
-rw-r--r--util/libreboot-utils/lib/usage.c2
-rw-r--r--util/libreboot-utils/lib/word.c2
11 files changed, 155 insertions, 153 deletions
diff --git a/util/libreboot-utils/lib/checksum.c b/util/libreboot-utils/lib/checksum.c
index 97b0efca..f71bcb4f 100644
--- a/util/libreboot-utils/lib/checksum.c
+++ b/util/libreboot-utils/lib/checksum.c
@@ -59,10 +59,10 @@ read_checksums(void)
if (_num_invalid >= _max_invalid) {
if (_max_invalid == 1)
- err_exit(ECANCELED, "%s: part %lu has a bad checksum",
+ exitf("%s: part %lu has a bad checksum",
f->fname, (size_t)f->part);
- err_exit(ECANCELED, "%s: No valid checksum found in file",
+ exitf("%s: No valid checksum found in file",
f->fname);
}
}
diff --git a/util/libreboot-utils/lib/command.c b/util/libreboot-utils/lib/command.c
index 5ccf9de9..526ad03b 100644
--- a/util/libreboot-utils/lib/command.c
+++ b/util/libreboot-utils/lib/command.c
@@ -43,24 +43,24 @@ sanitize_command_index(size_t c)
check_command_num(c);
if (cmd->argc < 3)
- err_exit(EINVAL, "cmd index %lu: argc below 3, %d",
+ exitf("cmd index %lu: argc below 3, %d",
(size_t)c, cmd->argc);
if (cmd->str == NULL)
- err_exit(EINVAL, "cmd index %lu: NULL str",
+ exitf("cmd index %lu: NULL str",
(size_t)c);
if (*cmd->str == '\0')
- err_exit(EINVAL, "cmd index %lu: empty str",
+ exitf("cmd index %lu: empty str",
(size_t)c);
if (slen(cmd->str, MAX_CMD_LEN +1, &rval) > MAX_CMD_LEN) {
- err_exit(EINVAL, "cmd index %lu: str too long: %s",
+ exitf("cmd index %lu: str too long: %s",
(size_t)c, cmd->str);
}
if (cmd->run == NULL)
- err_exit(EINVAL, "cmd index %lu: cmd ptr null",
+ exitf("cmd index %lu: cmd ptr null",
(size_t)c);
check_bin(cmd->arg_part, "cmd.arg_part");
@@ -74,19 +74,19 @@ sanitize_command_index(size_t c)
case NVM_SIZE:
break;
default:
- err_exit(EINVAL, "Unsupported rw_size: %lu",
+ exitf("Unsupported rw_size: %lu",
(size_t)gbe_rw_size);
}
if (gbe_rw_size > GBE_PART_SIZE)
- err_exit(EINVAL, "rw_size larger than GbE part: %lu",
+ exitf("rw_size larger than GbE part: %lu",
(size_t)gbe_rw_size);
_flag = (cmd->flags & O_ACCMODE);
if (_flag != O_RDONLY &&
_flag != O_RDWR)
- err_exit(EINVAL, "invalid cmd.flags setting");
+ exitf("invalid cmd.flags setting");
}
void
@@ -114,7 +114,7 @@ set_cmd(int argc, char *argv[])
return;
}
- err_exit(EINVAL,
+ exitf(
"Too few args on command '%s'", cmd);
}
@@ -139,11 +139,11 @@ set_cmd_args(int argc, char *argv[])
/* Maintainer bug
*/
if (cmd->arg_part && argc < 4)
- err_exit(EINVAL,
+ exitf(
"arg_part set for command that needs argc4");
if (cmd->arg_part && i == CMD_SETMAC)
- err_exit(EINVAL,
+ exitf(
"arg_part set on CMD_SETMAC");
if (i == CMD_SETMAC) {
@@ -165,13 +165,13 @@ conv_argv_part_num(const char *part_str)
unsigned char ch;
if (part_str[0] == '\0' || part_str[1] != '\0')
- err_exit(EINVAL, "Partnum string '%s' wrong length", part_str);
+ exitf("Partnum string '%s' wrong length", part_str);
/* char signedness is implementation-defined
*/
ch = (unsigned char)part_str[0];
if (ch < '0' || ch > '1')
- err_exit(EINVAL, "Bad part number (%c)", ch);
+ exitf("Bad part number (%c)", ch);
return (size_t)(ch - '0');
}
@@ -180,7 +180,7 @@ void
check_command_num(size_t c)
{
if (!valid_command(c))
- err_exit(EINVAL, "Invalid run_cmd arg: %lu",
+ exitf("Invalid run_cmd arg: %lu",
(size_t)c);
}
@@ -196,7 +196,7 @@ valid_command(size_t c)
cmd = &x->cmd[c];
if (c != cmd->chk)
- err_exit(EINVAL,
+ exitf(
"Invalid cmd chk value (%lu) vs arg: %lu",
cmd->chk, c);
@@ -231,7 +231,7 @@ parse_mac_string(void)
size_t rval;
if (slen(x->mac.str, 18, &rval) != 17)
- err_exit(EINVAL, "MAC address is the wrong length");
+ exitf("MAC address is the wrong length");
memset(mac->mac_buf, 0, sizeof(mac->mac_buf));
@@ -239,10 +239,10 @@ parse_mac_string(void)
set_mac_byte(mac_byte);
if ((mac->mac_buf[0] | mac->mac_buf[1] | mac->mac_buf[2]) == 0)
- err_exit(EINVAL, "Must not specify all-zeroes MAC address");
+ exitf("Must not specify all-zeroes MAC address");
if (mac->mac_buf[0] & 1)
- err_exit(EINVAL, "Must not specify multicast MAC address");
+ exitf("Must not specify multicast MAC address");
}
void
@@ -260,7 +260,7 @@ set_mac_byte(size_t mac_byte_pos)
if (mac_str_pos < 15) {
if ((separator = mac->str[mac_str_pos + 2]) != ':')
- err_exit(EINVAL, "Invalid MAC address separator '%c'",
+ exitf("Invalid MAC address separator '%c'",
separator);
}
@@ -282,9 +282,9 @@ set_mac_nib(size_t mac_str_pos,
if ((hex_num = hextonum(mac_ch)) > 15) {
if (hex_num >= 17)
- err_exit(EIO, "Randomisation failure");
+ exitf("Randomisation failure");
else
- err_exit(EINVAL, "Invalid character '%c'",
+ exitf("Invalid character '%c'",
mac->str[mac_str_pos + mac_nib_pos]);
}
@@ -467,7 +467,7 @@ cat(size_t nff)
if ((size_t)x->cat != nff) {
- err_exit(ECANCELED, "erroneous call to cat");
+ exitf("erroneous call to cat");
}
fflush(NULL);
@@ -490,11 +490,11 @@ void
cat_buf(unsigned char *b)
{
if (b == NULL)
- err_exit(errno, "null pointer in cat command");
+ exitf("null pointer in cat command");
if (rw_file_exact(STDOUT_FILENO, b,
- GBE_PART_SIZE, 0, IO_WRITE, MAX_ZERO_RW_RETRY, OFF_ERR) < 0)
- err_exit(errno, "stdout: cat");
+ GBE_PART_SIZE, 0, IO_WRITE, MAX_ZERO_RW_RETRY) < 0)
+ exitf("stdout: cat");
}
void
check_cmd(void (*fn)(void),
@@ -504,7 +504,7 @@ check_cmd(void (*fn)(void),
size_t i = x->i;
if (x->cmd[i].run != fn)
- err_exit(ECANCELED, "Running %s, but cmd %s is set",
+ exitf("Running %s, but cmd %s is set",
name, x->cmd[i].str);
/* prevent second command
@@ -516,6 +516,6 @@ check_cmd(void (*fn)(void),
void
cmd_helper_err(void)
{
- err_exit(ECANCELED,
+ exitf(
"Erroneously running command twice");
}
diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c
index 805db726..685cdf9a 100644
--- a/util/libreboot-utils/lib/file.c
+++ b/util/libreboot-utils/lib/file.c
@@ -16,6 +16,9 @@ more correct usage example:
long max = pathconf("/", _PC_PATH_MAX);
*/
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
#include <sys/types.h>
#include <sys/stat.h>
@@ -31,6 +34,7 @@ long max = pathconf("/", _PC_PATH_MAX);
#ifdef __linux__
#include <linux/openat2.h>
#include <sys/syscall.h>
+long syscall(long number, ...);
#endif
#include "../include/common.h"
@@ -137,7 +141,7 @@ err_fsync_dir:
ssize_t
rw_file_exact(int fd, unsigned char *mem, size_t nrw,
- off_t off, int rw_type, size_t max_retries, int off_reset)
+ off_t off, int rw_type, size_t max_retries)
{
int saved_errno = errno;
ssize_t rval = 0;
@@ -207,7 +211,6 @@ rw(int fd, void *mem, size_t nrw,
{
ssize_t rval = 0;
ssize_t r = -1;
- struct stat st;
int saved_errno = errno;
errno = 0;
@@ -566,11 +569,11 @@ open_file_on_eintr(const char *path,
errno = 0;
if (path == NULL)
- err_exit(EINVAL, "open_file_on_eintr: null path");
+ exitf("open_file_on_eintr: null path");
if (fd == NULL)
- err_exit(EFAULT, "%s: open_file_on_eintr: null fd ptr", path);
+ exitf("%s: open_file_on_eintr: null fd ptr", path);
if (*fd >= 0)
- err_exit(EBADF,
+ exitf(
"%s: open_file_on_eintr: file already open", path);
errno = 0;
@@ -578,7 +581,7 @@ open_file_on_eintr(const char *path,
rval = open(path, flags, mode)));
if (rval < 0)
- err_exit(errno,
+ exitf(
"%s: open_file_on_eintr: could not close", path);
reset_caller_errno(rval);
@@ -594,14 +597,14 @@ open_file_on_eintr(const char *path,
if (st != NULL) {
if (fstat(*fd, st) < 0)
- err_exit(errno, "%s: stat", path);
+ exitf("%s: stat", path);
if (!S_ISREG(st->st_mode))
- err_exit(errno, "%s: not a regular file", path);
+ exitf("%s: not a regular file", path);
}
- if (lseek_on_eintr(*fd, 0, SEEK_CUR, 1, 1) == (off_t)-1)
- err_exit(errno, "%s: file not seekable", path);
+ if (lseek_on_eintr(*fd, 0, SEEK_CUR) == (off_t)-1)
+ exitf("%s: file not seekable", path);
errno = saved_errno; /* see previous comment */
}
@@ -662,8 +665,7 @@ openat_on_eintr(int dirfd, const char *path,
#endif
off_t
-lseek_on_eintr(int fd, off_t off, int whence,
- int loop_eagain, int loop_eintr)
+lseek_on_eintr(int fd, off_t off, int whence)
{
int saved_errno = errno;
off_t rval = 0;
@@ -803,7 +805,7 @@ close_on_eintr(int *fd)
int rval = 0;
if (fd == NULL)
- err_exit(EINVAL, "close_on_eintr: null pointer");
+ exitf("close_on_eintr: null pointer");
if (*fd < 0)
return;
@@ -812,7 +814,7 @@ close_on_eintr(int *fd)
rval = close(*fd)));
if (rval < 0)
- err_exit(errno, "close_on_eintr: could not close");
+ exitf("close_on_eintr: could not close");
*fd = -1;
@@ -826,12 +828,14 @@ close_on_eintr(int *fd)
* ONE MACRO TO RULE THEM ALL:
*/
#define fs_err_retry() \
- if ((rval == -1) && \
- (errno == EINTR)) \
- return 1; \
- if (rval >= 0 && !errno) \
- errno = saved_errno; \
- return 0
+ do { \
+ if ((rval == -1) && \
+ (errno == EINTR)) \
+ return 1; \
+ if (rval >= 0 && !errno) \
+ errno = saved_errno; \
+ return 0; \
+ } while(0)
/*
* Regarding the errno logic above:
* on success, it is permitted that
diff --git a/util/libreboot-utils/lib/io.c b/util/libreboot-utils/lib/io.c
index 9bbf1f30..f8e67977 100644
--- a/util/libreboot-utils/lib/io.c
+++ b/util/libreboot-utils/lib/io.c
@@ -28,6 +28,8 @@ open_gbe_file(void)
struct xstate *x = xstatus();
struct commands *cmd = &x->cmd[x->i];
struct xfile *f = &x->f;
+ int saved_errno = errno;
+ errno = 0;
int _flags;
@@ -39,16 +41,17 @@ open_gbe_file(void)
&f->gbe_st);
if (f->gbe_st.st_nlink > 1)
- err_exit(EINVAL,
+ exitf(
"%s: warning: file has multiple (%lu) hard links\n",
f->fname, (size_t)f->gbe_st.st_nlink);
if (f->gbe_st.st_nlink == 0)
- err_exit(EIO, "%s: file unlinked while open", f->fname);
+ exitf("%s: file unlinked while open", f->fname);
- _flags = fcntl(f->gbe_fd, F_GETFL);
+ while (fs_retry(saved_errno,
+ _flags = fcntl(f->gbe_fd, F_GETFL)));
if (_flags == -1)
- err_exit(errno, "%s: fcntl(F_GETFL)", f->fname);
+ exitf("%s: fcntl(F_GETFL)", f->fname);
/* O_APPEND allows POSIX write() to ignore
* the current write offset and write at EOF,
@@ -56,7 +59,7 @@ open_gbe_file(void)
*/
if (_flags & O_APPEND)
- err_exit(EIO, "%s: O_APPEND flag", f->fname);
+ exitf("%s: O_APPEND flag", f->fname);
f->gbe_file_size = f->gbe_st.st_size;
@@ -66,14 +69,16 @@ open_gbe_file(void)
case SIZE_128KB:
break;
default:
- err_exit(EINVAL, "File size must be 8KB, 16KB or 128KB");
+ exitf("File size must be 8KB, 16KB or 128KB");
}
/* currently fails (EBADF), locks are advisory anyway: */
/*
if (lock_file(f->gbe_fd, cmd->flags) == -1)
- err_exit(errno, "%s: can't lock", f->fname);
+ exitf("%s: can't lock", f->fname);
*/
+
+ reset_caller_errno(0);
}
void
@@ -104,44 +109,44 @@ read_file(void)
/* read main file
*/
_r = rw_file_exact(f->gbe_fd, f->buf, f->gbe_file_size,
- 0, IO_PREAD, MAX_ZERO_RW_RETRY, OFF_ERR);
+ 0, IO_PREAD, MAX_ZERO_RW_RETRY);
if (_r < 0)
- err_exit(errno, "%s: read failed", f->fname);
+ exitf("%s: read failed", f->fname);
/* copy to tmpfile
*/
_r = rw_file_exact(f->tmp_fd, f->buf, f->gbe_file_size,
- 0, IO_PWRITE, MAX_ZERO_RW_RETRY, OFF_ERR);
+ 0, IO_PWRITE, MAX_ZERO_RW_RETRY);
if (_r < 0)
- err_exit(errno, "%s: %s: copy failed",
+ exitf("%s: %s: copy failed",
f->fname, f->tname);
/* file size comparison
*/
if (fstat(f->tmp_fd, &_st) == -1)
- err_exit(errno, "%s: stat", f->tname);
+ exitf("%s: stat", f->tname);
f->gbe_tmp_size = _st.st_size;
if (f->gbe_tmp_size != f->gbe_file_size)
- err_exit(EIO, "%s: %s: not the same size",
+ exitf("%s: %s: not the same size",
f->fname, f->tname);
/* needs sync, for verification
*/
if (fsync_on_eintr(f->tmp_fd) == -1)
- err_exit(errno, "%s: fsync (tmpfile copy)", f->tname);
+ exitf("%s: fsync (tmpfile copy)", f->tname);
_r = rw_file_exact(f->tmp_fd, f->bufcmp, f->gbe_file_size,
- 0, IO_PREAD, MAX_ZERO_RW_RETRY, OFF_ERR);
+ 0, IO_PREAD, MAX_ZERO_RW_RETRY);
if (_r < 0)
- err_exit(errno, "%s: read failed (cmp)", f->tname);
+ exitf("%s: read failed (cmp)", f->tname);
if (vcmp(f->buf, f->bufcmp, f->gbe_file_size) != 0)
- err_exit(errno, "%s: %s: read contents differ (pre-test)",
+ exitf("%s: %s: read contents differ (pre-test)",
f->fname, f->tname);
}
@@ -159,10 +164,10 @@ write_gbe_file(void)
return;
if (same_file(f->tmp_fd, &f->tmp_st, 0) < 0)
- err_exit(errno, "%s: file inode/device changed", f->tname);
+ exitf("%s: file inode/device changed", f->tname);
if (same_file(f->gbe_fd, &f->gbe_st, 1) < 0)
- err_exit(errno, "%s: file has changed", f->fname);
+ exitf("%s: file has changed", f->fname);
update_checksum = cmd->chksum_write;
@@ -195,7 +200,7 @@ rw_gbe_file_part(size_t p, int rw_type,
gbe_rw_size = cmd->rw_size;
if (rw_type < IO_PREAD || rw_type > IO_PWRITE)
- err_exit(errno, "%s: %s: part %lu: invalid rw_type, %d",
+ exitf("%s: %s: part %lu: invalid rw_type, %d",
f->fname, rw_type_str, (size_t)p, rw_type);
mem_offset = gbe_mem_offset(p, rw_type_str);
@@ -205,11 +210,11 @@ rw_gbe_file_part(size_t p, int rw_type,
gbe_rw_size, file_offset, rw_type);
if (rval == -1)
- err_exit(errno, "%s: %s: part %lu",
+ exitf("%s: %s: part %lu",
f->fname, rw_type_str, (size_t)p);
if ((size_t)rval != gbe_rw_size)
- err_exit(EIO, "%s: partial %s: part %lu",
+ exitf("%s: partial %s: part %lu",
f->fname, rw_type_str, (size_t)p);
}
@@ -233,7 +238,7 @@ write_to_gbe_bin(void)
*/
if (fsync_on_eintr(f->tmp_fd) == -1)
- err_exit(errno, "%s: fsync (pre-verification)",
+ exitf("%s: fsync (pre-verification)",
f->tname);
check_written_part(0);
@@ -242,7 +247,7 @@ write_to_gbe_bin(void)
report_io_err_rw();
if (f->io_err_gbe)
- err_exit(EIO, "%s: bad write", f->fname);
+ exitf("%s: bad write", f->fname);
saved_errno = errno;
@@ -314,10 +319,10 @@ check_written_part(size_t p)
memset(f->pad, 0xff, sizeof(f->pad));
if (same_file(f->tmp_fd, &f->tmp_st, 0) < 0)
- err_exit(errno, "%s: file inode/device changed", f->tname);
+ exitf("%s: file inode/device changed", f->tname);
if (same_file(f->gbe_fd, &f->gbe_st, 1) < 0)
- err_exit(errno, "%s: file changed during write", f->fname);
+ exitf("%s: file changed during write", f->fname);
rval = rw_gbe_file_exact(f->tmp_fd, f->pad,
gbe_rw_size, file_offset, IO_PREAD);
@@ -419,24 +424,10 @@ gbe_mv(void)
int saved_errno;
int tmp_gbe_bin_exists;
- char *dest_tmp;
- int dest_fd = -1;
-
- char *dir = NULL;
- char *base = NULL;
- char *dest_name = NULL;
-
- int dirfd = -1;
-
- struct stat st_dir;
-
/* will be set 0 if it doesn't
*/
tmp_gbe_bin_exists = 1;
- dest_tmp = NULL;
- dest_fd = -1;
-
saved_errno = errno;
rval = fs_rename_at(f->dirfd, f->tmpbase,
@@ -445,7 +436,6 @@ gbe_mv(void)
if (rval > -1)
tmp_gbe_bin_exists = 0;
-ret_gbe_mv:
if (f->gbe_fd > -1) {
close_on_eintr(&f->gbe_fd);
@@ -527,11 +517,11 @@ gbe_x_offset(size_t p, const char *f_op, const char *d_type,
off = ((off_t)p) * (off_t)nsize;
if (off > ncmp - GBE_PART_SIZE)
- err_exit(ECANCELED, "%s: GbE %s %s out of bounds",
+ exitf("%s: GbE %s %s out of bounds",
f->fname, d_type, f_op);
if (off != 0 && off != ncmp >> 1)
- err_exit(ECANCELED, "%s: GbE %s %s at bad offset",
+ exitf("%s: GbE %s %s at bad offset",
f->fname, d_type, f_op);
return off;
@@ -567,7 +557,7 @@ rw_gbe_file_exact(int fd, unsigned char *mem, size_t nrw,
goto err_rw_gbe_file_exact;
r = rw_file_exact(fd, mem, nrw, off, rw_type,
- MAX_ZERO_RW_RETRY, OFF_ERR);
+ MAX_ZERO_RW_RETRY);
return rw_over_nrw(r, nrw);
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index c1574634..46f6419f 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -4,9 +4,8 @@
* Hardened mktemp (be nice to the demon).
*/
-#if defined(__linux__) && !defined(_GNU_SOURCE)
-/* for openat2 syscall on linux */
-#define _GNU_SOURCE 1
+#if !defined(_POSIX_C_SOURCE)
+#define _POSIX_C_SOURCE 200809L
#endif
#include <sys/types.h>
@@ -58,10 +57,8 @@ new_tmp_common(int *fd, char **path, int type,
struct stat st;
const char *templatestr;
- size_t templatestr_len;
size_t dirlen;
- size_t destlen;
char *dest = NULL; /* final path (will be written into "path") */
int saved_errno = errno;
int dirfd = -1;
@@ -114,9 +111,8 @@ new_tmp_common(int *fd, char **path, int type,
else
templatestr = "tmp.XXXXXXXXXX";
- /* may as well calculate in advance */
- destlen = slen(tmpdir, PATH_MAX, &dirlen) + 1
- + slen(templatestr, PATH_MAX, &templatestr_len);
+ /* may as well calculate in advance */
+ dirlen = slen(tmpdir, PATH_MAX, &dirlen);
/* full path: */
dest = scatn(3, (const char *[]) { tmpdir, "/", templatestr },
PATH_MAX, &dest);
@@ -441,7 +437,6 @@ mkhtemp(int *fd,
size_t retries;
- int close_errno;
int saved_errno = errno;
int r;
@@ -699,6 +694,10 @@ mkhtemp_tmpfile_linux(int dirfd,
linked = 1; /* file created */
+ /* TODO: potential fd leak here.
+ * probably should only set *fd on successful
+ * return from this function (see below)
+ */
if (fd_verify_dir_identity(dirfd, st_dir_first) < 0 ||
fstat(*fd = tmpfd, st) < 0 ||
secure_file(fd, st, st, O_APPEND, 1, 1, 0600) < 0)
@@ -740,15 +739,21 @@ int secure_file(int *fd,
int do_lock,
mode_t mode)
{
- int flags;
+ int flags = -1;
struct stat st_now;
int saved_errno = errno;
errno = 0;
if (if_err(fd == NULL || st == NULL, EFAULT) ||
- if_err(*fd < 0, EBADF) ||
- if_err_sys((flags = fcntl(*fd, F_GETFL)) == -1) ||
- if_err(bad_flags > 0 && (flags & bad_flags), EPERM))
+ if_err(*fd < 0, EBADF))
+ goto err_demons;
+
+ while (fs_retry(saved_errno,
+ flags = fcntl(*fd, F_GETFL)));
+ if (flags == -1)
+ goto err_demons;
+
+ if (if_err(bad_flags > 0 && (flags & bad_flags), EPERM))
goto err_demons;
if (expected != NULL) {
@@ -874,6 +879,7 @@ lock_file(int fd, int flags)
{
struct flock fl;
int saved_errno = errno;
+ int fcntl_rval = -1;
errno = 0;
if (if_err(fd < 0, EBADF) ||
@@ -889,7 +895,9 @@ lock_file(int fd, int flags)
fl.l_whence = SEEK_SET;
- if (fcntl(fd, F_SETLK, &fl) == -1)
+ while (fs_retry(saved_errno,
+ fcntl_rval = fcntl(fd, F_SETLK, &fl)));
+ if (fcntl_rval == -1)
goto err_lock_file;
reset_caller_errno(0);
diff --git a/util/libreboot-utils/lib/num.c b/util/libreboot-utils/lib/num.c
index ad349173..ce5e420d 100644
--- a/util/libreboot-utils/lib/num.c
+++ b/util/libreboot-utils/lib/num.c
@@ -29,7 +29,6 @@ unsigned short
hextonum(char ch_s)
{
unsigned char ch;
- size_t rval;
ch = (unsigned char)ch_s;
@@ -81,7 +80,7 @@ spew_hex(const void *data, size_t len)
if (len <= 4294967296) /* below 4GB */
printf("%08zx ", i);
else
- printf("%0*zx ", sizeof(size_t) * 2, i);
+ printf("%16zu ", i);
for (j = 0; j < 16; j++) {
@@ -112,6 +111,6 @@ void
check_bin(size_t a, const char *a_name)
{
if (a > 1)
- err_exit(EINVAL, "%s must be 0 or 1, but is %lu",
+ exitf("%s must be 0 or 1, but is %lu",
a_name, (size_t)a);
}
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index adfad3d7..8c3fbf4c 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -12,19 +12,16 @@
#endif
#include <sys/types.h>
-#ifndef USE_URANDOM
-#define USE_URANDOM 0
-#endif
-
-#include <errno.h>
#if defined(USE_URANDOM) && \
((USE_URANDOM) > 0)
#include <fcntl.h> /* if not arc4random: /dev/urandom */
#elif defined(__linux__)
#include <sys/random.h>
#include <sys/syscall.h>
+long syscall(long number, ...);
#endif
+#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
@@ -107,7 +104,7 @@ rsize(size_t n)
{
size_t rval = SIZE_MAX;
if (!n)
- err_exit(EFAULT, "rsize: division by zero");
+ exitf("rsize: division by zero");
/* rejection sampling (clamp rand to eliminate modulo bias) */
for (; rval >= SIZE_MAX - (SIZE_MAX % n); rset(&rval, sizeof(rval)));
@@ -133,7 +130,7 @@ rset(void *buf, size_t n)
goto err;
if (n == 0)
- err_exit(EPERM, "rset: zero-byte request");
+ exitf("rset: zero-byte request");
#if (defined(__OpenBSD__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__APPLE__) || \
@@ -144,7 +141,7 @@ rset(void *buf, size_t n)
#else
size_t off = 0;
-retry_rand:
+retry_rand: {
#if defined(USE_URANDOM) && \
((USE_URANDOM) > 0)
@@ -178,6 +175,7 @@ retry_rand:
((USE_URANDOM) > 0)
close_on_eintr(&fd);
#endif
+}
#endif
reset_caller_errno(0);
@@ -188,7 +186,7 @@ err:
close_on_eintr(&fd);
#endif
(void) with_fallback_errno(ECANCELED);
- err_exit(errno, "Randomisierungsfehler");
+ exitf("Randomisierungsfehler");
exit(EXIT_FAILURE);
}
#endif
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c
index f32d4078..a7c85bfb 100644
--- a/util/libreboot-utils/lib/state.c
+++ b/util/libreboot-utils/lib/state.c
@@ -4,6 +4,10 @@
* State machine (singleton) for nvmutil data.
*/
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
+
#include <sys/types.h>
#include <sys/stat.h>
@@ -92,9 +96,9 @@ xstart(int argc, char *argv[])
return &us;
if (argc < 3)
- err_exit(EINVAL, "xstart: Too few arguments");
+ exitf("xstart: Too few arguments");
if (argv == NULL)
- err_exit(EINVAL, "xstart: NULL argv");
+ exitf("xstart: NULL argv");
first_run = 0;
@@ -106,11 +110,11 @@ xstart(int argc, char *argv[])
us.f.tname = NULL;
if ((realdir = realpath(us.f.fname, NULL)) == NULL)
- err_exit(errno, "xstart: can't get realpath of %s",
+ exitf("xstart: can't get realpath of %s",
us.f.fname);
if (fs_dirname_basename(realdir, &dir, &base, 0) < 0)
- err_exit(errno, "xstart: don't know CWD of %s",
+ exitf("xstart: don't know CWD of %s",
us.f.fname);
sdup(base, PATH_MAX, &us.f.base);
@@ -118,26 +122,26 @@ xstart(int argc, char *argv[])
us.f.dirfd = fs_open(dir,
O_RDONLY | O_DIRECTORY);
if (us.f.dirfd < 0)
- err_exit(errno, "%s: open dir", dir);
+ exitf("%s: open dir", dir);
if (new_tmpfile(&us.f.tmp_fd, &us.f.tname, dir, ".gbe.XXXXXXXXXX") < 0)
- err_exit(errno, "%s", us.f.tname);
+ exitf("%s", us.f.tname);
if (fs_dirname_basename(us.f.tname,
&tmpdir, &tmpbase_local, 0) < 0)
- err_exit(errno, "tmp basename");
+ exitf("tmp basename");
sdup(tmpbase_local, PATH_MAX, &us.f.tmpbase);
free_and_set_null(&tmpdir);
if (us.f.tname == NULL)
- err_exit(errno, "x->f.tname null");
+ exitf("x->f.tname null");
if (*us.f.tname == '\0')
- err_exit(errno, "x->f.tname empty");
+ exitf("x->f.tname empty");
if (fstat(us.f.tmp_fd, &us.f.tmp_st) < 0)
- err_exit(errno, "%s: stat", us.f.tname);
+ exitf("%s: stat", us.f.tname);
memset(us.f.real_buf, 0, sizeof(us.f.real_buf));
memset(us.f.bufcmp, 0, sizeof(us.f.bufcmp));
@@ -154,7 +158,7 @@ xstatus(void)
struct xstate *x = xstart(0, NULL);
if (x == NULL)
- err_exit(EACCES, "NULL pointer to xstate");
+ exitf("NULL pointer to xstate");
return x;
}
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c
index ce54a524..5e0b4c33 100644
--- a/util/libreboot-utils/lib/string.c
+++ b/util/libreboot-utils/lib/string.c
@@ -54,7 +54,7 @@ pagesize(void)
if (!set) {
if ((rval = sysconf(_SC_PAGESIZE)) < 0)
- err_exit(errno, "could not determine page size");
+ exitf("could not determine page size");
set = 1;
}
@@ -66,7 +66,7 @@ void
free_and_set_null(char **buf)
{
if (buf == NULL)
- err_exit(EFAULT,
+ exitf(
"null ptr (to ptr for freeing) in free_and_set_null");
if (*buf == NULL)
@@ -99,9 +99,9 @@ vmalloc(void **buf, size_t size)
errno = 0;
if (size >= SIZE_MAX - 1)
- err_exit(EOVERFLOW, "integer overflow in vmalloc");
+ exitf("integer overflow in vmalloc");
if (buf == NULL)
- err_exit(EFAULT, "Bad pointer passed to vmalloc");
+ exitf("Bad pointer passed to vmalloc");
/* lots of programs will
* re-initialise a buffer
@@ -111,14 +111,14 @@ vmalloc(void **buf, size_t size)
* force the programmer to behave
*/
if (*buf != NULL)
- err_exit(EFAULT, "Non-null pointer given to vmalloc");
+ exitf("Non-null pointer given to vmalloc");
if (!size)
- err_exit(EFAULT,
+ exitf(
"Tried to vmalloc(0) and that is very bad. Fix it now");
if ((rval = malloc(size)) == NULL)
- err_exit(errno, "malloc fail in vmalloc");
+ exitf("malloc fail in vmalloc");
reset_caller_errno(0);
return *buf = rval;
@@ -181,7 +181,7 @@ err:
if (rval != NULL)
*rval = -1;
- err_exit(errno, "scmp");
+ exitf("scmp");
return -1;
out:
reset_caller_errno(0);
@@ -195,7 +195,7 @@ int ccmp(const char *a, const char *b,
unsigned char bc;
if (if_err(a == NULL || b == NULL || rval == NULL, EFAULT))
- err_exit(errno, "ccmp");
+ exitf("ccmp");
ac = (unsigned char)a[i];
bc = (unsigned char)b[i];
@@ -263,7 +263,7 @@ err:
if (rval != NULL)
*rval = 0;
- err_exit(errno, "slen"); /* abort */
+ exitf("slen"); /* abort */
return 0; /* gcc15 is happy */
out:
reset_caller_errno(0);
@@ -335,7 +335,7 @@ err:
*dest = NULL;
(void) with_fallback_errno(EFAULT);
- err_exit(errno, "sdup");
+ exitf("sdup");
return NULL;
out:
@@ -352,7 +352,7 @@ scatn(ssize_t sc, const char **sv,
char *final = NULL;
char *rcur = NULL;
char *rtmp = NULL;
- size_t i;
+ ssize_t i;
errno = 0;
if (if_err(sc < 2, EINVAL) ||
@@ -387,7 +387,7 @@ err:
(void) with_fallback_errno(EFAULT);
- err_exit(errno, "scatn");
+ exitf("scatn");
return NULL;
}
@@ -425,7 +425,7 @@ err:
(void) with_fallback_errno(EINVAL);
if (dest != NULL)
*dest = NULL;
- err_exit(errno, "scat");
+ exitf("scat");
return NULL;
}
@@ -471,7 +471,7 @@ err:
free_and_set_null(&rval2);
(void) with_fallback_errno(EINVAL);
- err_exit(errno, "dcat");
+ exitf("dcat");
}
/* because no libc reimagination is complete
@@ -491,7 +491,7 @@ vcmp(const void *s1, const void *s2, size_t n)
errno = 0;
if (if_err(s1 == NULL || s2 == NULL, EFAULT))
- err_exit(EFAULT, "vcmp: null input");
+ exitf("vcmp: null input");
x = s1;
y = s2;
@@ -529,11 +529,10 @@ with_fallback_errno(int fallback)
/* the one for nvmutil state is in state.c */
/* this one just exits */
void
-err_exit(int nvm_errval, const char *msg, ...)
+exitf(const char *msg, ...)
{
va_list args;
int saved_errno = errno;
- const char *p;
func_t err_cleanup = errhook(NULL);
err_cleanup();
@@ -561,7 +560,7 @@ err_exit(int nvm_errval, const char *msg, ...)
* e.g. you might want to
* close some files, depending
* on your program.
- * see: err_exit()
+ * see: exitf()
*/
func_t errhook(func_t ptr)
{
@@ -632,7 +631,7 @@ xpledgex(const char *promises, const char *execpromises)
errno = 0;
#ifdef __OpenBSD__
if (pledge(promises, execpromises) == -1)
- err_exit(errno, "pledge");
+ exitf("pledge");
#endif
reset_caller_errno(0);
return 0;
@@ -645,7 +644,7 @@ xunveilx(const char *path, const char *permissions)
errno = 0;
#ifdef __OpenBSD__
if (pledge(promises, execpromises) == -1)
- err_exit(errno, "pledge");
+ exitf("pledge");
#endif
reset_caller_errno(0);
return 0;
diff --git a/util/libreboot-utils/lib/usage.c b/util/libreboot-utils/lib/usage.c
index 7c9fa34b..4ade2f9e 100644
--- a/util/libreboot-utils/lib/usage.c
+++ b/util/libreboot-utils/lib/usage.c
@@ -26,5 +26,5 @@ usage(void)
util, util, util, util,
util, util, util);
- err_exit(EINVAL, "Too few arguments");
+ exitf("Too few arguments");
}
diff --git a/util/libreboot-utils/lib/word.c b/util/libreboot-utils/lib/word.c
index 85e1d88b..45ac3d48 100644
--- a/util/libreboot-utils/lib/word.c
+++ b/util/libreboot-utils/lib/word.c
@@ -63,6 +63,6 @@ check_nvm_bound(size_t c, size_t p)
check_bin(p, "part number");
if (c >= NVM_WORDS)
- err_exit(ECANCELED, "check_nvm_bound: out of bounds %lu",
+ exitf("check_nvm_bound: out of bounds %lu",
(size_t)c);
}