Return an error from libuboot_open() if the lockfile cannot be open()'d
and thus no locking can be done. Also return flock() errors which
however hint to flock() usage errors by libubootenv.
Use EBUSY as general lockfile-related error code to distinguish it from
other errors of libuboot_load().
On any error, for fw_printenv, the default environment is next tried to be
read and only if that also fails, fw_printenv bails out.
For fw_setenv, any error is unconditionally fatal.
Signed-off-by: Christian Storm <
christi...@siemens.com>
---
src/fw_printenv.c | 12 ++++++++++--
src/uboot_env.c | 11 +++++++++--
2 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/src/fw_printenv.c b/src/fw_printenv.c
index c515938..a542a70 100644
--- a/src/fw_printenv.c
+++ b/src/fw_printenv.c
@@ -13,6 +13,7 @@
#include <getopt.h>
#include <string.h>
#include <stdbool.h>
+#include <errno.h>
#include "libuboot.h"
@@ -159,9 +160,16 @@ int main (int argc, char **argv) {
defenvfile = DEFAULT_ENV_FILE;
if ((ret = libuboot_open(ctx)) < 0) {
- fprintf(stderr, "Cannot read environment, using default\n");
+ if (is_setenv) {
+ fprintf(stderr, "Cannot %s environment: %s\n",
+ ret == -EBUSY ? "lock" : "read", strerror(errno));
+ exit(ret);
+ }
+ fprintf(stderr, "Cannot %s environment: %s. Using default\n",
+ ret == -EBUSY ? "lock" : "read", strerror(errno));
if ((ret = libuboot_load_file(ctx, defenvfile)) < 0) {
- fprintf(stderr, "Cannot read default environment from file\n");
+ fprintf(stderr, "Cannot read default environment from file: %s\n",
+ strerror(errno));
exit (ret);
}
default_used = true;
diff --git a/src/uboot_env.c b/src/uboot_env.c
index c4b2405..66ba09a 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -65,11 +65,15 @@ static int libuboot_lock(struct uboot_ctx *ctx)
int lockfd = -1;
lockfd = open(ctx->lockfile ?: default_lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (lockfd < 0) {
+ /*
+ * Note: flock() failures are likely API usage errors, hence
+ * use EBUSY as general lockfile-related error code.
+ */
return -EBUSY;
}
if (flock(lockfd, LOCK_EX) < 0) {
close(lockfd);
- return -EIO;
+ return -EBUSY;
}
ctx->lock = lockfd;
@@ -1106,7 +1110,10 @@ int libuboot_initialize(struct uboot_ctx **out,
int libuboot_open(struct uboot_ctx *ctx) {
if (!ctx)
return -EINVAL;
- libuboot_lock(ctx);
+
+ int ret = libuboot_lock(ctx);
+ if (ret < 0)
+ return ret;
return libuboot_load(ctx);
}
--
2.55.0