LocalServer.cpp 4.4 KB

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