[posix] add uart-exclusive option to enable flock / TIOCEXCL (#13015)

When uart-exclusive is specified as a radio URL parameter, the UART
device is locked using flock(LOCK_EX) to prevent concurrent access,
and TIOCEXCL is set where supported.
This commit is contained in:
sarveshkumarv3
2026-05-04 09:23:57 -07:00
committed by GitHub
parent 928c78a01b
commit 73cc8a5c05
2 changed files with 22 additions and 0 deletions
+21
View File
@@ -49,6 +49,7 @@
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/stat.h>
@@ -463,6 +464,26 @@ int HdlcInterface::OpenFile(const Url::Url &aRadioUrl)
ExitNow();
}
if (aRadioUrl.HasParam("uart-exclusive"))
{
// Lock the device early to prevent concurrent access
if (flock(fd, LOCK_EX | LOCK_NB) == -1)
{
perror("flock uart failed, device already in use");
close(fd);
fd = -1;
ExitNow();
}
#ifdef TIOCEXCL
// Set exclusive access mode if supported by the platform
if (ioctl(fd, TIOCEXCL) == -1)
{
LogWarn("ioctl(TIOCEXCL) failed: %s", strerror(errno));
}
#endif
}
if (isatty(fd))
{
struct termios tios;
+1
View File
@@ -85,6 +85,7 @@ const char *otSysGetRadioUrlHelpString(void)
" uart-flow-control Enable flow control, disabled by default.\n" \
" uart-init-deassert Deassert lines on init when flow control is disabled.\n" \
" uart-reset Reset connection after hard resetting RCP(USB CDC ACM).\n" \
" uart-exclusive Lock uart device using flock / TIOCEXCL.\n" \
"\n"
#else
#define OT_SPINEL_HDLC_RADIO_URL_HELP_BUS