From fab307311f6e1e87f1c3580b03c5a3e36d1b2645 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 29 Oct 2024 18:34:24 -0400 Subject: kernel: syscall: Add SYS_stat Signed-off-by: Ian Moffett --- sys/kern/kern_syscall.c | 1 + sys/kern/vfs_syscalls.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) (limited to 'sys/kern') diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c index b36c6af..f4d9cb8 100644 --- a/sys/kern/kern_syscall.c +++ b/sys/kern/kern_syscall.c @@ -38,6 +38,7 @@ scret_t(*g_sctab[])(struct syscall_args *) = { sys_open, /* SYS_open */ sys_read, /* SYS_read */ sys_close, /* SYS_close */ + sys_stat, /* SYS_stat */ }; const size_t MAX_SYSCALLS = NELEM(g_sctab); diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index f4c5c59..0ef3d42 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -28,10 +28,61 @@ */ #include +#include +#include +#include +#include +#include +#include +#include #include #include #include +static int +vfs_dostat(const char *path, struct stat *sbuf) +{ + char pathbuf[PATH_MAX]; + struct vattr *attr; + struct stat st; + struct vnode *vp; + struct vop_getattr_args gattr; + struct nameidata nd; + int error; + + if (sbuf == NULL || path == NULL) { + return -EINVAL; + } + + if ((copyinstr(path, pathbuf, sizeof(path))) < 0) { + return -EFAULT; + } + + nd.path = path; + nd.flags = 0; + + if ((error = namei(&nd)) != 0) { + return error; + } + + vp = nd.vp; + gattr.vp = vp; + error = vfs_vop_getattr(vp, &gattr); + + if (error != 0) { + return error; + } + + attr = gattr.res; + memset(&st, VNOVAL, sizeof(st)); + + /* Copy stat data to userspace statbuf */ + st.st_mode = attr->mode; + st.st_size = attr->size; + copyout(&st, sbuf, sizeof(*sbuf)); + return 0; +} + /* * arg0: pathname * arg1: oflags @@ -64,3 +115,13 @@ sys_read(struct syscall_args *scargs) return fd_read(scargs->arg0, (void *)scargs->arg1, scargs->arg2); } + +/* + * arg0: path + * arg1: buf + */ +scret_t +sys_stat(struct syscall_args *scargs) +{ + return vfs_dostat((const char *)scargs->arg0, (void *)scargs->arg1); +} -- cgit v1.2.3