MMDVMHost-Private/PseudoTTYController.cpp

87 lines
2.1 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2002-2004,2007-2011,2013,2014-2017,2019,2020 by Jonathan Naylor G4KLX
* Copyright (C) 1999-2001 by Thomas Sailor HB9JNX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined(_WIN32) && !defined(_WIN64)
2020-06-25 20:23:43 +00:00
#include "PseudoTTYController.h"
#include "Log.h"
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
2020-06-26 19:17:14 +00:00
#include <pty.h>
2020-06-27 21:33:24 +00:00
CPseudoTTYController::CPseudoTTYController(const std::string& symlink, unsigned int speed, bool assertRTS) :
CSerialController(speed, assertRTS),
2020-06-27 21:33:24 +00:00
m_symlink(symlink)
{
}
CPseudoTTYController::~CPseudoTTYController()
{
}
bool CPseudoTTYController::open()
{
assert(m_fd == -1);
2020-06-26 19:17:14 +00:00
int slavefd;
2020-06-27 21:33:24 +00:00
char slave[300];
int result = ::openpty(&m_fd, &slavefd, slave, NULL, NULL);
2020-06-26 19:17:14 +00:00
if (result < 0) {
2020-06-27 21:33:24 +00:00
LogError("Cannot open the pseudo tty - errno : %d", errno);
return false;
}
// Remove any previous stale symlink
2020-06-27 21:33:24 +00:00
::unlink(m_symlink.c_str());
2020-06-27 21:33:24 +00:00
int ret = ::symlink(slave, m_symlink.c_str());
if (ret != 0) {
2020-06-27 21:33:24 +00:00
LogError("Cannot make symlink to %s with %s", slave, m_symlink.c_str());
close();
return false;
}
2020-06-27 21:33:24 +00:00
LogMessage("Made symbolic link from %s to %s", slave, m_symlink.c_str());
m_device = std::string(::ttyname(m_fd));
2020-06-27 21:33:24 +00:00
return setRaw();
}
void CPseudoTTYController::close()
{
CSerialController::close();
2020-06-27 21:33:24 +00:00
::unlink(m_symlink.c_str());
}
#endif