-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1897 from ghaerr/sysctl
[kernel,cmds] Add sysctl system call and option get/set command
- Loading branch information
Showing
12 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef __LINUXMT_SYSCTL_H | ||
#define __LINUXMT_SYSCTL_H | ||
|
||
/* sysctl(2) parameters */ | ||
|
||
#define CTL_MAXNAMESZ 32 /* max size of option name */ | ||
|
||
#define CTL_LIST 0 /* get option list from index */ | ||
#define CTL_GET (-1) /* get option value by name */ | ||
#define CTL_SET (-2) /* set option value by name */ | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <linuxmt/mm.h> | ||
#include <linuxmt/errno.h> | ||
#include <linuxmt/string.h> | ||
#include <linuxmt/sysctl.h> | ||
|
||
#include <linuxmt/trace.h> | ||
#include <linuxmt/kernel.h> | ||
|
||
struct sysctl { | ||
const char *name; | ||
int *value; | ||
}; | ||
|
||
static int malloc_debug; | ||
static int net_debug; | ||
|
||
struct sysctl sysctl[] = { | ||
{ "kern.debug", &dprintk_on }, /* debug (^P) on/off */ | ||
{ "kern.strace", &tracing }, /* strace=1, kstack=2 */ | ||
{ "kern.console", (int *)&dev_console }, /* console */ | ||
{ "malloc.debug", &malloc_debug }, | ||
{ "net.debug", &net_debug }, | ||
}; | ||
|
||
static char ctlname[CTL_MAXNAMESZ]; | ||
|
||
#define ARRAYLEN(a) (sizeof(a)/sizeof(a[0])) | ||
|
||
int sys_sysctl(int op, char *name, int *value) | ||
{ | ||
int error, n; | ||
struct sysctl *sc; | ||
|
||
if (op >= CTL_LIST) { | ||
if (!name) | ||
return -EFAULT; | ||
if (op >= ARRAYLEN(sysctl)) | ||
return -ENOTDIR; | ||
sc = &sysctl[op]; | ||
return verified_memcpy_tofs(name, (void *)sc->name, strlen(sc->name)+1); | ||
} | ||
|
||
n = strlen_fromfs(name, CTL_MAXNAMESZ) + 1; | ||
if (n >= CTL_MAXNAMESZ) return -EFAULT; | ||
error = verified_memcpy_fromfs(ctlname, name, n); | ||
if (error) return error; | ||
error = verfy_area(value, sizeof(int)); | ||
if (error) return error; | ||
|
||
for (sc = sysctl;; sc++) { | ||
if (sc >= &sysctl[ARRAYLEN(sysctl)]) return -ENOTDIR; | ||
if (!strcmp(sc->name, ctlname)) | ||
break; | ||
} | ||
|
||
if (op == CTL_GET) | ||
put_user(*sc->value, value); | ||
else if (op == CTL_SET) | ||
*sc->value = get_user(value); | ||
else | ||
return -EINVAL; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ ps | |
reboot | ||
sercat | ||
shutdown | ||
sysctl | ||
umount | ||
unreal16 | ||
who |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* sysctl - system control utility for ELKS | ||
* | ||
* 8 May 2024 Greg Haerr | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <stdarg.h> | ||
#include <sys/sysctl.h> | ||
|
||
#define errmsg(str) write(STDERR_FILENO, str, sizeof(str) - 1) | ||
|
||
static int digit(char c, int base) | ||
{ | ||
int d; | ||
if (c <= '9') { | ||
d = c - '0'; | ||
} else if (c < 'A') { /* NEATLIBC bugfix */ | ||
return -1; | ||
} else if (c <= 'Z') { | ||
d = 10 + c - 'A'; | ||
} else { | ||
d = 10 + c - 'a'; | ||
} | ||
return d < base ? d : -1; | ||
} | ||
|
||
static int strtoi(const char *s) | ||
{ | ||
int num; | ||
int dig; | ||
int base; | ||
int sgn = 1; | ||
|
||
if (*s == '-') | ||
sgn = ',' - *s++; | ||
if (*s == '0') { | ||
if (s[1] == 'x' || s[1] == 'X') { | ||
base = 16; | ||
s += 2; | ||
} else | ||
base = 8; | ||
} else { | ||
base = 10; | ||
} | ||
for (num = 0; (dig = digit(*s, base)) >= 0; s++) | ||
num = num*base + dig; | ||
num *= sgn; | ||
return num; | ||
} | ||
|
||
static void msg(int fd, const char *s, ...) | ||
{ | ||
va_list va; | ||
|
||
va_start(va, s); | ||
do { | ||
write(fd, s, strlen(s)); | ||
s = va_arg(va, const char *); | ||
} while (s); | ||
va_end(va); | ||
} | ||
|
||
int main(int ac, char **av) | ||
{ | ||
int i, ret, val; | ||
char name[CTL_MAXNAMESZ]; | ||
|
||
if (ac < 2) { | ||
errmsg("usage: sysctl [-a] [name[=value]]\n"); | ||
exit(-1); | ||
} | ||
|
||
if (av[1][0] == '-' && av[1][1] == 'a') { | ||
for (i = 0;; i++) { | ||
ret = sysctl(CTL_LIST+i, name, 0); | ||
if (ret) exit(0); | ||
ret = sysctl(CTL_GET, name, &val); | ||
msg(STDOUT_FILENO, name, "=", itoa(val), "\n", 0); | ||
} | ||
} | ||
|
||
for (i = 1; i < ac; i++) { | ||
char *p = strchr(av[i], '='); | ||
if (p) { | ||
*p = '\0'; | ||
val = strtoi(p+1); | ||
ret = sysctl(CTL_SET, av[i], &val); | ||
if (ret) { | ||
msg(STDOUT_FILENO, "Unknown option: ", av[i], "\n", 0); | ||
continue; | ||
} | ||
} | ||
ret = sysctl(CTL_GET, av[i], &val); | ||
if (ret) | ||
msg(STDERR_FILENO, "Invalid option: ", av[i], "\n", 0); | ||
else { | ||
msg(STDOUT_FILENO, av[i], "=", itoa(val), "\n", 0); | ||
} | ||
} | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef __SYS_SYSCTL_H | ||
#define __SYS_SYSCTL_H | ||
|
||
#include <features.h> | ||
#include __SYSINC__(sysctl.h) | ||
|
||
int sysctl(int op, char *name, int *value); | ||
|
||
#endif |