exclusively lock serial device to prevent simultaneous access, fix for closing serial device that is no tty
This commit is contained in:
+11
-1
@@ -28,6 +28,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/file.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#ifdef HAVE_PPOLL
|
#ifdef HAVE_PPOLL
|
||||||
@@ -214,8 +215,17 @@ result_t SerialDevice::open()
|
|||||||
// open file descriptor
|
// open file descriptor
|
||||||
m_fd = ::open(m_name, O_RDWR | O_NOCTTY);
|
m_fd = ::open(m_name, O_RDWR | O_NOCTTY);
|
||||||
|
|
||||||
if (m_fd < 0 || isatty(m_fd) == 0)
|
if (m_fd < 0)
|
||||||
return RESULT_ERR_NOTFOUND;
|
return RESULT_ERR_NOTFOUND;
|
||||||
|
if (isatty(m_fd) == 0) {
|
||||||
|
close();
|
||||||
|
return RESULT_ERR_NOTFOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flock(m_fd, LOCK_EX|LOCK_NB)) {
|
||||||
|
close();
|
||||||
|
return RESULT_ERR_DEVICE;
|
||||||
|
}
|
||||||
|
|
||||||
// save current settings
|
// save current settings
|
||||||
tcgetattr(m_fd, &m_oldSettings);
|
tcgetattr(m_fd, &m_oldSettings);
|
||||||
|
|||||||
Reference in New Issue
Block a user