vfs: add open and close syscalls
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
09116d65ea
commit
debd580784
2 changed files with 78 additions and 2 deletions
|
|
@ -1,7 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/proc.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/types.h>
|
||||
#include <kernel/syscall.h>
|
||||
#include <lib/errno.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
static ssize_t sys_pread(int fd, void *buf, size_t nbyte, off_t offset)
|
||||
{
|
||||
|
|
@ -25,12 +28,72 @@ static ssize_t sys_write(int fd, const void *buf, size_t nbyte)
|
|||
|
||||
static int sys_open(const char *path, int flags)
|
||||
{
|
||||
return -ENOSYS;
|
||||
struct proc *proc = get_cur_proc();
|
||||
|
||||
dirent_t *f;
|
||||
int res = vfs_open(path, &f);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
spinlock_acquire(&proc->fd_lock);
|
||||
|
||||
int fdno = -1;
|
||||
|
||||
for (int i = 0; i < proc->fd_array_size; i++) {
|
||||
struct file *fd = proc->fdt + i;
|
||||
if (!fd->dirent) {
|
||||
memset(fd, 0, sizeof(struct file));
|
||||
fd->dirent = f;
|
||||
fd->flags = flags;
|
||||
fd->refcount = 1;
|
||||
fdno = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (fdno == -1) {
|
||||
size_t osize = proc->fd_array_size;
|
||||
size_t newsize = MIN(osize * 2, 32);
|
||||
void *new = krealloc(proc->fdt, newsize, ALLOC_KERN);
|
||||
if (!new) {
|
||||
vfs_close(f);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
proc->fdt = new;
|
||||
|
||||
memset(proc->fdt + osize, 0, osize);
|
||||
struct file *fd = proc->fdt + osize;
|
||||
fd->dirent = f;
|
||||
fd->flags = flags;
|
||||
fd->refcount = 1;
|
||||
fdno = osize;
|
||||
}
|
||||
|
||||
spinlock_release(&proc->fd_lock);
|
||||
|
||||
return fdno;
|
||||
}
|
||||
|
||||
static int sys_close(int fd)
|
||||
{
|
||||
return -ENOSYS;
|
||||
struct proc *proc = get_cur_proc();
|
||||
|
||||
if (fd >= proc->fd_array_size)
|
||||
return -EBADF;
|
||||
|
||||
struct file *f = proc->fdt + fd;
|
||||
if (!f->dirent)
|
||||
return -EBADF;
|
||||
|
||||
spinlock_acquire(&proc->fd_lock);
|
||||
|
||||
vfs_close(f->dirent);
|
||||
memset(f, 0, sizeof(struct file));
|
||||
|
||||
spinlock_acquire(&proc->fd_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vfs_uapi_init()
|
||||
|
|
|
|||
Loading…
Reference in a new issue