Kernel: open() and openat() now take mode as parameter

O_CREAT now tries to create file if O_CREAT is specified
This commit is contained in:
Bananymous 2023-07-10 15:08:30 +03:00
parent a0ecbed726
commit f7c4bc908e
5 changed files with 38 additions and 11 deletions

View File

@ -77,8 +77,8 @@ namespace Kernel
BAN::ErrorOr<long> sys_geteuid() const { return m_credentials.euid(); }
BAN::ErrorOr<long> sys_getegid() const { return m_credentials.egid(); }
BAN::ErrorOr<long> sys_open(BAN::StringView, int);
BAN::ErrorOr<long> sys_openat(int, BAN::StringView, int);
BAN::ErrorOr<long> sys_open(BAN::StringView, int, mode_t = 0);
BAN::ErrorOr<long> sys_openat(int, BAN::StringView, int, mode_t = 0);
BAN::ErrorOr<long> sys_close(int fd);
BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count);
BAN::ErrorOr<long> sys_write(int fd, const void* buffer, size_t count);

View File

@ -490,14 +490,28 @@ namespace Kernel
}
}
BAN::ErrorOr<long> Process::sys_open(BAN::StringView path, int flags)
BAN::ErrorOr<long> Process::sys_open(BAN::StringView path, int flags, mode_t mode)
{
LockGuard _(m_lock);
BAN::String absolute_path = TRY(absolute_path_of(path));
if (flags & O_CREAT)
{
auto file_or_error = VirtualFileSystem::get().file_from_absolute_path(m_credentials, absolute_path, O_WRONLY);
if (file_or_error.is_error())
{
if (file_or_error.error().get_error_code() == ENOENT)
TRY(sys_creat(path, mode));
else
return file_or_error.release_error();
}
flags &= ~O_CREAT;
}
return TRY(m_open_file_descriptors.open(absolute_path, flags));
}
BAN::ErrorOr<long> Process::sys_openat(int fd, BAN::StringView path, int flags)
BAN::ErrorOr<long> Process::sys_openat(int fd, BAN::StringView path, int flags, mode_t mode)
{
LockGuard _(m_lock);
@ -506,7 +520,7 @@ namespace Kernel
TRY(absolute_path.push_back('/'));
TRY(absolute_path.append(path));
return sys_open(absolute_path, flags);
return sys_open(absolute_path, flags, mode);
}
BAN::ErrorOr<long> Process::sys_close(int fd)

View File

@ -49,10 +49,10 @@ namespace Kernel
ret = Process::current().sys_close((int)arg1);
break;
case SYS_OPEN:
ret = Process::current().sys_open((const char*)arg1, (int)arg2);
ret = Process::current().sys_open((const char*)arg1, (int)arg2, (mode_t)arg3);
break;
case SYS_OPENAT:
ret = Process::current().sys_openat((int)arg1, (const char*)arg2, (int)arg3);
ret = Process::current().sys_openat((int)arg1, (const char*)arg2, (int)arg3, (mode_t)arg4);
break;
case SYS_ALLOC:
ret = Process::current().sys_alloc((size_t)arg1);

View File

@ -1,14 +1,25 @@
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/syscall.h>
#include <unistd.h>
int open(const char* path, int oflag, ...)
{
return syscall(SYS_OPEN, path, oflag);
va_list args;
va_start(args, oflag);
mode_t mode = va_arg(args, mode_t);
va_end(args);
return syscall(SYS_OPEN, path, oflag, mode);
}
int openat(int fd, const char* path, int oflag, ...)
{
return syscall(SYS_OPENAT, fd, path, oflag);
va_list args;
va_start(args, oflag);
mode_t mode = va_arg(args, mode_t);
va_end(args);
return syscall(SYS_OPENAT, fd, path, oflag, mode);
}

View File

@ -69,7 +69,8 @@ long syscall(long syscall, ...)
{
const char* path = va_arg(args, const char*);
int oflags = va_arg(args, int);
ret = Kernel::syscall(SYS_OPEN, (uintptr_t)path, oflags);
mode_t mode = va_arg(args, mode_t);
ret = Kernel::syscall(SYS_OPEN, (uintptr_t)path, oflags, mode);
break;
}
case SYS_OPENAT:
@ -77,7 +78,8 @@ long syscall(long syscall, ...)
int fd = va_arg(args, int);
const char* path = va_arg(args, const char*);
int oflags = va_arg(args, int);
ret = Kernel::syscall(SYS_OPENAT, fd, (uintptr_t)path, oflags);
mode_t mode = va_arg(args, mode_t);
ret = Kernel::syscall(SYS_OPENAT, fd, (uintptr_t)path, oflags, mode);
break;
}
case SYS_ALLOC: