diff mbox series

[PATCH/libubootenv] Handle race conditions in UBI_IOCATT

Message ID 20240411175429.4147404-1-james.hilliard1@gmail.com
State Accepted
Delegated to: Stefano Babic
Headers show
Series [PATCH/libubootenv] Handle race conditions in UBI_IOCATT | expand

Commit Message

James Hilliard April 11, 2024, 5:54 p.m. UTC
If UBI_IOCATT was called while the attachment was already in progress
we may get an EEXIST error.

If this happens get the ubi_num with ubi_get_dev_id_from_mtd instead.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 src/uboot_mtd.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Comments

Stefano Babic April 16, 2024, 11:37 a.m. UTC | #1
On 11.04.24 19:54, James Hilliard wrote:
> If UBI_IOCATT was called while the attachment was already in progress
> we may get an EEXIST error.
>
> If this happens get the ubi_num with ubi_get_dev_id_from_mtd instead.
>
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>   src/uboot_mtd.c | 14 ++++++++++++--
>   1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/src/uboot_mtd.c b/src/uboot_mtd.c
> index 27b63ef..e5a9ce1 100644
> --- a/src/uboot_mtd.c
> +++ b/src/uboot_mtd.c
> @@ -250,8 +250,18 @@ int libubootenv_ubi_update_name(struct uboot_flash_env *dev)
>
>   				ret = ioctl(fd, UBI_IOCATT, &req);
>   				close(fd);
> -				if (ret == -1)
> -					return -EBADF;
> +				if (ret == -1) {
> +					/* Handle race condition where MTD was already being attached. */
> +					if (errno == EEXIST) {
> +						ret = ubi_get_dev_id_from_mtd(device);
> +						if (ret >= 0)
> +							req.ubi_num = ret;
> +						else
> +							return -EBADF;
> +					} else {
> +						return -EBADF;
> +					}
> +				}
>
>   				sprintf(dev->devname, DEVICE_UBI_NAME"%d:%s", req.ubi_num, volume);
>   			} else {

Acked-by: Stefano Babic <stefano.babic@swupdate.org>

Best regards,
Stefano
diff mbox series

Patch

diff --git a/src/uboot_mtd.c b/src/uboot_mtd.c
index 27b63ef..e5a9ce1 100644
--- a/src/uboot_mtd.c
+++ b/src/uboot_mtd.c
@@ -250,8 +250,18 @@  int libubootenv_ubi_update_name(struct uboot_flash_env *dev)
 
 				ret = ioctl(fd, UBI_IOCATT, &req);
 				close(fd);
-				if (ret == -1)
-					return -EBADF;
+				if (ret == -1) {
+					/* Handle race condition where MTD was already being attached. */
+					if (errno == EEXIST) {
+						ret = ubi_get_dev_id_from_mtd(device);
+						if (ret >= 0)
+							req.ubi_num = ret;
+						else
+							return -EBADF;
+					} else {
+						return -EBADF;
+					}
+				}
 
 				sprintf(dev->devname, DEVICE_UBI_NAME"%d:%s", req.ubi_num, volume);
 			} else {