LocalServer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/Notifier.h>
  8. #include <LibCore/Stream.h>
  9. #include <LibCore/System.h>
  10. #include <LibCore/SystemServerTakeover.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. auto socket = TRY(take_over_socket_from_system_server(socket_path));
  34. m_fd = TRY(socket->release_fd());
  35. m_listening = true;
  36. setup_notifier();
  37. return {};
  38. }
  39. void LocalServer::setup_notifier()
  40. {
  41. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  42. m_notifier->on_ready_to_read = [this] {
  43. if (on_accept) {
  44. auto maybe_client_socket = accept();
  45. if (maybe_client_socket.is_error()) {
  46. dbgln("LocalServer::on_ready_to_read: Error accepting a connection: {} (FIXME: should propagate!)", maybe_client_socket.error());
  47. return;
  48. }
  49. on_accept(maybe_client_socket.release_value());
  50. }
  51. };
  52. }
  53. bool LocalServer::listen(const String& address)
  54. {
  55. if (m_listening)
  56. return false;
  57. int rc;
  58. #ifdef SOCK_NONBLOCK
  59. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  60. #else
  61. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  62. int option = 1;
  63. ioctl(m_fd, FIONBIO, &option);
  64. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  65. #endif
  66. VERIFY(m_fd >= 0);
  67. #ifndef AK_OS_MACOS
  68. rc = fchmod(m_fd, 0600);
  69. if (rc < 0) {
  70. perror("fchmod");
  71. VERIFY_NOT_REACHED();
  72. }
  73. #endif
  74. auto socket_address = SocketAddress::local(address);
  75. auto un_optional = socket_address.to_sockaddr_un();
  76. if (!un_optional.has_value()) {
  77. perror("bind");
  78. return false;
  79. }
  80. auto un = un_optional.value();
  81. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  82. if (rc < 0) {
  83. perror("bind");
  84. return false;
  85. }
  86. rc = ::listen(m_fd, 5);
  87. if (rc < 0) {
  88. perror("listen");
  89. return false;
  90. }
  91. m_listening = true;
  92. setup_notifier();
  93. return true;
  94. }
  95. ErrorOr<NonnullOwnPtr<Stream::LocalSocket>> LocalServer::accept()
  96. {
  97. VERIFY(m_listening);
  98. sockaddr_un un;
  99. socklen_t un_size = sizeof(un);
  100. #ifndef AK_OS_MACOS
  101. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  102. #else
  103. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  104. #endif
  105. if (accepted_fd < 0) {
  106. return Error::from_syscall("accept", -errno);
  107. }
  108. #ifdef AK_OS_MACOS
  109. int option = 1;
  110. ioctl(m_fd, FIONBIO, &option);
  111. (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
  112. #endif
  113. return Stream::LocalSocket::adopt_fd(accepted_fd);
  114. }
  115. }