341 lines
9.3 KiB
C++
341 lines
9.3 KiB
C++
#include "Log.h"
|
|
#include "Pledge.h"
|
|
#include "fs/Pipe.h"
|
|
#include "fs/VFS.h"
|
|
#include "memory/MemoryManager.h"
|
|
#include "sys/Syscall.h"
|
|
#include "thread/Scheduler.h"
|
|
#include <bits/atfile.h>
|
|
#include <bits/fcntl.h>
|
|
#include <bits/open-flags.h>
|
|
#include <bits/seek.h>
|
|
#include <bits/utime.h>
|
|
#include <luna/SafeArithmetic.h>
|
|
#include <sys/types.h>
|
|
|
|
Result<u64> sys_read(Registers* regs, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
u8* buf = (u8*)args[1];
|
|
usize size = (usize)args[2];
|
|
|
|
if (!size) return 0;
|
|
|
|
if (!MemoryManager::validate_user_write(buf, size)) return err(EFAULT);
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
|
|
|
if (!descriptor.is_readable()) return err(EBADF);
|
|
|
|
while (descriptor.inode()->will_block_if_read())
|
|
{
|
|
if (descriptor.should_block()) kernel_sleep(10);
|
|
else
|
|
return err(EAGAIN);
|
|
|
|
if (current->interrupted)
|
|
{
|
|
kdbgln("signal: read interrupted by signal");
|
|
if (current->will_ignore_pending_signal())
|
|
{
|
|
current->process_pending_signals(regs);
|
|
continue;
|
|
}
|
|
return err(EINTR);
|
|
}
|
|
}
|
|
|
|
usize nread = TRY(descriptor.inode()->read(buf, descriptor.offset, size));
|
|
|
|
if (VFS::is_seekable(descriptor.inode())) descriptor.offset += nread;
|
|
|
|
return nread;
|
|
}
|
|
|
|
Result<u64> sys_write(Registers*, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
const u8* buf = (const u8*)args[1];
|
|
usize size = (usize)args[2];
|
|
|
|
if (!size) return 0;
|
|
|
|
if (!MemoryManager::validate_user_read(buf, size)) return err(EFAULT);
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
|
|
|
if (!descriptor.is_writable()) return err(EBADF);
|
|
|
|
if (descriptor.should_append() && VFS::is_seekable(descriptor.inode()))
|
|
descriptor.offset = descriptor.inode()->metadata().size;
|
|
|
|
usize nwritten = TRY(descriptor.inode()->write(buf, descriptor.offset, size));
|
|
|
|
if (VFS::is_seekable(descriptor.inode())) descriptor.offset += nwritten;
|
|
|
|
return nwritten;
|
|
}
|
|
|
|
Result<u64> sys_lseek(Registers*, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
off_t offset = (long)args[1];
|
|
int whence = (int)args[2];
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
|
|
|
if (descriptor.inode()->type() == VFS::InodeType::FIFO) return err(ESPIPE);
|
|
|
|
if (!VFS::is_seekable(descriptor.inode())) return descriptor.offset;
|
|
|
|
off_t new_offset;
|
|
|
|
switch (whence)
|
|
{
|
|
case SEEK_SET: new_offset = offset; break;
|
|
case SEEK_CUR: new_offset = TRY(safe_add((long)descriptor.offset, offset)); break;
|
|
case SEEK_END: new_offset = TRY(safe_add((long)descriptor.inode()->metadata().size, offset)); break;
|
|
default: return err(EINVAL);
|
|
}
|
|
|
|
if (new_offset < 0) return err(EINVAL);
|
|
|
|
descriptor.offset = (usize)new_offset;
|
|
|
|
return (u64)new_offset;
|
|
}
|
|
|
|
Result<u64> sys_fcntl(Registers*, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
int cmd = (int)args[1];
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
|
|
|
bool is_cloexec = true;
|
|
|
|
switch (cmd)
|
|
{
|
|
case F_DUPFD: is_cloexec = false; [[fallthrough]];
|
|
case F_DUPFD_CLOEXEC: {
|
|
int arg = (int)args[2];
|
|
int new_fd = TRY(current->allocate_fd(arg));
|
|
|
|
current->fd_table[new_fd] = descriptor;
|
|
|
|
if (is_cloexec) current->fd_table[new_fd]->flags |= O_CLOEXEC;
|
|
else
|
|
current->fd_table[new_fd]->flags &= ~O_CLOEXEC;
|
|
|
|
return (u64)new_fd;
|
|
}
|
|
case F_GETFD: return (u64) !!(descriptor.flags & O_CLOEXEC);
|
|
case F_SETFD: {
|
|
int arg = (int)args[2];
|
|
if (arg == FD_CLOEXEC) descriptor.flags |= O_CLOEXEC;
|
|
else
|
|
descriptor.flags &= ~O_CLOEXEC;
|
|
return 0;
|
|
}
|
|
case F_GETFL: return (u64)descriptor.status_flags();
|
|
case F_SETFL: {
|
|
int arg = (int)args[2];
|
|
|
|
descriptor.status_flags() &= ~(O_APPEND | O_NONBLOCK);
|
|
arg &= (O_APPEND | O_NONBLOCK);
|
|
|
|
descriptor.status_flags() |= arg;
|
|
|
|
return 0;
|
|
}
|
|
default: return err(EINVAL);
|
|
}
|
|
}
|
|
|
|
Result<u64> sys_ioctl(Registers*, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
int request = (int)args[1];
|
|
void* arg = (void*)args[2];
|
|
|
|
Thread* current = Scheduler::current();
|
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
|
|
|
return descriptor.inode()->ioctl(request, arg);
|
|
}
|
|
|
|
Result<u64> sys_isatty(Registers*, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
|
|
Thread* current = Scheduler::current();
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
|
|
|
return descriptor.inode()->isatty();
|
|
}
|
|
|
|
Result<u64> sys_dup2(Registers*, SyscallArgs args)
|
|
{
|
|
int oldfd = (int)args[0];
|
|
int newfd = (int)args[1];
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
if (newfd < 0 || newfd >= FD_MAX) return err(EBADF);
|
|
|
|
auto descriptor = *TRY(current->resolve_fd(oldfd));
|
|
|
|
if (newfd == oldfd) return (u64)newfd;
|
|
|
|
current->fd_table[newfd] = descriptor;
|
|
current->fd_table[newfd]->flags &= ~O_CLOEXEC;
|
|
|
|
return (u64)newfd;
|
|
}
|
|
|
|
Result<u64> sys_pipe(Registers*, SyscallArgs args)
|
|
{
|
|
int* pfds = (int*)args[0];
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
int rfd = TRY(current->allocate_fd(0));
|
|
int wfd = TRY(current->allocate_fd(rfd + 1));
|
|
|
|
if (!MemoryManager::copy_to_user_typed(pfds, &rfd)) return err(EFAULT);
|
|
if (!MemoryManager::copy_to_user_typed(pfds + 1, &wfd)) return err(EFAULT);
|
|
|
|
SharedPtr<VFS::Inode> rpipe;
|
|
SharedPtr<VFS::Inode> wpipe;
|
|
|
|
TRY(Pipe::create(rpipe, wpipe));
|
|
|
|
current->fd_table[rfd] = FileDescriptor { TRY(make_shared<OpenFileDescription>(rpipe, O_RDONLY)), 0 };
|
|
current->fd_table[wfd] = FileDescriptor { TRY(make_shared<OpenFileDescription>(wpipe, O_WRONLY)), 0 };
|
|
|
|
return 0;
|
|
}
|
|
|
|
Result<u64> sys_umask(Registers*, SyscallArgs args)
|
|
{
|
|
mode_t new_umask = (mode_t)args[0];
|
|
|
|
auto* current = Scheduler::current();
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
mode_t old_umask = current->umask;
|
|
|
|
current->umask = new_umask & 0777;
|
|
|
|
return old_umask;
|
|
}
|
|
|
|
Result<u64> sys_truncate(Registers*, SyscallArgs args)
|
|
{
|
|
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
|
|
size_t length = (size_t)args[1];
|
|
|
|
auto* current = Scheduler::current();
|
|
TRY(check_pledge(current, Promise::p_wpath));
|
|
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
|
|
|
|
if (!VFS::can_write(inode, current->auth)) return err(EACCES);
|
|
|
|
TRY(inode->truncate(length));
|
|
|
|
return 0;
|
|
}
|
|
|
|
Result<u64> sys_ftruncate(Registers*, SyscallArgs args)
|
|
{
|
|
int fd = (int)args[0];
|
|
size_t length = (size_t)args[1];
|
|
|
|
auto* current = Scheduler::current();
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
auto description = TRY(current->resolve_fd(fd))->description;
|
|
if (!(description->flags & O_WRONLY)) return err(EBADF);
|
|
|
|
TRY(description->inode->truncate(length));
|
|
|
|
return 0;
|
|
}
|
|
|
|
Result<u64> sys_utimensat(Registers*, SyscallArgs args)
|
|
{
|
|
int dirfd = (int)args[0];
|
|
auto path = TRY(MemoryManager::strdup_from_user(args[1]));
|
|
const auto* times = (const struct timespec*)args[2];
|
|
int flags = (int)args[3];
|
|
|
|
auto* current = Scheduler::current();
|
|
TRY(check_pledge(current, Promise::p_fattr));
|
|
auto inode = TRY(current->resolve_atfile(dirfd, path, flags & AT_EMPTY_PATH, !(flags & AT_SYMLINK_NOFOLLOW)));
|
|
|
|
struct timespec ktimes[2];
|
|
ktimes[0].tv_sec = ktimes[1].tv_sec = 0;
|
|
ktimes[0].tv_nsec = ktimes[1].tv_nsec = UTIME_NOW;
|
|
|
|
if (times && !MemoryManager::copy_from_user(times, ktimes, sizeof(ktimes))) return err(EFAULT);
|
|
|
|
// No permission checks are performed, since no actual modification is done, but the above checks are still
|
|
// performed.
|
|
if (ktimes[0].tv_nsec == UTIME_OMIT && ktimes[1].tv_nsec == UTIME_OMIT) return 0;
|
|
|
|
bool allow_write_access = ktimes[0].tv_nsec == UTIME_NOW && ktimes[1].tv_nsec == UTIME_NOW;
|
|
|
|
if (allow_write_access)
|
|
{
|
|
if (!VFS::can_write(inode, current->auth) && current->auth.euid != inode->metadata().uid &&
|
|
current->auth.euid != 0)
|
|
return err(EACCES);
|
|
}
|
|
else if (current->auth.euid != inode->metadata().uid && current->auth.euid != 0)
|
|
return err(EPERM);
|
|
|
|
auto metadata = inode->metadata();
|
|
if (ktimes[0].tv_nsec != UTIME_OMIT)
|
|
{
|
|
if (ktimes[0].tv_nsec == UTIME_NOW) metadata.atime = *Timer::realtime_clock();
|
|
else
|
|
{
|
|
if (ktimes[0].tv_nsec < 0 || ktimes[0].tv_nsec > 999'999'999) return err(EINVAL);
|
|
metadata.atime = ktimes[0];
|
|
}
|
|
}
|
|
if (ktimes[1].tv_nsec != UTIME_OMIT)
|
|
{
|
|
if (ktimes[1].tv_nsec == UTIME_NOW) metadata.mtime = *Timer::realtime_clock();
|
|
else
|
|
{
|
|
if (ktimes[1].tv_nsec < 0 || ktimes[1].tv_nsec > 999'999'999) return err(EINVAL);
|
|
metadata.mtime = ktimes[1];
|
|
}
|
|
}
|
|
TRY(inode->set_metadata(metadata));
|
|
|
|
return 0;
|
|
}
|