OpenFileDescription.cpp 12 KB

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