LocalSocket.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. m_prebind_uid = current->process().uid();
  32. m_prebind_gid = current->process().gid();
  33. m_prebind_mode = 0666;
  34. #ifdef DEBUG_LOCAL_SOCKET
  35. kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->process().name().characters(), current->pid(), this, type);
  36. #endif
  37. }
  38. LocalSocket::~LocalSocket()
  39. {
  40. LOCKER(all_sockets().lock());
  41. all_sockets().resource().remove(this);
  42. }
  43. bool LocalSocket::get_local_address(sockaddr* address, socklen_t* address_size)
  44. {
  45. // FIXME: Look into what fallback behavior we should have here.
  46. if (*address_size != sizeof(sockaddr_un))
  47. return false;
  48. memcpy(address, &m_address, sizeof(sockaddr_un));
  49. *address_size = sizeof(sockaddr_un);
  50. return true;
  51. }
  52. bool LocalSocket::get_peer_address(sockaddr* address, socklen_t* address_size)
  53. {
  54. return get_local_address(address, address_size);
  55. }
  56. KResult LocalSocket::bind(const sockaddr* address, socklen_t address_size)
  57. {
  58. ASSERT(setup_state() == SetupState::Unstarted);
  59. if (address_size != sizeof(sockaddr_un))
  60. return KResult(-EINVAL);
  61. if (address->sa_family != AF_LOCAL)
  62. return KResult(-EINVAL);
  63. const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
  64. char safe_address[sizeof(local_address.sun_path) + 1] = { 0 };
  65. memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
  66. #ifdef DEBUG_LOCAL_SOCKET
  67. kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
  68. #endif
  69. mode_t mode = S_IFSOCK | (m_prebind_mode & 04777);
  70. UidAndGid owner { m_prebind_uid, m_prebind_gid };
  71. auto result = VFS::the().open( safe_address, O_CREAT | O_EXCL, mode, current->process().current_directory(), owner);
  72. if (result.is_error()) {
  73. if (result.error() == -EEXIST)
  74. return KResult(-EADDRINUSE);
  75. return result.error();
  76. }
  77. m_file = move(result.value());
  78. ASSERT(m_file->inode());
  79. m_file->inode()->bind_socket(*this);
  80. m_address = local_address;
  81. m_bound = true;
  82. return KSuccess;
  83. }
  84. KResult LocalSocket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock)
  85. {
  86. ASSERT(!m_bound);
  87. if (address_size != sizeof(sockaddr_un))
  88. return KResult(-EINVAL);
  89. if (address->sa_family != AF_LOCAL)
  90. return KResult(-EINVAL);
  91. if (is_connected())
  92. return KResult(-EISCONN);
  93. const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
  94. char safe_address[sizeof(local_address.sun_path) + 1] = { 0 };
  95. memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
  96. #ifdef DEBUG_LOCAL_SOCKET
  97. kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
  98. #endif
  99. auto description_or_error = VFS::the().open(safe_address, 0, 0, current->process().current_directory());
  100. if (description_or_error.is_error())
  101. return KResult(-ECONNREFUSED);
  102. m_file = move(description_or_error.value());
  103. ASSERT(m_file->inode());
  104. if (!m_file->inode()->socket())
  105. return KResult(-ECONNREFUSED);
  106. m_address = local_address;
  107. ASSERT(m_connect_side_fd == &description);
  108. m_connect_side_role = Role::Connecting;
  109. auto peer = m_file->inode()->socket();
  110. auto result = peer->queue_connection_from(*this);
  111. if (result.is_error()) {
  112. m_connect_side_role = Role::None;
  113. return result;
  114. }
  115. if (is_connected()) {
  116. m_connect_side_role = Role::Connected;
  117. return KSuccess;
  118. }
  119. if (current->block<Thread::ConnectBlocker>(description) == Thread::BlockResult::InterruptedBySignal) {
  120. m_connect_side_role = Role::None;
  121. return KResult(-EINTR);
  122. }
  123. #ifdef DEBUG_LOCAL_SOCKET
  124. kprintf("%s(%u) LocalSocket{%p} connect(%s) status is %s\n", current->process().name().characters(), current->pid(), this, safe_address, to_string(setup_state()));
  125. #endif
  126. if (!is_connected()) {
  127. m_connect_side_role = Role::None;
  128. return KResult(-ECONNREFUSED);
  129. }
  130. m_connect_side_role = Role::Connected;
  131. return KSuccess;
  132. }
  133. KResult LocalSocket::listen(int backlog)
  134. {
  135. LOCKER(lock());
  136. if (type() != SOCK_STREAM)
  137. return KResult(-EOPNOTSUPP);
  138. set_backlog(backlog);
  139. m_connect_side_role = m_role = Role::Listener;
  140. #ifdef DEBUG_LOCAL_SOCKET
  141. kprintf("LocalSocket{%p} listening with backlog=%d\n", this, backlog);
  142. #endif
  143. return KSuccess;
  144. }
  145. void LocalSocket::attach(FileDescription& description)
  146. {
  147. ASSERT(!m_accept_side_fd_open);
  148. if (m_connect_side_role == Role::None) {
  149. ASSERT(m_connect_side_fd == nullptr);
  150. m_connect_side_fd = &description;
  151. } else {
  152. ASSERT(m_connect_side_fd != &description);
  153. m_accept_side_fd_open = true;
  154. }
  155. }
  156. void LocalSocket::detach(FileDescription& description)
  157. {
  158. if (m_connect_side_fd == &description) {
  159. m_connect_side_fd = nullptr;
  160. } else {
  161. ASSERT(m_accept_side_fd_open);
  162. m_accept_side_fd_open = false;
  163. }
  164. }
  165. bool LocalSocket::can_read(const FileDescription& description) const
  166. {
  167. auto role = this->role(description);
  168. if (role == Role::Listener)
  169. return can_accept();
  170. if (role == Role::Accepted)
  171. return !has_attached_peer(description) || !m_for_server.is_empty();
  172. if (role == Role::Connected)
  173. return !has_attached_peer(description) || !m_for_client.is_empty();
  174. return false;
  175. }
  176. bool LocalSocket::has_attached_peer(const FileDescription& description) const
  177. {
  178. auto role = this->role(description);
  179. if (role == Role::Accepted)
  180. return m_connect_side_fd != nullptr;
  181. if (role == Role::Connected)
  182. return m_accept_side_fd_open;
  183. ASSERT_NOT_REACHED();
  184. }
  185. bool LocalSocket::can_write(const FileDescription& description) const
  186. {
  187. auto role = this->role(description);
  188. if (role == Role::Accepted)
  189. return !has_attached_peer(description) || m_for_client.bytes_in_write_buffer() < 16384;
  190. if (role == Role::Connected)
  191. return !has_attached_peer(description) || m_for_server.bytes_in_write_buffer() < 16384;
  192. return false;
  193. }
  194. ssize_t LocalSocket::sendto(FileDescription& description, const void* data, size_t data_size, int, const sockaddr*, socklen_t)
  195. {
  196. if (!has_attached_peer(description))
  197. return -EPIPE;
  198. ssize_t nwritten = send_buffer_for(description).write((const u8*)data, data_size);
  199. if (nwritten > 0)
  200. current->did_unix_socket_write(nwritten);
  201. return nwritten;
  202. }
  203. DoubleBuffer& LocalSocket::receive_buffer_for(FileDescription& description)
  204. {
  205. auto role = this->role(description);
  206. if (role == Role::Accepted)
  207. return m_for_server;
  208. if (role == Role::Connected)
  209. return m_for_client;
  210. ASSERT_NOT_REACHED();
  211. }
  212. DoubleBuffer& LocalSocket::send_buffer_for(FileDescription& description)
  213. {
  214. auto role = this->role(description);
  215. if (role == Role::Connected)
  216. return m_for_server;
  217. if (role == Role::Accepted)
  218. return m_for_client;
  219. ASSERT_NOT_REACHED();
  220. }
  221. ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t buffer_size, int, sockaddr*, socklen_t*)
  222. {
  223. auto& buffer_for_me = receive_buffer_for(description);
  224. if (!description.is_blocking()) {
  225. if (buffer_for_me.is_empty()) {
  226. if (!has_attached_peer(description))
  227. return 0;
  228. return -EAGAIN;
  229. }
  230. } else if (!can_read(description)) {
  231. auto result = current->block<Thread::ReceiveBlocker>(description);
  232. if (result == Thread::BlockResult::InterruptedBySignal)
  233. return -EINTR;
  234. }
  235. if (!has_attached_peer(description) && buffer_for_me.is_empty())
  236. return 0;
  237. ASSERT(!buffer_for_me.is_empty());
  238. int nread = buffer_for_me.read((u8*)buffer, buffer_size);
  239. if (nread > 0)
  240. current->did_unix_socket_read(nread);
  241. return nread;
  242. }
  243. StringView LocalSocket::socket_path() const
  244. {
  245. size_t len = strnlen(m_address.sun_path, sizeof(m_address.sun_path));
  246. return { m_address.sun_path, len };
  247. }
  248. String LocalSocket::absolute_path(const FileDescription& description) const
  249. {
  250. StringBuilder builder;
  251. builder.append("socket:");
  252. builder.append(socket_path());
  253. switch (role(description)) {
  254. case Role::Listener:
  255. builder.append(" (listening)");
  256. break;
  257. case Role::Accepted:
  258. builder.appendf(" (accepted from pid %d)", origin_pid());
  259. break;
  260. case Role::Connected:
  261. builder.appendf(" (connected to pid %d)", acceptor_pid());
  262. break;
  263. case Role::Connecting:
  264. builder.append(" (connecting)");
  265. break;
  266. default:
  267. break;
  268. }
  269. return builder.to_string();
  270. }
  271. KResult LocalSocket::getsockopt(FileDescription& description, int level, int option, void* value, socklen_t* value_size)
  272. {
  273. if (level != SOL_SOCKET)
  274. return Socket::getsockopt(description, level, option, value, value_size);
  275. switch (option) {
  276. case SO_PEERCRED: {
  277. if (*value_size < sizeof(ucred))
  278. return KResult(-EINVAL);
  279. auto& creds = *(ucred*)value;
  280. switch (role(description)) {
  281. case Role::Accepted:
  282. creds = m_origin;
  283. *value_size = sizeof(ucred);
  284. return KSuccess;
  285. case Role::Connected:
  286. creds = m_acceptor;
  287. *value_size = sizeof(ucred);
  288. return KSuccess;
  289. case Role::Connecting:
  290. return KResult(-ENOTCONN);
  291. default:
  292. return KResult(-EINVAL);
  293. }
  294. break;
  295. }
  296. default:
  297. return Socket::getsockopt(description, level, option, value, value_size);
  298. }
  299. }
  300. KResult LocalSocket::chmod(mode_t mode)
  301. {
  302. if (m_file)
  303. return m_file->chmod(mode);
  304. m_prebind_mode = mode & 04777;
  305. return KSuccess;
  306. }
  307. KResult LocalSocket::chown(uid_t uid, gid_t gid)
  308. {
  309. if (m_file)
  310. return m_file->chown(uid, gid);
  311. if (!current->process().is_superuser() && (current->process().euid() != uid || !current->process().in_group(gid)))
  312. return KResult(-EPERM);
  313. m_prebind_uid = uid;
  314. m_prebind_gid = gid;
  315. return KSuccess;
  316. }