CLocalServer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <LibCore/CLocalServer.h>
  2. #include <LibCore/CLocalSocket.h>
  3. #include <LibCore/CNotifier.h>
  4. #include <stdio.h>
  5. #include <sys/socket.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #ifndef SOCK_NONBLOCK
  9. #include <sys/ioctl.h>
  10. #endif
  11. CLocalServer::CLocalServer(CObject* parent)
  12. : CObject(parent)
  13. {
  14. }
  15. CLocalServer::~CLocalServer()
  16. {
  17. }
  18. bool CLocalServer::take_over_from_system_server()
  19. {
  20. if (m_listening)
  21. return false;
  22. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  23. if (getenv(socket_takeover)) {
  24. dbg() << "Taking the socket over from SystemServer";
  25. // Sanity check: it has to be a socket.
  26. struct stat stat;
  27. int rc = fstat(3, &stat);
  28. if (rc == 0 && S_ISSOCK(stat.st_mode)) {
  29. // The SystemServer has passed us the socket as fd 3,
  30. // so use that instead of creating our own.
  31. m_fd = 3;
  32. // It had to be !CLOEXEC for obvious reasons, but we
  33. // don't need it to be !CLOEXEC anymore, so set the
  34. // CLOEXEC flag now.
  35. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  36. // We wouldn't want our children to think we're passing
  37. // them a socket either, so unset the env variable.
  38. unsetenv(socket_takeover);
  39. m_listening = true;
  40. setup_notifier();
  41. return true;
  42. } else {
  43. if (rc != 0)
  44. perror("fstat");
  45. dbg() << "It's not a socket, what the heck??";
  46. }
  47. }
  48. dbg() << "Failed to take the socket over from SystemServer";
  49. return false;
  50. }
  51. void CLocalServer::setup_notifier()
  52. {
  53. m_notifier = CNotifier::construct(m_fd, CNotifier::Event::Read, this);
  54. m_notifier->on_ready_to_read = [this] {
  55. if (on_ready_to_accept)
  56. on_ready_to_accept();
  57. };
  58. }
  59. bool CLocalServer::listen(const String& address)
  60. {
  61. if (m_listening)
  62. return false;
  63. int rc;
  64. #ifdef SOCK_NONBLOCK
  65. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  66. #else
  67. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  68. int option = 1;
  69. ioctl(m_fd, FIONBIO, &option);
  70. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  71. #endif
  72. ASSERT(m_fd >= 0);
  73. auto socket_address = CSocketAddress::local(address);
  74. auto un = socket_address.to_sockaddr_un();
  75. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  76. if (rc < 0) {
  77. perror("bind");
  78. ASSERT_NOT_REACHED();
  79. }
  80. rc = ::listen(m_fd, 5);
  81. if (rc < 0) {
  82. perror("listen");
  83. ASSERT_NOT_REACHED();
  84. }
  85. m_listening = true;
  86. setup_notifier();
  87. return true;
  88. }
  89. RefPtr<CLocalSocket> CLocalServer::accept()
  90. {
  91. ASSERT(m_listening);
  92. sockaddr_un un;
  93. socklen_t un_size = sizeof(un);
  94. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  95. if (accepted_fd < 0) {
  96. perror("accept");
  97. return nullptr;
  98. }
  99. return CLocalSocket::construct(accepted_fd);
  100. }