Luna/kernel/src/sys/open.cpp

103 lines
3.0 KiB
C++
Raw Normal View History

#include "Log.h"
#include "fs/VFS.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
2023-03-12 15:55:46 +00:00
#include <bits/modes.h>
#include <bits/open-flags.h>
// These flags are needed after open(), the rest only affect open().
2023-03-24 20:19:24 +00:00
constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK | O_CLOEXEC;
Result<u64> sys_openat(Registers*, SyscallArgs args)
{
int dirfd = (int)args[0];
auto path = TRY(MemoryManager::strdup_from_user(args[1]));
int flags = (int)args[2];
mode_t mode = (mode_t)args[3];
Thread* current = Scheduler::current();
SharedPtr<VFS::Inode> inode;
// Caller did not pass either O_RDONLY, O_WRONLY or O_RDWR
if ((flags & O_RDWR) == 0) { return err(EINVAL); }
if (flags & O_TMPFILE)
{
if (!(flags & O_WRONLY)) return err(EINVAL);
if (flags & O_CREAT) return err(EINVAL);
}
int error;
SharedPtr<VFS::Inode> parent_inode;
bool ok = current->resolve_atfile(dirfd, path, false, !(flags & O_NOFOLLOW), &parent_inode)
.try_set_value_or_error(inode, error);
if (!ok)
{
if (error == ENOENT && (flags & O_CREAT) && !path.is_empty())
2023-03-12 15:55:46 +00:00
{
inode = TRY(VFS::create_file(path.chars(), current->auth, parent_inode));
2023-05-26 20:27:49 +00:00
inode->chmod(mode & ~current->umask);
inode->chown(current->auth.euid, current->auth.egid);
2023-03-12 15:55:46 +00:00
}
else
return err(error);
}
else if (flags & O_EXCL)
return err(EEXIST);
else
2023-03-12 15:55:46 +00:00
{
if ((flags & O_RDONLY) && !VFS::can_read(inode, current->auth)) return err(EACCES);
if ((flags & O_WRONLY) && !VFS::can_write(inode, current->auth)) return err(EACCES);
2023-03-12 15:55:46 +00:00
}
// This should only be possible if O_NOFOLLOW was in flags.
if (inode->type() == VFS::InodeType::Symlink) return err(ELOOP);
if (flags & O_TMPFILE)
{
if (inode->type() != VFS::InodeType::Directory) return err(EINVAL);
inode = TRY(inode->fs()->create_file_inode());
inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid);
}
if (inode->type() != VFS::InodeType::Directory && (flags & O_DIRECTORY)) return err(ENOTDIR);
if (inode->type() == VFS::InodeType::Directory)
{
if ((flags & O_WRONLY) || (flags & O_CREAT)) return err(EISDIR);
}
if ((flags & O_WRONLY) && (flags & O_TRUNC)) inode->truncate(0);
int fd = TRY(current->allocate_fd(0));
kinfoln("openat: opening file %s from dirfd %d, flags %d, mode %#o = fd %d", path.chars(), dirfd, flags, mode, fd);
inode->add_handle();
current->fd_table[fd] = FileDescriptor { inode, 0, flags & FLAGS_TO_KEEP };
return (u64)fd;
}
Result<u64> sys_close(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table[fd];
2023-03-11 17:02:50 +00:00
if (!descriptor.has_value()) return err(EBADF);
descriptor->inode->remove_handle();
2023-03-11 17:02:50 +00:00
descriptor = {};
return 0;
}