LocalServer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/LocalServer.h>
  27. #include <LibCore/LocalSocket.h>
  28. #include <LibCore/Notifier.h>
  29. #include <fcntl.h>
  30. #include <stdio.h>
  31. #include <sys/socket.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. #ifndef SOCK_NONBLOCK
  35. # include <sys/ioctl.h>
  36. #endif
  37. namespace Core {
  38. LocalServer::LocalServer(Object* parent)
  39. : Object(parent)
  40. {
  41. }
  42. LocalServer::~LocalServer()
  43. {
  44. if (m_fd >= 0)
  45. ::close(m_fd);
  46. }
  47. bool LocalServer::take_over_from_system_server()
  48. {
  49. if (m_listening)
  50. return false;
  51. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  52. if (getenv(socket_takeover)) {
  53. // Sanity check: it has to be a socket.
  54. struct stat stat;
  55. int rc = fstat(3, &stat);
  56. if (rc == 0 && S_ISSOCK(stat.st_mode)) {
  57. // The SystemServer has passed us the socket as fd 3,
  58. // so use that instead of creating our own.
  59. m_fd = 3;
  60. // It had to be !CLOEXEC for obvious reasons, but we
  61. // don't need it to be !CLOEXEC anymore, so set the
  62. // CLOEXEC flag now.
  63. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  64. // We wouldn't want our children to think we're passing
  65. // them a socket either, so unset the env variable.
  66. unsetenv(socket_takeover);
  67. m_listening = true;
  68. setup_notifier();
  69. return true;
  70. } else {
  71. if (rc != 0)
  72. perror("fstat");
  73. dbgln("It's not a socket, what the heck??");
  74. }
  75. }
  76. dbgln("Failed to take the socket over from SystemServer");
  77. return false;
  78. }
  79. void LocalServer::setup_notifier()
  80. {
  81. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  82. m_notifier->on_ready_to_read = [this] {
  83. if (on_ready_to_accept)
  84. on_ready_to_accept();
  85. };
  86. }
  87. bool LocalServer::listen(const String& address)
  88. {
  89. if (m_listening)
  90. return false;
  91. int rc;
  92. #ifdef SOCK_NONBLOCK
  93. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  94. #else
  95. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  96. int option = 1;
  97. ioctl(m_fd, FIONBIO, &option);
  98. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  99. #endif
  100. ASSERT(m_fd >= 0);
  101. #ifndef __APPLE__
  102. rc = fchmod(m_fd, 0600);
  103. if (rc < 0) {
  104. perror("fchmod");
  105. ASSERT_NOT_REACHED();
  106. }
  107. #endif
  108. auto socket_address = SocketAddress::local(address);
  109. auto un_optional = socket_address.to_sockaddr_un();
  110. if (!un_optional.has_value()) {
  111. perror("bind");
  112. return false;
  113. }
  114. auto un = un_optional.value();
  115. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  116. if (rc < 0) {
  117. perror("bind");
  118. return false;
  119. }
  120. rc = ::listen(m_fd, 5);
  121. if (rc < 0) {
  122. perror("listen");
  123. return false;
  124. }
  125. m_listening = true;
  126. setup_notifier();
  127. return true;
  128. }
  129. RefPtr<LocalSocket> LocalServer::accept()
  130. {
  131. ASSERT(m_listening);
  132. sockaddr_un un;
  133. socklen_t un_size = sizeof(un);
  134. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  135. if (accepted_fd < 0) {
  136. perror("accept");
  137. return nullptr;
  138. }
  139. return LocalSocket::construct(accepted_fd);
  140. }
  141. }