Skip to content

Commit

Permalink
device: track who is using board
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
calebccff committed Oct 17, 2024
1 parent de2f592 commit 7a25dee
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -62,25 +63,49 @@ 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);

/* check that connection isn't gone */
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)

Check warning

Code scanning / CodeQL

Exposure of system data to an unauthorized control sphere Medium

This operation exposes system data from
*call to getenv
.
This operation exposes system data from
*call to getenv
.
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,
Expand Down

0 comments on commit 7a25dee

Please sign in to comment.