Skip to content

Commit 73cc8a5

Browse files
[posix] add uart-exclusive option to enable flock / TIOCEXCL (openthread#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.
1 parent 928c78a commit 73cc8a5

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/posix/platform/hdlc_interface.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#endif
5050
#include <stdarg.h>
5151
#include <stdlib.h>
52+
#include <sys/file.h>
5253
#include <sys/ioctl.h>
5354
#include <sys/resource.h>
5455
#include <sys/stat.h>
@@ -463,6 +464,26 @@ int HdlcInterface::OpenFile(const Url::Url &aRadioUrl)
463464
ExitNow();
464465
}
465466

467+
if (aRadioUrl.HasParam("uart-exclusive"))
468+
{
469+
// Lock the device early to prevent concurrent access
470+
if (flock(fd, LOCK_EX | LOCK_NB) == -1)
471+
{
472+
perror("flock uart failed, device already in use");
473+
close(fd);
474+
fd = -1;
475+
ExitNow();
476+
}
477+
478+
#ifdef TIOCEXCL
479+
// Set exclusive access mode if supported by the platform
480+
if (ioctl(fd, TIOCEXCL) == -1)
481+
{
482+
LogWarn("ioctl(TIOCEXCL) failed: %s", strerror(errno));
483+
}
484+
#endif
485+
}
486+
466487
if (isatty(fd))
467488
{
468489
struct termios tios;

src/posix/platform/radio_url.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const char *otSysGetRadioUrlHelpString(void)
8585
" uart-flow-control Enable flow control, disabled by default.\n" \
8686
" uart-init-deassert Deassert lines on init when flow control is disabled.\n" \
8787
" uart-reset Reset connection after hard resetting RCP(USB CDC ACM).\n" \
88+
" uart-exclusive Lock uart device using flock / TIOCEXCL.\n" \
8889
"\n"
8990
#else
9091
#define OT_SPINEL_HDLC_RADIO_URL_HELP_BUS

0 commit comments

Comments
 (0)