2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-04-28 10:02:55 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-08-19 12:26:07 -03:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2021-11-07 20:51:39 -03:00
|
|
|
#include <AK/Error.h>
|
2021-12-11 12:43:03 -03:00
|
|
|
#include <AK/StringView.h>
|
2019-04-28 09:53:50 -03:00
|
|
|
#include <AK/Types.h>
|
2020-02-15 21:50:16 -03:00
|
|
|
#include <Kernel/Forward.h>
|
2022-08-19 15:53:40 -03:00
|
|
|
#include <Kernel/Library/LockWeakable.h>
|
|
|
|
|
#include <Kernel/Library/NonnullLockRefPtr.h>
|
2019-05-30 08:39:17 -03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2020-09-12 00:11:07 -03:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2020-05-16 07:00:04 -03:00
|
|
|
#include <Kernel/VirtualAddress.h>
|
2019-04-28 09:53:50 -03:00
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2020-11-29 20:05:27 -03:00
|
|
|
class File;
|
|
|
|
|
|
2021-08-22 10:59:47 -03:00
|
|
|
class FileBlockerSet final : public Thread::BlockerSet {
|
2020-11-29 20:05:27 -03:00
|
|
|
public:
|
2021-08-22 10:59:47 -03:00
|
|
|
FileBlockerSet() { }
|
2020-11-29 20:05:27 -03:00
|
|
|
|
|
|
|
|
virtual bool should_add_blocker(Thread::Blocker& b, void* data) override
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
|
2020-11-29 20:05:27 -03:00
|
|
|
auto& blocker = static_cast<Thread::FileBlocker&>(b);
|
2021-09-04 19:12:49 -03:00
|
|
|
return !blocker.unblock_if_conditions_are_met(true, data);
|
2020-11-29 20:05:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 12:38:16 -03:00
|
|
|
void unblock_all_blockers_whose_conditions_are_met()
|
2020-11-29 20:05:27 -03:00
|
|
|
{
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-08-22 12:38:16 -03:00
|
|
|
BlockerSet::unblock_all_blockers_whose_conditions_are_met_locked([&](auto& b, void* data, bool&) {
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
|
2020-11-29 20:05:27 -03:00
|
|
|
auto& blocker = static_cast<Thread::FileBlocker&>(b);
|
2021-09-04 19:12:49 -03:00
|
|
|
return blocker.unblock_if_conditions_are_met(false, data);
|
2020-11-29 20:05:27 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-07 08:39:11 -03:00
|
|
|
// File is the base class for anything that can be referenced by a OpenFileDescription.
|
2019-06-02 04:23:37 -03:00
|
|
|
//
|
|
|
|
|
// The most important functions in File are:
|
|
|
|
|
//
|
|
|
|
|
// read() and write()
|
|
|
|
|
// - Implement reading and writing.
|
|
|
|
|
// - Return the number of bytes read/written, OR a negative error code.
|
|
|
|
|
//
|
|
|
|
|
// can_read() and can_write()
|
|
|
|
|
//
|
|
|
|
|
// - Used to implement blocking I/O, and the select() and poll() syscalls.
|
|
|
|
|
// - Return true if read() or write() would succeed, respectively.
|
|
|
|
|
// - Note that can_read() should return true in EOF conditions,
|
|
|
|
|
// and a subsequent call to read() should return 0.
|
|
|
|
|
//
|
|
|
|
|
// ioctl()
|
|
|
|
|
//
|
|
|
|
|
// - Optional. If unimplemented, ioctl() on this File will fail with -ENOTTY.
|
|
|
|
|
// - Can be overridden in subclasses to implement arbitrary functionality.
|
|
|
|
|
// - Subclasses should take care to validate incoming addresses before dereferencing.
|
|
|
|
|
//
|
2022-08-23 13:51:18 -03:00
|
|
|
// vmobject_for_mmap()
|
2019-06-02 04:23:37 -03:00
|
|
|
//
|
|
|
|
|
// - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
|
|
|
|
|
// - Called by mmap() when userspace wants to memory-map this File somewhere.
|
2022-08-23 13:51:18 -03:00
|
|
|
// - Should return a VMObject suitable for mapping into the calling process.
|
2019-06-02 04:23:37 -03:00
|
|
|
|
2020-09-06 13:46:46 -03:00
|
|
|
class File
|
2022-08-19 12:26:07 -03:00
|
|
|
: public AtomicRefCounted<File>
|
2022-08-19 15:53:40 -03:00
|
|
|
, public LockWeakable<File> {
|
2019-04-28 09:53:50 -03:00
|
|
|
public:
|
2022-08-19 12:26:07 -03:00
|
|
|
virtual bool unref() const { return AtomicRefCounted<File>::unref(); }
|
2021-12-28 20:00:29 -03:00
|
|
|
virtual void will_be_destroyed() { }
|
2019-04-28 09:53:50 -03:00
|
|
|
virtual ~File();
|
|
|
|
|
|
2023-03-06 15:29:25 -03:00
|
|
|
virtual ErrorOr<NonnullRefPtr<OpenFileDescription>> open(int options);
|
2021-11-07 20:51:39 -03:00
|
|
|
virtual ErrorOr<void> close();
|
2019-04-28 09:53:50 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const = 0;
|
|
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const = 0;
|
2019-04-28 09:53:50 -03:00
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
virtual ErrorOr<void> attach(OpenFileDescription&);
|
2021-09-07 08:39:11 -03:00
|
|
|
virtual void detach(OpenFileDescription&);
|
|
|
|
|
virtual void did_seek(OpenFileDescription&, off_t) { }
|
2021-11-07 20:51:39 -03:00
|
|
|
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
|
2022-04-01 14:58:27 -03:00
|
|
|
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) = 0;
|
2021-11-07 20:51:39 -03:00
|
|
|
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg);
|
2022-08-23 13:51:18 -03:00
|
|
|
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
|
2021-12-17 07:22:27 -03:00
|
|
|
virtual ErrorOr<struct stat> stat() const { return EBADF; }
|
2019-04-28 09:53:50 -03:00
|
|
|
|
2021-10-29 19:45:23 -03:00
|
|
|
// Although this might be better described "name" or "description", these terms already have other meanings.
|
2022-04-01 14:58:27 -03:00
|
|
|
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const = 0;
|
2019-04-28 09:53:50 -03:00
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
virtual ErrorOr<void> truncate(u64) { return EINVAL; }
|
|
|
|
|
virtual ErrorOr<void> sync() { return EINVAL; }
|
2022-08-21 11:15:29 -03:00
|
|
|
virtual ErrorOr<void> chown(Credentials const&, OpenFileDescription&, UserID, GroupID) { return EBADF; }
|
|
|
|
|
virtual ErrorOr<void> chmod(Credentials const&, OpenFileDescription&, mode_t) { return EBADF; }
|
2019-05-30 08:39:17 -03:00
|
|
|
|
2021-07-10 20:46:09 -03:00
|
|
|
virtual StringView class_name() const = 0;
|
2019-04-28 09:53:50 -03:00
|
|
|
|
|
|
|
|
virtual bool is_seekable() const { return false; }
|
|
|
|
|
|
2019-05-30 08:39:17 -03:00
|
|
|
virtual bool is_inode() const { return false; }
|
2019-04-28 23:55:54 -03:00
|
|
|
virtual bool is_fifo() const { return false; }
|
2019-04-28 09:53:50 -03:00
|
|
|
virtual bool is_device() const { return false; }
|
|
|
|
|
virtual bool is_tty() const { return false; }
|
|
|
|
|
virtual bool is_master_pty() const { return false; }
|
|
|
|
|
virtual bool is_block_device() const { return false; }
|
|
|
|
|
virtual bool is_character_device() const { return false; }
|
2019-05-03 15:42:43 -03:00
|
|
|
virtual bool is_socket() const { return false; }
|
2021-05-12 16:17:51 -03:00
|
|
|
virtual bool is_inode_watcher() const { return false; }
|
2019-04-28 09:53:50 -03:00
|
|
|
|
2022-11-27 19:50:28 -03:00
|
|
|
virtual bool is_regular_file() const { return false; }
|
|
|
|
|
|
2021-08-22 10:59:47 -03:00
|
|
|
virtual FileBlockerSet& blocker_set() { return m_blocker_set; }
|
2020-11-29 20:05:27 -03:00
|
|
|
|
2021-04-30 05:33:33 -03:00
|
|
|
size_t attach_count() const { return m_attach_count; }
|
|
|
|
|
|
2019-04-28 09:53:50 -03:00
|
|
|
protected:
|
|
|
|
|
File();
|
2020-11-29 20:05:27 -03:00
|
|
|
|
|
|
|
|
void evaluate_block_conditions()
|
|
|
|
|
{
|
2021-11-06 18:06:08 -03:00
|
|
|
if (Processor::current_in_irq() != 0) {
|
2020-11-29 20:05:27 -03:00
|
|
|
// If called from an IRQ handler we need to delay evaluation
|
2020-12-08 01:29:41 -03:00
|
|
|
// and unblocking of waiting threads. Note that this File
|
|
|
|
|
// instance may be deleted until the deferred call is executed!
|
2022-02-13 16:21:14 -03:00
|
|
|
Processor::deferred_call_queue([self = try_make_weak_ptr().release_value_but_fixme_should_propagate_errors()]() {
|
2020-12-08 01:29:41 -03:00
|
|
|
if (auto file = self.strong_ref())
|
|
|
|
|
file->do_evaluate_block_conditions();
|
2020-11-29 20:05:27 -03:00
|
|
|
});
|
|
|
|
|
} else {
|
2020-12-08 01:29:41 -03:00
|
|
|
do_evaluate_block_conditions();
|
2020-11-29 20:05:27 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-12-08 01:29:41 -03:00
|
|
|
ALWAYS_INLINE void do_evaluate_block_conditions()
|
|
|
|
|
{
|
2021-08-22 07:21:31 -03:00
|
|
|
VERIFY(!Processor::current_in_irq());
|
2021-08-22 12:38:16 -03:00
|
|
|
blocker_set().unblock_all_blockers_whose_conditions_are_met();
|
2020-12-08 01:29:41 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 10:59:47 -03:00
|
|
|
FileBlockerSet m_blocker_set;
|
2021-04-30 05:33:33 -03:00
|
|
|
size_t m_attach_count { 0 };
|
2019-04-28 09:53:50 -03:00
|
|
|
};
|
2020-02-15 21:27:42 -03:00
|
|
|
|
|
|
|
|
}
|