Support writing sysctls from userspace

This commit is contained in:
Ali Mashtizadeh 2015-12-01 15:14:17 -08:00
parent a274a78a5b
commit c1461694ca
5 changed files with 88 additions and 10 deletions

View File

@ -152,19 +152,19 @@ OSSysCtl(const char *node, void *oldvar, void *newvar)
}
int
OS_FSMount(const char *mntpt, const char *device, uint64_t flags)
OSFSMount(const char *mntpt, const char *device, uint64_t flags)
{
return syscall(SYSCALL_FSMOUNT, mntpt, device, flags);
}
int
OS_FSUnmount(const char *mntpt)
OSFSUnmount(const char *mntpt)
{
return syscall(SYSCALL_FSUNMOUNT, mntpt);
}
int
OS_FSInfo(struct statfs *info, uint64_t max)
OSFSInfo(struct statfs *info, uint64_t max)
{
return syscall(SYSCALL_FSINFO, info, max);
}

View File

@ -2,6 +2,9 @@
#ifndef __SYS_LOADER_H__
#define __SYS_LOADER_H__
#include <sys/elf64.h>
bool Loader_CheckHeader(const Elf64_Ehdr *ehdr);
bool Loader_Load(Thread *thr, VNode *vn, void *buf, uint64_t len);
#endif /* __SYS_LOADER_H__ */

View File

@ -65,6 +65,7 @@ SYSCTL_LIST
uint64_t SysCtl_GetType(const char *node);
void *SysCtl_GetObject(const char *node);
uint64_t SysCtl_SetObject(const char *node, void *obj);
#endif /* __SYS_SYSCTL_H__ */

View File

@ -73,33 +73,37 @@ Syscall_Spawn(uint64_t user_path)
status = CopyStrIn(user_path, &path, sizeof(path));
if (status != 0)
return status;
return SYSCALL_PACK(status, 0);
Log(syscall, "Spawn(%s)\n", path);
pg = PAlloc_AllocPage();
if (!pg) {
return ENOMEM;
return SYSCALL_PACK(ENOMEM, 0);
}
file = VFS_Lookup(path);
if (!file) {
// Free
return ENOENT;
return SYSCALL_PACK(ENOENT, 0);
}
status = VFS_Open(file);
if (status < 0) {
Log(syscall, "Error VFS_Open\n");
// Release & free
return status;
return SYSCALL_PACK(status, 0);
}
status = VFS_Read(file, pg, 0, 1024);
if (status < 0) {
Log(syscall, "Error VFS_Read\n");
// Release & free
return status;
return SYSCALL_PACK(status, 0);
}
if (!Loader_CheckHeader(pg)) {
return SYSCALL_PACK(EINVAL, 0);
}
cur = Sched_Current();
@ -121,7 +125,7 @@ Syscall_Spawn(uint64_t user_path)
Sched_SetRunnable(thr);
return proc->pid;
return SYSCALL_PACK(0, proc->pid);
}
uint64_t
@ -573,7 +577,44 @@ Syscall_SysCtl(uint64_t user_node, uint64_t user_oldval, uint64_t user_newval)
return status;
}
// XXX: Support setting
if (user_newval == 0) {
return 0;
}
switch (scType) {
case SYSCTL_TYPE_STR: {
SysCtlString scStr;
status = CopyIn(user_newval, &scStr, sizeof(scStr));
if (status != 0) {
return status;
}
status = SysCtl_SetObject(node, (void *)&scStr);
break;
}
case SYSCTL_TYPE_INT: {
SysCtlInt scInt;
status = CopyIn(user_newval, &scInt, sizeof(scInt));
if (status != 0) {
return status;
}
status = SysCtl_SetObject(node, (void *)&scInt);
break;
}
case SYSCTL_TYPE_BOOL: {
SysCtlBool scBool;
status = CopyIn(user_newval, &scBool, sizeof(scBool));
if (status != 0) {
return status;
}
status = SysCtl_SetObject(node, (void *)&scBool);
break;
}
default: {
status = SYSCALL_PACK(ENOENT, 0);
}
}
return status;
}
uint64_t

View File

@ -75,6 +75,39 @@ SysCtl_GetObject(const char *node)
return SYSCTLTable[i].node;
}
uint64_t
SysCtl_SetObject(const char *node, void *obj)
{
int i = SysCtl_Lookup(node);
if (i == -1) {
return -1;
}
// Validate inputs
if (SYSCTLTable[i].flags == SYSCTL_FLAG_RO) {
kprintf("Sysctl node is read-only!\n");
return -1;
}
switch (SYSCTLTable[i].type) {
case SYSCTL_TYPE_STR: {
SysCtlString *val = (SysCtlString *)SYSCTLTable[i].node;
memcpy(val, obj, sizeof(*val));
}
case SYSCTL_TYPE_INT: {
SysCtlInt *val = (SysCtlInt *)SYSCTLTable[i].node;
memcpy(val, obj, sizeof(*val));
}
case SYSCTL_TYPE_BOOL: {
SysCtlBool *val = (SysCtlBool *)SYSCTLTable[i].node;
memcpy(val, obj, sizeof(*val));
}
}
return 0;
}
void
Debug_SysCtl(int argc, const char *argv[])
{