OpenFileDescription.cpp 12 KB

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