FileDescription.cpp 11 KB

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