FileDescription.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/MemoryStream.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Devices/BlockDevice.h>
  10. #include <Kernel/FileSystem/Custody.h>
  11. #include <Kernel/FileSystem/FIFO.h>
  12. #include <Kernel/FileSystem/FileDescription.h>
  13. #include <Kernel/FileSystem/FileSystem.h>
  14. #include <Kernel/FileSystem/InodeFile.h>
  15. #include <Kernel/FileSystem/InodeWatcher.h>
  16. #include <Kernel/Net/Socket.h>
  17. #include <Kernel/Process.h>
  18. #include <Kernel/TTY/MasterPTY.h>
  19. #include <Kernel/TTY/TTY.h>
  20. #include <Kernel/UnixTypes.h>
  21. #include <Kernel/VM/MemoryManager.h>
  22. #include <LibC/errno_numbers.h>
  23. namespace Kernel {
  24. KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(Custody& custody)
  25. {
  26. auto inode_file = InodeFile::create(custody.inode());
  27. if (inode_file.is_error())
  28. return inode_file.error();
  29. auto description = adopt_ref_if_nonnull(new FileDescription(*inode_file.release_value()));
  30. if (!description)
  31. return ENOMEM;
  32. description->m_custody = custody;
  33. auto result = description->attach();
  34. if (result.is_error()) {
  35. dbgln_if(FILEDESCRIPTION_DEBUG, "Failed to create file description for custody: {}", result);
  36. return result;
  37. }
  38. return description.release_nonnull();
  39. }
  40. KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(File& file)
  41. {
  42. auto description = adopt_ref_if_nonnull(new FileDescription(file));
  43. if (!description)
  44. return ENOMEM;
  45. auto result = description->attach();
  46. if (result.is_error()) {
  47. dbgln_if(FILEDESCRIPTION_DEBUG, "Failed to create file description for file: {}", result);
  48. return result;
  49. }
  50. return description.release_nonnull();
  51. }
  52. FileDescription::FileDescription(File& file)
  53. : m_file(file)
  54. {
  55. if (file.is_inode())
  56. m_inode = static_cast<InodeFile&>(file).inode();
  57. m_is_directory = metadata().is_directory();
  58. }
  59. FileDescription::~FileDescription()
  60. {
  61. m_file->detach(*this);
  62. if (is_fifo())
  63. static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
  64. // FIXME: Should this error path be observed somehow?
  65. (void)m_file->close();
  66. if (m_inode)
  67. m_inode->detach(*this);
  68. }
  69. KResult FileDescription::attach()
  70. {
  71. if (m_inode) {
  72. auto result = m_inode->attach(*this);
  73. if (result.is_error())
  74. return result;
  75. }
  76. return m_file->attach(*this);
  77. }
  78. Thread::FileBlocker::BlockFlags FileDescription::should_unblock(Thread::FileBlocker::BlockFlags block_flags) const
  79. {
  80. using BlockFlags = Thread::FileBlocker::BlockFlags;
  81. BlockFlags unblock_flags = BlockFlags::None;
  82. if (has_flag(block_flags, BlockFlags::Read) && can_read())
  83. unblock_flags |= BlockFlags::Read;
  84. if (has_flag(block_flags, BlockFlags::Write) && can_write())
  85. unblock_flags |= BlockFlags::Write;
  86. // TODO: Implement Thread::FileBlocker::BlockFlags::Exception
  87. if (has_flag(block_flags, BlockFlags::SocketFlags)) {
  88. auto* sock = socket();
  89. VERIFY(sock);
  90. if (has_flag(block_flags, BlockFlags::Accept) && sock->can_accept())
  91. unblock_flags |= BlockFlags::Accept;
  92. if (has_flag(block_flags, BlockFlags::Connect) && sock->setup_state() == Socket::SetupState::Completed)
  93. unblock_flags |= BlockFlags::Connect;
  94. }
  95. return unblock_flags;
  96. }
  97. KResult FileDescription::stat(::stat& buffer)
  98. {
  99. Locker locker(m_lock);
  100. return m_file->stat(buffer);
  101. }
  102. KResultOr<off_t> FileDescription::seek(off_t offset, int whence)
  103. {
  104. Locker locker(m_lock);
  105. if (!m_file->is_seekable())
  106. return ESPIPE;
  107. off_t new_offset;
  108. switch (whence) {
  109. case SEEK_SET:
  110. new_offset = offset;
  111. break;
  112. case SEEK_CUR:
  113. if (Checked<off_t>::addition_would_overflow(m_current_offset, offset))
  114. return EOVERFLOW;
  115. new_offset = m_current_offset + offset;
  116. break;
  117. case SEEK_END:
  118. if (!metadata().is_valid())
  119. return EIO;
  120. if (Checked<off_t>::addition_would_overflow(metadata().size, offset))
  121. return EOVERFLOW;
  122. new_offset = metadata().size + offset;
  123. break;
  124. default:
  125. return EINVAL;
  126. }
  127. if (new_offset < 0)
  128. return EINVAL;
  129. // FIXME: Return EINVAL if attempting to seek past the end of a seekable device.
  130. m_current_offset = new_offset;
  131. m_file->did_seek(*this, new_offset);
  132. if (m_inode)
  133. m_inode->did_seek(*this, new_offset);
  134. evaluate_block_conditions();
  135. return m_current_offset;
  136. }
  137. KResultOr<size_t> FileDescription::read(UserOrKernelBuffer& buffer, size_t count)
  138. {
  139. Locker locker(m_lock);
  140. if (Checked<off_t>::addition_would_overflow(m_current_offset, count))
  141. return EOVERFLOW;
  142. auto nread_or_error = m_file->read(*this, offset(), buffer, count);
  143. if (!nread_or_error.is_error()) {
  144. if (m_file->is_seekable())
  145. m_current_offset += nread_or_error.value();
  146. evaluate_block_conditions();
  147. }
  148. return nread_or_error;
  149. }
  150. KResultOr<size_t> FileDescription::write(const UserOrKernelBuffer& data, size_t size)
  151. {
  152. Locker locker(m_lock);
  153. if (Checked<off_t>::addition_would_overflow(m_current_offset, size))
  154. return EOVERFLOW;
  155. auto nwritten_or_error = m_file->write(*this, offset(), data, size);
  156. if (!nwritten_or_error.is_error()) {
  157. if (m_file->is_seekable())
  158. m_current_offset += nwritten_or_error.value();
  159. evaluate_block_conditions();
  160. }
  161. return nwritten_or_error;
  162. }
  163. bool FileDescription::can_write() const
  164. {
  165. return m_file->can_write(*this, offset());
  166. }
  167. bool FileDescription::can_read() const
  168. {
  169. return m_file->can_read(*this, offset());
  170. }
  171. KResultOr<NonnullOwnPtr<KBuffer>> FileDescription::read_entire_file()
  172. {
  173. // HACK ALERT: (This entire function)
  174. VERIFY(m_file->is_inode());
  175. VERIFY(m_inode);
  176. return m_inode->read_entire(this);
  177. }
  178. KResultOr<ssize_t> FileDescription::get_dir_entries(UserOrKernelBuffer& output_buffer, ssize_t size)
  179. {
  180. Locker locker(m_lock, Lock::Mode::Shared);
  181. if (!is_directory())
  182. return ENOTDIR;
  183. auto metadata = this->metadata();
  184. if (!metadata.is_valid())
  185. return EIO;
  186. if (size < 0)
  187. return EINVAL;
  188. size_t remaining = size;
  189. KResult error = KSuccess;
  190. u8 stack_buffer[PAGE_SIZE];
  191. Bytes temp_buffer(stack_buffer, sizeof(stack_buffer));
  192. OutputMemoryStream stream { temp_buffer };
  193. auto flush_stream_to_output_buffer = [&error, &stream, &remaining, &output_buffer]() -> bool {
  194. if (error != 0)
  195. return false;
  196. if (stream.size() == 0)
  197. return true;
  198. if (remaining < stream.size()) {
  199. error = EINVAL;
  200. return false;
  201. } else if (!output_buffer.write(stream.bytes())) {
  202. error = EFAULT;
  203. return false;
  204. }
  205. output_buffer = output_buffer.offset(stream.size());
  206. remaining -= stream.size();
  207. stream.reset();
  208. return true;
  209. };
  210. KResult result = VFS::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &error, &remaining, &output_buffer, &stream, this](auto& entry) {
  211. size_t serialized_size = sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * entry.name.length();
  212. if (serialized_size > stream.remaining()) {
  213. if (!flush_stream_to_output_buffer()) {
  214. return false;
  215. }
  216. }
  217. stream << (u32)entry.inode.index().value();
  218. stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
  219. stream << (u32)entry.name.length();
  220. stream << entry.name.bytes();
  221. return true;
  222. });
  223. flush_stream_to_output_buffer();
  224. if (result.is_error()) {
  225. // We should only return -EFAULT when the userspace buffer is too small,
  226. // so that userspace can reliably use it as a signal to increase its
  227. // buffer size.
  228. VERIFY(result != -EFAULT);
  229. return result;
  230. }
  231. if (error) {
  232. return error;
  233. }
  234. return size - remaining;
  235. }
  236. bool FileDescription::is_device() const
  237. {
  238. return m_file->is_device();
  239. }
  240. const Device* FileDescription::device() const
  241. {
  242. if (!is_device())
  243. return nullptr;
  244. return static_cast<const Device*>(m_file.ptr());
  245. }
  246. Device* FileDescription::device()
  247. {
  248. if (!is_device())
  249. return nullptr;
  250. return static_cast<Device*>(m_file.ptr());
  251. }
  252. bool FileDescription::is_tty() const
  253. {
  254. return m_file->is_tty();
  255. }
  256. const TTY* FileDescription::tty() const
  257. {
  258. if (!is_tty())
  259. return nullptr;
  260. return static_cast<const TTY*>(m_file.ptr());
  261. }
  262. TTY* FileDescription::tty()
  263. {
  264. if (!is_tty())
  265. return nullptr;
  266. return static_cast<TTY*>(m_file.ptr());
  267. }
  268. bool FileDescription::is_inode_watcher() const
  269. {
  270. return m_file->is_inode_watcher();
  271. }
  272. const InodeWatcher* FileDescription::inode_watcher() const
  273. {
  274. if (!is_inode_watcher())
  275. return nullptr;
  276. return static_cast<const InodeWatcher*>(m_file.ptr());
  277. }
  278. InodeWatcher* FileDescription::inode_watcher()
  279. {
  280. if (!is_inode_watcher())
  281. return nullptr;
  282. return static_cast<InodeWatcher*>(m_file.ptr());
  283. }
  284. bool FileDescription::is_master_pty() const
  285. {
  286. return m_file->is_master_pty();
  287. }
  288. const MasterPTY* FileDescription::master_pty() const
  289. {
  290. if (!is_master_pty())
  291. return nullptr;
  292. return static_cast<const MasterPTY*>(m_file.ptr());
  293. }
  294. MasterPTY* FileDescription::master_pty()
  295. {
  296. if (!is_master_pty())
  297. return nullptr;
  298. return static_cast<MasterPTY*>(m_file.ptr());
  299. }
  300. KResult FileDescription::close()
  301. {
  302. if (m_file->attach_count() > 0)
  303. return KSuccess;
  304. return m_file->close();
  305. }
  306. String FileDescription::absolute_path() const
  307. {
  308. if (m_custody)
  309. return m_custody->absolute_path();
  310. return m_file->absolute_path(*this);
  311. }
  312. InodeMetadata FileDescription::metadata() const
  313. {
  314. if (m_inode)
  315. return m_inode->metadata();
  316. return {};
  317. }
  318. KResultOr<Region*> FileDescription::mmap(Process& process, const Range& range, u64 offset, int prot, bool shared)
  319. {
  320. Locker locker(m_lock);
  321. return m_file->mmap(process, *this, range, offset, prot, shared);
  322. }
  323. KResult FileDescription::truncate(u64 length)
  324. {
  325. Locker locker(m_lock);
  326. return m_file->truncate(length);
  327. }
  328. bool FileDescription::is_fifo() const
  329. {
  330. return m_file->is_fifo();
  331. }
  332. FIFO* FileDescription::fifo()
  333. {
  334. if (!is_fifo())
  335. return nullptr;
  336. return static_cast<FIFO*>(m_file.ptr());
  337. }
  338. bool FileDescription::is_socket() const
  339. {
  340. return m_file->is_socket();
  341. }
  342. Socket* FileDescription::socket()
  343. {
  344. if (!is_socket())
  345. return nullptr;
  346. return static_cast<Socket*>(m_file.ptr());
  347. }
  348. const Socket* FileDescription::socket() const
  349. {
  350. if (!is_socket())
  351. return nullptr;
  352. return static_cast<const Socket*>(m_file.ptr());
  353. }
  354. void FileDescription::set_file_flags(u32 flags)
  355. {
  356. Locker locker(m_lock);
  357. m_is_blocking = !(flags & O_NONBLOCK);
  358. m_should_append = flags & O_APPEND;
  359. m_direct = flags & O_DIRECT;
  360. m_file_flags = flags;
  361. }
  362. KResult FileDescription::chmod(mode_t mode)
  363. {
  364. Locker locker(m_lock);
  365. return m_file->chmod(*this, mode);
  366. }
  367. KResult FileDescription::chown(uid_t uid, gid_t gid)
  368. {
  369. Locker locker(m_lock);
  370. return m_file->chown(*this, uid, gid);
  371. }
  372. FileBlockCondition& FileDescription::block_condition()
  373. {
  374. return m_file->block_condition();
  375. }
  376. }