Kernel: Remove SYS_OPEN

This can be done with SYS_OPENAT with fd set to AT_FDCWD
This commit is contained in:
Bananymous 2024-09-17 16:16:47 +03:00
parent 7177da7d62
commit 708a720d9d
6 changed files with 27 additions and 44 deletions

View File

@ -102,8 +102,6 @@ namespace Kernel
BAN::ErrorOr<long> open_inode(VirtualFileSystem::File&&, int flags); BAN::ErrorOr<long> open_inode(VirtualFileSystem::File&&, int flags);
BAN::ErrorOr<void> create_file_or_dir(const VirtualFileSystem::File& parent, BAN::StringView path, mode_t mode) const; BAN::ErrorOr<void> create_file_or_dir(const VirtualFileSystem::File& parent, BAN::StringView path, mode_t mode) const;
BAN::ErrorOr<long> open_file_impl(const VirtualFileSystem::File& parent, BAN::StringView path, int oflag, mode_t = 0);
BAN::ErrorOr<long> sys_open(const char* path, int, mode_t);
BAN::ErrorOr<long> sys_openat(int, const char* path, int, mode_t); BAN::ErrorOr<long> sys_openat(int, const char* path, int, mode_t);
BAN::ErrorOr<long> sys_close(int fd); BAN::ErrorOr<long> sys_close(int fd);
BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count); BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count);

View File

@ -849,14 +849,31 @@ namespace Kernel
return TRY(m_open_file_descriptors.open(BAN::move(file), flags)); return TRY(m_open_file_descriptors.open(BAN::move(file), flags));
} }
BAN::ErrorOr<long> Process::open_file_impl(const VirtualFileSystem::File& parent, BAN::StringView path, int flags, mode_t mode) BAN::ErrorOr<long> Process::sys_openat(int fd, const char* path, int flags, mode_t mode)
{ {
if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT)) if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
LockGuard _(m_process_lock); LockGuard _(m_process_lock);
auto file_or_error = VirtualFileSystem::get().file_from_relative_path(parent, m_credentials, path, flags | O_NOFOLLOW); TRY(validate_string_access(path));
VirtualFileSystem::File parent_file;
if (path[0] == '/')
parent_file = VirtualFileSystem::get().root_file();
else if (fd == AT_FDCWD)
parent_file = TRY(m_working_directory.clone());
else
{
int flags = TRY(m_open_file_descriptors.flags_of(fd));
if (!(flags & O_RDONLY) && !(flags & O_SEARCH))
return BAN::Error::from_errno(EBADF);
if (!TRY(m_open_file_descriptors.inode_of(fd))->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
parent_file = TRY(m_open_file_descriptors.file_of(fd));
}
auto file_or_error = VirtualFileSystem::get().file_from_relative_path(parent_file, m_credentials, path, flags | O_NOFOLLOW);
VirtualFileSystem::File file; VirtualFileSystem::File file;
if (file_or_error.is_error()) if (file_or_error.is_error())
@ -865,8 +882,8 @@ namespace Kernel
return file_or_error.release_error(); return file_or_error.release_error();
// FIXME: There is a race condition between next two lines // FIXME: There is a race condition between next two lines
TRY(create_file_or_dir(parent, path, (mode & 0777) | Inode::Mode::IFREG)); TRY(create_file_or_dir(parent_file, path, (mode & 0777) | Inode::Mode::IFREG));
file = TRY(VirtualFileSystem::get().file_from_relative_path(parent, m_credentials, path, flags)); file = TRY(VirtualFileSystem::get().file_from_relative_path(parent_file, m_credentials, path, flags));
} }
else else
{ {
@ -879,13 +896,13 @@ namespace Kernel
if (!file.inode->mode().ifdir() && (flags & O_DIRECTORY)) if (!file.inode->mode().ifdir() && (flags & O_DIRECTORY))
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
if (file.inode->mode().iflnk() && !(flags & O_NOFOLLOW)) if (file.inode->mode().iflnk() && !(flags & O_NOFOLLOW))
file = TRY(VirtualFileSystem::get().file_from_relative_path(parent, m_credentials, path, flags)); file = TRY(VirtualFileSystem::get().file_from_relative_path(parent_file, m_credentials, path, flags));
} }
auto inode = file.inode; auto inode = file.inode;
ASSERT(inode); ASSERT(inode);
int fd = TRY(m_open_file_descriptors.open(BAN::move(file), flags)); fd = TRY(m_open_file_descriptors.open(BAN::move(file), flags));
// Open controlling terminal // Open controlling terminal
if (!(flags & O_NOCTTY) && inode->is_tty() && is_session_leader() && !m_controlling_terminal) if (!(flags & O_NOCTTY) && inode->is_tty() && is_session_leader() && !m_controlling_terminal)
@ -894,37 +911,6 @@ namespace Kernel
return fd; return fd;
} }
BAN::ErrorOr<long> Process::sys_open(const char* path, int flags, mode_t mode)
{
LockGuard _(m_process_lock);
TRY(validate_string_access(path));
if (path[0] == '/')
return open_file_impl(VirtualFileSystem::get().root_file(), path, flags, mode);
return open_file_impl(m_working_directory, path, flags, mode);
}
BAN::ErrorOr<long> Process::sys_openat(int fd, const char* path, int flags, mode_t mode)
{
LockGuard _(m_process_lock);
TRY(validate_string_access(path));
VirtualFileSystem::File parent_file;
if (fd == AT_FDCWD)
parent_file = TRY(m_working_directory.clone());
else if (path[0] != '/')
{
int flags = TRY(m_open_file_descriptors.flags_of(fd));
if (!(flags & O_RDONLY) && !(flags & O_SEARCH))
return BAN::Error::from_errno(EBADF);
if (!TRY(m_open_file_descriptors.inode_of(fd))->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
parent_file = TRY(m_open_file_descriptors.file_of(fd));
}
return open_file_impl(parent_file, path, flags, mode);
}
BAN::ErrorOr<long> Process::sys_close(int fd) BAN::ErrorOr<long> Process::sys_close(int fd)
{ {
LockGuard _(m_process_lock); LockGuard _(m_process_lock);

View File

@ -15,7 +15,7 @@ int open(const char* path, int oflag, ...)
mode_t mode = va_arg(args, mode_t); mode_t mode = va_arg(args, mode_t);
va_end(args); va_end(args);
return syscall(SYS_OPEN, path, oflag, __UMASKED_MODE(mode)); return openat(AT_FDCWD, path, oflag, mode);
} }
int openat(int fd, const char* path, int oflag, ...) int openat(int fd, const char* path, int oflag, ...)

View File

@ -11,7 +11,6 @@ __BEGIN_DECLS
O(SYS_WRITE, write) \ O(SYS_WRITE, write) \
O(SYS_TERMID, termid) \ O(SYS_TERMID, termid) \
O(SYS_CLOSE, close) \ O(SYS_CLOSE, close) \
O(SYS_OPEN, open) \
O(SYS_OPENAT, openat) \ O(SYS_OPENAT, openat) \
O(SYS_SEEK, seek) \ O(SYS_SEEK, seek) \
O(SYS_TELL, tell) \ O(SYS_TELL, tell) \

View File

@ -557,7 +557,7 @@ static void handle_dynamic(LoadedElf& elf)
if (auto ret = syscall(SYS_REALPATH, path_buffer, realpath); ret < 0) if (auto ret = syscall(SYS_REALPATH, path_buffer, realpath); ret < 0)
print_error_and_exit("realpath", ret); print_error_and_exit("realpath", ret);
int library_fd = syscall(SYS_OPEN, realpath, O_RDONLY); int library_fd = syscall(SYS_OPENAT, AT_FDCWD, realpath, O_RDONLY);
if (library_fd < 0) if (library_fd < 0)
print_error_and_exit("could not open library", library_fd); print_error_and_exit("could not open library", library_fd);
@ -882,7 +882,7 @@ int _entry(int argc, char** argv, char** envp, int fd)
argc--; argc--;
argv++; argv++;
fd = syscall(SYS_OPEN, argv[0], O_RDONLY); fd = syscall(SYS_OPENAT, AT_FDCWD, argv[0], O_RDONLY);
if (fd < 0) if (fd < 0)
print_error_and_exit("could not open program", fd); print_error_and_exit("could not open program", fd);
} }

View File

@ -68,7 +68,7 @@ static int s_random_fd;
void init_random() void init_random()
{ {
s_random_fd = syscall(SYS_OPEN, "/dev/random", O_RDONLY); s_random_fd = syscall(SYS_OPENAT, AT_FDCWD, "/dev/random", O_RDONLY);
if (s_random_fd < 0) if (s_random_fd < 0)
print_error_and_exit("could not open /dev/random", s_random_fd); print_error_and_exit("could not open /dev/random", s_random_fd);
} }