Basic sysctl infrastructure

This commit is contained in:
Ali Mashtizadeh 2014-11-25 12:07:14 -08:00
parent 287b3bc043
commit 8d9580b273
3 changed files with 162 additions and 0 deletions

View File

@ -51,6 +51,7 @@ src_common = [
"kern/slab.c",
"kern/spinlock.c",
"kern/syscall.c",
"kern/sysctl.c",
"kern/thread.c",
"kern/vfs.c",
"dev/ahci.c",

57
sys/include/sysctl.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef __SYS_SYSCTL_H__
#define __SYS_SYSCTL_H__
/*
* System Control Macros
* SYSCTL_STR(PATH, FLAGS, DESCRIPTION, DEFAULT)
* SYSCTL_INT(PATH, FLAGS, DESCRIPTION, DEFAULT)
* SYSCTL_BOOL(PATH, FLAGS, DESCRIPTION, DEFAULT)
* SYSCTL_END()
*/
#define SYSCTL_FLAG_RO 1
#define SYSCTL_FLAG_RW 2
#define SYSCTL_LIST \
SYSCTL_STR(kern_ostype, SYSCTL_FLAG_RO, "OS Type", "Castor") \
SYSCTL_INT(kern_hz, SYSCTL_FLAG_RW, "Tick frequency", 100) \
SYSCTL_INT(time_tzadj, SYSCTL_FLAG_RW, "time zone offset in seconds", 0) \
#define SYSCTL_STR_MAXLENGTH 128
typedef struct SysCtlString {
char path[64];
char value[SYSCTL_STR_MAXLENGTH];
} SysCtlString;
typedef struct SysCtlInt {
char path[64];
int64_t value;
} SysCtlInt;
typedef struct SysCtlBool {
char path[64];
bool value;
} SysCtlBool;
#define SYSCTL_STR(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
extern SysCtlString SYSCTL_##_PATH;
#define SYSCTL_INT(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
extern SysCtlInt SYSCTL_##_PATH;
#define SYSCTL_BOOL(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
extern SysCtlBool SYSCTL_##_PATH;
SYSCTL_LIST
#undef SYSCTL_STR
#undef SYSCTL_INT
#undef SYSCTL_BOOL
#define SYSCTL_GETSTR(_PATH) SYSCTL_##_PATH.value
#define SYSCTL_SETSTR(_PATH, _VALUE) strncpy(SYSCTL_##_PATH.value, _VALUE, SYSCTL_STR_MAXLENGTH);
#define SYSCTL_GETINT(_PATH) SYSCTL_##_PATH.value
#define SYSCTL_SETINT(_PATH, _VALUE) SYSCTL_##_PATH.value = _VALUE
#define SYSCTL_GETBOOL(_PATH) SYSCTL_##_PATH.value
#define SYSCTL_SETBOOL(_PATH, _VALUE) SYSCTL_##_PATH.value = _VALUE
#endif /* __SYS_SYSCTL_H__ */

104
sys/kern/sysctl.c Normal file
View File

@ -0,0 +1,104 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/sysctl.h>
#define SYSCTL_TYPE_STR 1
#define SYSCTL_TYPE_INT 2
#define SYSCTL_TYPE_BOOL 3
typedef struct SysCtlEntry {
char path[64];
int type;
int flags;
char description[128];
void *node;
} SysCtlEntry;
#define SYSCTL_STR(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
{ #_PATH, SYSCTL_TYPE_STR, _FLAGS, _DESCRIPTION, &SYSCTL_##_PATH },
#define SYSCTL_INT(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
{ #_PATH, SYSCTL_TYPE_INT, _FLAGS, _DESCRIPTION, &SYSCTL_##_PATH },
#define SYSCTL_BOOL(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
{ #_PATH, SYSCTL_TYPE_BOOL, _FLAGS, _DESCRIPTION, &SYSCTL_##_PATH },
SysCtlEntry SYSCTLTable[] = {
SYSCTL_LIST
{ "", 0, 0, "", NULL },
};
#undef SYSCTL_STR
#undef SYSCTL_INT
#undef SYSCTL_BOOL
#define SYSCTL_STR(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
SysCtlString SYSCTL_##_PATH = { #_PATH, _DEFAULT };
#define SYSCTL_INT(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
SysCtlInt SYSCTL_##_PATH = { #_PATH, _DEFAULT };
#define SYSCTL_BOOL(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT) \
SysCtlBool SYSCTL_##_PATH = { #_PATH, _DEFAULT };
SYSCTL_LIST
#undef SYSCTL_STR
#undef SYSCTL_INT
#undef SYSCTL_BOOL
int
SysCtl_Lookup(const char *path)
{
int i;
for (i = 0; SYSCTLTable[i].path[0] != '\0'; i++) {
if (strcmp(path, SYSCTLTable[i].path) == 0)
return i;
}
return -1;
}
void
Debug_SysCtl(int argc, const char *argv[])
{
int i;
if (argc == 1) {
kprintf("%-20s %s\n", "Name", "Description");
for (i = 0; SYSCTLTable[i].path[0] != '\0'; i++) {
kprintf("%-20s %s\n", SYSCTLTable[i].path, SYSCTLTable[i].description);
}
return;
}
i = SysCtl_Lookup(argv[1]);
if (i == -1) {
kprintf("Unknown sysctl node!\n");
return;
}
if (argc == 2) {
switch (SYSCTLTable[i].type) {
case SYSCTL_TYPE_STR: {
SysCtlString *val = (SysCtlString *)SYSCTLTable[i].node;
kprintf("%s: %s\n", argv[1], val->value);
break;
}
case SYSCTL_TYPE_INT: {
SysCtlInt *val = (SysCtlInt *)SYSCTLTable[i].node;
kprintf("%s: %ld\n", argv[1], val->value);
break;
}
case SYSCTL_TYPE_BOOL: {
SysCtlBool *val = (SysCtlBool *)SYSCTLTable[i].node;
kprintf("%s: %s\n", argv[1], val->value ? "true" : "false");
break;
}
}
return;
}
}
REGISTER_DBGCMD(sysctl, "SYSCTL", Debug_SysCtl);