metal-cos/sys/kern/sysctl.c
2014-11-25 12:07:14 -08:00

105 lines
2.4 KiB
C

#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);