PTYMultiplexer.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "PTYMultiplexer.h"
  2. #include "MasterPTY.h"
  3. #include <Kernel/Process.h>
  4. #include <LibC/errno_numbers.h>
  5. static const unsigned s_max_pty_pairs = 8;
  6. static PTYMultiplexer* s_the;
  7. PTYMultiplexer& PTYMultiplexer::the()
  8. {
  9. ASSERT(s_the);
  10. return *s_the;
  11. }
  12. PTYMultiplexer::PTYMultiplexer()
  13. : CharacterDevice(5, 2)
  14. {
  15. s_the = this;
  16. m_freelist.ensure_capacity(s_max_pty_pairs);
  17. for (int i = s_max_pty_pairs; i > 0; --i)
  18. m_freelist.unchecked_append(i - 1);
  19. }
  20. PTYMultiplexer::~PTYMultiplexer()
  21. {
  22. }
  23. RetainPtr<FileDescriptor> PTYMultiplexer::open(int& error, int options)
  24. {
  25. LOCKER(m_lock);
  26. if (m_freelist.is_empty()) {
  27. error = -EBUSY;
  28. return nullptr;
  29. }
  30. auto master_index = m_freelist.take_last();
  31. auto master = adopt(*new MasterPTY(master_index));
  32. dbgprintf("PTYMultiplexer::open: Vending master %u\n", master->index());
  33. return VFS::the().open(move(master), error, options);
  34. }
  35. void PTYMultiplexer::notify_master_destroyed(Badge<MasterPTY>, unsigned index)
  36. {
  37. LOCKER(m_lock);
  38. m_freelist.append(index);
  39. dbgprintf("PTYMultiplexer: %u added to freelist\n", index);
  40. }