From 1153b2ef5c9eb7b12941e08389f1019e18f346e7 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Wed, 6 Jan 2021 14:41:08 -0800 Subject: cbfstool: Use flock() when accessing CBFS files Trying to do multiple operations on the same CBFS image at the same time likely leads to data corruption. For this reason, add BSD advisory file locking (flock()) to cbfstool (and ifittool which is using the same file I/O library), so that only one process will operate on the same file at the same time and the others will wait in line. This should help resolve parallel build issues with the INTERMEDIATE target on certain platforms. Unfortunately, some platforms use the INTERMEDIATE target to do a direct dd into the CBFS image. This should generally be discouraged and future platforms should aim to clearly deliminate regions that need to be written directly by platform scripts with custom FMAP sections, so that they can be written with `cbfstool write`. For the time being, update the legacy platforms that do this with explicit calls to the `flock` utility. Signed-off-by: Julius Werner Change-Id: I022468f6957415ae68a7a7e70428ae6f82d23b06 Reviewed-on: https://review.coreboot.org/c/coreboot/+/49190 Tested-by: build bot (Jenkins) Reviewed-by: Arthur Heymans Reviewed-by: Furquan Shaikh --- util/cbfstool/partitioned_file.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'util') diff --git a/util/cbfstool/partitioned_file.c b/util/cbfstool/partitioned_file.c index b6d4f1b808..6e75600cca 100644 --- a/util/cbfstool/partitioned_file.c +++ b/util/cbfstool/partitioned_file.c @@ -8,6 +8,7 @@ #include #include #include +#include struct partitioned_file { struct fmap *fmap; @@ -57,7 +58,7 @@ static partitioned_file_t *reopen_flat_file(const char *filename, access_mode = write_access ? "rb+" : "rb"; file->stream = fopen(filename, access_mode); - if (!file->stream) { + if (!file->stream || flock(fileno(file->stream), LOCK_EX)) { perror(filename); partitioned_file_close(file); return NULL; @@ -78,7 +79,7 @@ partitioned_file_t *partitioned_file_create_flat(const char *filename, } file->stream = fopen(filename, "wb"); - if (!file->stream) { + if (!file->stream || flock(fileno(file->stream), LOCK_EX)) { perror(filename); free(file); return NULL; @@ -268,6 +269,7 @@ void partitioned_file_close(partitioned_file_t *file) file->fmap = NULL; buffer_delete(&file->buffer); if (file->stream) { + flock(fileno(file->stream), LOCK_UN); fclose(file->stream); file->stream = NULL; } -- cgit v1.2.3