File.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #pragma once
  27. #include <AK/NonnullRefPtr.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/String.h>
  30. #include <AK/Types.h>
  31. #include <AK/Weakable.h>
  32. #include <Kernel/Forward.h>
  33. #include <Kernel/KResult.h>
  34. #include <Kernel/UnixTypes.h>
  35. #include <Kernel/UserOrKernelBuffer.h>
  36. #include <Kernel/VirtualAddress.h>
  37. namespace Kernel {
  38. class File;
  39. class FileBlockCondition : public Thread::BlockCondition {
  40. public:
  41. FileBlockCondition() { }
  42. virtual bool should_add_blocker(Thread::Blocker& b, void* data) override
  43. {
  44. VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
  45. auto& blocker = static_cast<Thread::FileBlocker&>(b);
  46. return !blocker.unblock(true, data);
  47. }
  48. void unblock()
  49. {
  50. ScopedSpinLock lock(m_lock);
  51. do_unblock([&](auto& b, void* data, bool&) {
  52. VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
  53. auto& blocker = static_cast<Thread::FileBlocker&>(b);
  54. return blocker.unblock(false, data);
  55. });
  56. }
  57. };
  58. // File is the base class for anything that can be referenced by a FileDescription.
  59. //
  60. // The most important functions in File are:
  61. //
  62. // read() and write()
  63. // - Implement reading and writing.
  64. // - Return the number of bytes read/written, OR a negative error code.
  65. //
  66. // can_read() and can_write()
  67. //
  68. // - Used to implement blocking I/O, and the select() and poll() syscalls.
  69. // - Return true if read() or write() would succeed, respectively.
  70. // - Note that can_read() should return true in EOF conditions,
  71. // and a subsequent call to read() should return 0.
  72. //
  73. // ioctl()
  74. //
  75. // - Optional. If unimplemented, ioctl() on this File will fail with -ENOTTY.
  76. // - Can be overridden in subclasses to implement arbitrary functionality.
  77. // - Subclasses should take care to validate incoming addresses before dereferencing.
  78. //
  79. // mmap()
  80. //
  81. // - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
  82. // - Called by mmap() when userspace wants to memory-map this File somewhere.
  83. // - Should create a Region in the Process and return it if successful.
  84. class File
  85. : public RefCounted<File>
  86. , public Weakable<File> {
  87. public:
  88. virtual ~File();
  89. virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options);
  90. virtual KResult close();
  91. virtual bool can_read(const FileDescription&, size_t) const = 0;
  92. virtual bool can_write(const FileDescription&, size_t) const = 0;
  93. virtual KResult attach(FileDescription&) { return KSuccess; }
  94. virtual void detach(FileDescription&) { }
  95. virtual void did_seek(FileDescription&, off_t) { }
  96. virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
  97. virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0;
  98. virtual int ioctl(FileDescription&, unsigned request, FlatPtr arg);
  99. virtual KResultOr<Region*> mmap(Process&, FileDescription&, const Range&, u64 offset, int prot, bool shared);
  100. virtual KResult stat(::stat&) const { return EBADF; }
  101. virtual String absolute_path(const FileDescription&) const = 0;
  102. virtual KResult truncate(u64) { return EINVAL; }
  103. virtual KResult chown(FileDescription&, uid_t, gid_t) { return EBADF; }
  104. virtual KResult chmod(FileDescription&, mode_t) { return EBADF; }
  105. virtual const char* class_name() const = 0;
  106. virtual bool is_seekable() const { return false; }
  107. virtual bool is_inode() const { return false; }
  108. virtual bool is_fifo() const { return false; }
  109. virtual bool is_device() const { return false; }
  110. virtual bool is_tty() const { return false; }
  111. virtual bool is_master_pty() const { return false; }
  112. virtual bool is_block_device() const { return false; }
  113. virtual bool is_character_device() const { return false; }
  114. virtual bool is_socket() const { return false; }
  115. virtual FileBlockCondition& block_condition() { return m_block_condition; }
  116. protected:
  117. File();
  118. void evaluate_block_conditions()
  119. {
  120. if (Processor::current().in_irq()) {
  121. // If called from an IRQ handler we need to delay evaluation
  122. // and unblocking of waiting threads. Note that this File
  123. // instance may be deleted until the deferred call is executed!
  124. Processor::deferred_call_queue([self = make_weak_ptr()]() {
  125. if (auto file = self.strong_ref())
  126. file->do_evaluate_block_conditions();
  127. });
  128. } else {
  129. do_evaluate_block_conditions();
  130. }
  131. }
  132. private:
  133. ALWAYS_INLINE void do_evaluate_block_conditions()
  134. {
  135. VERIFY(!Processor::current().in_irq());
  136. block_condition().unblock();
  137. }
  138. FileBlockCondition m_block_condition;
  139. };
  140. }