LocalServer.cpp 3.2 KB

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