LocalSocket.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <AK/StringBuilder.h>
  2. #include <Kernel/FileSystem/FileDescription.h>
  3. #include <Kernel/FileSystem/VirtualFileSystem.h>
  4. #include <Kernel/Net/LocalSocket.h>
  5. #include <Kernel/Process.h>
  6. #include <Kernel/UnixTypes.h>
  7. #include <LibC/errno_numbers.h>
  8. //#define DEBUG_LOCAL_SOCKET
  9. Lockable<InlineLinkedList<LocalSocket>>& LocalSocket::all_sockets()
  10. {
  11. static Lockable<InlineLinkedList<LocalSocket>>* s_list;
  12. if (!s_list)
  13. s_list = new Lockable<InlineLinkedList<LocalSocket>>();
  14. return *s_list;
  15. }
  16. void LocalSocket::for_each(Function<void(LocalSocket&)> callback)
  17. {
  18. LOCKER(all_sockets().lock());
  19. for (auto& socket : all_sockets().resource())
  20. callback(socket);
  21. }
  22. NonnullRefPtr<LocalSocket> LocalSocket::create(int type)
  23. {
  24. return adopt(*new LocalSocket(type));
  25. }
  26. LocalSocket::LocalSocket(int type)
  27. : Socket(AF_LOCAL, type, 0)
  28. {
  29. LOCKER(all_sockets().lock());
  30. all_sockets().resource().append(this);
  31. #ifdef DEBUG_LOCAL_SOCKET
  32. kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->process().name().characters(), current->pid(), this, type);
  33. #endif
  34. }
  35. LocalSocket::~LocalSocket()
  36. {
  37. LOCKER(all_sockets().lock());
  38. all_sockets().resource().remove(this);
  39. }
  40. bool LocalSocket::get_local_address(sockaddr* address, socklen_t* address_size)
  41. {
  42. // FIXME: Look into what fallback behavior we should have here.
  43. if (*address_size != sizeof(sockaddr_un))
  44. return false;
  45. memcpy(address, &m_address, sizeof(sockaddr_un));
  46. *address_size = sizeof(sockaddr_un);
  47. return true;
  48. }
  49. bool LocalSocket::get_peer_address(sockaddr* address, socklen_t* address_size)
  50. {
  51. return get_local_address(address, address_size);
  52. }
  53. KResult LocalSocket::bind(const sockaddr* address, socklen_t address_size)
  54. {
  55. ASSERT(setup_state() == SetupState::Unstarted);
  56. if (address_size != sizeof(sockaddr_un))
  57. return KResult(-EINVAL);
  58. if (address->sa_family != AF_LOCAL)
  59. return KResult(-EINVAL);
  60. const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
  61. char safe_address[sizeof(local_address.sun_path) + 1] = { 0 };
  62. memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
  63. #ifdef DEBUG_LOCAL_SOCKET
  64. kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
  65. #endif
  66. auto result = VFS::the().open(safe_address, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->process().current_directory());
  67. if (result.is_error()) {
  68. if (result.error() == -EEXIST)
  69. return KResult(-EADDRINUSE);
  70. return result.error();
  71. }
  72. m_file = move(result.value());
  73. ASSERT(m_file->inode());
  74. m_file->inode()->bind_socket(*this);
  75. m_address = local_address;
  76. m_bound = true;
  77. return KSuccess;
  78. }
  79. KResult LocalSocket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock)
  80. {
  81. ASSERT(!m_bound);
  82. if (address_size != sizeof(sockaddr_un))
  83. return KResult(-EINVAL);
  84. if (address->sa_family != AF_LOCAL)
  85. return KResult(-EINVAL);
  86. const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
  87. char safe_address[sizeof(local_address.sun_path) + 1] = { 0 };
  88. memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
  89. #ifdef DEBUG_LOCAL_SOCKET
  90. kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
  91. #endif
  92. auto description_or_error = VFS::the().open(safe_address, 0, 0, current->process().current_directory());
  93. if (description_or_error.is_error())
  94. return KResult(-ECONNREFUSED);
  95. m_file = move(description_or_error.value());
  96. ASSERT(m_file->inode());
  97. if (!m_file->inode()->socket())
  98. return KResult(-ECONNREFUSED);
  99. m_address = local_address;
  100. ASSERT(m_connect_side_fd == &description);
  101. m_connect_side_role = Role::Connecting;
  102. auto peer = m_file->inode()->socket();
  103. auto result = peer->queue_connection_from(*this);
  104. if (result.is_error()) {
  105. m_connect_side_role = Role::None;
  106. return result;
  107. }
  108. if (is_connected()) {
  109. m_connect_side_role = Role::Connected;
  110. return KSuccess;
  111. }
  112. if (current->block<Thread::ConnectBlocker>(description) == Thread::BlockResult::InterruptedBySignal) {
  113. m_connect_side_role = Role::None;
  114. return KResult(-EINTR);
  115. }
  116. #ifdef DEBUG_LOCAL_SOCKET
  117. kprintf("%s(%u) LocalSocket{%p} connect(%s) status is %s\n", current->process().name().characters(), current->pid(), this, safe_address, to_string(setup_state()));
  118. #endif
  119. if (!is_connected()) {
  120. m_connect_side_role = Role::None;
  121. return KResult(-ECONNREFUSED);
  122. }
  123. m_connect_side_role = Role::Connected;
  124. return KSuccess;
  125. }
  126. KResult LocalSocket::listen(int backlog)
  127. {
  128. LOCKER(lock());
  129. if (type() != SOCK_STREAM)
  130. return KResult(-EOPNOTSUPP);
  131. set_backlog(backlog);
  132. m_connect_side_role = m_role = Role::Listener;
  133. kprintf("LocalSocket{%p} listening with backlog=%d\n", this, backlog);
  134. return KSuccess;
  135. }
  136. void LocalSocket::attach(FileDescription& description)
  137. {
  138. ASSERT(!m_accept_side_fd_open);
  139. if (m_connect_side_role == Role::None) {
  140. ASSERT(m_connect_side_fd == nullptr);
  141. m_connect_side_fd = &description;
  142. } else {
  143. ASSERT(m_connect_side_fd != &description);
  144. m_accept_side_fd_open = true;
  145. }
  146. }
  147. void LocalSocket::detach(FileDescription& description)
  148. {
  149. if (m_connect_side_fd == &description) {
  150. m_connect_side_fd = nullptr;
  151. } else {
  152. ASSERT(m_accept_side_fd_open);
  153. m_accept_side_fd_open = false;
  154. }
  155. }
  156. bool LocalSocket::can_read(FileDescription& description) const
  157. {
  158. auto role = this->role(description);
  159. if (role == Role::Listener)
  160. return can_accept();
  161. if (role == Role::Accepted)
  162. return !has_attached_peer(description) || !m_for_server.is_empty();
  163. if (role == Role::Connected)
  164. return !has_attached_peer(description) || !m_for_client.is_empty();
  165. ASSERT_NOT_REACHED();
  166. }
  167. bool LocalSocket::has_attached_peer(const FileDescription& description) const
  168. {
  169. auto role = this->role(description);
  170. if (role == Role::Accepted)
  171. return m_connect_side_fd != nullptr;
  172. if (role == Role::Connected)
  173. return m_accept_side_fd_open;
  174. ASSERT_NOT_REACHED();
  175. }
  176. bool LocalSocket::can_write(FileDescription& description) const
  177. {
  178. auto role = this->role(description);
  179. if (role == Role::Accepted)
  180. return !has_attached_peer(description) || m_for_client.bytes_in_write_buffer() < 16384;
  181. if (role == Role::Connected)
  182. return !has_attached_peer(description) || m_for_server.bytes_in_write_buffer() < 16384;
  183. ASSERT_NOT_REACHED();
  184. }
  185. ssize_t LocalSocket::sendto(FileDescription& description, const void* data, size_t data_size, int, const sockaddr*, socklen_t)
  186. {
  187. if (!has_attached_peer(description))
  188. return -EPIPE;
  189. auto role = this->role(description);
  190. if (role == Role::Accepted)
  191. return m_for_client.write((const u8*)data, data_size);
  192. if (role == Role::Connected)
  193. return m_for_server.write((const u8*)data, data_size);
  194. ASSERT_NOT_REACHED();
  195. }
  196. DoubleBuffer& LocalSocket::buffer_for(FileDescription& description)
  197. {
  198. auto role = this->role(description);
  199. if (role == Role::Accepted)
  200. return m_for_server;
  201. if (role == Role::Connected)
  202. return m_for_client;
  203. ASSERT_NOT_REACHED();
  204. }
  205. ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t buffer_size, int, sockaddr*, socklen_t*)
  206. {
  207. auto& buffer_for_me = buffer_for(description);
  208. if (!description.is_blocking()) {
  209. if (buffer_for_me.is_empty()) {
  210. if (!has_attached_peer(description))
  211. return 0;
  212. return -EAGAIN;
  213. }
  214. } else if (!can_read(description)) {
  215. auto result = current->block<Thread::ReceiveBlocker>(description);
  216. if (result == Thread::BlockResult::InterruptedBySignal)
  217. return -EINTR;
  218. }
  219. if (!has_attached_peer(description) && buffer_for_me.is_empty())
  220. return 0;
  221. ASSERT(!buffer_for_me.is_empty());
  222. return buffer_for_me.read((u8*)buffer, buffer_size);
  223. }
  224. StringView LocalSocket::socket_path() const
  225. {
  226. int len = strnlen(m_address.sun_path, sizeof(m_address.sun_path));
  227. return { m_address.sun_path, len };
  228. }
  229. String LocalSocket::absolute_path(const FileDescription& description) const
  230. {
  231. StringBuilder builder;
  232. builder.append("socket:");
  233. builder.append(socket_path());
  234. switch (role(description)) {
  235. case Role::Listener:
  236. builder.append(" (listening)");
  237. break;
  238. case Role::Accepted:
  239. builder.appendf(" (accepted from pid %d)", origin_pid());
  240. break;
  241. case Role::Connected:
  242. builder.appendf(" (connected to pid %d)", acceptor_pid());
  243. break;
  244. case Role::Connecting:
  245. builder.append(" (connecting)");
  246. break;
  247. default:
  248. break;
  249. }
  250. return builder.to_string();
  251. }