kernel+libc: Start interfacing with the VFS from userspace (open & close)
This commit adds open and close syscalls to the kernel, and adds matching wrappers to libc. No read/write support, so file descriptors are kind of useless for now.
This commit is contained in:
parent
831973c39a
commit
810c4bc257
15
apps/app.c
15
apps/app.c
@ -1,6 +1,8 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void bye()
|
||||
{
|
||||
@ -12,13 +14,14 @@ int main()
|
||||
atexit(bye);
|
||||
printf("Welcome to %s from userspace!\n", "Luna");
|
||||
|
||||
char* address = (char*)malloc(1);
|
||||
printf("address: %p\n", address);
|
||||
printf("memory at address: %c\n", *address);
|
||||
*address = 'e';
|
||||
printf("memory at address: %c\n", *address);
|
||||
int fd = open("/etc/motd", 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(address);
|
||||
close(fd);
|
||||
|
||||
time_t now = time(NULL);
|
||||
printf("date: %s", ctime(&now));
|
||||
|
1
initrd/etc/motd
Normal file
1
initrd/etc/motd
Normal file
@ -0,0 +1 @@
|
||||
G'day mate!
|
@ -26,6 +26,7 @@ set(SOURCES
|
||||
src/sys/clock_gettime.cpp
|
||||
src/sys/allocate_memory.cpp
|
||||
src/sys/usleep.cpp
|
||||
src/sys/open.cpp
|
||||
src/fs/VFS.cpp
|
||||
src/fs/tmpfs/FileSystem.cpp
|
||||
src/InitRD.cpp
|
||||
|
45
kernel/src/sys/open.cpp
Normal file
45
kernel/src/sys/open.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "Log.h"
|
||||
#include "fs/VFS.h"
|
||||
#include "memory/MemoryManager.h"
|
||||
#include "sys/Syscall.h"
|
||||
#include "thread/Scheduler.h"
|
||||
|
||||
Result<u64> sys_open(Registers*, SyscallArgs args)
|
||||
{
|
||||
u64 path_address = args[0];
|
||||
if (!MemoryManager::validate_userspace_string(path_address)) return err(EFAULT);
|
||||
|
||||
const char* path = (const char*)path_address;
|
||||
int flags = (int)args[1];
|
||||
|
||||
Thread* current = Scheduler::current();
|
||||
|
||||
kinfoln("open: trying to open file %s, flags %d", path, flags);
|
||||
|
||||
auto inode = TRY(VFS::resolve_path(path));
|
||||
|
||||
int fd = TRY(current->allocate_fd(0));
|
||||
|
||||
current->fd_table->fds[fd] = FileDescriptor { inode, 0, flags };
|
||||
|
||||
kinfoln("open: allocated file descriptor %d for inode %zu", fd, inode->inode_number());
|
||||
|
||||
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();
|
||||
|
||||
if (!current->fd_table->fds[fd].has_value()) return err(EBADF);
|
||||
|
||||
kinfoln("close: closing file descriptor %d (was referencing inode %zu)", fd,
|
||||
current->fd_table->fds[fd]->inode->inode_number());
|
||||
|
||||
current->fd_table->fds[fd] = {};
|
||||
|
||||
return 0;
|
||||
}
|
@ -151,6 +151,8 @@ namespace Scheduler
|
||||
guard.deactivate();
|
||||
directory_guard.deactivate();
|
||||
|
||||
thread->fd_table = FileDescriptorTable {};
|
||||
|
||||
kinfoln("Created userspace thread: id %lu with ip %#.16lx and sp %#.16lx (ksp %#lx)", thread->id, thread->ip(),
|
||||
thread->sp(), thread->kernel_stack.top());
|
||||
|
||||
|
@ -19,3 +19,16 @@ Result<Thread*> new_thread()
|
||||
|
||||
return thread;
|
||||
}
|
||||
|
||||
Result<int> Thread::allocate_fd(int min)
|
||||
{
|
||||
if (min < 0 || min >= FD_MAX) return err(EINVAL);
|
||||
for (int i = min; i < FD_MAX; i++)
|
||||
{
|
||||
// FIXME: Possible race condition if multiple threads share a FileDescriptorTable? Let's not worry about it for
|
||||
// now, we're still a long way away from reaching that point.
|
||||
if (!fd_table->fds[i].has_value()) { return i; }
|
||||
}
|
||||
|
||||
return err(EMFILE);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "arch/MMU.h"
|
||||
#include "fs/VFS.h"
|
||||
#include "memory/UserVM.h"
|
||||
#include <luna/LinkedList.h>
|
||||
#include <luna/OwnedPtr.h>
|
||||
@ -21,6 +22,20 @@ enum class ThreadState
|
||||
Dying
|
||||
};
|
||||
|
||||
struct FileDescriptor
|
||||
{
|
||||
SharedPtr<VFS::Inode> inode;
|
||||
usize offset { 0 };
|
||||
int flags { 0 };
|
||||
};
|
||||
|
||||
static constexpr int FD_MAX = 64;
|
||||
|
||||
struct FileDescriptorTable
|
||||
{
|
||||
Option<FileDescriptor> fds[FD_MAX] = {};
|
||||
};
|
||||
|
||||
struct Thread : public LinkedListNode<Thread>
|
||||
{
|
||||
Registers regs;
|
||||
@ -38,6 +53,9 @@ struct Thread : public LinkedListNode<Thread>
|
||||
Stack kernel_stack;
|
||||
|
||||
OwnedPtr<UserVM> vm_allocator;
|
||||
Option<FileDescriptorTable> fd_table;
|
||||
|
||||
Result<int> allocate_fd(int min);
|
||||
|
||||
FPData fp_data;
|
||||
|
||||
|
@ -7,6 +7,7 @@ set(SOURCES
|
||||
src/unistd.cpp
|
||||
src/errno.cpp
|
||||
src/string.cpp
|
||||
src/fcntl.cpp
|
||||
src/assert.cpp
|
||||
src/atexit.cpp
|
||||
src/ctype.cpp
|
||||
|
18
libc/include/fcntl.h
Normal file
18
libc/include/fcntl.h
Normal file
@ -0,0 +1,18 @@
|
||||
/* fcntl.h: File control. */
|
||||
|
||||
#ifndef _FCNTL_H
|
||||
#define _FCNTL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Open a file path and return a file descriptor to it. */
|
||||
int open(const char* path, int flags);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -30,6 +30,9 @@ extern "C"
|
||||
/* Sleeps for X seconds. */
|
||||
unsigned long sleep(unsigned long seconds);
|
||||
|
||||
/* Closes a file descriptor. */
|
||||
int close(int fd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
13
libc/src/fcntl.cpp
Normal file
13
libc/src/fcntl.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <bits/errno-return.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int open(const char* path, int flags)
|
||||
{
|
||||
long rc = syscall(SYS_open, path, flags);
|
||||
__errno_return(rc, int);
|
||||
}
|
||||
}
|
@ -37,4 +37,10 @@ extern "C"
|
||||
syscall(SYS_usleep, seconds * 1000000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
{
|
||||
long rc = syscall(SYS_close, fd);
|
||||
__errno_return(rc, int);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define enumerate_syscalls(_e) \
|
||||
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep)
|
||||
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) _e(close)
|
||||
|
||||
enum Syscalls
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user