From 73cc8a5c05c8354d4e581a2876522c2eb0131a95 Mon Sep 17 00:00:00 2001 From: sarveshkumarv3 <86755931+sarveshkumarv3@users.noreply.github.com> Date: Mon, 4 May 2026 09:23:57 -0700 Subject: [PATCH] [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. --- src/posix/platform/hdlc_interface.cpp | 21 +++++++++++++++++++++ src/posix/platform/radio_url.cpp | 1 + 2 files changed, 22 insertions(+) diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index 889a06cb4..775722a04 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -49,6 +49,7 @@ #endif #include #include +#include #include #include #include @@ -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; diff --git a/src/posix/platform/radio_url.cpp b/src/posix/platform/radio_url.cpp index 0a0cf73a6..08f34ad96 100644 --- a/src/posix/platform/radio_url.cpp +++ b/src/posix/platform/radio_url.cpp @@ -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