1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

FileSystem: Port most of the code over to using custodies.

The current working directory is now stored as a custody. Likewise for a
process executable file. This unbreaks /proc/PID/fd which has not been
working since we made the filesystem bigger.

This still needs a bunch of work, for instance when renaming or removing
a file somewhere, we have to update the relevant custody links.
This commit is contained in:
Andreas Kling 2019-05-30 18:58:59 +02:00
parent 4cb87b1753
commit 393851418b
11 changed files with 280 additions and 220 deletions

View file

@ -1,4 +1,5 @@
#include <AK/HashTable.h> #include <AK/HashTable.h>
#include <AK/StringBuilder.h>
#include <Kernel/FileSystem/Custody.h> #include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/Inode.h> #include <Kernel/FileSystem/Inode.h>
#include <Kernel/Lock.h> #include <Kernel/Lock.h>
@ -25,3 +26,16 @@ Custody::~Custody()
LOCKER(all_custodies().lock()); LOCKER(all_custodies().lock());
all_custodies().resource().remove(this); all_custodies().resource().remove(this);
} }
String Custody::absolute_path() const
{
Vector<const Custody*, 32> custody_chain;
for (auto* custody = this; custody; custody = custody->parent())
custody_chain.append(custody);
StringBuilder builder;
for (int i = custody_chain.size() - 2; i >= 0; --i) {
builder.append('/');
builder.append(custody_chain[i]->name().characters());
}
return builder.to_string();
}

View file

@ -23,6 +23,8 @@ public:
const String& name() const { return m_name; } const String& name() const { return m_name; }
String absolute_path() const;
private: private:
Custody(Custody* parent, const String& name, Inode&); Custody(Custody* parent, const String& name, Inode&);

View file

@ -1,6 +1,7 @@
#include <AK/BufferStream.h> #include <AK/BufferStream.h>
#include <Kernel/Devices/BlockDevice.h> #include <Kernel/Devices/BlockDevice.h>
#include <Kernel/Devices/CharacterDevice.h> #include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FIFO.h> #include <Kernel/FileSystem/FIFO.h>
#include <Kernel/FileSystem/FileDescriptor.h> #include <Kernel/FileSystem/FileDescriptor.h>
#include <Kernel/FileSystem/FileSystem.h> #include <Kernel/FileSystem/FileSystem.h>
@ -14,9 +15,11 @@
#include <Kernel/VM/MemoryManager.h> #include <Kernel/VM/MemoryManager.h>
#include <LibC/errno_numbers.h> #include <LibC/errno_numbers.h>
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Inode>&& inode) Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Custody>&& custody)
{ {
return adopt(*new FileDescriptor(InodeFile::create(*inode))); auto descriptor = adopt(*new FileDescriptor(InodeFile::create(custody->inode())));
descriptor->m_custody = move(custody);
return descriptor;
} }
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file, SocketRole role) Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file, SocketRole role)
@ -62,6 +65,7 @@ Retained<FileDescriptor> FileDescriptor::clone()
descriptor = fifo()->open_direction(m_fifo_direction); descriptor = fifo()->open_direction(m_fifo_direction);
} else { } else {
descriptor = FileDescriptor::create(m_file.copy_ref(), m_socket_role); descriptor = FileDescriptor::create(m_file.copy_ref(), m_socket_role);
descriptor->m_custody = m_custody.copy_ref();
descriptor->m_inode = m_inode.copy_ref(); descriptor->m_inode = m_inode.copy_ref();
} }
ASSERT(descriptor); ASSERT(descriptor);
@ -102,7 +106,7 @@ KResult FileDescriptor::fchmod(mode_t mode)
{ {
if (!m_inode) if (!m_inode)
return KResult(-EBADF); return KResult(-EBADF);
return VFS::the().chmod(*m_inode, mode); return VFS::the().fchmod(*m_inode, mode);
} }
off_t FileDescriptor::seek(off_t offset, int whence) off_t FileDescriptor::seek(off_t offset, int whence)
@ -259,6 +263,9 @@ int FileDescriptor::close()
KResultOr<String> FileDescriptor::absolute_path() KResultOr<String> FileDescriptor::absolute_path()
{ {
if (m_custody)
return m_custody->absolute_path();
dbgprintf("FileDescriptor::absolute_path() for FD without custody, File type: %s\n", m_file->class_name());
return m_file->absolute_path(*this); return m_file->absolute_path(*this);
} }

View file

@ -21,7 +21,7 @@ class SharedMemory;
class FileDescriptor : public Retainable<FileDescriptor> { class FileDescriptor : public Retainable<FileDescriptor> {
public: public:
static Retained<FileDescriptor> create(RetainPtr<Inode>&&); static Retained<FileDescriptor> create(RetainPtr<Custody>&&);
static Retained<FileDescriptor> create(RetainPtr<File>&&, SocketRole = SocketRole::None); static Retained<FileDescriptor> create(RetainPtr<File>&&, SocketRole = SocketRole::None);
~FileDescriptor(); ~FileDescriptor();
@ -64,6 +64,9 @@ public:
Inode* inode() { return m_inode.ptr(); } Inode* inode() { return m_inode.ptr(); }
const Inode* inode() const { return m_inode.ptr(); } const Inode* inode() const { return m_inode.ptr(); }
Custody* custody() { return m_custody.ptr(); }
const Custody* custody() const { return m_custody.ptr(); }
KResultOr<Region*> mmap(Process&, LinearAddress, size_t offset, size_t, int prot); KResultOr<Region*> mmap(Process&, LinearAddress, size_t offset, size_t, int prot);
bool is_blocking() const { return m_is_blocking; } bool is_blocking() const { return m_is_blocking; }
@ -103,6 +106,7 @@ private:
FileDescriptor(RetainPtr<File>&&, SocketRole = SocketRole::None); FileDescriptor(RetainPtr<File>&&, SocketRole = SocketRole::None);
FileDescriptor(FIFO&, FIFO::Direction); FileDescriptor(FIFO&, FIFO::Direction);
RetainPtr<Custody> m_custody;
RetainPtr<Inode> m_inode; RetainPtr<Inode> m_inode;
RetainPtr<File> m_file; RetainPtr<File> m_file;

View file

@ -1,5 +1,6 @@
#include "ProcFS.h" #include "ProcFS.h"
#include "Process.h" #include "Process.h"
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileDescriptor.h> #include <Kernel/FileSystem/FileDescriptor.h>
#include <Kernel/FileSystem/VirtualFileSystem.h> #include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/VM/MemoryManager.h> #include <Kernel/VM/MemoryManager.h>
@ -359,12 +360,9 @@ ByteBuffer procfs$pid_exe(InodeIdentifier identifier)
if (!handle) if (!handle)
return { }; return { };
auto& process = handle->process(); auto& process = handle->process();
auto inode = process.executable_inode(); auto* custody = process.executable_custody();
ASSERT(inode); ASSERT(custody);
auto result = VFS::the().absolute_path(*inode); return custody->absolute_path().to_byte_buffer();
if (result.is_error())
return { };
return result.value().to_byte_buffer();
} }
ByteBuffer procfs$pid_cwd(InodeIdentifier identifier) ByteBuffer procfs$pid_cwd(InodeIdentifier identifier)
@ -372,10 +370,7 @@ ByteBuffer procfs$pid_cwd(InodeIdentifier identifier)
auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier)); auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
if (!handle) if (!handle)
return { }; return { };
auto result = VFS::the().absolute_path(handle->process().cwd_inode()); return handle->process().cwd_custody().absolute_path().to_byte_buffer();
if (result.is_error())
return { };
return result.value().to_byte_buffer();
} }
ByteBuffer procfs$self(InodeIdentifier) ByteBuffer procfs$self(InodeIdentifier)
@ -958,7 +953,7 @@ bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)
auto& process = handle->process(); auto& process = handle->process();
for (auto& entry : fs().m_entries) { for (auto& entry : fs().m_entries) {
if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) { if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
if (entry.proc_file_type == FI_PID_exe && !process.executable_inode()) if (entry.proc_file_type == FI_PID_exe && !process.executable_custody())
continue; continue;
// FIXME: strlen() here is sad. // FIXME: strlen() here is sad.
callback({ entry.name, (int)strlen(entry.name), to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 }); callback({ entry.name, (int)strlen(entry.name), to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
@ -1039,7 +1034,7 @@ InodeIdentifier ProcFSInode::lookup(const String& name)
auto& process = handle->process(); auto& process = handle->process();
for (auto& entry : fs().m_entries) { for (auto& entry : fs().m_entries) {
if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) { if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
if (entry.proc_file_type == FI_PID_exe && !process.executable_inode()) if (entry.proc_file_type == FI_PID_exe && !process.executable_custody())
continue; continue;
if (entry.name == nullptr) if (entry.name == nullptr)
continue; continue;

View file

@ -121,7 +121,7 @@ void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::Dir
}); });
} }
KResult VFS::utime(StringView path, Inode& base, time_t atime, time_t mtime) KResult VFS::utime(StringView path, Custody& base, time_t atime, time_t mtime)
{ {
auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base); auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
if (descriptor_or_error.is_error()) if (descriptor_or_error.is_error())
@ -141,28 +141,29 @@ KResult VFS::utime(StringView path, Inode& base, time_t atime, time_t mtime)
return KSuccess; return KSuccess;
} }
KResult VFS::stat(StringView path, int options, Inode& base, struct stat& statbuf) KResult VFS::stat(StringView path, int options, Custody& base, struct stat& statbuf)
{ {
auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options); auto custody_or_error = resolve_path_to_custody(path, base, nullptr, options);
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return inode_or_error.error(); return custody_or_error.error();
return FileDescriptor::create(inode_or_error.value().ptr())->fstat(statbuf); return FileDescriptor::create(custody_or_error.value().ptr())->fstat(statbuf);
} }
KResultOr<Retained<FileDescriptor>> VFS::open(StringView path, int options, mode_t mode, Inode& base) KResultOr<Retained<FileDescriptor>> VFS::open(StringView path, int options, mode_t mode, Custody& base)
{ {
auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options); auto custody_or_error = resolve_path_to_custody(path, base, nullptr, options);
if (options & O_CREAT) { if (options & O_CREAT) {
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return create(path, options, mode, base); return create(path, options, mode, base);
if (options & O_EXCL) if (options & O_EXCL)
return KResult(-EEXIST); return KResult(-EEXIST);
} }
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return inode_or_error.error(); return custody_or_error.error();
auto inode = inode_or_error.value(); auto& custody = *custody_or_error.value();
auto metadata = inode->metadata(); auto& inode = custody.inode();
auto metadata = inode.metadata();
bool should_truncate_file = false; bool should_truncate_file = false;
@ -188,41 +189,42 @@ KResultOr<Retained<FileDescriptor>> VFS::open(StringView path, int options, mode
auto descriptor_or_error = (*it).value->open(options); auto descriptor_or_error = (*it).value->open(options);
if (descriptor_or_error.is_error()) if (descriptor_or_error.is_error())
return descriptor_or_error.error(); return descriptor_or_error.error();
descriptor_or_error.value()->set_original_inode(Badge<VFS>(), *inode); descriptor_or_error.value()->set_original_inode(Badge<VFS>(), inode);
return descriptor_or_error; return descriptor_or_error;
} }
if (should_truncate_file) if (should_truncate_file)
inode->truncate(0); inode.truncate(0);
return FileDescriptor::create(*inode); return FileDescriptor::create(custody);
} }
KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Inode& base) KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
{ {
if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode)) if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
return KResult(-EINVAL); return KResult(-EINVAL);
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto existing_file_or_error = resolve_path_to_inode(path, base, &parent_inode); auto existing_file_or_error = resolve_path_to_custody(path, base, &parent_custody);
if (!existing_file_or_error.is_error()) if (!existing_file_or_error.is_error())
return KResult(-EEXIST); return KResult(-EEXIST);
if (!parent_inode) if (!parent_custody)
return KResult(-ENOENT); return KResult(-ENOENT);
if (existing_file_or_error.error() != -ENOENT) if (existing_file_or_error.error() != -ENOENT)
return existing_file_or_error.error(); return existing_file_or_error.error();
if (!parent_inode->metadata().may_write(current->process())) auto& parent_inode = parent_custody->inode();
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
FileSystemPath p(path); FileSystemPath p(path);
dbgprintf("VFS::mknod: '%s' mode=%o dev=%u in %u:%u\n", p.basename().characters(), mode, dev, parent_inode->fsid(), parent_inode->index()); dbgprintf("VFS::mknod: '%s' mode=%o dev=%u in %u:%u\n", p.basename().characters(), mode, dev, parent_inode.fsid(), parent_inode.index());
int error; int error;
auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), mode, 0, dev, error); auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, dev, error);
if (!new_file) if (!new_file)
return KResult(error); return KResult(error);
return KSuccess; return KSuccess;
} }
KResultOr<Retained<FileDescriptor>> VFS::create(StringView path, int options, mode_t mode, Inode& base) KResultOr<Retained<FileDescriptor>> VFS::create(StringView path, int options, mode_t mode, Custody& base)
{ {
(void)options; (void)options;
@ -231,57 +233,61 @@ KResultOr<Retained<FileDescriptor>> VFS::create(StringView path, int options, mo
mode |= 0100000; mode |= 0100000;
} }
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto existing_file_or_error = resolve_path_to_inode(path, base, &parent_inode); auto existing_custody_or_error = resolve_path_to_custody(path, base, &parent_custody);
if (!existing_file_or_error.is_error()) if (!existing_custody_or_error.is_error())
return KResult(-EEXIST); return KResult(-EEXIST);
if (!parent_inode) if (!parent_custody)
return KResult(-ENOENT); return KResult(-ENOENT);
if (existing_file_or_error.error() != -ENOENT) auto& parent_inode = parent_custody->inode();
return existing_file_or_error.error(); if (existing_custody_or_error.error() != -ENOENT)
if (!parent_inode->metadata().may_write(current->process())) return existing_custody_or_error.error();
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
FileSystemPath p(path); FileSystemPath p(path);
dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index()); dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode.fsid(), parent_inode.index());
int error; int error;
auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), mode, 0, 0, error); auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, error);
if (!new_file) if (!new_file)
return KResult(error); return KResult(error);
return FileDescriptor::create(move(new_file)); auto new_custody = Custody::create(parent_custody, p.basename(), *new_file);
return FileDescriptor::create(*new_custody);
} }
KResult VFS::mkdir(StringView path, mode_t mode, Inode& base) KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
{ {
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto result = resolve_path_to_inode(path, base, &parent_inode); auto result = resolve_path_to_custody(path, base, &parent_custody);
if (!result.is_error()) if (!result.is_error())
return KResult(-EEXIST); return KResult(-EEXIST);
if (!parent_inode) if (!parent_custody)
return KResult(-ENOENT); return KResult(-ENOENT);
if (result.error() != -ENOENT) if (result.error() != -ENOENT)
return result.error(); return result.error();
if (!parent_inode->metadata().may_write(current->process())) auto& parent_inode = parent_custody->inode();
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
FileSystemPath p(path); FileSystemPath p(path);
dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index()); dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode.fsid(), parent_inode.index());
int error; int error;
auto new_dir = parent_inode->fs().create_directory(parent_inode->identifier(), p.basename(), mode, error); auto new_dir = parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, error);
if (new_dir) if (new_dir)
return KSuccess; return KSuccess;
return KResult(error); return KResult(error);
} }
KResult VFS::access(StringView path, int mode, Inode& base) KResult VFS::access(StringView path, int mode, Custody& base)
{ {
auto inode_or_error = resolve_path_to_inode(path, base); auto custody_or_error = resolve_path_to_custody(path, base);
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return inode_or_error.error(); return custody_or_error.error();
auto inode = inode_or_error.value(); auto& custody = *custody_or_error.value();
auto metadata = inode->metadata(); auto& inode = custody.inode();
auto metadata = inode.metadata();
if (mode & R_OK) { if (mode & R_OK) {
if (!metadata.may_read(current->process())) if (!metadata.may_read(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
@ -297,20 +303,21 @@ KResult VFS::access(StringView path, int mode, Inode& base)
return KSuccess; return KSuccess;
} }
KResultOr<Retained<Inode>> VFS::open_directory(StringView path, Inode& base) KResultOr<Retained<Custody>> VFS::open_directory(StringView path, Custody& base)
{ {
auto inode_or_error = resolve_path_to_inode(path, base); auto inode_or_error = resolve_path_to_custody(path, base);
if (inode_or_error.is_error()) if (inode_or_error.is_error())
return inode_or_error.error(); return inode_or_error.error();
auto inode = inode_or_error.value(); auto& custody = *inode_or_error.value();
if (!inode->is_directory()) auto& inode = custody.inode();
if (!inode.is_directory())
return KResult(-ENOTDIR); return KResult(-ENOTDIR);
if (!inode->metadata().may_execute(current->process())) if (!inode.metadata().may_execute(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
return Retained<Inode>(*inode); return custody;
} }
KResult VFS::chmod(Inode& inode, mode_t mode) KResult VFS::fchmod(Inode& inode, mode_t mode)
{ {
if (inode.fs().is_readonly()) if (inode.fs().is_readonly())
return KResult(-EROFS); return KResult(-EROFS);
@ -323,83 +330,90 @@ KResult VFS::chmod(Inode& inode, mode_t mode)
return inode.chmod(mode); return inode.chmod(mode);
} }
KResult VFS::chmod(StringView path, mode_t mode, Inode& base) KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
{ {
auto inode_or_error = resolve_path_to_inode(path, base); auto custody_or_error = resolve_path_to_custody(path, base);
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return inode_or_error.error(); return custody_or_error.error();
auto inode = inode_or_error.value(); auto& custody = *custody_or_error.value();
return chmod(*inode, mode); auto& inode = custody.inode();
return fchmod(inode, mode);
} }
KResult VFS::rename(StringView old_path, StringView new_path, Inode& base) KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
{ {
RetainPtr<Inode> old_parent_inode; RetainPtr<Custody> old_parent_custody;
auto old_inode_or_error = resolve_path_to_inode(old_path, base, &old_parent_inode); auto old_custody_or_error = resolve_path_to_custody(old_path, base, &old_parent_custody);
if (old_inode_or_error.is_error()) if (old_custody_or_error.is_error())
return old_inode_or_error.error(); return old_custody_or_error.error();
auto old_inode = old_inode_or_error.value(); auto& old_custody = *old_custody_or_error.value();
auto& old_inode = old_custody.inode();
RetainPtr<Inode> new_parent_inode; RetainPtr<Custody> new_parent_custody;
auto new_inode_or_error = resolve_path_to_inode(new_path, base, &new_parent_inode); auto new_custody_or_error = resolve_path_to_custody(new_path, base, &new_parent_custody);
if (new_inode_or_error.is_error()) { if (new_custody_or_error.is_error()) {
if (new_inode_or_error.error() != -ENOENT) if (new_custody_or_error.error() != -ENOENT)
return new_inode_or_error.error(); return new_custody_or_error.error();
} }
if (!new_parent_inode->metadata().may_write(current->process())) auto& old_parent_inode = old_parent_custody->inode();
auto& new_parent_inode = new_parent_custody->inode();
if (!new_parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
if (!old_parent_inode->metadata().may_write(current->process())) if (!old_parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
if (old_parent_inode->metadata().is_sticky()) { if (old_parent_inode.metadata().is_sticky()) {
if (!current->process().is_superuser() && old_inode->metadata().uid != current->process().euid()) if (!current->process().is_superuser() && old_inode.metadata().uid != current->process().euid())
return KResult(-EACCES); return KResult(-EACCES);
} }
if (!new_inode_or_error.is_error()) { if (!new_custody_or_error.is_error()) {
auto new_inode = new_inode_or_error.value(); auto& new_custody = *new_custody_or_error.value();
auto& new_inode = new_custody.inode();
// FIXME: Is this really correct? Check what other systems do. // FIXME: Is this really correct? Check what other systems do.
if (new_inode == old_inode) if (&new_inode == &old_inode)
return KSuccess; return KSuccess;
if (new_parent_inode->metadata().is_sticky()) { if (new_parent_inode.metadata().is_sticky()) {
if (!current->process().is_superuser() && new_inode->metadata().uid != current->process().euid()) if (!current->process().is_superuser() && new_inode.metadata().uid != current->process().euid())
return KResult(-EACCES); return KResult(-EACCES);
} }
if (new_inode->is_directory() && !old_inode->is_directory()) if (new_inode.is_directory() && !old_inode.is_directory())
return KResult(-EISDIR); return KResult(-EISDIR);
auto result = new_parent_inode->remove_child(FileSystemPath(new_path).basename()); auto result = new_parent_inode.remove_child(FileSystemPath(new_path).basename());
if (result.is_error()) if (result.is_error())
return result; return result;
} }
auto result = new_parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0 /* FIXME: file type? */); auto result = new_parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), 0 /* FIXME: file type? */);
if (result.is_error()) if (result.is_error())
return result; return result;
result = old_parent_inode->remove_child(FileSystemPath(old_path).basename()); result = old_parent_inode.remove_child(FileSystemPath(old_path).basename());
if (result.is_error()) if (result.is_error())
return result; return result;
return KSuccess; return KSuccess;
} }
KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Inode& base) KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
{ {
auto inode_or_error = resolve_path_to_inode(path, base); auto custody_or_error = resolve_path_to_custody(path, base);
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return inode_or_error.error(); return custody_or_error.error();
auto inode = inode_or_error.value(); auto& custody = *custody_or_error.value();
auto& inode = custody.inode();
if (inode->fs().is_readonly()) if (inode.fs().is_readonly())
return KResult(-EROFS); return KResult(-EROFS);
if (current->process().euid() != inode->metadata().uid && !current->process().is_superuser()) if (current->process().euid() != inode.metadata().uid && !current->process().is_superuser())
return KResult(-EPERM); return KResult(-EPERM);
uid_t new_uid = inode->metadata().uid; uid_t new_uid = inode.metadata().uid;
gid_t new_gid = inode->metadata().gid; gid_t new_gid = inode.metadata().gid;
if (a_uid != (uid_t)-1) { if (a_uid != (uid_t)-1) {
if (current->process().euid() != a_uid && !current->process().is_superuser()) if (current->process().euid() != a_uid && !current->process().is_superuser())
@ -412,8 +426,8 @@ KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Inode& base)
new_gid = a_gid; new_gid = a_gid;
} }
dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode->fsid(), inode->index(), new_uid, new_gid); dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode.fsid(), inode.index(), new_uid, new_gid);
return inode->chown(new_uid, new_gid); return inode.chown(new_uid, new_gid);
} }
KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(StringView path, Inode& base, RetainPtr<Inode>* parent_inode, int options) KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(StringView path, Inode& base, RetainPtr<Inode>* parent_inode, int options)
@ -431,72 +445,78 @@ KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(StringView path, Inode& ba
return Retained<Inode>(*get_inode(result.value())); return Retained<Inode>(*get_inode(result.value()));
} }
KResult VFS::link(StringView old_path, StringView new_path, Inode& base) KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
{ {
auto old_inode_or_error = resolve_path_to_inode(old_path, base); auto old_custody_or_error = resolve_path_to_custody(old_path, base);
if (old_inode_or_error.is_error()) if (old_custody_or_error.is_error())
return old_inode_or_error.error(); return old_custody_or_error.error();
auto old_inode = old_inode_or_error.value(); auto& old_custody = *old_custody_or_error.value();
auto& old_inode = old_custody.inode();
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto new_inode_or_error = resolve_path_to_inode(new_path, base, &parent_inode); auto new_custody_or_error = resolve_path_to_custody(new_path, base, &parent_custody);
if (!new_inode_or_error.is_error()) if (!new_custody_or_error.is_error())
return KResult(-EEXIST); return KResult(-EEXIST);
if (!parent_inode) if (!parent_custody)
return KResult(-ENOENT); return KResult(-ENOENT);
if (parent_inode->fsid() != old_inode->fsid()) auto& parent_inode = parent_custody->inode();
if (parent_inode.fsid() != old_inode.fsid())
return KResult(-EXDEV); return KResult(-EXDEV);
if (parent_inode->fs().is_readonly()) if (parent_inode.fs().is_readonly())
return KResult(-EROFS); return KResult(-EROFS);
if (!parent_inode->metadata().may_write(current->process())) if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
return parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0); return parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), 0);
} }
KResult VFS::unlink(StringView path, Inode& base) KResult VFS::unlink(StringView path, Custody& base)
{ {
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode); auto custody_or_error = resolve_path_to_custody(path, base, &parent_custody);
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return inode_or_error.error(); return custody_or_error.error();
auto inode = inode_or_error.value(); auto& custody = *custody_or_error.value();
auto& inode = custody.inode();
if (inode->is_directory()) if (inode.is_directory())
return KResult(-EISDIR); return KResult(-EISDIR);
if (!parent_inode->metadata().may_write(current->process())) auto& parent_inode = parent_custody->inode();
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
if (parent_inode->metadata().is_sticky()) { if (parent_inode.metadata().is_sticky()) {
if (!current->process().is_superuser() && inode->metadata().uid != current->process().euid()) if (!current->process().is_superuser() && inode.metadata().uid != current->process().euid())
return KResult(-EACCES); return KResult(-EACCES);
} }
return parent_inode->remove_child(FileSystemPath(path).basename()); return parent_inode.remove_child(FileSystemPath(path).basename());
} }
KResult VFS::symlink(StringView target, StringView linkpath, Inode& base) KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
{ {
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto existing_file_or_error = resolve_path_to_inode(linkpath, base, &parent_inode); auto existing_custody_or_error = resolve_path_to_custody(linkpath, base, &parent_custody);
if (!existing_file_or_error.is_error()) if (!existing_custody_or_error.is_error())
return KResult(-EEXIST); return KResult(-EEXIST);
if (!parent_inode) if (!parent_custody)
return KResult(-ENOENT); return KResult(-ENOENT);
if (existing_file_or_error.error() != -ENOENT) if (existing_custody_or_error.error() != -ENOENT)
return existing_file_or_error.error(); return existing_custody_or_error.error();
if (!parent_inode->metadata().may_write(current->process())) auto& parent_inode = parent_custody->inode();
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
FileSystemPath p(linkpath); FileSystemPath p(linkpath);
dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode->fsid(), parent_inode->index()); dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode.fsid(), parent_inode.index());
int error; int error;
auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), 0120644, 0, 0, error); auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, error);
if (!new_file) if (!new_file)
return KResult(error); return KResult(error);
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const byte*)target.characters(), nullptr); ssize_t nwritten = new_file->write_bytes(0, target.length(), (const byte*)target.characters(), nullptr);
@ -505,38 +525,41 @@ KResult VFS::symlink(StringView target, StringView linkpath, Inode& base)
return KSuccess; return KSuccess;
} }
KResult VFS::rmdir(StringView path, Inode& base) KResult VFS::rmdir(StringView path, Custody& base)
{ {
RetainPtr<Inode> parent_inode; RetainPtr<Custody> parent_custody;
auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode); auto custody_or_error = resolve_path_to_custody(path, base, &parent_custody);
if (inode_or_error.is_error()) if (custody_or_error.is_error())
return KResult(inode_or_error.error()); return KResult(custody_or_error.error());
auto inode = inode_or_error.value(); auto& custody = *custody_or_error.value();
if (inode->fs().is_readonly()) auto& inode = custody.inode();
if (inode.fs().is_readonly())
return KResult(-EROFS); return KResult(-EROFS);
// FIXME: We should return EINVAL if the last component of the path is "." // FIXME: We should return EINVAL if the last component of the path is "."
// FIXME: We should return ENOTEMPTY if the last component of the path is ".." // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
if (!inode->is_directory()) if (!inode.is_directory())
return KResult(-ENOTDIR); return KResult(-ENOTDIR);
if (!parent_inode->metadata().may_write(current->process())) auto& parent_inode = parent_custody->inode();
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES); return KResult(-EACCES);
if (inode->directory_entry_count() != 2) if (inode.directory_entry_count() != 2)
return KResult(-ENOTEMPTY); return KResult(-ENOTEMPTY);
auto result = inode->remove_child("."); auto result = inode.remove_child(".");
if (result.is_error()) if (result.is_error())
return result; return result;
result = inode->remove_child(".."); result = inode.remove_child("..");
if (result.is_error()) if (result.is_error())
return result; return result;
return parent_inode->remove_child(FileSystemPath(path).basename()); return parent_inode.remove_child(FileSystemPath(path).basename());
} }
KResultOr<InodeIdentifier> VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode) KResultOr<InodeIdentifier> VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode)
@ -748,7 +771,7 @@ Custody& VFS::root_custody()
return *m_root_custody; return *m_root_custody;
} }
KResultOr<Retained<Custody>> VFS::resolve_path_to_custody(StringView path, Custody& base, int options) KResultOr<Retained<Custody>> VFS::resolve_path_to_custody(StringView path, Custody& base, RetainPtr<Custody>* parent_custody, int options)
{ {
if (path.is_empty()) if (path.is_empty())
return KResult(-EINVAL); return KResult(-EINVAL);
@ -759,7 +782,7 @@ KResultOr<Retained<Custody>> VFS::resolve_path_to_custody(StringView path, Custo
Vector<Retained<Custody>, 32> custody_chain; Vector<Retained<Custody>, 32> custody_chain;
if (path[0] == '/') { if (path[0] == '/') {
custody_chain.append(Retained<Custody>(base)); custody_chain.append(root_custody());
crumb_id = root_inode_id(); crumb_id = root_inode_id();
} else { } else {
for (auto* custody = &base; custody; custody = custody->parent()) { for (auto* custody = &base; custody; custody = custody->parent()) {
@ -769,6 +792,9 @@ KResultOr<Retained<Custody>> VFS::resolve_path_to_custody(StringView path, Custo
crumb_id = base.inode().identifier(); crumb_id = base.inode().identifier();
} }
if (parent_custody)
*parent_custody = custody_chain.last();
for (int i = 0; i < parts.size(); ++i) { for (int i = 0; i < parts.size(); ++i) {
bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode(); bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
auto& part = parts[i]; auto& part = parts[i];
@ -798,6 +824,12 @@ KResultOr<Retained<Custody>> VFS::resolve_path_to_custody(StringView path, Custo
ASSERT(crumb_inode); ASSERT(crumb_inode);
custody_chain.append(Custody::create(custody_chain.last().ptr(), part, *crumb_inode)); custody_chain.append(Custody::create(custody_chain.last().ptr(), part, *crumb_inode));
metadata = crumb_inode->metadata(); metadata = crumb_inode->metadata();
if (metadata.is_directory()) {
if (i != parts.size() - 1) {
if (parent_custody)
*parent_custody = custody_chain.last();
}
}
if (metadata.is_symlink()) { if (metadata.is_symlink()) {
if (i == parts.size() - 1) { if (i == parts.size() - 1) {
if (options & O_NOFOLLOW) if (options & O_NOFOLLOW)

View file

@ -63,22 +63,22 @@ public:
bool mount(RetainPtr<FS>&&, StringView path); bool mount(RetainPtr<FS>&&, StringView path);
KResultOr<Retained<FileDescriptor>> open(RetainPtr<Device>&&, int options); KResultOr<Retained<FileDescriptor>> open(RetainPtr<Device>&&, int options);
KResultOr<Retained<FileDescriptor>> open(StringView path, int options, mode_t mode, Inode& base); KResultOr<Retained<FileDescriptor>> open(StringView path, int options, mode_t mode, Custody& base);
KResultOr<Retained<FileDescriptor>> create(StringView path, int options, mode_t mode, Inode& base); KResultOr<Retained<FileDescriptor>> create(StringView path, int options, mode_t mode, Custody& base);
KResult mkdir(StringView path, mode_t mode, Inode& base); KResult mkdir(StringView path, mode_t mode, Custody& base);
KResult link(StringView old_path, StringView new_path, Inode& base); KResult link(StringView old_path, StringView new_path, Custody& base);
KResult unlink(StringView path, Inode& base); KResult unlink(StringView path, Custody& base);
KResult symlink(StringView target, StringView linkpath, Inode& base); KResult symlink(StringView target, StringView linkpath, Custody& base);
KResult rmdir(StringView path, Inode& base); KResult rmdir(StringView path, Custody& base);
KResult chmod(StringView path, mode_t, Inode& base); KResult chmod(StringView path, mode_t, Custody& base);
KResult chmod(Inode&, mode_t); KResult fchmod(Inode&, mode_t);
KResult chown(StringView path, uid_t, gid_t, Inode& base); KResult chown(StringView path, uid_t, gid_t, Custody& base);
KResult access(StringView path, int mode, Inode& base); KResult access(StringView path, int mode, Custody& base);
KResult stat(StringView path, int options, Inode& base, struct stat&); KResult stat(StringView path, int options, Custody& base, struct stat&);
KResult utime(StringView path, Inode& base, time_t atime, time_t mtime); KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
KResult rename(StringView oldpath, StringView newpath, Inode& base); KResult rename(StringView oldpath, StringView newpath, Custody& base);
KResult mknod(StringView path, mode_t, dev_t, Inode& base); KResult mknod(StringView path, mode_t, dev_t, Custody& base);
KResultOr<Retained<Inode>> open_directory(StringView path, Inode& base); KResultOr<Retained<Custody>> open_directory(StringView path, Custody& base);
void register_device(Device&); void register_device(Device&);
void unregister_device(Device&); void unregister_device(Device&);
@ -98,7 +98,7 @@ public:
Device* get_device(unsigned major, unsigned minor); Device* get_device(unsigned major, unsigned minor);
Custody& root_custody(); Custody& root_custody();
KResultOr<Retained<Custody>> resolve_path_to_custody(StringView path, Custody& base, int options = 0); KResultOr<Retained<Custody>> resolve_path_to_custody(StringView path, Custody& base, RetainPtr<Custody>* parent = nullptr, int options = 0);
private: private:
friend class FileDescriptor; friend class FileDescriptor;

View file

@ -154,7 +154,7 @@ void init_ksyms()
void load_ksyms() void load_ksyms()
{ {
auto result = VFS::the().open("/kernel.map", 0, 0, *VFS::the().root_inode()); auto result = VFS::the().open("/kernel.map", 0, 0, VFS::the().root_custody());
ASSERT(!result.is_error()); ASSERT(!result.is_error());
auto descriptor = result.value(); auto descriptor = result.value();
auto buffer = descriptor->read_entire_file(); auto buffer = descriptor->read_entire_file();

View file

@ -55,7 +55,7 @@ KResult LocalSocket::bind(const sockaddr* address, socklen_t address_size)
kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->process().name().characters(), current->pid(), this, safe_address); kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
#endif #endif
auto result = VFS::the().open(safe_address, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->process().cwd_inode()); auto result = VFS::the().open(safe_address, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->process().cwd_custody());
if (result.is_error()) { if (result.is_error()) {
if (result.error() == -EEXIST) if (result.error() == -EEXIST)
return KResult(-EADDRINUSE); return KResult(-EADDRINUSE);
@ -87,7 +87,7 @@ KResult LocalSocket::connect(FileDescriptor& descriptor, const sockaddr* address
kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address); kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
#endif #endif
auto descriptor_or_error = VFS::the().open(safe_address, 0, 0, current->process().cwd_inode()); auto descriptor_or_error = VFS::the().open(safe_address, 0, 0, current->process().cwd_custody());
if (descriptor_or_error.is_error()) if (descriptor_or_error.is_error())
return KResult(-ECONNREFUSED); return KResult(-ECONNREFUSED);
m_file = move(descriptor_or_error.value()); m_file = move(descriptor_or_error.value());

View file

@ -24,6 +24,7 @@
#include <AK/Time.h> #include <AK/Time.h>
#include <Kernel/SharedMemory.h> #include <Kernel/SharedMemory.h>
#include <Kernel/ProcessTracer.h> #include <Kernel/ProcessTracer.h>
#include <Kernel/FileSystem/Custody.h>
//#define DEBUG_POLL_SELECT //#define DEBUG_POLL_SELECT
//#define DEBUG_IO //#define DEBUG_IO
@ -227,7 +228,7 @@ int Process::sys$gethostname(char* buffer, ssize_t size)
Process* Process::fork(RegisterDump& regs) Process* Process::fork(RegisterDump& regs)
{ {
auto* child = new Process(String(m_name), m_uid, m_gid, m_pid, m_ring, m_cwd.copy_ref(), m_executable.copy_ref(), m_tty, this); auto* child = new Process(String(m_name), m_uid, m_gid, m_pid, m_ring, m_cwd_custody.copy_ref(), m_executable_custody.copy_ref(), m_tty, this);
if (!child) if (!child)
return nullptr; return nullptr;
@ -310,7 +311,7 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
if (parts.is_empty()) if (parts.is_empty())
return -ENOENT; return -ENOENT;
auto result = VFS::the().open(path.view(), 0, 0, cwd_inode()); auto result = VFS::the().open(path.view(), 0, 0, cwd_custody());
if (result.is_error()) if (result.is_error())
return result.error(); return result.error();
auto descriptor = result.value(); auto descriptor = result.value();
@ -434,7 +435,7 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
main_thread().m_tss.esp0 = old_esp0; main_thread().m_tss.esp0 = old_esp0;
main_thread().m_tss.ss2 = m_pid; main_thread().m_tss.ss2 = m_pid;
m_executable = descriptor->inode(); m_executable_custody = descriptor->custody();
if (descriptor->metadata().is_setuid()) if (descriptor->metadata().is_setuid())
m_euid = descriptor->metadata().uid; m_euid = descriptor->metadata().uid;
@ -520,15 +521,15 @@ Process* Process::create_user_process(const String& path, uid_t uid, gid_t gid,
if (arguments.is_empty()) { if (arguments.is_empty()) {
arguments.append(parts.last()); arguments.append(parts.last());
} }
RetainPtr<Inode> cwd; RetainPtr<Custody> cwd;
{ {
InterruptDisabler disabler; InterruptDisabler disabler;
if (auto* parent = Process::from_pid(parent_pid)) if (auto* parent = Process::from_pid(parent_pid))
cwd = parent->m_cwd.copy_ref(); cwd = parent->m_cwd_custody.copy_ref();
} }
if (!cwd) if (!cwd)
cwd = VFS::the().root_inode(); cwd = VFS::the().root_custody();
auto* process = new Process(parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty); auto* process = new Process(parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty);
@ -566,7 +567,7 @@ Process* Process::create_kernel_process(String&& name, void (*e)())
return process; return process;
} }
Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RetainPtr<Inode>&& cwd, RetainPtr<Inode>&& executable, TTY* tty, Process* fork_parent) Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RetainPtr<Custody>&& cwd, RetainPtr<Custody>&& executable, TTY* tty, Process* fork_parent)
: m_name(move(name)) : m_name(move(name))
, m_pid(next_pid++) // FIXME: RACE: This variable looks racy! , m_pid(next_pid++) // FIXME: RACE: This variable looks racy!
, m_uid(uid) , m_uid(uid)
@ -574,8 +575,8 @@ Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring
, m_euid(uid) , m_euid(uid)
, m_egid(gid) , m_egid(gid)
, m_ring(ring) , m_ring(ring)
, m_cwd(move(cwd)) , m_executable_custody(move(executable))
, m_executable(move(executable)) , m_cwd_custody(move(cwd))
, m_tty(tty) , m_tty(tty)
, m_ppid(ppid) , m_ppid(ppid)
{ {
@ -997,14 +998,14 @@ int Process::sys$utime(const char* pathname, const utimbuf* buf)
mtime = now.tv_sec; mtime = now.tv_sec;
atime = now.tv_sec; atime = now.tv_sec;
} }
return VFS::the().utime(StringView(pathname), cwd_inode(), atime, mtime); return VFS::the().utime(StringView(pathname), cwd_custody(), atime, mtime);
} }
int Process::sys$access(const char* pathname, int mode) int Process::sys$access(const char* pathname, int mode)
{ {
if (!validate_read_str(pathname)) if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
return VFS::the().access(StringView(pathname), mode, cwd_inode()); return VFS::the().access(StringView(pathname), mode, cwd_custody());
} }
int Process::sys$fcntl(int fd, int cmd, dword arg) int Process::sys$fcntl(int fd, int cmd, dword arg)
@ -1058,14 +1059,14 @@ int Process::sys$lstat(const char* path, stat* statbuf)
{ {
if (!validate_write_typed(statbuf)) if (!validate_write_typed(statbuf))
return -EFAULT; return -EFAULT;
return VFS::the().stat(StringView(path), O_NOFOLLOW_NOERROR, cwd_inode(), *statbuf); return VFS::the().stat(StringView(path), O_NOFOLLOW_NOERROR, cwd_custody(), *statbuf);
} }
int Process::sys$stat(const char* path, stat* statbuf) int Process::sys$stat(const char* path, stat* statbuf)
{ {
if (!validate_write_typed(statbuf)) if (!validate_write_typed(statbuf))
return -EFAULT; return -EFAULT;
return VFS::the().stat(StringView(path), O_NOFOLLOW_NOERROR, cwd_inode(), *statbuf); return VFS::the().stat(StringView(path), O_NOFOLLOW_NOERROR, cwd_custody(), *statbuf);
} }
int Process::sys$readlink(const char* path, char* buffer, ssize_t size) int Process::sys$readlink(const char* path, char* buffer, ssize_t size)
@ -1077,7 +1078,7 @@ int Process::sys$readlink(const char* path, char* buffer, ssize_t size)
if (!validate_write(buffer, size)) if (!validate_write(buffer, size))
return -EFAULT; return -EFAULT;
auto result = VFS::the().open(path, O_RDONLY | O_NOFOLLOW_NOERROR, 0, cwd_inode()); auto result = VFS::the().open(path, O_RDONLY | O_NOFOLLOW_NOERROR, 0, cwd_custody());
if (result.is_error()) if (result.is_error())
return result.error(); return result.error();
auto descriptor = result.value(); auto descriptor = result.value();
@ -1099,10 +1100,10 @@ int Process::sys$chdir(const char* path)
{ {
if (!validate_read_str(path)) if (!validate_read_str(path))
return -EFAULT; return -EFAULT;
auto directory_or_error = VFS::the().open_directory(StringView(path), cwd_inode()); auto directory_or_error = VFS::the().open_directory(StringView(path), cwd_custody());
if (directory_or_error.is_error()) if (directory_or_error.is_error())
return directory_or_error.error(); return directory_or_error.error();
m_cwd = *directory_or_error.value(); m_cwd_custody = *directory_or_error.value();
return 0; return 0;
} }
@ -1112,10 +1113,7 @@ int Process::sys$getcwd(char* buffer, ssize_t size)
return -EINVAL; return -EINVAL;
if (!validate_write(buffer, size)) if (!validate_write(buffer, size))
return -EFAULT; return -EFAULT;
auto path_or_error = VFS::the().absolute_path(cwd_inode()); auto path = cwd_custody().absolute_path();
if (path_or_error.is_error())
return path_or_error.error();
auto path = path_or_error.value();
if (size < path.length() + 1) if (size < path.length() + 1)
return -ERANGE; return -ERANGE;
strcpy(buffer, path.characters()); strcpy(buffer, path.characters());
@ -1142,7 +1140,7 @@ int Process::sys$open(const char* path, int options, mode_t mode)
int fd = alloc_fd(); int fd = alloc_fd();
if (fd < 0) if (fd < 0)
return fd; return fd;
auto result = VFS::the().open(path, options, mode & ~umask(), cwd_inode()); auto result = VFS::the().open(path, options, mode & ~umask(), cwd_custody());
if (result.is_error()) if (result.is_error())
return result.error(); return result.error();
auto descriptor = result.value(); auto descriptor = result.value();
@ -1321,6 +1319,15 @@ uid_t Process::sys$getuid()
gid_t Process::sys$getgid() gid_t Process::sys$getgid()
{ {
auto result = VFS::the().resolve_path_to_custody("/home/anon", VFS::the().root_custody());
if (result.is_error()) {
dbgprintf("resolve_path_to_custody failed\n");
} else {
for (auto* custody = result.value().ptr(); custody; custody = custody->parent()) {
dbgprintf("Custody{%p} name=%s, inode=%u:%u\n", custody, custody->name().characters(), custody->inode().fsid(), custody->inode().index());
}
}
return m_gid; return m_gid;
} }
@ -1755,7 +1762,7 @@ int Process::sys$mkdir(const char* pathname, mode_t mode)
return -EINVAL; return -EINVAL;
if (pathname_length >= 255) if (pathname_length >= 255)
return -ENAMETOOLONG; return -ENAMETOOLONG;
return VFS::the().mkdir(StringView(pathname, pathname_length), mode & ~umask(), cwd_inode()); return VFS::the().mkdir(StringView(pathname, pathname_length), mode & ~umask(), cwd_custody());
} }
clock_t Process::sys$times(tms* times) clock_t Process::sys$times(tms* times)
@ -1934,12 +1941,11 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout)
return fds_with_revents; return fds_with_revents;
} }
Inode& Process::cwd_inode() Custody& Process::cwd_custody()
{ {
// FIXME: This is retarded factoring. if (!m_cwd_custody)
if (!m_cwd) m_cwd_custody = VFS::the().root_custody();
m_cwd = VFS::the().root_inode(); return *m_cwd_custody;
return *m_cwd;
} }
int Process::sys$link(const char* old_path, const char* new_path) int Process::sys$link(const char* old_path, const char* new_path)
@ -1948,14 +1954,14 @@ int Process::sys$link(const char* old_path, const char* new_path)
return -EFAULT; return -EFAULT;
if (!validate_read_str(new_path)) if (!validate_read_str(new_path))
return -EFAULT; return -EFAULT;
return VFS::the().link(StringView(old_path), StringView(new_path), cwd_inode()); return VFS::the().link(StringView(old_path), StringView(new_path), cwd_custody());
} }
int Process::sys$unlink(const char* pathname) int Process::sys$unlink(const char* pathname)
{ {
if (!validate_read_str(pathname)) if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
return VFS::the().unlink(StringView(pathname), cwd_inode()); return VFS::the().unlink(StringView(pathname), cwd_custody());
} }
int Process::sys$symlink(const char* target, const char* linkpath) int Process::sys$symlink(const char* target, const char* linkpath)
@ -1964,14 +1970,14 @@ int Process::sys$symlink(const char* target, const char* linkpath)
return -EFAULT; return -EFAULT;
if (!validate_read_str(linkpath)) if (!validate_read_str(linkpath))
return -EFAULT; return -EFAULT;
return VFS::the().symlink(StringView(target), StringView(linkpath), cwd_inode()); return VFS::the().symlink(StringView(target), StringView(linkpath), cwd_custody());
} }
int Process::sys$rmdir(const char* pathname) int Process::sys$rmdir(const char* pathname)
{ {
if (!validate_read_str(pathname)) if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
return VFS::the().rmdir(StringView(pathname), cwd_inode()); return VFS::the().rmdir(StringView(pathname), cwd_custody());
} }
int Process::sys$read_tsc(dword* lsw, dword* msw) int Process::sys$read_tsc(dword* lsw, dword* msw)
@ -1988,7 +1994,7 @@ int Process::sys$chmod(const char* pathname, mode_t mode)
{ {
if (!validate_read_str(pathname)) if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
return VFS::the().chmod(StringView(pathname), mode, cwd_inode()); return VFS::the().chmod(StringView(pathname), mode, cwd_custody());
} }
int Process::sys$fchmod(int fd, mode_t mode) int Process::sys$fchmod(int fd, mode_t mode)
@ -2003,7 +2009,7 @@ int Process::sys$chown(const char* pathname, uid_t uid, gid_t gid)
{ {
if (!validate_read_str(pathname)) if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
return VFS::the().chown(StringView(pathname), uid, gid, cwd_inode()); return VFS::the().chown(StringView(pathname), uid, gid, cwd_custody());
} }
void Process::finalize() void Process::finalize()
@ -2706,7 +2712,7 @@ int Process::sys$rename(const char* oldpath, const char* newpath)
return -EFAULT; return -EFAULT;
if (!validate_read_str(newpath)) if (!validate_read_str(newpath))
return -EFAULT; return -EFAULT;
return VFS::the().rename(StringView(oldpath), StringView(newpath), cwd_inode()); return VFS::the().rename(StringView(oldpath), StringView(newpath), cwd_custody());
} }
int Process::sys$shm_open(const char* name, int flags, mode_t mode) int Process::sys$shm_open(const char* name, int flags, mode_t mode)
@ -2780,5 +2786,5 @@ int Process::sys$mknod(const char* pathname, mode_t mode, dev_t dev)
if (!validate_read_str(pathname)) if (!validate_read_str(pathname))
return -EFAULT; return -EFAULT;
return VFS::the().mknod(StringView(pathname), mode, dev, cwd_inode()); return VFS::the().mknod(StringView(pathname), mode, dev, cwd_custody());
} }

View file

@ -233,8 +233,8 @@ public:
template<typename T> template<typename T>
bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); } bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
Inode& cwd_inode(); Custody& cwd_custody();
Inode* executable_inode() { return m_executable.ptr(); } Custody* executable_custody() { return m_executable_custody.ptr(); }
int number_of_open_file_descriptors() const; int number_of_open_file_descriptors() const;
int max_open_file_descriptors() const { return m_max_open_file_descriptors; } int max_open_file_descriptors() const { return m_max_open_file_descriptors; }
@ -273,7 +273,7 @@ private:
friend class Scheduler; friend class Scheduler;
friend class Region; friend class Region;
Process(String&& name, uid_t, gid_t, pid_t ppid, RingLevel, RetainPtr<Inode>&& cwd = nullptr, RetainPtr<Inode>&& executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr); Process(String&& name, uid_t, gid_t, pid_t ppid, RingLevel, RetainPtr<Custody>&& cwd = nullptr, RetainPtr<Custody>&& executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
Range allocate_range(LinearAddress, size_t); Range allocate_range(LinearAddress, size_t);
@ -319,8 +319,8 @@ private:
byte m_termination_status { 0 }; byte m_termination_status { 0 };
byte m_termination_signal { 0 }; byte m_termination_signal { 0 };
RetainPtr<Inode> m_cwd; RetainPtr<Custody> m_executable_custody;
RetainPtr<Inode> m_executable; RetainPtr<Custody> m_cwd_custody;
TTY* m_tty { nullptr }; TTY* m_tty { nullptr };