Kernel/LibC: implement chmod syscall + libc wrapper

This commit is contained in:
Bananymous 2023-10-25 02:35:37 +03:00
parent e8890062d6
commit 15bb1804ef
11 changed files with 59 additions and 2 deletions

View File

@ -37,6 +37,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
private:
uint32_t fs_block_of_data_block_index(uint32_t data_block_index);

View File

@ -99,6 +99,7 @@ namespace Kernel
BAN::ErrorOr<size_t> read(off_t, BAN::ByteSpan buffer);
BAN::ErrorOr<size_t> write(off_t, BAN::ConstByteSpan buffer);
BAN::ErrorOr<void> truncate(size_t);
BAN::ErrorOr<void> chmod(mode_t);
bool has_data() const;
protected:
@ -115,6 +116,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> chmod_impl(mode_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual bool has_data_impl() const { dwarnln("nonblock not supported"); return true; }
private:

View File

@ -56,6 +56,8 @@ namespace Kernel
ASSERT((inode_info.mode & Inode::Mode::TYPE_MASK) == 0);
}
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
protected:
RamFileSystem& m_fs;
FullInodeInfo m_inode_info;

View File

@ -97,6 +97,8 @@ namespace Kernel
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_chmod(const char*, mode_t);
BAN::ErrorOr<long> sys_pipe(int fildes[2]);
BAN::ErrorOr<long> sys_dup(int fildes);
BAN::ErrorOr<long> sys_dup2(int fildes, int fildes2);

View File

@ -242,6 +242,16 @@ namespace Kernel
return {};
}
BAN::ErrorOr<void> Ext2Inode::chmod_impl(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
if (m_inode.mode == mode)
return {};
m_inode.mode = (m_inode.mode & Inode::Mode::TYPE_MASK) | mode;
TRY(sync());
return {};
}
BAN::ErrorOr<void> Ext2Inode::list_next_inodes_impl(off_t offset, DirectoryEntryList* list, size_t list_size)
{
ASSERT(mode().ifdir());

View File

@ -129,6 +129,14 @@ namespace Kernel
return truncate_impl(size);
}
BAN::ErrorOr<void> Inode::chmod(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
LockGuard _(m_lock);
Thread::TerminateBlocker blocker(Thread::current());
return chmod_impl(mode);
}
bool Inode::has_data() const
{
LockGuard _(m_lock);

View File

@ -26,6 +26,14 @@ namespace Kernel
this->rdev = 0;
}
BAN::ErrorOr<void> RamInode::chmod_impl(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
m_inode_info.mode = (m_inode_info.mode & Inode::Mode::TYPE_MASK) | mode;
return {};
}
/*
RAM FILE INODE
@ -193,9 +201,9 @@ namespace Kernel
{
BAN::RefPtr<RamInode> inode;
if (Mode(mode).ifreg())
inode = TRY(RamFileInode::create(m_fs, mode, uid, gid));
inode = TRY(RamFileInode::create(m_fs, mode & ~Inode::Mode::TYPE_MASK, uid, gid));
else if (Mode(mode).ifdir())
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode, uid, gid));
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode & ~Inode::Mode::TYPE_MASK, uid, gid));
else
ASSERT_NOT_REACHED();

View File

@ -733,6 +733,21 @@ namespace Kernel
return TRY(m_open_file_descriptors.write(fd, BAN::ByteSpan((uint8_t*)buffer, count)));
}
BAN::ErrorOr<long> Process::sys_chmod(const char* path, mode_t mode)
{
if (mode & S_IFMASK)
return BAN::Error::from_errno(EINVAL);
LockGuard _(m_lock);
validate_string_access(path);
auto absolute_path = TRY(absolute_path_of(path));
auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, absolute_path, O_WRONLY));
TRY(file.inode->chmod(mode));
return 0;
}
BAN::ErrorOr<long> Process::sys_pipe(int fildes[2])
{
LockGuard _(m_lock);

View File

@ -199,6 +199,9 @@ namespace Kernel
case SYS_POWEROFF:
ret = Process::current().sys_poweroff((int)arg1);
break;
case SYS_CHMOD:
ret = Process::current().sys_chmod((const char*)arg1, (mode_t)arg2);
break;
default:
dwarnln("Unknown syscall {}", syscall);
break;

View File

@ -55,6 +55,7 @@ __BEGIN_DECLS
#define SYS_MUNMAP 52
#define SYS_TTY_CTRL 53
#define SYS_POWEROFF 54
#define SYS_CHMOD 55
__END_DECLS

View File

@ -4,6 +4,11 @@
#include <sys/syscall.h>
#include <unistd.h>
int chmod(const char* path, mode_t mode)
{
return syscall(SYS_CHMOD, path, mode);
}
int fstat(int fildes, struct stat* buf)
{
return syscall(SYS_FSTAT, fildes, buf);