Skip to content

Commit

Permalink
serial: delete stale lock files
Browse files Browse the repository at this point in the history
If microcom crashes, e.g. due to the buffer underflow described in pengutronix#10,
it may leave a stale lock file behind. Teach microcom detection of
these stale lock files.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
  • Loading branch information
a3f committed Aug 23, 2019
1 parent eb89a27 commit 6f15b32
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,34 @@ struct ios_ops * serial_init(char *device)
fd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0444);
if (fd < 0) {
if (errno == EEXIST) {
char pidbuf[12];
ssize_t nbytes = 0;
if (opt_force) {
printf("lockfile for port exists, ignoring\n");
serial_unlock();
goto relock;
}

fd = open(lockfile, O_RDONLY);
if (fd < 0)
main_usage(3, "lockfile for port can't be opened", device);

do {
ret = read(fd, &pidbuf[nbytes], sizeof(pidbuf) - nbytes - 1);
nbytes += ret;
} while (ret > 0 && nbytes < sizeof (pidbuf) - 1);

if (ret >= 0) {
pidbuf[nbytes] = '\0';
ret = sscanf(pidbuf, "%10ld\n", &pid);

if (ret == 1 && kill(pid, 0) < 0 && errno == ESRCH) {
printf("lockfile contains stale pid, ignoring\n");
serial_unlock();
goto relock;
}
}

main_usage(3, "lockfile for port exists", device);
}

Expand Down

0 comments on commit 6f15b32

Please sign in to comment.