PTYMultiplexer.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. void PTYMultiplexer::initialize_statics()
  13. {
  14. s_the = nullptr;
  15. }
  16. PTYMultiplexer::PTYMultiplexer()
  17. : CharacterDevice(5, 2)
  18. {
  19. s_the = this;
  20. m_freelist.ensure_capacity(s_max_pty_pairs);
  21. for (int i = s_max_pty_pairs; i > 0; --i)
  22. m_freelist.unchecked_append(i - 1);
  23. }
  24. PTYMultiplexer::~PTYMultiplexer()
  25. {
  26. }
  27. RetainPtr<FileDescriptor> PTYMultiplexer::open(int& error, int options)
  28. {
  29. LOCKER(m_lock);
  30. if (m_freelist.is_empty()) {
  31. error = -EBUSY;
  32. return nullptr;
  33. }
  34. auto master_index = m_freelist.take_last();
  35. auto master = adopt(*new MasterPTY(master_index));
  36. dbgprintf("PTYMultiplexer::open: Vending master %u\n", master->index());
  37. return VFS::the().open(move(master), error, options);
  38. }
  39. void PTYMultiplexer::notify_master_destroyed(Badge<MasterPTY>, unsigned index)
  40. {
  41. LOCKER(m_lock);
  42. m_freelist.append(index);
  43. dbgprintf("PTYMultiplexer: %u added to freelist\n", index);
  44. }