2019-01-16 13:36:10 +01:00
|
|
|
#include "PTYMultiplexer.h"
|
|
|
|
#include "MasterPTY.h"
|
2019-01-31 05:55:30 +01:00
|
|
|
#include <Kernel/Process.h>
|
2019-01-16 13:36:10 +01:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
|
2019-03-27 15:07:12 +01:00
|
|
|
//#define PTMX_DEBUG
|
|
|
|
|
2019-01-30 00:49:20 +01:00
|
|
|
static const unsigned s_max_pty_pairs = 8;
|
2019-01-30 18:26:19 +01:00
|
|
|
static PTYMultiplexer* s_the;
|
|
|
|
|
|
|
|
PTYMultiplexer& PTYMultiplexer::the()
|
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:36:10 +01:00
|
|
|
PTYMultiplexer::PTYMultiplexer()
|
|
|
|
: CharacterDevice(5, 2)
|
2019-02-07 11:12:23 +01:00
|
|
|
, m_lock("PTYMultiplexer")
|
2019-01-16 13:36:10 +01:00
|
|
|
{
|
2019-01-30 18:26:19 +01:00
|
|
|
s_the = this;
|
2019-01-30 00:49:20 +01:00
|
|
|
m_freelist.ensure_capacity(s_max_pty_pairs);
|
|
|
|
for (int i = s_max_pty_pairs; i > 0; --i)
|
|
|
|
m_freelist.unchecked_append(i - 1);
|
2019-01-16 13:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PTYMultiplexer::~PTYMultiplexer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:14:31 +01:00
|
|
|
KResultOr<Retained<FileDescriptor>> PTYMultiplexer::open(int options)
|
2019-01-16 13:36:10 +01:00
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
2019-03-06 22:14:31 +01:00
|
|
|
if (m_freelist.is_empty())
|
|
|
|
return KResult(-EBUSY);
|
2019-01-30 00:49:20 +01:00
|
|
|
auto master_index = m_freelist.take_last();
|
|
|
|
auto master = adopt(*new MasterPTY(master_index));
|
2019-03-27 15:07:12 +01:00
|
|
|
#ifdef PTMX_DEBUG
|
2019-01-16 13:36:10 +01:00
|
|
|
dbgprintf("PTYMultiplexer::open: Vending master %u\n", master->index());
|
2019-03-27 15:07:12 +01:00
|
|
|
#endif
|
2019-03-06 22:14:31 +01:00
|
|
|
return VFS::the().open(move(master), options);
|
2019-01-16 13:36:10 +01:00
|
|
|
}
|
2019-01-30 18:26:19 +01:00
|
|
|
|
|
|
|
void PTYMultiplexer::notify_master_destroyed(Badge<MasterPTY>, unsigned index)
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
m_freelist.append(index);
|
2019-03-27 15:07:12 +01:00
|
|
|
#ifdef PTMX_DEBUG
|
2019-01-30 18:26:19 +01:00
|
|
|
dbgprintf("PTYMultiplexer: %u added to freelist\n", index);
|
2019-03-27 15:07:12 +01:00
|
|
|
#endif
|
2019-01-30 18:26:19 +01:00
|
|
|
}
|