LocalServer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_ready_to_accept)
  74. on_ready_to_accept();
  75. };
  76. }
  77. bool LocalServer::listen(const String& address)
  78. {
  79. if (m_listening)
  80. return false;
  81. int rc;
  82. #ifdef SOCK_NONBLOCK
  83. m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  84. #else
  85. m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  86. int option = 1;
  87. ioctl(m_fd, FIONBIO, &option);
  88. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  89. #endif
  90. VERIFY(m_fd >= 0);
  91. #ifndef __APPLE__
  92. rc = fchmod(m_fd, 0600);
  93. if (rc < 0) {
  94. perror("fchmod");
  95. VERIFY_NOT_REACHED();
  96. }
  97. #endif
  98. auto socket_address = SocketAddress::local(address);
  99. auto un_optional = socket_address.to_sockaddr_un();
  100. if (!un_optional.has_value()) {
  101. perror("bind");
  102. return false;
  103. }
  104. auto un = un_optional.value();
  105. rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
  106. if (rc < 0) {
  107. perror("bind");
  108. return false;
  109. }
  110. rc = ::listen(m_fd, 5);
  111. if (rc < 0) {
  112. perror("listen");
  113. return false;
  114. }
  115. m_listening = true;
  116. setup_notifier();
  117. return true;
  118. }
  119. RefPtr<LocalSocket> LocalServer::accept()
  120. {
  121. VERIFY(m_listening);
  122. sockaddr_un un;
  123. socklen_t un_size = sizeof(un);
  124. #ifndef AK_OS_MACOS
  125. int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
  126. #else
  127. int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
  128. #endif
  129. if (accepted_fd < 0) {
  130. perror("accept");
  131. return nullptr;
  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 LocalSocket::construct(accepted_fd);
  139. }
  140. }