LibC/Kernel: Implement access

This commit is contained in:
Bananymous 2024-07-30 11:51:07 +03:00
parent e5bb843059
commit f0be4f86a6
4 changed files with 33 additions and 0 deletions

View File

@ -106,6 +106,7 @@ namespace Kernel
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);
BAN::ErrorOr<long> sys_access(const char* path, int amode);
BAN::ErrorOr<long> sys_create(const char*, mode_t);
BAN::ErrorOr<long> sys_create_dir(const char*, mode_t);
BAN::ErrorOr<long> sys_unlink(const char*);

View File

@ -784,6 +784,32 @@ namespace Kernel
return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count)));
}
BAN::ErrorOr<long> Process::sys_access(const char* path, int amode)
{
int flags = 0;
if (amode & F_OK)
flags |= O_SEARCH;
if (amode & R_OK)
flags |= O_RDONLY;
if (amode & W_OK)
flags |= O_WRONLY;
if (amode & X_OK)
flags |= O_EXEC;
static_assert((O_RDONLY | O_WRONLY) == O_RDWR);
LockGuard _(m_process_lock);
TRY(validate_string_access(path));
auto credentials = m_credentials;
credentials.set_euid(credentials.ruid());
credentials.set_egid(credentials.rgid());
auto absolute_path = TRY(absolute_path_of(path));
TRY(VirtualFileSystem::get().file_from_absolute_path(credentials, absolute_path, flags));
return 0;
}
BAN::ErrorOr<long> Process::sys_create(const char* path, mode_t mode)
{
LockGuard _(m_process_lock);

View File

@ -83,6 +83,7 @@ __BEGIN_DECLS
O(SYS_SETSOCKOPT, setsockopt) \
O(SYS_REALPATH, realpath) \
O(SYS_TTYNAME, ttyname) \
O(SYS_ACCESS, access) \
enum Syscall
{

View File

@ -496,3 +496,8 @@ char* ttyname(int fildes)
return nullptr;
return storage;
}
int access(const char* path, int amode)
{
return syscall(SYS_ACCESS, path, amode);
}