kernel: Give each thread a working directory

This commit is contained in:
apio 2023-04-11 22:14:57 +02:00
parent dfce93c18f
commit 2d30935fdb
Signed by: apio
GPG Key ID: B8A7D06E42258954
9 changed files with 31 additions and 16 deletions

View File

@ -13,13 +13,15 @@ namespace VFS
return *root_fs->root_inode();
}
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth)
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{
auto parser = TRY(PathParser::create(path));
SharedPtr<Inode> current_inode = root_fs->root_inode();
SharedPtr<Inode> current_inode;
// FIXME: Properly handle relative paths.
if (parser.is_absolute() || !working_directory) current_inode = root_fs->root_inode();
else
current_inode = working_directory;
const char* section;
while (parser.next().try_set_value(section))
@ -31,12 +33,12 @@ namespace VFS
return current_inode;
}
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth)
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{
auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth));
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
if (!can_write(parent_inode, auth)) return err(EACCES);
@ -47,12 +49,12 @@ namespace VFS
return parent_inode->create_subdirectory(child_name.chars());
}
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth)
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{
auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth));
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
if (!can_write(parent_inode, auth)) return err(EACCES);

View File

@ -175,10 +175,14 @@ namespace VFS
extern SharedPtr<FileSystem> root_fs;
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth);
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth);
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth);
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
bool can_read(SharedPtr<Inode> inode, Credentials auth);

View File

@ -104,6 +104,8 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
memcpy(&current->regs, regs, sizeof(*regs));
auto current_directory_path = TRY(current->current_directory_path.clone());
auto image = TRY(ThreadImage::clone_from_thread(current));
auto thread = TRY(new_thread());
@ -114,6 +116,8 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
thread->fp_data.save();
thread->name = current->name;
thread->auth = current->auth;
thread->current_directory = current->current_directory;
thread->current_directory_path = move(current_directory_path);
for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; }

View File

@ -99,7 +99,7 @@ Result<u64> sys_chmod(Registers*, SyscallArgs args)
Credentials& auth = Scheduler::current()->auth;
auto inode = TRY(VFS::resolve_path(path.chars(), auth));
auto inode = TRY(VFS::resolve_path(path.chars(), auth, Scheduler::current()->current_directory));
if (auth.euid != 0 && auth.euid != inode->uid()) return err(EPERM);

View File

@ -13,7 +13,7 @@ Result<u64> sys_mkdir(Registers*, SyscallArgs args)
kinfoln("mkdir: attempting to create %s", path.chars());
auto inode = TRY(VFS::create_directory(path.chars(), current->auth));
auto inode = TRY(VFS::create_directory(path.chars(), current->auth, current->current_directory));
inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid);

View File

@ -24,7 +24,7 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
auto dirname = TRY(parser.dirname());
auto basename = TRY(parser.basename());
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth));
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
auto inode = TRY(parent->fs().create_device_inode(maj, min));

View File

@ -25,12 +25,13 @@ Result<u64> sys_open(Registers*, SyscallArgs args)
if ((flags & O_RDWR) == 0) { return err(EINVAL); }
int error;
bool ok = VFS::resolve_path(path.chars(), current->auth).try_set_value_or_error(inode, error);
bool ok =
VFS::resolve_path(path.chars(), current->auth, current->current_directory).try_set_value_or_error(inode, error);
if (!ok)
{
if (error == ENOENT && (flags & O_CREAT))
{
inode = TRY(VFS::create_file(path.chars(), current->auth));
inode = TRY(VFS::create_file(path.chars(), current->auth, current->current_directory));
inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid);
}

View File

@ -42,7 +42,7 @@ Result<u64> sys_stat(Registers*, SyscallArgs args)
Thread* current = Scheduler::current();
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth));
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
return do_stat(inode, st);
}

View File

@ -8,6 +8,7 @@
#include <luna/Result.h>
#include <luna/Stack.h>
#include <luna/StaticString.h>
#include <luna/String.h>
#ifdef ARCH_X86_64
#include "arch/x86_64/CPU.h"
@ -83,6 +84,9 @@ struct Thread : public LinkedListNode<Thread>
StaticString<128> name;
String current_directory_path = {};
SharedPtr<VFS::Inode> current_directory = {};
PageDirectory* directory;
bool is_idle()