LocalServer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <stdio.h>
  30. #include <sys/socket.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. #ifndef SOCK_NONBLOCK
  34. # include <sys/ioctl.h>
  35. #endif
  36. namespace Core {
  37. LocalServer::LocalServer(Object* parent)
  38. : Object(parent)
  39. {
  40. }
  41. LocalServer::~LocalServer()
  42. {
  43. if (m_fd >= 0)
  44. ::close(m_fd);
  45. }
  46. bool LocalServer::take_over_from_system_server()
  47. {
  48. if (m_listening)
  49. return false;
  50. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  51. if (getenv(socket_takeover)) {
  52. // Sanity check: it has to be a socket.
  53. struct stat stat;
  54. int rc = fstat(3, &stat);
  55. if (rc == 0 && S_ISSOCK(stat.st_mode)) {
  56. // The SystemServer has passed us the socket as fd 3,
  57. // so use that instead of creating our own.
  58. m_fd = 3;
  59. // It had to be !CLOEXEC for obvious reasons, but we
  60. // don't need it to be !CLOEXEC anymore, so set the
  61. // CLOEXEC flag now.
  62. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  63. // We wouldn't want our children to think we're passing
  64. // them a socket either, so unset the env variable.
  65. unsetenv(socket_takeover);
  66. m_listening = true;
  67. setup_notifier();
  68. return true;
  69. } else {
  70. if (rc != 0)
  71. perror("fstat");
  72. dbg() << "It's not a socket, what the heck??";
  73. }
  74. }
  75. dbg() << "Failed to take the socket over from SystemServer";
  76. return false;
  77. }
  78. void LocalServer::setup_notifier()
  79. {
  80. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  81. m_notifier->on_ready_to_read = [this] {
  82. if (on_ready_to_accept)
  83. on_ready_to_accept();
  84. };
  85. }
  86. bool LocalServer::listen(const String& address)
  87. {
  88. if (m_listening)
  89. return false;
  90. int rc;
  91. #ifdef SOCK_NONBLOCK
  92. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  93. #else
  94. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  95. int option = 1;
  96. ioctl(m_fd, FIONBIO, &option);
  97. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  98. #endif
  99. ASSERT(m_fd >= 0);
  100. #ifndef __APPLE__
  101. rc = fchmod(m_fd, 0600);
  102. if (rc < 0) {
  103. perror("fchmod");
  104. ASSERT_NOT_REACHED();
  105. }
  106. #endif
  107. auto socket_address = SocketAddress::local(address);
  108. auto un_optional = socket_address.to_sockaddr_un();
  109. if (!un_optional.has_value()) {
  110. perror("bind");
  111. return false;
  112. }
  113. auto un = un_optional.value();
  114. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  115. if (rc < 0) {
  116. perror("bind");
  117. return false;
  118. }
  119. rc = ::listen(m_fd, 5);
  120. if (rc < 0) {
  121. perror("listen");
  122. return false;
  123. }
  124. m_listening = true;
  125. setup_notifier();
  126. return true;
  127. }
  128. RefPtr<LocalSocket> LocalServer::accept()
  129. {
  130. ASSERT(m_listening);
  131. sockaddr_un un;
  132. socklen_t un_size = sizeof(un);
  133. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  134. if (accepted_fd < 0) {
  135. perror("accept");
  136. return nullptr;
  137. }
  138. return LocalSocket::construct(accepted_fd);
  139. }
  140. }