LocalServer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(String const& socket_path)
  48. {
  49. if (m_listening)
  50. return false;
  51. if (!LocalSocket::s_overtaken_sockets_parsed)
  52. LocalSocket::parse_sockets_from_system_server();
  53. int fd = -1;
  54. if (socket_path.is_null()) {
  55. // We want the first (and only) socket.
  56. if (LocalSocket::s_overtaken_sockets.size() == 1) {
  57. fd = LocalSocket::s_overtaken_sockets.begin()->value;
  58. }
  59. } else {
  60. auto it = LocalSocket::s_overtaken_sockets.find(socket_path);
  61. if (it != LocalSocket::s_overtaken_sockets.end()) {
  62. fd = it->value;
  63. }
  64. }
  65. if (fd >= 0) {
  66. // Sanity check: it has to be a socket.
  67. struct stat stat;
  68. int rc = fstat(fd, &stat);
  69. if (rc == 0 && S_ISSOCK(stat.st_mode)) {
  70. // The SystemServer has passed us the socket, so use that instead of
  71. // creating our own.
  72. m_fd = fd;
  73. // It had to be !CLOEXEC for obvious reasons, but we
  74. // don't need it to be !CLOEXEC anymore, so set the
  75. // CLOEXEC flag now.
  76. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  77. m_listening = true;
  78. setup_notifier();
  79. return true;
  80. } else {
  81. if (rc != 0)
  82. perror("fstat");
  83. dbgln("It's not a socket, what the heck??");
  84. }
  85. }
  86. dbgln("Failed to take the socket over from SystemServer");
  87. return false;
  88. }
  89. void LocalServer::setup_notifier()
  90. {
  91. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  92. m_notifier->on_ready_to_read = [this] {
  93. if (on_ready_to_accept)
  94. on_ready_to_accept();
  95. };
  96. }
  97. bool LocalServer::listen(const String& address)
  98. {
  99. if (m_listening)
  100. return false;
  101. int rc;
  102. #ifdef SOCK_NONBLOCK
  103. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  104. #else
  105. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  106. int option = 1;
  107. ioctl(m_fd, FIONBIO, &option);
  108. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  109. #endif
  110. VERIFY(m_fd >= 0);
  111. #ifndef __APPLE__
  112. rc = fchmod(m_fd, 0600);
  113. if (rc < 0) {
  114. perror("fchmod");
  115. VERIFY_NOT_REACHED();
  116. }
  117. #endif
  118. auto socket_address = SocketAddress::local(address);
  119. auto un_optional = socket_address.to_sockaddr_un();
  120. if (!un_optional.has_value()) {
  121. perror("bind");
  122. return false;
  123. }
  124. auto un = un_optional.value();
  125. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  126. if (rc < 0) {
  127. perror("bind");
  128. return false;
  129. }
  130. rc = ::listen(m_fd, 5);
  131. if (rc < 0) {
  132. perror("listen");
  133. return false;
  134. }
  135. m_listening = true;
  136. setup_notifier();
  137. return true;
  138. }
  139. RefPtr<LocalSocket> LocalServer::accept()
  140. {
  141. VERIFY(m_listening);
  142. sockaddr_un un;
  143. socklen_t un_size = sizeof(un);
  144. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  145. if (accepted_fd < 0) {
  146. perror("accept");
  147. return nullptr;
  148. }
  149. return LocalSocket::construct(accepted_fd);
  150. }
  151. }