LocalServer.cpp 3.2 KB

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