LocalSocket.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. set_setup_state(SetupState::Completed);
  78. return KSuccess;
  79. }
  80. KResult LocalSocket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock)
  81. {
  82. ASSERT(!m_bound);
  83. if (address_size != sizeof(sockaddr_un))
  84. return KResult(-EINVAL);
  85. if (address->sa_family != AF_LOCAL)
  86. return KResult(-EINVAL);
  87. const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
  88. char safe_address[sizeof(local_address.sun_path) + 1] = { 0 };
  89. memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
  90. #ifdef DEBUG_LOCAL_SOCKET
  91. kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
  92. #endif
  93. auto description_or_error = VFS::the().open(safe_address, 0, 0, current->process().current_directory());
  94. if (description_or_error.is_error())
  95. return KResult(-ECONNREFUSED);
  96. m_file = move(description_or_error.value());
  97. ASSERT(m_file->inode());
  98. if (!m_file->inode()->socket())
  99. return KResult(-ECONNREFUSED);
  100. m_address = local_address;
  101. ASSERT(m_connect_side_fd == &description);
  102. m_connect_side_role = Role::Connecting;
  103. auto peer = m_file->inode()->socket();
  104. auto result = peer->queue_connection_from(*this);
  105. if (result.is_error()) {
  106. m_connect_side_role = Role::None;
  107. return result;
  108. }
  109. if (is_connected()) {
  110. m_connect_side_role = Role::Connected;
  111. return KSuccess;
  112. }
  113. if (current->block<Thread::ConnectBlocker>(description) == Thread::BlockResult::InterruptedBySignal) {
  114. m_connect_side_role = Role::None;
  115. return KResult(-EINTR);
  116. }
  117. #ifdef DEBUG_LOCAL_SOCKET
  118. kprintf("%s(%u) LocalSocket{%p} connect(%s) status is %s\n", current->process().name().characters(), current->pid(), this, safe_address, to_string(setup_state()));
  119. #endif
  120. if (!is_connected()) {
  121. m_connect_side_role = Role::None;
  122. return KResult(-ECONNREFUSED);
  123. }
  124. m_connect_side_role = Role::Connected;
  125. return KSuccess;
  126. }
  127. KResult LocalSocket::listen(int backlog)
  128. {
  129. LOCKER(lock());
  130. if (type() != SOCK_STREAM)
  131. return KResult(-EOPNOTSUPP);
  132. set_backlog(backlog);
  133. m_connect_side_role = m_role = Role::Listener;
  134. kprintf("LocalSocket{%p} listening with backlog=%d\n", this, backlog);
  135. return KSuccess;
  136. }
  137. void LocalSocket::attach(FileDescription& description)
  138. {
  139. ASSERT(!m_accept_side_fd_open);
  140. if (m_connect_side_role == Role::None) {
  141. ASSERT(m_connect_side_fd == nullptr);
  142. m_connect_side_fd = &description;
  143. } else {
  144. ASSERT(m_connect_side_fd != &description);
  145. m_accept_side_fd_open = true;
  146. }
  147. }
  148. void LocalSocket::detach(FileDescription& description)
  149. {
  150. if (m_connect_side_fd == &description) {
  151. m_connect_side_fd = nullptr;
  152. } else {
  153. ASSERT(m_accept_side_fd_open);
  154. m_accept_side_fd_open = false;
  155. }
  156. }
  157. bool LocalSocket::can_read(FileDescription& description) const
  158. {
  159. auto role = this->role(description);
  160. if (role == Role::Listener)
  161. return can_accept();
  162. if (role == Role::Accepted)
  163. return !has_attached_peer(description) || !m_for_server.is_empty();
  164. if (role == Role::Connected)
  165. return !has_attached_peer(description) || !m_for_client.is_empty();
  166. ASSERT_NOT_REACHED();
  167. }
  168. bool LocalSocket::has_attached_peer(const FileDescription& description) const
  169. {
  170. auto role = this->role(description);
  171. if (role == Role::Accepted)
  172. return m_connect_side_fd != nullptr;
  173. if (role == Role::Connected)
  174. return m_accept_side_fd_open;
  175. ASSERT_NOT_REACHED();
  176. }
  177. bool LocalSocket::can_write(FileDescription& description) const
  178. {
  179. auto role = this->role(description);
  180. if (role == Role::Accepted)
  181. return !has_attached_peer(description) || m_for_client.bytes_in_write_buffer() < 16384;
  182. if (role == Role::Connected)
  183. return !has_attached_peer(description) || m_for_server.bytes_in_write_buffer() < 16384;
  184. ASSERT_NOT_REACHED();
  185. }
  186. ssize_t LocalSocket::sendto(FileDescription& description, const void* data, size_t data_size, int, const sockaddr*, socklen_t)
  187. {
  188. if (!has_attached_peer(description))
  189. return -EPIPE;
  190. auto role = this->role(description);
  191. if (role == Role::Accepted)
  192. return m_for_client.write((const u8*)data, data_size);
  193. if (role == Role::Connected)
  194. return m_for_server.write((const u8*)data, data_size);
  195. ASSERT_NOT_REACHED();
  196. }
  197. DoubleBuffer& LocalSocket::buffer_for(FileDescription& description)
  198. {
  199. auto role = this->role(description);
  200. if (role == Role::Accepted)
  201. return m_for_server;
  202. if (role == Role::Connected)
  203. return m_for_client;
  204. ASSERT_NOT_REACHED();
  205. }
  206. ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t buffer_size, int, sockaddr*, socklen_t*)
  207. {
  208. auto& buffer_for_me = buffer_for(description);
  209. if (!description.is_blocking()) {
  210. if (buffer_for_me.is_empty()) {
  211. if (!has_attached_peer(description))
  212. return 0;
  213. return -EAGAIN;
  214. }
  215. } else if (!can_read(description)) {
  216. auto result = current->block<Thread::ReceiveBlocker>(description);
  217. if (result == Thread::BlockResult::InterruptedBySignal)
  218. return -EINTR;
  219. }
  220. if (!has_attached_peer(description) && buffer_for_me.is_empty())
  221. return 0;
  222. ASSERT(!buffer_for_me.is_empty());
  223. return buffer_for_me.read((u8*)buffer, buffer_size);
  224. }
  225. StringView LocalSocket::socket_path() const
  226. {
  227. int len = strnlen(m_address.sun_path, sizeof(m_address.sun_path));
  228. return { m_address.sun_path, len };
  229. }
  230. String LocalSocket::absolute_path(const FileDescription& description) const
  231. {
  232. StringBuilder builder;
  233. builder.append("socket:");
  234. builder.append(socket_path());
  235. switch (role(description)) {
  236. case Role::Listener:
  237. builder.append(" (listening)");
  238. break;
  239. case Role::Accepted:
  240. builder.appendf(" (accepted from pid %d)", origin_pid());
  241. break;
  242. case Role::Connected:
  243. builder.appendf(" (connected to pid %d)", acceptor_pid());
  244. break;
  245. case Role::Connecting:
  246. builder.append(" (connecting)");
  247. break;
  248. default:
  249. break;
  250. }
  251. return builder.to_string();
  252. }