From 5db3df360a153529b4f9f162425db40243624dbb Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Tue, 21 May 2024 20:12:21 +0300 Subject: [PATCH] device: loop around flock/warn Loop around the flock(), sleeping in the middle. This allows CDBA to break early if user closes the terminal (by killing SSH or by Ctrl-A-Q sequence). Signed-off-by: Dmitry Baryshkov --- device.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/device.c b/device.c index 848f552..79856bb 100644 --- a/device.c +++ b/device.c @@ -64,15 +64,21 @@ static void device_lock(struct device *device) if (fd < 0) err(1, "failed to open lockfile %s", lock); - n = flock(fd, LOCK_EX | LOCK_NB); - if (!n) - return; + while (1) { + char c; - warnx("board is in use, waiting..."); + n = flock(fd, LOCK_EX | LOCK_NB); + if (!n) + return; - n = flock(fd, LOCK_EX); - if (n < 0) - err(1, "failed to lock lockfile %s", lock); + warnx("board is in use, waiting..."); + + sleep(3); + + /* check that connection isn't gone */ + if (read(STDIN_FILENO, &c, 1) == 0) + errx(1, "connection is gone"); + } } static bool device_check_access(struct device *device,