Skip to content

Commit

Permalink
Add check for squelch state PTY being open before write
Browse files Browse the repository at this point in the history
  • Loading branch information
sm0svx committed Dec 22, 2014
1 parent b912f48 commit 23566ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/async/core/AsyncPty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ using namespace Async;
****************************************************************************/

Pty::Pty(const std::string &slave_link)
: slave_link(slave_link), master(-1), slave(-1), watch(0)
: slave_link(slave_link), master(-1), slave(-1), watch(0), is_open(false)
{
} /* Pty::Pty */

Expand Down Expand Up @@ -199,12 +199,15 @@ bool Pty::open(void)
*/
}

is_open = true;

return true;
} /* Pty::open */


void Pty::close(void)
{
is_open = false;
if (!slave_link.empty())
{
unlink(slave_link.c_str());
Expand Down
7 changes: 7 additions & 0 deletions src/async/core/AsyncPty.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ class Pty : public sigc::trackable
*/
ssize_t write(const void *buf, size_t count);

/**
* @brief Check if the PTY is open or not
* @return Returns \em true if the PTY has been successfully opened
*/
bool isOpen(void) const { return is_open; }

/**
* @brief Signal that is emitted when data has been received
* @param buf A buffer containing the received data
Expand All @@ -175,6 +181,7 @@ class Pty : public sigc::trackable
int master;
int slave;
Async::FdWatch *watch;
bool is_open;

Pty(const Pty&);
Pty& operator=(const Pty&);
Expand Down
6 changes: 3 additions & 3 deletions src/async/core/AsyncPtyStreamBuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ using namespace Async;
PtyStreamBuf::PtyStreamBuf(Pty *pty, size_t buf_size)
: m_pty(pty), m_buf(buf_size + 1)
{
assert(m_pty != 0);
char *base = &m_buf.front();
setp(base, base + m_buf.size() - 1);
} /* PtyStreamBuf::PtyStreamBuf */
Expand Down Expand Up @@ -142,8 +143,7 @@ PtyStreamBuf::~PtyStreamBuf(void)

PtyStreamBuf::int_type PtyStreamBuf::overflow(int_type ch)
{
// FIXME: Check the state of the PTY before writing
if (ch != traits_type::eof())
if (m_pty->isOpen() && (ch != traits_type::eof()))
{
assert(std::less_equal<char *>()(pptr(), epptr()));
*pptr() = ch;
Expand All @@ -160,7 +160,7 @@ PtyStreamBuf::int_type PtyStreamBuf::overflow(int_type ch)

int PtyStreamBuf::sync(void)
{
return writeToPty() ? 0 : -1;
return (m_pty->isOpen() && writeToPty()) ? 0 : -1;
} /* PtyStreamBuf::sync */


Expand Down

0 comments on commit 23566ac

Please sign in to comment.