From afd9cdc068d117b1e68dfd15534e0034ab47efb6 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Sun, 15 Nov 2015 15:17:18 -0800 Subject: [PATCH] Adding sysctl command line tool --- SConstruct | 3 ++ include/syscall.h | 7 ++++ lib/libc/syscall.c | 24 ++++++++++++ release/bootdisk.manifest | 1 + sbin/sysctl/SConscript | 23 ++++++++++++ sbin/sysctl/sysctl.c | 77 +++++++++++++++++++++++++++++++++++++++ sys/include/mount.h | 13 +++++++ sys/include/syscall.h | 6 +++ sys/include/sysctl.h | 8 ++++ sys/kern/syscall.c | 71 ++++++++++++++++++++++++++++++++++++ sys/kern/sysctl.c | 26 +++++++++++-- 11 files changed, 255 insertions(+), 4 deletions(-) create mode 100644 sbin/sysctl/SConscript create mode 100644 sbin/sysctl/sysctl.c create mode 100644 sys/include/mount.h diff --git a/SConstruct b/SConstruct index e5bd95d..fff0278 100644 --- a/SConstruct +++ b/SConstruct @@ -64,6 +64,7 @@ if env["WITH_GPROF"] == "1": env.Append(CPPFLAGS = [ "-pg" ]) env.Append(LINKFLAGS = [ "-pg" ]) +env.Append(CPPFLAGS = "-DBUILDTYPE=" + env["BUILDTYPE"]) if env["BUILDTYPE"] == "DEBUG": env.Append(CPPFLAGS = [ "-g", "-DDEBUG", "-Wall", "-Wno-deprecated-declarations" ]) @@ -175,6 +176,7 @@ SConscript('bin/ethinject/SConscript', variant_dir='build/bin/ethinject') SConscript('bin/shell/SConscript', variant_dir='build/bin/shell') SConscript('sbin/ifconfig/SConscript', variant_dir='build/sbin/ifconfig') SConscript('sbin/init/SConscript', variant_dir='build/sbin/init') +SConscript('sbin/sysctl/SConscript', variant_dir='build/sbin/sysctl') SConscript('tests/SConscript', variant_dir='build/tests') # Build Tools @@ -199,6 +201,7 @@ if env["BOOTDISK"] == "1": Depends(bootdisk, "#build/bin/shell/shell") Depends(bootdisk, "#build/sbin/ifconfig/ifconfig") Depends(bootdisk, "#build/sbin/init/init") + Depends(bootdisk, "#build/sbin/sysctl/sysctl") Depends(bootdisk, "#build/sys/castor") Depends(bootdisk, "#build/tests/lwiptest") Depends(bootdisk, "#build/tests/pthreadtest") diff --git a/include/syscall.h b/include/syscall.h index 3791a92..5c137d3 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -5,6 +5,7 @@ #include #include #include +#include uint64_t OSTime(); uint64_t OSGetPID(); @@ -40,5 +41,11 @@ int OSNICStat(uint64_t nicNo, NIC *nic); int OSNICSend(uint64_t nicNo, MBuf *mbuf); int OSNICRecv(uint64_t nicNo, MBuf *mbuf); +// System +int OSSysCtl(const char *node, void *oldval, void *newval); +int OSFSMount(const char *mntpt, const char *device, uint64_t flags); +int OSFSUnmount(const char *mntpt); +int OSFSInfo(struct statfs *info, uint64_t max); + #endif /* __SYSCALL_H__ */ diff --git a/lib/libc/syscall.c b/lib/libc/syscall.c index d9c669e..a489e46 100644 --- a/lib/libc/syscall.c +++ b/lib/libc/syscall.c @@ -145,3 +145,27 @@ OSNICRecv(uint64_t nicNo, MBuf *mbuf) return syscall(SYSCALL_NICRECV, nicNo, mbuf); } +int +OSSysCtl(const char *node, void *oldvar, void *newvar) +{ + return syscall(SYSCALL_SYSCTL, node, oldvar, newvar); +} + +int +OS_FSMount(const char *mntpt, const char *device, uint64_t flags) +{ + return syscall(SYSCALL_FSMOUNT, mntpt, device, flags); +} + +int +OS_FSUnmount(const char *mntpt) +{ + return syscall(SYSCALL_FSUNMOUNT, mntpt); +} + +int +OS_FSInfo(struct statfs *info, uint64_t max) +{ + return syscall(SYSCALL_FSINFO, info, max); +} + diff --git a/release/bootdisk.manifest b/release/bootdisk.manifest index 5a8c3c3..9031d01 100644 --- a/release/bootdisk.manifest +++ b/release/bootdisk.manifest @@ -12,6 +12,7 @@ DIR / DIR sbin FILE ifconfig build/sbin/ifconfig/ifconfig FILE init build/sbin/init/init + FILE sysctl build/sbin/sysctl/sysctl END DIR tests FILE lwiptest build/tests/lwiptest diff --git a/sbin/sysctl/SConscript b/sbin/sysctl/SConscript new file mode 100644 index 0000000..066dc64 --- /dev/null +++ b/sbin/sysctl/SConscript @@ -0,0 +1,23 @@ +import sys + +Import('env') + +sysctl_env = env.Clone() + +src = [ ] + +src_common = [ + "sysctl.c" +] + +src.append(env["CRTBEGIN"]) +src.append(src_common) +src.append(env["CRTEND"]) + +sysctl_env.Append(LINKFLAGS = ['-nostdlib']) +sysctl_env.Append(CPPFLAGS = ['-nostdinc']) +sysctl_env.Append(CPPPATH = ['#build/include']) +sysctl_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c']) + +sysctl_env.Program("sysctl", src) + diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c new file mode 100644 index 0000000..2ab4bc7 --- /dev/null +++ b/sbin/sysctl/sysctl.c @@ -0,0 +1,77 @@ + +#include +#include +#include + +#include + +typedef struct SysCtlEntry { + char path[64]; + int type; + int flags; + char description[128]; +} SysCtlEntry; + +#define SYSCTL_STR(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \ + { #_PATH, SYSCTL_TYPE_STR, _FLAGS, _DESCRIPTION }, +#define SYSCTL_INT(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \ + { #_PATH, SYSCTL_TYPE_INT, _FLAGS, _DESCRIPTION }, +#define SYSCTL_BOOL(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \ + { #_PATH, SYSCTL_TYPE_BOOL, _FLAGS, _DESCRIPTION }, +SysCtlEntry SYSCTLTable[] = { + SYSCTL_LIST + { "", 0, 0, "" }, +}; +#undef SYSCTL_STR +#undef SYSCTL_INT +#undef SYSCTL_BOOL + +int +main(int argc, const char *argv[]) +{ + int i; + uint64_t status; + + printf("%d\n", argc); + if (true) { + printf("%-20s %s\n", "Name", "Description"); + for (i = 0; SYSCTLTable[i].type != 0; i++) { + printf("%-20s %s\n", + SYSCTLTable[i].path, + SYSCTLTable[i].description); + } + } + + if (false) { + printf("Usage: sysctl [NODE] [VALUE]\n"); + return 1; + } + + for (i = 0; SYSCTLTable[i].type != 0; i++) { + SysCtlString scStr; + SysCtlInt scInt; + SysCtlBool scBool; + + switch (SYSCTLTable[i].type) { + case SYSCTL_TYPE_STR: + OSSysCtl(SYSCTLTable[i].path, &scStr, NULL); + printf("%s: %s\n", SYSCTLTable[i].path, scStr.value); + break; + case SYSCTL_TYPE_INT: + OSSysCtl(SYSCTLTable[i].path, &scInt, NULL); + printf("%s: %ld\n", SYSCTLTable[i].path, scInt.value); + break; + case SYSCTL_TYPE_BOOL: + OSSysCtl(SYSCTLTable[i].path, &scBool, NULL); + printf("%s: %s\n", SYSCTLTable[i].path, + scBool.value ? "true" : "false"); + break; + default: + printf("%s: Unsupported type\n", SYSCTLTable[i].path); + break; + } + } + + return 0; +} + diff --git a/sys/include/mount.h b/sys/include/mount.h new file mode 100644 index 0000000..1d76abf --- /dev/null +++ b/sys/include/mount.h @@ -0,0 +1,13 @@ + +#ifndef __SYS_MOUNT_H__ +#define __SYS_MOUNT_H__ + +struct statfs { +}; + +#define MNT_RDONLY 0x0001 /* Read-only */ +#define MNT_SYNC 0x0002 /* Synchronous */ +#define MNT_ASYNC 0x0004 /* Asynchronous */ + +#endif /* __SYS_MOUNT_H__ */ + diff --git a/sys/include/syscall.h b/sys/include/syscall.h index a2d27a5..97df679 100644 --- a/sys/include/syscall.h +++ b/sys/include/syscall.h @@ -40,6 +40,12 @@ #define SYSCALL_NICSEND 0x41 #define SYSCALL_NICRECV 0x42 +// System +#define SYSCALL_SYSCTL 0x80 +#define SYSCALL_FSMOUNT 0x81 +#define SYSCALL_FSUNMOUNT 0x82 +#define SYSCALL_FSINFO 0x83 + uint64_t Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5); diff --git a/sys/include/sysctl.h b/sys/include/sysctl.h index df116ac..bc096fa 100644 --- a/sys/include/sysctl.h +++ b/sys/include/sysctl.h @@ -12,6 +12,11 @@ * SYSCTL_END() */ +#define SYSCTL_TYPE_INVALID 0 +#define SYSCTL_TYPE_STR 1 +#define SYSCTL_TYPE_INT 2 +#define SYSCTL_TYPE_BOOL 3 + #define SYSCTL_FLAG_RO 1 #define SYSCTL_FLAG_RW 2 @@ -58,5 +63,8 @@ SYSCTL_LIST #define SYSCTL_GETBOOL(_PATH) SYSCTL_##_PATH.value #define SYSCTL_SETBOOL(_PATH, _VALUE) SYSCTL_##_PATH.value = _VALUE +uint64_t SysCtl_GetType(const char *node); +void *SysCtl_GetObject(const char *node); + #endif /* __SYS_SYSCTL_H__ */ diff --git a/sys/kern/syscall.c b/sys/kern/syscall.c index fb42f96..b5dc17a 100644 --- a/sys/kern/syscall.c +++ b/sys/kern/syscall.c @@ -16,6 +16,7 @@ #include #include #include +#include Handle *Console_OpenHandle(); @@ -531,6 +532,68 @@ Syscall_NICRecv(uint64_t nicNo, uint64_t user_mbuf) return 0; } +uint64_t +Syscall_SysCtl(uint64_t user_node, uint64_t user_oldval, uint64_t user_newval) +{ + uint64_t status; + char node[64]; + + status = CopyStrIn(user_node, &node, sizeof(node)); + if (status != 0) { + return status; + } + + uint64_t scType = SysCtl_GetType(node); + if (scType == SYSCTL_TYPE_INVALID) { + return SYSCALL_PACK(ENOENT, 0); + } + + switch (scType) { + case SYSCTL_TYPE_STR: { + SysCtlString *scStr = SysCtl_GetObject(node); + status = CopyOut(scStr, user_oldval, sizeof(*scStr)); + break; + } + case SYSCTL_TYPE_INT: { + SysCtlInt *scInt = SysCtl_GetObject(node); + status = CopyOut(scInt, user_oldval, sizeof(*scInt)); + break; + } + case SYSCTL_TYPE_BOOL: { + SysCtlBool *scBool = SysCtl_GetObject(node); + status = CopyOut(scBool, user_oldval, sizeof(scBool)); + break; + } + default: { + status = SYSCALL_PACK(ENOENT, 0); + } + } + + if (status != 0) { + return status; + } + + // XXX: Support setting +} + +uint64_t +Syscall_FSMount(uint64_t user_mntpt, uint64_t user_device, uint64_t flags) +{ + return SYSCALL_PACK(ENOSYS, 0); +} + +uint64_t +Syscall_FSUnmount(uint64_t user_mntpt) +{ + return SYSCALL_PACK(ENOSYS, 0); +} + +uint64_t +Syscall_FSInfo(uint64_t user_fsinfo, uint64_t max) +{ + return SYSCALL_PACK(ENOSYS, 0); +} + uint64_t Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5) @@ -587,6 +650,14 @@ Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2, return Syscall_NICSend(a1, a2); case SYSCALL_NICRECV: return Syscall_NICRecv(a1, a2); + case SYSCALL_SYSCTL: + return Syscall_SysCtl(a1, a2, a3); + case SYSCALL_FSMOUNT: + return Syscall_FSMount(a1, a2, a3); + case SYSCALL_FSUNMOUNT: + return Syscall_FSUnmount(a1); + case SYSCALL_FSINFO: + return Syscall_FSInfo(a1, a2); default: return (uint64_t)-1; } diff --git a/sys/kern/sysctl.c b/sys/kern/sysctl.c index 0018beb..ee60ab4 100644 --- a/sys/kern/sysctl.c +++ b/sys/kern/sysctl.c @@ -7,10 +7,6 @@ #include #include -#define SYSCTL_TYPE_STR 1 -#define SYSCTL_TYPE_INT 2 -#define SYSCTL_TYPE_BOOL 3 - typedef struct SysCtlEntry { char path[64]; int type; @@ -57,6 +53,28 @@ SysCtl_Lookup(const char *path) return -1; } +uint64_t +SysCtl_GetType(const char *node) +{ + int i = SysCtl_Lookup(node); + if (i == -1) { + return SYSCTL_TYPE_INVALID; + } + + return SYSCTLTable[i].type; +} + +void * +SysCtl_GetObject(const char *node) +{ + int i = SysCtl_Lookup(node); + if (i == -1) { + return NULL; + } + + return SYSCTLTable[i].node; +} + void Debug_SysCtl(int argc, const char *argv[]) {