diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 18733d2bdf..c5b62fd26a 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -96,7 +96,7 @@ namespace Kernel BAN::ErrorOr sys_getpgid(pid_t); BAN::ErrorOr create_file_or_dir(BAN::StringView name, mode_t mode); - BAN::ErrorOr open_file(BAN::StringView path, int, mode_t = 0); + BAN::ErrorOr open_file(BAN::StringView path, int oflag, mode_t = 0); BAN::ErrorOr sys_open(const char* path, int, mode_t); BAN::ErrorOr sys_openat(int, const char* path, int, mode_t); BAN::ErrorOr sys_close(int fd); @@ -180,6 +180,8 @@ namespace Kernel // Return false if access was page violation (segfault) BAN::ErrorOr allocate_page_for_demand_paging(vaddr_t addr); + BAN::ErrorOr absolute_path_of(BAN::StringView) const; + private: Process(const Credentials&, pid_t pid, pid_t parent, pid_t sid, pid_t pgrp); static Process* create_process(const Credentials&, pid_t parent, pid_t sid = 0, pid_t pgrp = 0); @@ -189,8 +191,6 @@ namespace Kernel BAN::ErrorOr block_until_exit(pid_t pid); - BAN::ErrorOr absolute_path_of(BAN::StringView) const; - BAN::ErrorOr validate_string_access(const char*); BAN::ErrorOr validate_pointer_access(const void*, size_t); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index f792008f3b..d544557f50 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -710,6 +710,8 @@ namespace Kernel BAN::ErrorOr Process::open_file(BAN::StringView path, int flags, mode_t mode) { + LockGuard _(m_lock); + BAN::String absolute_path = TRY(absolute_path_of(path)); if (flags & O_CREAT) @@ -717,6 +719,8 @@ namespace Kernel if (flags & O_DIRECTORY) return BAN::Error::from_errno(ENOTSUP); auto file_or_error = VirtualFileSystem::get().file_from_absolute_path(m_credentials, absolute_path, O_WRONLY); + if (!file_or_error.is_error() && (flags & O_EXCL)) + return BAN::Error::from_errno(EEXIST); if (file_or_error.is_error()) { if (file_or_error.error().get_error_code() == ENOENT) @@ -1744,7 +1748,7 @@ namespace Kernel BAN::ErrorOr Process::absolute_path_of(BAN::StringView path) const { - ASSERT(m_lock.is_locked()); + LockGuard _(m_lock); if (path.empty() || path == "."sv) return m_working_directory;