From de2f592b6e0169dd3b992d626e715a7ced9a0ca6 Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Thu, 17 Oct 2024 14:36:45 +0200 Subject: [PATCH 1/2] make cdba user variable global Allow it to be accessed outside cdba-server.c Signed-off-by: Caleb Connolly --- cdba-server.c | 18 ++++++++---------- cdba-server.h | 2 ++ device.c | 2 ++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cdba-server.c b/cdba-server.c index c463f19..9772d68 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -22,8 +22,6 @@ #include "list.h" #include "watch.h" -static const char *username; - struct device *selected_device; static void fastboot_opened(struct fastboot *fb, void *data) @@ -55,7 +53,7 @@ static struct fastboot_ops fastboot_ops = { static void msg_select_board(const void *param) { - selected_device = device_open(param, username); + selected_device = device_open(param, cdba_user); if (!selected_device) { fprintf(stderr, "failed to open %s\n", (const char *)param); watch_quit(); @@ -177,10 +175,10 @@ static int handle_stdin(int fd, void *buf) device_send_break(selected_device); break; case MSG_LIST_DEVICES: - device_list_devices(username); + device_list_devices(cdba_user); break; case MSG_BOARD_INFO: - device_info(username, msg->data, msg->len); + device_info(cdba_user, msg->data, msg->len); break; case MSG_FASTBOOT_CONTINUE: msg_fastboot_continue(); @@ -215,11 +213,11 @@ int main(int argc, char **argv) fprintf(stderr, "Starting cdba server\n"); - username = getenv("CDBA_USER"); - if (!username) - username = getenv("USER"); - if (!username) - username = "nobody"; + cdba_user = getenv("CDBA_USER"); + if (!cdba_user) + cdba_user = getenv("USER"); + if (!cdba_user) + cdba_user = "nobody"; openlog("cdba-server", LOG_PID, LOG_DAEMON); atexit(atexit_handler); diff --git a/cdba-server.h b/cdba-server.h index 4176311..e0269f9 100644 --- a/cdba-server.h +++ b/cdba-server.h @@ -6,6 +6,8 @@ #include "cdba.h" +extern const char *cdba_user; + void cdba_send_buf(int type, size_t len, const void *buf); #define cdba_send(type) cdba_send_buf(type, 0, NULL) diff --git a/device.c b/device.c index 79856bb..18f94d7 100644 --- a/device.c +++ b/device.c @@ -25,6 +25,8 @@ #include "status-cmd.h" #include "watch.h" +const char *cdba_user; + #define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0]))) #define device_has_control(_dev, _op) \ From 7a25dee95d8645880b8cc9fb5d139dfa8bf9cdb3 Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Thu, 17 Oct 2024 13:56:34 +0200 Subject: [PATCH 2/2] device: track who is using board Keep track of who is using a board by writing $CDBA_USER to the lockfile and display it when the board is blocked. Signed-off-by: Caleb Connolly --- device.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/device.c b/device.c index 18f94d7..e1b8746 100644 --- a/device.c +++ b/device.c @@ -51,6 +51,7 @@ void device_add(struct device *device) static void device_lock(struct device *device) { char lock[PATH_MAX]; + char user[128] = { 0 }; int fd; int n; @@ -62,18 +63,26 @@ static void device_lock(struct device *device) if (fd >= 0) close(fd); - fd = open(lock, O_RDONLY | O_CLOEXEC); + fd = open(lock, O_RDWR | O_CLOEXEC); if (fd < 0) err(1, "failed to open lockfile %s", lock); + /* Read current user out of the lockfile if there is one */ + n = read(fd, user, sizeof(user)-1); + if (n < 0) + err(1, "failed to read lockfile %s", lock); + /* Strip newline */ + if (n) + user[n-1] = '\0'; + while (1) { char c; n = flock(fd, LOCK_EX | LOCK_NB); if (!n) - return; + break; - warnx("board is in use, waiting..."); + warnx("board is in use by %s, waiting...", user); sleep(3); @@ -81,6 +90,22 @@ static void device_lock(struct device *device) if (read(STDIN_FILENO, &c, 1) == 0) errx(1, "connection is gone"); } + + /* Write our username to the lockfile */ + n = snprintf(user, sizeof(user), "%s\n", cdba_user); + if (n >= (int)sizeof(user)) + errx(1, "failed to build lockfile username"); + + if (ftruncate(fd, 0) < 0) + err(1, "failed to truncate lockfile %s", lock); + + lseek(fd, 0, SEEK_SET); + if (write(fd, user, n) < 0) + err(1, "failed to write lockfile %s", lock); + + warnx("board locked by %s", cdba_user); + + fsync(fd); } static bool device_check_access(struct device *device,