From 5db3df360a153529b4f9f162425db40243624dbb Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Tue, 21 May 2024 20:12:21 +0300 Subject: [PATCH 1/3] 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; + + n = flock(fd, LOCK_EX | LOCK_NB); + if (!n) + return; - warnx("board is in use, waiting..."); + warnx("board is in use, waiting..."); - n = flock(fd, LOCK_EX); - if (n < 0) - err(1, "failed to lock lockfile %s", lock); + 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, From 95ff3049f8aedf5a0e24603a1d91c1decd456a08 Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Tue, 21 May 2024 20:13:40 +0300 Subject: [PATCH 2/3] cdba-server: include PID to the syslog message Include PID into syslog message to simplify identifying the CDBA sessions. Signed-off-by: Dmitry Baryshkov --- cdba-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdba-server.c b/cdba-server.c index 2b16661..9681a75 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -216,7 +216,7 @@ int main(int argc, char **argv) if (!username) username = "nobody"; - openlog("cdba-server", 0, LOG_DAEMON); + openlog("cdba-server", LOG_PID, LOG_DAEMON); ret = device_parser(".cdba"); if (ret) { From cbad92a838bc0288ef1ca4c7ba0e821893a072d6 Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Tue, 21 May 2024 20:14:41 +0300 Subject: [PATCH 3/3] cdba-server: log message on CDBA exit To identify closing of the session, log the message when CDBA quits. Signed-off-by: Dmitry Baryshkov --- cdba-server.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cdba-server.c b/cdba-server.c index 9681a75..c463f19 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -201,6 +201,11 @@ static void sigpipe_handler(int signo) watch_quit(); } +static void atexit_handler(void) +{ + syslog(LOG_INFO, "exiting"); +} + int main(int argc, char **argv) { int flags; @@ -217,6 +222,7 @@ int main(int argc, char **argv) username = "nobody"; openlog("cdba-server", LOG_PID, LOG_DAEMON); + atexit(atexit_handler); ret = device_parser(".cdba"); if (ret) {