2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-06-21 18:45:35 +02:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2020-02-09 18:15:58 +02:00
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
|
#include <AK/String.h>
|
2019-04-28 14:53:50 +02:00
|
|
|
#include <AK/Types.h>
|
2020-09-06 18:46:46 +02:00
|
|
|
#include <AK/Weakable.h>
|
2020-02-16 01:50:16 +01:00
|
|
|
#include <Kernel/Forward.h>
|
2019-04-29 04:55:54 +02:00
|
|
|
#include <Kernel/KResult.h>
|
2019-05-30 13:39:17 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2020-09-11 21:11:07 -06:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2020-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/VirtualAddress.h>
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
class File;
|
|
|
|
|
|
|
|
|
|
class FileBlockCondition : public Thread::BlockCondition {
|
|
|
|
|
public:
|
2021-03-24 19:11:59 +01:00
|
|
|
FileBlockCondition() { }
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
|
virtual bool should_add_blocker(Thread::Blocker& b, void* data) override
|
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
|
2020-11-29 16:05:27 -07:00
|
|
|
auto& blocker = static_cast<Thread::FileBlocker&>(b);
|
|
|
|
|
return !blocker.unblock(true, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void unblock()
|
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-12-22 11:17:56 -07:00
|
|
|
do_unblock([&](auto& b, void* data, bool&) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::File);
|
2020-11-29 16:05:27 -07:00
|
|
|
auto& blocker = static_cast<Thread::FileBlocker&>(b);
|
|
|
|
|
return blocker.unblock(false, data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
// File is the base class for anything that can be referenced by a FileDescription.
|
2019-06-02 09:23:37 +02: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.
|
|
|
|
|
//
|
|
|
|
|
// mmap()
|
|
|
|
|
//
|
|
|
|
|
// - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
|
|
|
|
|
// - Called by mmap() when userspace wants to memory-map this File somewhere.
|
|
|
|
|
// - Should create a Region in the Process and return it if successful.
|
|
|
|
|
|
2020-09-06 18:46:46 +02:00
|
|
|
class File
|
2021-08-16 20:58:23 +02:00
|
|
|
: public RefCountedBase
|
2020-09-06 18:46:46 +02:00
|
|
|
, public Weakable<File> {
|
2019-04-28 14:53:50 +02:00
|
|
|
public:
|
2021-08-16 20:58:23 +02:00
|
|
|
virtual bool unref() const;
|
2019-04-28 14:53:50 +02:00
|
|
|
virtual ~File();
|
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options);
|
2020-06-02 19:20:05 +03:00
|
|
|
virtual KResult close();
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2020-04-10 19:44:42 +10:00
|
|
|
virtual bool can_read(const FileDescription&, size_t) const = 0;
|
|
|
|
|
virtual bool can_write(const FileDescription&, size_t) const = 0;
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2021-04-30 10:33:33 +02:00
|
|
|
virtual KResult attach(FileDescription&);
|
|
|
|
|
virtual void detach(FileDescription&);
|
2020-09-17 13:51:09 -06:00
|
|
|
virtual void did_seek(FileDescription&, off_t) { }
|
2021-03-17 13:18:51 +01:00
|
|
|
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
|
|
|
|
|
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0;
|
2021-07-26 03:47:25 -07:00
|
|
|
virtual KResult ioctl(FileDescription&, unsigned request, Userspace<void*> arg);
|
2021-08-06 13:54:48 +02:00
|
|
|
virtual KResultOr<Memory::Region*> mmap(Process&, FileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared);
|
2021-01-20 23:11:17 +01:00
|
|
|
virtual KResult stat(::stat&) const { return EBADF; }
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual String absolute_path(const FileDescription&) const = 0;
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2021-01-20 23:11:17 +01:00
|
|
|
virtual KResult truncate(u64) { return EINVAL; }
|
|
|
|
|
virtual KResult chown(FileDescription&, uid_t, gid_t) { return EBADF; }
|
|
|
|
|
virtual KResult chmod(FileDescription&, mode_t) { return EBADF; }
|
2019-05-30 13:39:17 +02:00
|
|
|
|
2021-07-11 01:46:09 +02:00
|
|
|
virtual StringView class_name() const = 0;
|
2019-04-28 14:53:50 +02:00
|
|
|
|
|
|
|
|
virtual bool is_seekable() const { return false; }
|
|
|
|
|
|
2019-05-30 13:39:17 +02:00
|
|
|
virtual bool is_inode() const { return false; }
|
2019-04-29 04:55:54 +02:00
|
|
|
virtual bool is_fifo() const { return false; }
|
2019-04-28 14:53:50 +02: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 20:42:43 +02:00
|
|
|
virtual bool is_socket() const { return false; }
|
2021-05-12 19:17:51 +00:00
|
|
|
virtual bool is_inode_watcher() const { return false; }
|
2019-04-28 14:53:50 +02:00
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
virtual FileBlockCondition& block_condition() { return m_block_condition; }
|
|
|
|
|
|
2021-04-30 10:33:33 +02:00
|
|
|
size_t attach_count() const { return m_attach_count; }
|
|
|
|
|
|
2019-04-28 14:53:50 +02:00
|
|
|
protected:
|
|
|
|
|
File();
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
|
void evaluate_block_conditions()
|
|
|
|
|
{
|
|
|
|
|
if (Processor::current().in_irq()) {
|
|
|
|
|
// If called from an IRQ handler we need to delay evaluation
|
2020-12-07 21:29:41 -07:00
|
|
|
// and unblocking of waiting threads. Note that this File
|
|
|
|
|
// instance may be deleted until the deferred call is executed!
|
|
|
|
|
Processor::deferred_call_queue([self = make_weak_ptr()]() {
|
|
|
|
|
if (auto file = self.strong_ref())
|
|
|
|
|
file->do_evaluate_block_conditions();
|
2020-11-29 16:05:27 -07:00
|
|
|
});
|
|
|
|
|
} else {
|
2020-12-07 21:29:41 -07:00
|
|
|
do_evaluate_block_conditions();
|
2020-11-29 16:05:27 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-12-07 21:29:41 -07:00
|
|
|
ALWAYS_INLINE void do_evaluate_block_conditions()
|
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!Processor::current().in_irq());
|
2020-12-07 21:29:41 -07:00
|
|
|
block_condition().unblock();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
FileBlockCondition m_block_condition;
|
2021-04-30 10:33:33 +02:00
|
|
|
size_t m_attach_count { 0 };
|
2019-04-28 14:53:50 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|