2003-10-28 21:23:31 +03:00
|
|
|
// ToDo: do we really need sysctl? And if so, for what?
|
|
|
|
// It is not part of the POSIX spec.
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <ktypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <vm.h>
|
|
|
|
#include <debug.h>
|
2002-10-30 02:07:06 +03:00
|
|
|
#include <malloc.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <sysctl.h>
|
2002-10-26 20:13:36 +04:00
|
|
|
#include <KernelExport.h>
|
2002-07-12 02:21:56 +04:00
|
|
|
#include <Errors.h>
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
/* Not sure where to put this definition yet (sys/param.h?),
|
|
|
|
* so just add it here
|
|
|
|
* XXX - horrible hack!
|
|
|
|
*/
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
2002-10-30 02:07:06 +03:00
|
|
|
# define MAXHOSTNAMELEN 256 /* max hostname size */
|
2002-07-09 16:24:59 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This is the place we store a few of the "global variables" that the OS
|
|
|
|
* needs.
|
|
|
|
*/
|
|
|
|
char hostname[MAXHOSTNAMELEN] = "openbeos\0";
|
|
|
|
int hostnamelen = 9;
|
|
|
|
char domainname[MAXHOSTNAMELEN] = "rocks.my.world.com\0";
|
|
|
|
int domainnamelen = 19;
|
|
|
|
|
|
|
|
/* These really don't belong here, but just for the moment until we figure out where
|
|
|
|
* these should live...
|
|
|
|
*/
|
|
|
|
|
|
|
|
char ostype[] = "OpenBeOS";
|
|
|
|
char osrelease[] = "0.01";
|
|
|
|
char osversion[] = "alpha";
|
|
|
|
char version[] = "0.0.1";
|
|
|
|
char kernel[] = "DEV";
|
|
|
|
|
|
|
|
char machine[] = "Intel";
|
|
|
|
char model[] = "MODEL";
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2003-10-28 21:23:31 +03:00
|
|
|
typedef int (sysctlfn)(int *, uint, void *, size_t *, void *, size_t);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-10-28 21:23:31 +03:00
|
|
|
// currently unused
|
|
|
|
#if 0
|
|
|
|
static int
|
2002-10-30 02:07:06 +03:00
|
|
|
sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
if (oldp && *oldlenp < sizeof(int))
|
|
|
|
return ENOMEM;
|
|
|
|
if (newp && newlen != sizeof(int))
|
|
|
|
return EINVAL;
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
*oldlenp = sizeof(int);
|
|
|
|
if (oldp)
|
|
|
|
*(int*)oldp = *valp;
|
|
|
|
if (newp)
|
|
|
|
*valp = *(int*)newp;
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2003-10-28 21:23:31 +03:00
|
|
|
static int
|
2002-10-30 02:07:06 +03:00
|
|
|
sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
if (oldp && *oldlenp < sizeof(int))
|
|
|
|
return ENOMEM;
|
|
|
|
if (newp)
|
|
|
|
return EPERM;
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
*oldlenp = sizeof(int);
|
|
|
|
if (oldp)
|
|
|
|
*(int*)oldp = val;
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2003-10-28 21:23:31 +03:00
|
|
|
#endif
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-10-28 21:23:31 +03:00
|
|
|
static int
|
2002-10-30 02:07:06 +03:00
|
|
|
sysctl__string(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
|
2003-10-28 21:23:31 +03:00
|
|
|
char *str, size_t maxlen, int trunc)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2003-10-28 21:23:31 +03:00
|
|
|
size_t len = strlen(str) + 1;
|
2002-07-09 16:24:59 +04:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (oldp && *oldlenp < len) {
|
|
|
|
if (trunc == 0 || *oldlenp == 0)
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
if (newp && newlen >= maxlen)
|
|
|
|
return EINVAL;
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
if (oldp) {
|
|
|
|
if (trunc && *oldlenp < len) {
|
|
|
|
/* need to truncate */
|
|
|
|
c = str[*oldlenp - 1];
|
|
|
|
str[*oldlenp - 1] = '\0';
|
|
|
|
memcpy(oldp, str, *oldlenp);
|
|
|
|
str[*oldlenp - 1] = c;
|
|
|
|
} else {
|
|
|
|
/* just copy */
|
|
|
|
*oldlenp = len;
|
|
|
|
memcpy(oldp, str, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newp) {
|
|
|
|
memcpy(str, newp, newlen);
|
|
|
|
str[newlen] = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2003-10-28 21:23:31 +03:00
|
|
|
/** Copy string, truncating if required */
|
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_tstring(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str, int maxlen)
|
|
|
|
{
|
|
|
|
return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-30 02:07:06 +03:00
|
|
|
sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, char *str)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2003-10-28 21:23:31 +03:00
|
|
|
size_t len = strlen(str) + 1;
|
2002-07-09 16:24:59 +04:00
|
|
|
if (oldp && *oldlenp < len)
|
|
|
|
return ENOMEM;
|
|
|
|
if (newp)
|
|
|
|
return EPERM;
|
|
|
|
*oldlenp = len;
|
|
|
|
if (oldp)
|
|
|
|
memcpy(oldp, str, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2003-10-28 21:23:31 +03:00
|
|
|
static int
|
|
|
|
sys_sysctl(int *name, uint namelen, void *oldp, size_t *oldlenp,
|
|
|
|
void *newp, size_t newlen)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (name[0]) {
|
|
|
|
case KERN_OSTYPE:
|
|
|
|
return sysctl_rdstring(oldp, oldlenp, newp, ostype);
|
|
|
|
case KERN_OSRELEASE:
|
|
|
|
return sysctl_rdstring(oldp, oldlenp, newp, osrelease);
|
|
|
|
case KERN_OSVERSION:
|
|
|
|
return sysctl_rdstring(oldp, oldlenp, newp, osversion);
|
|
|
|
case KERN_HOSTNAME:
|
|
|
|
error = sysctl_tstring(oldp, oldlenp, newp, newlen,
|
|
|
|
hostname, sizeof(hostname));
|
|
|
|
if (newp && !error)
|
|
|
|
hostnamelen = newlen;
|
|
|
|
return (error);
|
|
|
|
case KERN_DOMAINNAME:
|
|
|
|
error = sysctl_tstring(oldp, oldlenp, newp, newlen,
|
|
|
|
domainname, sizeof(domainname));
|
|
|
|
if (newp && !error)
|
|
|
|
domainnamelen = newlen;
|
|
|
|
return (error);
|
|
|
|
case KERN_VERSION:
|
|
|
|
return sysctl_rdstring(oldp, oldlenp, newp, kernel);
|
|
|
|
default:
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
/* If we get here we're in trouble... */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
hw_sysctl(int *name, uint namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
|
|
|
|
{
|
|
|
|
switch (name[0]) {
|
|
|
|
case HW_MACHINE:
|
|
|
|
return sysctl_rdstring(oldp, oldlenp, newp, machine);
|
|
|
|
case HW_MODEL:
|
|
|
|
return sysctl_rdstring(oldp, oldlenp, newp, model);
|
|
|
|
default:
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
/* If we get here we're in trouble... */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
sysctl(int *name, uint namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
|
|
|
|
{
|
|
|
|
sysctlfn *fn = NULL;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (name[0]) {
|
|
|
|
case CTL_KERN:
|
|
|
|
fn = sys_sysctl;
|
|
|
|
break;
|
|
|
|
case CTL_HW:
|
|
|
|
fn = hw_sysctl;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dprintf("sysctl: no suppport added yet for %d\n", name[0]);
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
error = (fn)(name + 1, namelen - 1, oldp, oldlenp, newp, newlen);
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
int
|
2004-08-29 00:51:47 +04:00
|
|
|
_user_sysctl(int *name, uint namelen, void *oldp, size_t *oldlenp,
|
2002-07-09 16:24:59 +04:00
|
|
|
void *newp, size_t newlen)
|
|
|
|
{
|
|
|
|
void *a1 = NULL, *a2 = NULL;
|
|
|
|
int *nam = NULL, rc;
|
|
|
|
size_t olen = *oldlenp, *ov=NULL;
|
|
|
|
|
|
|
|
if (namelen < 2 || namelen > CTL_MAXNAME)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (name && namelen > 0) {
|
2002-10-30 02:07:06 +03:00
|
|
|
nam = (int *)malloc(namelen * sizeof(int));
|
2002-07-09 16:24:59 +04:00
|
|
|
if (!nam)
|
|
|
|
return ENOMEM;
|
|
|
|
user_memcpy(nam, name, namelen * sizeof(int));
|
|
|
|
}
|
|
|
|
if (oldp && oldlenp) {
|
2002-10-30 02:07:06 +03:00
|
|
|
a1 = malloc(olen);
|
2002-07-09 16:24:59 +04:00
|
|
|
user_memcpy(a1, oldp, olen);
|
|
|
|
}
|
|
|
|
if (oldlenp) {
|
2002-10-30 02:07:06 +03:00
|
|
|
ov = (size_t*)malloc(sizeof(oldlenp));
|
2002-07-09 16:24:59 +04:00
|
|
|
user_memcpy(ov, oldlenp, sizeof(oldlenp));
|
|
|
|
}
|
|
|
|
if (newp && newlen > 0) {
|
2002-10-30 02:07:06 +03:00
|
|
|
a2 = malloc(newlen);
|
2002-07-09 16:24:59 +04:00
|
|
|
user_memcpy(a2, newp, newlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sysctl(nam, namelen, a1, ov, a2, newlen);
|
|
|
|
|
|
|
|
if (nam)
|
2002-10-30 02:07:06 +03:00
|
|
|
free(nam);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
/* We don't need to copy back the name settings as they won't have changed. */
|
|
|
|
if (oldp && *ov > 0) {
|
|
|
|
memcpy(oldp, a1, *ov);
|
|
|
|
memcpy(oldlenp, ov, sizeof(oldlenp));
|
|
|
|
}
|
|
|
|
if (newp && newlen > 0) {
|
|
|
|
/* We passed namelen thru so don't worry about updating it here... */
|
|
|
|
memcpy(newp, a2, newlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
bailout:
|
2002-10-30 02:07:06 +03:00
|
|
|
free(a1);
|
|
|
|
free(a2);
|
|
|
|
free(ov);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return rc;
|
2003-10-28 21:23:31 +03:00
|
|
|
}
|
|
|
|
|