LocalServer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2018-2020, 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 <fcntl.h>
  10. #include <stdio.h>
  11. #include <sys/socket.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14. #ifndef SOCK_NONBLOCK
  15. # include <sys/ioctl.h>
  16. #endif
  17. namespace Core {
  18. LocalServer::LocalServer(Object* parent)
  19. : Object(parent)
  20. {
  21. }
  22. LocalServer::~LocalServer()
  23. {
  24. if (m_fd >= 0)
  25. ::close(m_fd);
  26. }
  27. bool LocalServer::take_over_from_system_server(String const& socket_path)
  28. {
  29. if (m_listening)
  30. return false;
  31. if (!LocalSocket::s_overtaken_sockets_parsed)
  32. LocalSocket::parse_sockets_from_system_server();
  33. int fd = -1;
  34. if (socket_path.is_null()) {
  35. // We want the first (and only) socket.
  36. if (LocalSocket::s_overtaken_sockets.size() == 1) {
  37. fd = LocalSocket::s_overtaken_sockets.begin()->value;
  38. }
  39. } else {
  40. auto it = LocalSocket::s_overtaken_sockets.find(socket_path);
  41. if (it != LocalSocket::s_overtaken_sockets.end()) {
  42. fd = it->value;
  43. }
  44. }
  45. if (fd >= 0) {
  46. // Sanity check: it has to be a socket.
  47. struct stat stat;
  48. int rc = fstat(fd, &stat);
  49. if (rc == 0 && S_ISSOCK(stat.st_mode)) {
  50. // The SystemServer has passed us the socket, so use that instead of
  51. // creating our own.
  52. m_fd = fd;
  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. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  57. m_listening = true;
  58. setup_notifier();
  59. return true;
  60. } else {
  61. if (rc != 0)
  62. perror("fstat");
  63. dbgln("It's not a socket, what the heck??");
  64. }
  65. }
  66. dbgln("Failed to take the socket over from SystemServer");
  67. return false;
  68. }
  69. void LocalServer::setup_notifier()
  70. {
  71. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  72. m_notifier->on_ready_to_read = [this] {
  73. if (on_accept) {
  74. if (auto client_socket = accept())
  75. on_accept(client_socket.release_nonnull());
  76. }
  77. };
  78. }
  79. bool LocalServer::listen(const String& address)
  80. {
  81. if (m_listening)
  82. return false;
  83. int rc;
  84. #ifdef SOCK_NONBLOCK
  85. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  86. #else
  87. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  88. int option = 1;
  89. ioctl(m_fd, FIONBIO, &option);
  90. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  91. #endif
  92. VERIFY(m_fd >= 0);
  93. #ifndef AK_OS_MACOS
  94. rc = fchmod(m_fd, 0600);
  95. if (rc < 0) {
  96. perror("fchmod");
  97. VERIFY_NOT_REACHED();
  98. }
  99. #endif
  100. auto socket_address = SocketAddress::local(address);
  101. auto un_optional = socket_address.to_sockaddr_un();
  102. if (!un_optional.has_value()) {
  103. perror("bind");
  104. return false;
  105. }
  106. auto un = un_optional.value();
  107. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  108. if (rc < 0) {
  109. perror("bind");
  110. return false;
  111. }
  112. rc = ::listen(m_fd, 5);
  113. if (rc < 0) {
  114. perror("listen");
  115. return false;
  116. }
  117. m_listening = true;
  118. setup_notifier();
  119. return true;
  120. }
  121. RefPtr<LocalSocket> LocalServer::accept()
  122. {
  123. VERIFY(m_listening);
  124. sockaddr_un un;
  125. socklen_t un_size = sizeof(un);
  126. #ifndef AK_OS_MACOS
  127. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  128. #else
  129. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  130. #endif
  131. if (accepted_fd < 0) {
  132. perror("accept");
  133. return nullptr;
  134. }
  135. #ifdef AK_OS_MACOS
  136. int option = 1;
  137. ioctl(m_fd, FIONBIO, &option);
  138. (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
  139. #endif
  140. return LocalSocket::construct(accepted_fd);
  141. }
  142. }