FileDescription.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/MemoryStream.h>
  27. #include <Kernel/Devices/BlockDevice.h>
  28. #include <Kernel/Devices/CharacterDevice.h>
  29. #include <Kernel/FileSystem/Custody.h>
  30. #include <Kernel/FileSystem/FIFO.h>
  31. #include <Kernel/FileSystem/FileDescription.h>
  32. #include <Kernel/FileSystem/FileSystem.h>
  33. #include <Kernel/FileSystem/InodeFile.h>
  34. #include <Kernel/Net/Socket.h>
  35. #include <Kernel/Process.h>
  36. #include <Kernel/TTY/MasterPTY.h>
  37. #include <Kernel/TTY/TTY.h>
  38. #include <Kernel/UnixTypes.h>
  39. #include <Kernel/VM/MemoryManager.h>
  40. #include <LibC/errno_numbers.h>
  41. namespace Kernel {
  42. NonnullRefPtr<FileDescription> FileDescription::create(Custody& custody)
  43. {
  44. auto description = adopt(*new FileDescription(InodeFile::create(custody.inode())));
  45. description->m_custody = custody;
  46. return description;
  47. }
  48. NonnullRefPtr<FileDescription> FileDescription::create(File& file)
  49. {
  50. return adopt(*new FileDescription(file));
  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. if (is_socket())
  58. socket()->attach(*this);
  59. m_is_directory = metadata().is_directory();
  60. }
  61. FileDescription::~FileDescription()
  62. {
  63. if (is_socket())
  64. socket()->detach(*this);
  65. if (is_fifo())
  66. static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
  67. // FIXME: Should this error path be observed somehow?
  68. (void)m_file->close();
  69. m_inode = nullptr;
  70. }
  71. Thread::FileBlocker::BlockFlags FileDescription::should_unblock(Thread::FileBlocker::BlockFlags block_flags) const
  72. {
  73. u32 unblock_flags = (u32)Thread::FileBlocker::BlockFlags::None;
  74. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Read) && can_read())
  75. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Read;
  76. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Write) && can_write())
  77. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Write;
  78. // TODO: Implement Thread::FileBlocker::BlockFlags::Exception
  79. if ((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::SocketFlags) {
  80. auto* sock = socket();
  81. ASSERT(sock);
  82. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Accept) && sock->can_accept())
  83. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Accept;
  84. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Connect) && sock->setup_state() == Socket::SetupState::Completed)
  85. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Connect;
  86. }
  87. return (Thread::FileBlocker::BlockFlags)unblock_flags;
  88. }
  89. KResult FileDescription::stat(::stat& buffer)
  90. {
  91. LOCKER(m_lock);
  92. // FIXME: This is a little awkward, why can't we always forward to File::stat()?
  93. if (m_inode)
  94. return metadata().stat(buffer);
  95. return m_file->stat(buffer);
  96. }
  97. off_t FileDescription::seek(off_t offset, int whence)
  98. {
  99. LOCKER(m_lock);
  100. if (!m_file->is_seekable())
  101. return -ESPIPE;
  102. off_t new_offset;
  103. switch (whence) {
  104. case SEEK_SET:
  105. new_offset = offset;
  106. break;
  107. case SEEK_CUR:
  108. new_offset = m_current_offset + offset;
  109. break;
  110. case SEEK_END:
  111. if (!metadata().is_valid())
  112. return -EIO;
  113. new_offset = metadata().size;
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. if (new_offset < 0)
  119. return -EINVAL;
  120. // FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
  121. m_current_offset = new_offset;
  122. evaluate_block_conditions();
  123. return m_current_offset;
  124. }
  125. KResultOr<size_t> FileDescription::read(UserOrKernelBuffer& buffer, size_t count)
  126. {
  127. LOCKER(m_lock);
  128. Checked<size_t> new_offset = m_current_offset;
  129. new_offset += count;
  130. if (new_offset.has_overflow())
  131. return -EOVERFLOW;
  132. auto nread_or_error = m_file->read(*this, offset(), buffer, count);
  133. if (!nread_or_error.is_error()) {
  134. if (m_file->is_seekable())
  135. m_current_offset += nread_or_error.value();
  136. evaluate_block_conditions();
  137. }
  138. return nread_or_error;
  139. }
  140. KResultOr<size_t> FileDescription::write(const UserOrKernelBuffer& data, size_t size)
  141. {
  142. LOCKER(m_lock);
  143. Checked<size_t> new_offset = m_current_offset;
  144. new_offset += size;
  145. if (new_offset.has_overflow())
  146. return -EOVERFLOW;
  147. auto nwritten_or_error = m_file->write(*this, offset(), data, size);
  148. if (!nwritten_or_error.is_error()) {
  149. if (m_file->is_seekable())
  150. m_current_offset += nwritten_or_error.value();
  151. evaluate_block_conditions();
  152. }
  153. return nwritten_or_error;
  154. }
  155. bool FileDescription::can_write() const
  156. {
  157. return m_file->can_write(*this, offset());
  158. }
  159. bool FileDescription::can_read() const
  160. {
  161. return m_file->can_read(*this, offset());
  162. }
  163. KResultOr<NonnullOwnPtr<KBuffer>> FileDescription::read_entire_file()
  164. {
  165. // HACK ALERT: (This entire function)
  166. ASSERT(m_file->is_inode());
  167. ASSERT(m_inode);
  168. return m_inode->read_entire(this);
  169. }
  170. ssize_t FileDescription::get_dir_entries(UserOrKernelBuffer& buffer, ssize_t size)
  171. {
  172. LOCKER(m_lock, Lock::Mode::Shared);
  173. if (!is_directory())
  174. return -ENOTDIR;
  175. auto metadata = this->metadata();
  176. if (!metadata.is_valid())
  177. return -EIO;
  178. if (size < 0)
  179. return -EINVAL;
  180. size_t size_to_allocate = max(static_cast<size_t>(PAGE_SIZE), static_cast<size_t>(metadata.size));
  181. auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
  182. OutputMemoryStream stream { temp_buffer };
  183. KResult result = VFS::the().traverse_directory_inode(*m_inode, [&stream, this](auto& entry) {
  184. stream << (u32)entry.inode.index();
  185. stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
  186. stream << (u32)entry.name.length();
  187. stream << entry.name.bytes();
  188. return true;
  189. });
  190. if (result.is_error())
  191. return result;
  192. if (stream.handle_recoverable_error())
  193. return -EINVAL;
  194. if (!buffer.write(stream.bytes()))
  195. return -EFAULT;
  196. return stream.size();
  197. }
  198. bool FileDescription::is_device() const
  199. {
  200. return m_file->is_device();
  201. }
  202. const Device* FileDescription::device() const
  203. {
  204. if (!is_device())
  205. return nullptr;
  206. return static_cast<const Device*>(m_file.ptr());
  207. }
  208. Device* FileDescription::device()
  209. {
  210. if (!is_device())
  211. return nullptr;
  212. return static_cast<Device*>(m_file.ptr());
  213. }
  214. bool FileDescription::is_tty() const
  215. {
  216. return m_file->is_tty();
  217. }
  218. const TTY* FileDescription::tty() const
  219. {
  220. if (!is_tty())
  221. return nullptr;
  222. return static_cast<const TTY*>(m_file.ptr());
  223. }
  224. TTY* FileDescription::tty()
  225. {
  226. if (!is_tty())
  227. return nullptr;
  228. return static_cast<TTY*>(m_file.ptr());
  229. }
  230. bool FileDescription::is_master_pty() const
  231. {
  232. return m_file->is_master_pty();
  233. }
  234. const MasterPTY* FileDescription::master_pty() const
  235. {
  236. if (!is_master_pty())
  237. return nullptr;
  238. return static_cast<const MasterPTY*>(m_file.ptr());
  239. }
  240. MasterPTY* FileDescription::master_pty()
  241. {
  242. if (!is_master_pty())
  243. return nullptr;
  244. return static_cast<MasterPTY*>(m_file.ptr());
  245. }
  246. KResult FileDescription::close()
  247. {
  248. if (m_file->ref_count() > 1)
  249. return KSuccess;
  250. return m_file->close();
  251. }
  252. String FileDescription::absolute_path() const
  253. {
  254. if (m_custody)
  255. return m_custody->absolute_path();
  256. return m_file->absolute_path(*this);
  257. }
  258. InodeMetadata FileDescription::metadata() const
  259. {
  260. if (m_inode)
  261. return m_inode->metadata();
  262. return {};
  263. }
  264. KResultOr<Region*> FileDescription::mmap(Process& process, VirtualAddress vaddr, size_t offset, size_t size, int prot, bool shared)
  265. {
  266. LOCKER(m_lock);
  267. return m_file->mmap(process, *this, vaddr, offset, size, prot, shared);
  268. }
  269. KResult FileDescription::truncate(u64 length)
  270. {
  271. LOCKER(m_lock);
  272. return m_file->truncate(length);
  273. }
  274. bool FileDescription::is_fifo() const
  275. {
  276. return m_file->is_fifo();
  277. }
  278. FIFO* FileDescription::fifo()
  279. {
  280. if (!is_fifo())
  281. return nullptr;
  282. return static_cast<FIFO*>(m_file.ptr());
  283. }
  284. bool FileDescription::is_socket() const
  285. {
  286. return m_file->is_socket();
  287. }
  288. Socket* FileDescription::socket()
  289. {
  290. if (!is_socket())
  291. return nullptr;
  292. return static_cast<Socket*>(m_file.ptr());
  293. }
  294. const Socket* FileDescription::socket() const
  295. {
  296. if (!is_socket())
  297. return nullptr;
  298. return static_cast<const Socket*>(m_file.ptr());
  299. }
  300. void FileDescription::set_file_flags(u32 flags)
  301. {
  302. LOCKER(m_lock);
  303. m_is_blocking = !(flags & O_NONBLOCK);
  304. m_should_append = flags & O_APPEND;
  305. m_direct = flags & O_DIRECT;
  306. m_file_flags = flags;
  307. }
  308. KResult FileDescription::chmod(mode_t mode)
  309. {
  310. LOCKER(m_lock);
  311. return m_file->chmod(*this, mode);
  312. }
  313. KResult FileDescription::chown(uid_t uid, gid_t gid)
  314. {
  315. LOCKER(m_lock);
  316. return m_file->chown(*this, uid, gid);
  317. }
  318. FileBlockCondition& FileDescription::block_condition()
  319. {
  320. return m_file->block_condition();
  321. }
  322. }