LocalSocket.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #ifdef DEBUG_LOCAL_SOCKET
  134. kprintf("LocalSocket{%p} listening with backlog=%d\n", this, backlog);
  135. #endif
  136. return KSuccess;
  137. }
  138. void LocalSocket::attach(FileDescription& description)
  139. {
  140. ASSERT(!m_accept_side_fd_open);
  141. if (m_connect_side_role == Role::None) {
  142. ASSERT(m_connect_side_fd == nullptr);
  143. m_connect_side_fd = &description;
  144. } else {
  145. ASSERT(m_connect_side_fd != &description);
  146. m_accept_side_fd_open = true;
  147. }
  148. }
  149. void LocalSocket::detach(FileDescription& description)
  150. {
  151. if (m_connect_side_fd == &description) {
  152. m_connect_side_fd = nullptr;
  153. } else {
  154. ASSERT(m_accept_side_fd_open);
  155. m_accept_side_fd_open = false;
  156. }
  157. }
  158. bool LocalSocket::can_read(const FileDescription& description) const
  159. {
  160. auto role = this->role(description);
  161. if (role == Role::Listener)
  162. return can_accept();
  163. if (role == Role::Accepted)
  164. return !has_attached_peer(description) || !m_for_server.is_empty();
  165. if (role == Role::Connected)
  166. return !has_attached_peer(description) || !m_for_client.is_empty();
  167. return false;
  168. }
  169. bool LocalSocket::has_attached_peer(const FileDescription& description) const
  170. {
  171. auto role = this->role(description);
  172. if (role == Role::Accepted)
  173. return m_connect_side_fd != nullptr;
  174. if (role == Role::Connected)
  175. return m_accept_side_fd_open;
  176. ASSERT_NOT_REACHED();
  177. }
  178. bool LocalSocket::can_write(const FileDescription& description) const
  179. {
  180. auto role = this->role(description);
  181. if (role == Role::Accepted)
  182. return !has_attached_peer(description) || m_for_client.bytes_in_write_buffer() < 16384;
  183. if (role == Role::Connected)
  184. return !has_attached_peer(description) || m_for_server.bytes_in_write_buffer() < 16384;
  185. return false;
  186. }
  187. ssize_t LocalSocket::sendto(FileDescription& description, const void* data, size_t data_size, int, const sockaddr*, socklen_t)
  188. {
  189. if (!has_attached_peer(description))
  190. return -EPIPE;
  191. auto role = this->role(description);
  192. if (role == Role::Accepted)
  193. return m_for_client.write((const u8*)data, data_size);
  194. if (role == Role::Connected)
  195. return m_for_server.write((const u8*)data, data_size);
  196. ASSERT_NOT_REACHED();
  197. }
  198. DoubleBuffer& LocalSocket::buffer_for(FileDescription& description)
  199. {
  200. auto role = this->role(description);
  201. if (role == Role::Accepted)
  202. return m_for_server;
  203. if (role == Role::Connected)
  204. return m_for_client;
  205. ASSERT_NOT_REACHED();
  206. }
  207. ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t buffer_size, int, sockaddr*, socklen_t*)
  208. {
  209. auto& buffer_for_me = buffer_for(description);
  210. if (!description.is_blocking()) {
  211. if (buffer_for_me.is_empty()) {
  212. if (!has_attached_peer(description))
  213. return 0;
  214. return -EAGAIN;
  215. }
  216. } else if (!can_read(description)) {
  217. auto result = current->block<Thread::ReceiveBlocker>(description);
  218. if (result == Thread::BlockResult::InterruptedBySignal)
  219. return -EINTR;
  220. }
  221. if (!has_attached_peer(description) && buffer_for_me.is_empty())
  222. return 0;
  223. ASSERT(!buffer_for_me.is_empty());
  224. return buffer_for_me.read((u8*)buffer, buffer_size);
  225. }
  226. StringView LocalSocket::socket_path() const
  227. {
  228. int len = strnlen(m_address.sun_path, sizeof(m_address.sun_path));
  229. return { m_address.sun_path, len };
  230. }
  231. String LocalSocket::absolute_path(const FileDescription& description) const
  232. {
  233. StringBuilder builder;
  234. builder.append("socket:");
  235. builder.append(socket_path());
  236. switch (role(description)) {
  237. case Role::Listener:
  238. builder.append(" (listening)");
  239. break;
  240. case Role::Accepted:
  241. builder.appendf(" (accepted from pid %d)", origin_pid());
  242. break;
  243. case Role::Connected:
  244. builder.appendf(" (connected to pid %d)", acceptor_pid());
  245. break;
  246. case Role::Connecting:
  247. builder.append(" (connecting)");
  248. break;
  249. default:
  250. break;
  251. }
  252. return builder.to_string();
  253. }