LocalServer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/LocalServer.h>
  7. #include <LibCore/LocalSocket.h>
  8. #include <LibCore/Notifier.h>
  9. #include <LibCore/Stream.h>
  10. #include <LibCore/System.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <sys/socket.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #ifndef SOCK_NONBLOCK
  17. # include <sys/ioctl.h>
  18. #endif
  19. namespace Core {
  20. LocalServer::LocalServer(Object* parent)
  21. : Object(parent)
  22. {
  23. }
  24. LocalServer::~LocalServer()
  25. {
  26. if (m_fd >= 0)
  27. ::close(m_fd);
  28. }
  29. ErrorOr<void> LocalServer::take_over_from_system_server(String const& socket_path)
  30. {
  31. if (m_listening)
  32. return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening"sv);
  33. if (!LocalSocket::s_overtaken_sockets_parsed)
  34. LocalSocket::parse_sockets_from_system_server();
  35. int fd = -1;
  36. if (socket_path.is_null()) {
  37. // We want the first (and only) socket.
  38. if (LocalSocket::s_overtaken_sockets.size() == 1) {
  39. fd = LocalSocket::s_overtaken_sockets.begin()->value;
  40. }
  41. } else {
  42. auto it = LocalSocket::s_overtaken_sockets.find(socket_path);
  43. if (it != LocalSocket::s_overtaken_sockets.end()) {
  44. fd = it->value;
  45. }
  46. }
  47. if (fd < 0)
  48. return Error::from_string_literal("Core::LocalServer: No file descriptor for socket takeover"sv);
  49. // Sanity check: it has to be a socket.
  50. auto stat = TRY(Core::System::fstat(fd));
  51. if (!S_ISSOCK(stat.st_mode))
  52. return Error::from_string_literal("Core::LocalServer: Attempted socket takeover with non-socket file descriptor"sv);
  53. // It had to be !CLOEXEC for obvious reasons, but we
  54. // don't need it to be !CLOEXEC anymore, so set the
  55. // CLOEXEC flag now.
  56. TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
  57. // The SystemServer has passed us the socket, so use that instead of
  58. // creating our own.
  59. m_fd = fd;
  60. m_listening = true;
  61. setup_notifier();
  62. return {};
  63. }
  64. void LocalServer::setup_notifier()
  65. {
  66. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  67. m_notifier->on_ready_to_read = [this] {
  68. if (on_accept) {
  69. auto maybe_client_socket = accept();
  70. if (maybe_client_socket.is_error()) {
  71. dbgln("LocalServer::on_ready_to_read: Error accepting a connection: {} (FIXME: should propagate!)", maybe_client_socket.error());
  72. return;
  73. }
  74. on_accept(maybe_client_socket.release_value());
  75. }
  76. };
  77. }
  78. bool LocalServer::listen(const String& address)
  79. {
  80. if (m_listening)
  81. return false;
  82. int rc;
  83. #ifdef SOCK_NONBLOCK
  84. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  85. #else
  86. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  87. int option = 1;
  88. ioctl(m_fd, FIONBIO, &option);
  89. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  90. #endif
  91. VERIFY(m_fd >= 0);
  92. #ifndef AK_OS_MACOS
  93. rc = fchmod(m_fd, 0600);
  94. if (rc < 0) {
  95. perror("fchmod");
  96. VERIFY_NOT_REACHED();
  97. }
  98. #endif
  99. auto socket_address = SocketAddress::local(address);
  100. auto un_optional = socket_address.to_sockaddr_un();
  101. if (!un_optional.has_value()) {
  102. perror("bind");
  103. return false;
  104. }
  105. auto un = un_optional.value();
  106. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  107. if (rc < 0) {
  108. perror("bind");
  109. return false;
  110. }
  111. rc = ::listen(m_fd, 5);
  112. if (rc < 0) {
  113. perror("listen");
  114. return false;
  115. }
  116. m_listening = true;
  117. setup_notifier();
  118. return true;
  119. }
  120. ErrorOr<NonnullOwnPtr<Stream::LocalSocket>> LocalServer::accept()
  121. {
  122. VERIFY(m_listening);
  123. sockaddr_un un;
  124. socklen_t un_size = sizeof(un);
  125. #ifndef AK_OS_MACOS
  126. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  127. #else
  128. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  129. #endif
  130. if (accepted_fd < 0) {
  131. return Error::from_syscall("accept", -errno);
  132. }
  133. #ifdef AK_OS_MACOS
  134. int option = 1;
  135. ioctl(m_fd, FIONBIO, &option);
  136. (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
  137. #endif
  138. return Stream::LocalSocket::adopt_fd(accepted_fd);
  139. }
  140. }