From 4e308e6bbb5c88cf18319df3a5d0b36702f940f7 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Mon, 10 May 2021 12:27:25 +0200 Subject: [PATCH] Add file-locking for serial2002 --- plugins/serial2002/serial2002.c | 7 +++++++ plugins/serial2002/serial2002_lib.c | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/serial2002/serial2002.c b/plugins/serial2002/serial2002.c index 59fe2ab..7de2a82 100644 --- a/plugins/serial2002/serial2002.c +++ b/plugins/serial2002/serial2002.c @@ -451,6 +451,7 @@ static struct moberg_status device_open(struct moberg_device_context *device) if (device->port.count == 0) { fd = open(device->port.name, O_RDWR); if (fd < 0) { goto err_errno; } + if (lockf(fd, F_TLOCK, 0)) { goto err_errno; } struct termios2 termios2; if (ioctl(fd, TCGETS2, &termios2) < 0) { goto err_errno; } termios2.c_iflag = 0; @@ -492,6 +493,7 @@ err_errno: result = MOBERG_ERRNO(errno); err_result: if (fd >= 0) { + lockf(fd, F_ULOCK, 0); close(fd); } return result; @@ -502,6 +504,7 @@ static struct moberg_status device_close(struct moberg_device_context *device) if (device->port.count < 0) { errno = ENODEV; goto err_errno; } device->port.count--; if (device->port.count == 0) { + lockf(device->port.io.fd, F_ULOCK, 0); if (close(device->port.io.fd) < 0) { goto err_errno; } } return MOBERG_OK; @@ -531,6 +534,9 @@ static int channel_down(struct moberg_channel *channel) static struct moberg_status channel_open(struct moberg_channel *channel) { struct moberg_status result = device_open(channel->context->device); + if (! OK(result)) { + goto return_result; + } int count = 0; switch (channel->kind) { case chan_ANALOGIN: @@ -553,6 +559,7 @@ static struct moberg_status channel_open(struct moberg_channel *channel) device_close(channel->context->device); result = MOBERG_ERRNO(ENODEV); } +return_result: return result; } diff --git a/plugins/serial2002/serial2002_lib.c b/plugins/serial2002/serial2002_lib.c index bc2837f..56c8668 100644 --- a/plugins/serial2002/serial2002_lib.c +++ b/plugins/serial2002/serial2002_lib.c @@ -287,7 +287,10 @@ static void discard_pending(struct serial2002_io *io) break; } else { char discard; - read(io->fd, &discard, 1); + err = read(io->fd, &discard, 1); + if (err <= 0) { + break; + } } } } -- GitLab