Rework the kernel environment subsystem. We now convert the static

environment needed at boot time to a dynamic subsystem when VM is
up.  The dynamic kernel environment is protected by an sx lock.

This adds some new functions to manipulate the kernel environment :
freeenv(), setenv(), unsetenv() and testenv().  freeenv() has to be
called after every getenv() when you have finished using the string.
testenv() only tests if an environment variable is present, and
doesn't require a freeenv() call. setenv() and unsetenv() are self
explanatory.

The kenv(2) syscall exports these new functionalities to userland,
mainly for kenv(1).

Reviewed by:	peter
This commit is contained in:
Maxime Henrion 2002-04-17 13:06:36 +00:00
parent fd448168b7
commit d786139c76
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=94936
27 changed files with 617 additions and 129 deletions

View File

@ -12,7 +12,8 @@ HDRS= a.out.h ar.h assert.h bitstring.h complex.h ctype.h db.h \
dirent.h \
dlfcn.h elf.h elf-hints.h err.h fnmatch.h fstab.h \
fts.h glob.h grp.h \
hesiod.h histedit.h ieeefp.h ifaddrs.h inttypes.h iso646.h langinfo.h \
hesiod.h histedit.h ieeefp.h ifaddrs.h inttypes.h iso646.h kenv.h \
langinfo.h \
libgen.h limits.h link.h locale.h malloc.h memory.h monetary.h mpool.h \
ndbm.h netconfig.h netdb.h nl_types.h nlist.h nsswitch.h objformat.h \
paths.h pthread.h pthread_np.h pwd.h \

39
include/kenv.h Normal file
View File

@ -0,0 +1,39 @@
/*-
* Copyright (c) 2002 Maxime Henrion <mux@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _KENV_H_
#define _KENV_H_
#include <sys/cdefs.h>
#include <sys/kenv.h>
__BEGIN_DECLS
int kenv(int, char *, char *, int);
__END_DECLS
#endif /* !_KENV_H_ */

View File

@ -834,6 +834,7 @@ alpha_init(pfn, ptb, bim, bip, biv)
printf("Ignoring invalid memory size of '%s'\n", p);
else
Maxmem = alpha_btop(AllowMem);
freeenv(p);
}
while (physmem > Maxmem) {
@ -1015,6 +1016,7 @@ alpha_init(pfn, ptb, bim, bip, biv)
boothowto |= RB_VERBOSE;
bootverbose = 1;
}
freeenv(p);
}
/*
@ -1025,6 +1027,7 @@ alpha_init(pfn, ptb, bim, bip, biv)
min(sizeof(kernelname), sizeof bootinfo.booted_kernel) - 1);
} else if ((p = getenv("kernelname")) != NULL) {
strncpy(kernelname, p, sizeof(kernelname) - 1);
freeenv(p);
}
/*

View File

@ -242,6 +242,7 @@ inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
{
u_int32_t a[4];
char *cp;
int count;
bzero(sa, sizeof(*sa));
sa->sin_len = sizeof(*sa);
@ -249,7 +250,9 @@ inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
if ((cp = getenv(ev)) == NULL)
return(1);
if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
freeenv(cp);
if (count != 4)
return(1);
/* XXX is this ordering correct? */
sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
@ -261,6 +264,7 @@ hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
{
char *cp;
u_int32_t a[6];
int count;
bzero(sa, sizeof(*sa));
sa->sdl_len = sizeof(*sa);
@ -269,7 +273,10 @@ hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
sa->sdl_alen = ETHER_ADDR_LEN;
if ((cp = getenv(ev)) == NULL)
return(1);
if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
freeenv(cp);
if (count != 6)
return(1);
sa->sdl_data[0] = a[0];
sa->sdl_data[1] = a[1];
@ -286,20 +293,30 @@ decode_nfshandle(char *ev, u_char *fh)
u_char *cp;
int len, val;
if (((cp = getenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
if ((cp = getenv(ev)) == NULL)
return(0);
if ((strlen(cp) < 2) || (*cp != 'X')) {
freeenv(cp);
return (0);
}
len = 0;
cp++;
for (;;) {
if (*cp == 'X')
if (*cp == 'X') {
freeenv(cp);
return(len);
if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
}
if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
freeenv(cp);
return(0);
}
*(fh++) = val;
len++;
cp += 2;
if (len > NFSX_V2FH)
if (len > NFSX_V2FH) {
freeenv(cp);
return(0);
}
}
}
@ -382,8 +399,10 @@ pxe_setup_nfsdiskless(void)
printf("PXE: no NFS handle\n");
return;
}
if ((cp = getenv("boot.nfsroot.path")) != NULL)
if ((cp = getenv("boot.nfsroot.path")) != NULL) {
strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
freeenv(cp);
}
nfs_diskless_valid = 1;
}

View File

@ -110,6 +110,8 @@ bios32_init(void *junk)
if (!bios32_SDlookup(&PCIbios) && bootverbose)
printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
}
if (p != NULL)
freeenv(p);
} else {
printf("bios32: Bad BIOS32 Service Directory\n");
}
@ -145,6 +147,8 @@ bios32_init(void *junk)
printf("pnpbios: Bad PnP BIOS data checksum\n");
}
}
if (p != NULL)
freeenv(p);
if (bootverbose) {
/* look for other know signatures */
printf("Other BIOS signatures found:\n");

View File

@ -1238,7 +1238,7 @@ getmemsize(int first)
struct vm86context vmc;
vm_offset_t pa, physmap[PHYSMAP_SIZE];
pt_entry_t *pte;
const char *cp;
char *cp;
struct bios_smap *smap;
bzero(&vmf, sizeof(struct vm86frame));
@ -1454,6 +1454,7 @@ getmemsize(int first)
printf("Ignoring invalid memory size of '%s'\n", cp);
else
Maxmem = atop(AllowMem);
freeenv(cp);
}
if (atop(physmap[physmap_idx + 1]) != Maxmem &&

View File

@ -210,7 +210,7 @@ acpi_identify(driver_t *driver, device_t parent)
int error;
caddr_t acpi_dsdt, p;
#ifdef ENABLE_DEBUGGER
char *debugpoint = getenv("debug.acpi.debugger");
char *debugpoint;
#endif
ACPI_FUNCTION_TRACE(__func__);
@ -240,16 +240,24 @@ acpi_identify(driver_t *driver, device_t parent)
* Start up the ACPI CA subsystem.
*/
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "init"))
acpi_EnterDebugger();
debugpoint = getenv("debug.acpi.debugger");
if (debugpoint) {
if (!strcmp(debugpoint, "init"))
acpi_EnterDebugger();
freeenv(debugpoint);
}
#endif
if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
return_VOID;
}
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "tables"))
acpi_EnterDebugger();
debugpoint = getenv("debug.acpi.debugger");
if (debugpoint) {
if (!strcmp(debugpoint, "tables"))
acpi_EnterDebugger();
freeenv(debugpoint);
}
#endif
if ((acpi_dsdt = preload_search_by_type("acpi_dsdt")) != NULL) {
@ -319,7 +327,7 @@ acpi_attach(device_t dev)
UINT32 flags;
#ifdef ENABLE_DEBUGGER
char *debugpoint = getenv("debug.acpi.debugger");
char *debugpoint;
#endif
ACPI_FUNCTION_TRACE(__func__);
@ -329,8 +337,12 @@ acpi_attach(device_t dev)
sc->acpi_dev = dev;
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "spaces"))
acpi_EnterDebugger();
debugpoint = getenv("debug.acpi.debugger");
if (debugpoint) {
if (!strcmp(debugpoint, "spaces"))
acpi_EnterDebugger();
freeenv(debugpoint);
}
#endif
/*
@ -372,11 +384,15 @@ acpi_attach(device_t dev)
* child devices, but on many systems it works here.
*/
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "enable"))
acpi_EnterDebugger();
debugpoint = getenv("debug.acpi.debugger");
if (debugpoint) {
if (!strcmp(debugpoint, "enable"))
acpi_EnterDebugger();
freeenv(debugpoint);
}
#endif
flags = 0;
if (getenv("debug.acpi.avoid") != NULL)
if (testenv("debug.acpi.avoid"))
flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
@ -433,8 +449,12 @@ acpi_attach(device_t dev)
* Scan the namespace and attach/initialise children.
*/
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "probe"))
acpi_EnterDebugger();
debugpoint = getenv("debug.acpi.debugger");
if (debugpoint) {
if (!strcmp(debugpoint, "probe"))
acpi_EnterDebugger();
freeenv(debugpoint);
}
#endif
if (!acpi_disabled("bus"))
acpi_probe_children(dev);
@ -466,8 +486,12 @@ acpi_attach(device_t dev)
sc->acpi_dev_t->si_drv1 = sc;
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "running"))
acpi_EnterDebugger();
debugpoint = getenv("debug.acpi.debugger");
if (debugpoint) {
if (!strcmp(debugpoint, "running"))
acpi_EnterDebugger();
freeenv(debugpoint);
}
#endif
#if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0
@ -1550,10 +1574,13 @@ acpi_avoid(ACPI_HANDLE handle)
len = 0;
while ((cp[len] != 0) && !isspace(cp[len]))
len++;
if (!strncmp(cp, np, len))
if (!strncmp(cp, np, len)) {
freeenv(cp);
return(1);
}
cp += len;
}
freeenv(cp);
return(0);
}
@ -1568,8 +1595,10 @@ acpi_disabled(char *subsys)
if ((cp = getenv("debug.acpi.disable")) == NULL)
return(0);
if (!strcmp(cp, "all"))
if (!strcmp(cp, "all")) {
freeenv(cp);
return(1);
}
/* scan the disable list checking for a match */
for (;;) {
@ -1580,10 +1609,13 @@ acpi_disabled(char *subsys)
len = 0;
while ((cp[len] != 0) && !isspace(cp[len]))
len++;
if (!strncmp(cp, subsys, len))
if (!strncmp(cp, subsys, len)) {
freeenv(cp);
return(1);
}
cp += len;
}
freeenv(cp);
return(0);
}
@ -1875,13 +1907,12 @@ acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
static void
acpi_set_debugging(void *junk)
{
char *cp;
AcpiDbgLayer = 0;
AcpiDbgLevel = 0;
if ((cp = getenv("debug.acpi.layer")) != NULL)
if (testenv("debug.acpi.layer"))
acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
if ((cp = getenv("debug.acpi.level")) != NULL)
if (testenv("debug.acpi.level"))
acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);

View File

@ -168,7 +168,7 @@ acpi_timer_identify(driver_t *driver, device_t parent)
device_printf(dev, "couldn't allocate I/O resource (port 0x%x)\n", AcpiGbl_FADT->V1_PmTmrBlk);
return_VOID;
}
if (getenv("debug.acpi.timer_test") != NULL)
if (testenv("debug.acpi.timer_test"))
acpi_timer_test();
acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;

View File

@ -1927,7 +1927,7 @@ mly_cam_attach(struct mly_softc *sc)
* If physical channel registration has been requested, register these first.
* Note that we enable tagged command queueing for physical channels.
*/
if (getenv("hw.mly.register_physical_channels") != NULL) {
if (testenv("hw.mly.register_physical_channels")) {
chn = 0;
for (i = 0; i < sc->mly_controllerinfo->physical_channels_present; i++, chn++) {

View File

@ -242,6 +242,7 @@ inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
{
u_int32_t a[4];
char *cp;
int count;
bzero(sa, sizeof(*sa));
sa->sin_len = sizeof(*sa);
@ -249,7 +250,9 @@ inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
if ((cp = getenv(ev)) == NULL)
return(1);
if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
freeenv(cp);
if (count != 4)
return(1);
/* XXX is this ordering correct? */
sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
@ -261,6 +264,7 @@ hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
{
char *cp;
u_int32_t a[6];
int count;
bzero(sa, sizeof(*sa));
sa->sdl_len = sizeof(*sa);
@ -269,7 +273,10 @@ hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
sa->sdl_alen = ETHER_ADDR_LEN;
if ((cp = getenv(ev)) == NULL)
return(1);
if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
freeenv(cp);
if (count != 6)
return(1);
sa->sdl_data[0] = a[0];
sa->sdl_data[1] = a[1];
@ -286,20 +293,30 @@ decode_nfshandle(char *ev, u_char *fh)
u_char *cp;
int len, val;
if (((cp = getenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
if ((cp = getenv(ev)) == NULL)
return(0);
if ((strlen(cp) < 2) || (*cp != 'X')) {
freeenv(cp);
return (0);
}
len = 0;
cp++;
for (;;) {
if (*cp == 'X')
if (*cp == 'X') {
freeenv(cp);
return(len);
if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
}
if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
freeenv(cp);
return(0);
}
*(fh++) = val;
len++;
cp += 2;
if (len > NFSX_V2FH)
if (len > NFSX_V2FH) {
freeenv(cp);
return(0);
}
}
}
@ -382,8 +399,10 @@ pxe_setup_nfsdiskless(void)
printf("PXE: no NFS handle\n");
return;
}
if ((cp = getenv("boot.nfsroot.path")) != NULL)
if ((cp = getenv("boot.nfsroot.path")) != NULL) {
strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
freeenv(cp);
}
nfs_diskless_valid = 1;
}

View File

@ -110,6 +110,8 @@ bios32_init(void *junk)
if (!bios32_SDlookup(&PCIbios) && bootverbose)
printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
}
if (p != NULL)
freeenv(p);
} else {
printf("bios32: Bad BIOS32 Service Directory\n");
}
@ -145,6 +147,8 @@ bios32_init(void *junk)
printf("pnpbios: Bad PnP BIOS data checksum\n");
}
}
if (p != NULL)
freeenv(p);
if (bootverbose) {
/* look for other know signatures */
printf("Other BIOS signatures found:\n");

View File

@ -1238,7 +1238,7 @@ getmemsize(int first)
struct vm86context vmc;
vm_offset_t pa, physmap[PHYSMAP_SIZE];
pt_entry_t *pte;
const char *cp;
char *cp;
struct bios_smap *smap;
bzero(&vmf, sizeof(struct vm86frame));
@ -1454,6 +1454,7 @@ getmemsize(int first)
printf("Ignoring invalid memory size of '%s'\n", cp);
else
Maxmem = atop(AllowMem);
freeenv(cp);
}
if (atop(physmap[physmap_idx + 1]) != Maxmem &&

View File

@ -479,6 +479,7 @@ ia64_init(u_int64_t arg1, u_int64_t arg2)
if (strcmp(p, "yes") == 0 || strcmp(p, "YES") == 0) {
boothowto |= RB_VERBOSE;
}
freeenv(p);
}
if (boothowto & RB_VERBOSE)
@ -543,8 +544,10 @@ ia64_init(u_int64_t arg1, u_int64_t arg2)
init_param1();
p = getenv("kernelname");
if (p)
if (p) {
strncpy(kernelname, p, sizeof(kernelname) - 1);
freeenv(p);
}
kernstartpfn = atop(IA64_RR_MASK(kernstart));
kernendpfn = atop(IA64_RR_MASK(kernend));

View File

@ -528,9 +528,12 @@ start_init(void *dummy)
if ((var = getenv("init_path")) != NULL) {
strncpy(init_path, var, sizeof init_path);
init_path[sizeof init_path - 1] = 0;
freeenv(var);
}
if ((var = getenv("kern.fallback_elf_brand")) != NULL)
if ((var = getenv("kern.fallback_elf_brand")) != NULL) {
fallback_elf_brand = strtol(var, NULL, 0);
freeenv(var);
}
for (path = init_path; *path != '\0'; path = next) {
while (*path == ':')

View File

@ -412,4 +412,5 @@ struct sysent sysent[] = {
{ 0, (sy_call_t *)nosys }, /* 387 = __mac_get_file */
{ 0, (sy_call_t *)nosys }, /* 388 = __mac_set_fd */
{ 0, (sy_call_t *)nosys }, /* 389 = __mac_set_file */
{ AS(kenv_args), (sy_call_t *)kenv }, /* 390 = kenv */
};

View File

@ -28,43 +28,324 @@
/*
* The unified bootloader passes us a pointer to a preserved copy of
* bootstrap/kernel environment variables.
* We make these available using sysctl for both in-kernel and
* out-of-kernel consumers.
* bootstrap/kernel environment variables. We convert them to a
* dynamic array of strings later when the VM subsystem is up.
*
* Note that the current sysctl infrastructure doesn't allow
* dynamic insertion or traversal through handled spaces. Grr.
* We make these available through the kenv(2) syscall for userland
* and through getenv()/freeenv() setenv() unsetenv() testenv() for
* the kernel.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/kernel.h>
#include <sys/sx.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/libkern.h>
#include <sys/kenv.h>
char *kern_envp;
MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
static char *kernenv_next(char *cp);
#define KENV_SIZE 512 /* Maximum number of environment strings */
/* pointer to the static environment */
char *kern_envp;
static char *kernenv_next(char *);
/* dynamic environment variables */
char **kenvp;
struct sx kenv_lock;
/*
* No need to protect this with a mutex
* since SYSINITS are single threaded.
*/
int dynamic_kenv = 0;
#define KENV_CHECK if (!dynamic_kenv) \
panic("%s: called before SI_SUB_KMEM", __func__)
int
kenv(td, uap)
struct thread *td;
struct kenv_args /* {
syscallarg(int) what;
syscallarg(const char *) name;
syscallarg(char *) value;
syscallarg(int) len;
} */ *uap;
{
char *name, *value;
size_t len, done;
int error, i;
KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
error = 0;
if (SCARG(uap, what) == KENV_DUMP) {
len = 0;
/* Return the size if called with a NULL buffer */
if (SCARG(uap, value) == NULL) {
sx_slock(&kenv_lock);
for (i = 0; kenvp[i] != NULL; i++)
len += strlen(kenvp[i]) + 1;
sx_sunlock(&kenv_lock);
td->td_retval[0] = len;
return (0);
}
done = 0;
sx_slock(&kenv_lock);
for (i = 0; kenvp[i] != NULL && done < SCARG(uap, len); i++) {
len = min(strlen(kenvp[i]) + 1, SCARG(uap, len) - done);
error = copyout(kenvp[i], SCARG(uap, value) + done,
len);
if (error) {
sx_sunlock(&kenv_lock);
return (error);
}
done += len;
}
sx_sunlock(&kenv_lock);
return (0);
}
if ((SCARG(uap, what) == KENV_SET) ||
(SCARG(uap, what) == KENV_UNSET)) {
error = suser(td);
if (error)
return (error);
}
name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
error = copyinstr(SCARG(uap, name), name, KENV_MNAMELEN, NULL);
if (error)
goto done;
switch (SCARG(uap, what)) {
case KENV_GET:
value = getenv(name);
if (value == NULL) {
error = ENOENT;
goto done;
}
len = strlen(value) + 1;
if (len > SCARG(uap, len))
len = SCARG(uap, len);
error = copyout(value, SCARG(uap, value), len);
freeenv(value);
if (error)
goto done;
td->td_retval[0] = len;
break;
case KENV_SET:
len = SCARG(uap, len);
if (len < 1) {
error = EINVAL;
goto done;
}
if (len > KENV_MVALLEN)
len = KENV_MVALLEN;
value = malloc(len, M_TEMP, M_WAITOK);
error = copyinstr(SCARG(uap, value), value, len, NULL);
if (error) {
free(value, M_TEMP);
goto done;
}
setenv(name, value);
free(value, M_TEMP);
break;
case KENV_UNSET:
error = unsetenv(name);
if (error)
error = ENOENT;
break;
default:
error = EINVAL;
break;
}
done:
free(name, M_TEMP);
return (error);
}
/*
* Setup the dynamic kernel environment.
*/
static void
init_dynamic_kenv(void *data __unused)
{
char *cp;
int len, i;
kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO);
i = 0;
for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
len = strlen(cp) + 1;
kenvp[i] = malloc(len, M_KENV, M_WAITOK);
strcpy(kenvp[i++], cp);
}
kenvp[i] = NULL;
sx_init(&kenv_lock, "kernel environment");
dynamic_kenv = 1;
}
SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
void
freeenv(char *env)
{
if (dynamic_kenv)
free(env, M_KENV);
}
/*
* Internal functions for string lookup.
*/
static char *
_getenv_dynamic(const char *name, int *idx)
{
char *cp;
int len, i;
sx_assert(&kenv_lock, SX_LOCKED);
len = strlen(name);
for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
if ((cp[len] == '=') &&
(strncmp(cp, name, len) == 0)) {
if (idx != NULL)
*idx = i;
return (cp + len + 1);
}
}
return (NULL);
}
static char *
_getenv_static(const char *name)
{
char *cp, *ep;
int len;
for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
;
len = ep - cp;
if (*ep == '=')
ep++;
if (!strncmp(name, cp, len))
return (ep);
}
return (NULL);
}
/*
* Look up an environment variable by name.
* Return a pointer to the string if found.
* The pointer has to be freed with freeenv()
* after use.
*/
char *
getenv(const char *name)
{
char *cp, *ep;
int len;
for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
;
len = ep - cp;
if (*ep == '=')
ep++;
if (!strncmp(name, cp, len))
return(ep);
}
return(NULL);
char *ret, *cp;
int len;
if (dynamic_kenv) {
sx_slock(&kenv_lock);
cp = _getenv_dynamic(name, NULL);
if (cp != NULL) {
len = strlen(cp) + 1;
ret = malloc(len, M_KENV, M_WAITOK);
strcpy(ret, cp);
} else
ret = NULL;
sx_sunlock(&kenv_lock);
} else
ret = _getenv_static(name);
return (ret);
}
/*
* Test if an environment variable is defined.
*/
int
testenv(const char *name)
{
char *cp;
if (dynamic_kenv) {
sx_slock(&kenv_lock);
cp = _getenv_dynamic(name, NULL);
sx_sunlock(&kenv_lock);
} else
cp = _getenv_static(name);
if (cp != NULL)
return (1);
return (0);
}
/*
* Set an environment variable by name.
*/
void
setenv(const char *name, const char *value)
{
char *buf, *cp;
int len, i;
KENV_CHECK;
len = strlen(name) + 1 + strlen(value) + 1;
buf = malloc(len, M_KENV, M_WAITOK);
sprintf(buf, "%s=%s", name, value);
sx_xlock(&kenv_lock);
cp = _getenv_dynamic(name, &i);
if (cp != NULL) {
free(kenvp[i], M_KENV);
kenvp[i] = buf;
} else {
/* We add the option if it wasn't found */
for (i = 0; (cp = kenvp[i]) != NULL; i++)
;
kenvp[i] = buf;
kenvp[i + 1] = NULL;
}
sx_xunlock(&kenv_lock);
}
/*
* Unset an environment variable string.
*/
int
unsetenv(const char *name)
{
char *cp;
int i, j;
KENV_CHECK;
sx_xlock(&kenv_lock);
cp = _getenv_dynamic(name, &i);
if (cp != NULL) {
free(kenvp[i], M_KENV);
for (j = i + 1; kenvp[j] != NULL; j++)
kenvp[i++] = kenvp[j];
kenvp[i] = NULL;
sx_xunlock(&kenv_lock);
return (0);
}
sx_xunlock(&kenv_lock);
return (-1);
}
/*
@ -78,6 +359,7 @@ getenv_string(const char *name, char *data, int size)
tmp = getenv(name);
if (tmp != NULL) {
strncpy(data, tmp, size);
freeenv(tmp);
data[size - 1] = 0;
return (1);
} else
@ -106,7 +388,7 @@ getenv_int(const char *name, int *data)
int
getenv_quad(const char *name, quad_t *data)
{
const char *value;
char *value;
char *vtp;
quad_t iv;
@ -114,49 +396,16 @@ getenv_quad(const char *name, quad_t *data)
return(0);
iv = strtoq(value, &vtp, 0);
if ((vtp == value) || (*vtp != '\0'))
if ((vtp == value) || (*vtp != '\0')) {
freeenv(value);
return(0);
}
freeenv(value);
*data = iv;
return(1);
}
/*
* Export for userland. See kenv(1) specifically.
*/
static int
sysctl_kernenv(SYSCTL_HANDLER_ARGS)
{
int *name = (int *)arg1;
u_int namelen = arg2;
char *cp;
int i, error;
if (kern_envp == NULL)
return(ENOENT);
name++;
namelen--;
if (namelen != 1)
return(EINVAL);
cp = kern_envp;
for (i = 0; i < name[0]; i++) {
cp = kernenv_next(cp);
if (cp == NULL)
break;
}
if (cp == NULL)
return(ENOENT);
error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
return (error);
}
SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kernenv, "kernel environment space");
/*
* Find the next entry after the one which (cp) falls within, return a
* pointer to its start or NULL if there are no more.

View File

@ -27,6 +27,8 @@
*/
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/sx.h>
#include <sys/systm.h>
#include <sys/bus.h>
@ -34,6 +36,7 @@
* Access functions for device resources.
*/
static int checkmethod = 1;
static char *hintp;
/*
@ -47,7 +50,7 @@ res_find(int *line, int *startln,
const char **ret_name, int *ret_namelen, int *ret_unit,
const char **ret_resname, int *ret_resnamelen, const char **ret_value)
{
int n = 0, hit;
int n = 0, hit, use_kenv, i = 0;
char r_name[32];
int r_unit;
char r_resname[32];
@ -55,40 +58,69 @@ res_find(int *line, int *startln,
const char *s, *cp;
char *p;
if (hintp == NULL) {
use_kenv = 0;
if (checkmethod) {
switch (hintmode) {
case 0: /* config supplied nothing */
hintp = kern_envp;
break;
case 1: /* static hints only */
hintp = static_hints;
checkmethod = 0;
break;
case 2: /* fallback mode */
cp = kern_envp;
while (cp) {
if (strncmp(cp, "hint.", 5) == 0) {
cp = NULL;
hintp = kern_envp;
break;
if (dynamic_kenv) {
sx_slock(&kenv_lock);
cp = kenvp[0];
for (i = 0; cp != NULL; cp = kenvp[++i]) {
if (!strncmp(cp, "hint.", 5)) {
use_kenv = 1;
checkmethod = 0;
break;
}
}
while (*cp != '\0')
sx_sunlock(&kenv_lock);
} else {
cp = kern_envp;
while (cp) {
if (strncmp(cp, "hint.", 5) == 0) {
cp = NULL;
hintp = kern_envp;
break;
}
while (*cp != '\0')
cp++;
cp++;
cp++;
if (*cp == '\0') {
cp = NULL;
hintp = static_hints;
break;
if (*cp == '\0') {
cp = NULL;
hintp = static_hints;
break;
}
}
}
break;
default:
break;
}
if (hintp == NULL)
hintp = kern_envp;
if (hintp == NULL) {
if (dynamic_kenv) {
use_kenv = 1;
checkmethod = 0;
} else
hintp = kern_envp;
}
}
cp = hintp;
if (use_kenv) {
sx_slock(&kenv_lock);
i = 0;
cp = kenvp[0];
if (cp == NULL) {
sx_sunlock(&kenv_lock);
return (ENOENT);
}
} else {
cp = hintp;
}
while (cp) {
hit = 1;
(*line)++;
@ -116,6 +148,8 @@ res_find(int *line, int *startln,
hit = 0;
if (hit)
break;
if (use_kenv)
cp = kenvp[++i];
while (*cp != '\0')
cp++;
cp++;
@ -124,6 +158,8 @@ res_find(int *line, int *startln,
break;
}
}
if (use_kenv)
sx_sunlock(&kenv_lock);
if (cp == NULL)
return ENOENT;

View File

@ -397,4 +397,5 @@ char *syscallnames[] = {
"#387", /* 387 = __mac_get_file */
"#388", /* 388 = __mac_set_fd */
"#389", /* 389 = __mac_set_file */
"kenv", /* 390 = kenv */
};

View File

@ -102,7 +102,8 @@ dev_t rootdev = NODEV;
void
vfs_mountroot(void *foo __unused)
{
int i;
char *cp;
int i, error;
/*
* The root filesystem information is compiled in, and we are
@ -139,8 +140,12 @@ vfs_mountroot(void *foo __unused)
* supplied via some other means. This is the preferred
* mechanism.
*/
if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
return;
if ((cp = getenv("vfs.root.mountfrom")) != NULL) {
error = vfs_mountroot_try(cp);
freeenv(cp);
if (!error)
return;
}
/*
* Try values that may have been computed by the machine-dependant

View File

@ -102,7 +102,8 @@ dev_t rootdev = NODEV;
void
vfs_mountroot(void *foo __unused)
{
int i;
char *cp;
int i, error;
/*
* The root filesystem information is compiled in, and we are
@ -139,8 +140,12 @@ vfs_mountroot(void *foo __unused)
* supplied via some other means. This is the preferred
* mechanism.
*/
if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
return;
if ((cp = getenv("vfs.root.mountfrom")) != NULL) {
error = vfs_mountroot_try(cp);
freeenv(cp);
if (!error)
return;
}
/*
* Try values that may have been computed by the machine-dependant

View File

@ -1263,7 +1263,7 @@ getmemsize(int first)
#endif
vm_offset_t pa, physmap[PHYSMAP_SIZE];
pt_entry_t *pte;
const char *cp;
char *cp;
#ifndef PC98
struct bios_smap *smap;
#endif
@ -1514,6 +1514,7 @@ getmemsize(int first)
printf("Ignoring invalid memory size of '%s'\n", cp);
else
Maxmem = atop(AllowMem);
freeenv(cp);
}
if (atop(physmap[physmap_idx + 1]) != Maxmem &&

View File

@ -1263,7 +1263,7 @@ getmemsize(int first)
#endif
vm_offset_t pa, physmap[PHYSMAP_SIZE];
pt_entry_t *pte;
const char *cp;
char *cp;
#ifndef PC98
struct bios_smap *smap;
#endif
@ -1514,6 +1514,7 @@ getmemsize(int first)
printf("Ignoring invalid memory size of '%s'\n", cp);
else
Maxmem = atop(AllowMem);
freeenv(cp);
}
if (atop(physmap[physmap_idx + 1]) != Maxmem &&

43
sys/sys/kenv.h Normal file
View File

@ -0,0 +1,43 @@
/*-
* Copyright (c) 2002 Maxime Henrion <mux@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _SYS_KENV_H_
#define _SYS_KENV_H_
/*
* Constants for the kenv(2) syscall
*/
#define KENV_GET 0
#define KENV_SET 1
#define KENV_UNSET 2
#define KENV_DUMP 3
#define KENV_MNAMELEN 128 /* Maximum name length (for the syscall) */
#define KENV_MVALLEN 128 /* Maximum value length (for the syscall) */
#endif /* !_SYS_KENV_H_ */

View File

@ -303,4 +303,5 @@
#define SYS_kse_new 381
#define SYS_thread_wakeup 382
#define SYS_kse_yield 383
#define SYS_MAXSYSCALL 390
#define SYS_kenv 390
#define SYS_MAXSYSCALL 391

View File

@ -252,4 +252,5 @@ MIASM = \
kse_wakeup.o \
kse_new.o \
thread_wakeup.o \
kse_yield.o
kse_yield.o \
kenv.o

View File

@ -1105,6 +1105,12 @@ struct thread_wakeup_args {
struct kse_yield_args {
register_t dummy;
};
struct kenv_args {
char what_l_[PADL_(int)]; int what; char what_r_[PADR_(int)];
char name_l_[PADL_(const char *)]; const char * name; char name_r_[PADR_(const char *)];
char value_l_[PADL_(char *)]; char * value; char value_r_[PADR_(char *)];
char len_l_[PADL_(int)]; int len; char len_r_[PADR_(int)];
};
int nosys(struct thread *, struct nosys_args *);
void sys_exit(struct thread *, struct sys_exit_args *);
int fork(struct thread *, struct fork_args *);
@ -1354,6 +1360,7 @@ int kse_wakeup(struct thread *, struct kse_wakeup_args *);
int kse_new(struct thread *, struct kse_new_args *);
int thread_wakeup(struct thread *, struct thread_wakeup_args *);
int kse_yield(struct thread *, struct kse_yield_args *);
int kenv(struct thread *, struct kenv_args *);
#ifdef COMPAT_43

View File

@ -45,6 +45,7 @@
#include <machine/atomic.h>
#include <machine/cpufunc.h>
#include <sys/callout.h>
#include <sys/queue.h>
extern int securelevel; /* system security level (see init(8)) */
extern int suser_enabled; /* suser() is permitted to return 0 */
@ -90,10 +91,14 @@ extern int maxusers; /* system tune hint */
*/
extern int envmode;
extern int hintmode; /* 0 = off. 1 = config, 2 = fallback */
extern int dynamic_kenv;
extern struct sx kenv_lock;
extern char *kern_envp;
extern char static_env[];
extern char static_hints[]; /* by config for now */
extern char **kenvp;
/*
* General function declarations.
*/
@ -201,9 +206,13 @@ int cr_cansee(struct ucred *u1, struct ucred *u2);
int cr_canseesocket(struct ucred *cred, struct socket *so);
char *getenv(const char *name);
void freeenv(char *env);
int getenv_int(const char *name, int *data);
int getenv_string(const char *name, char *data, int size);
int getenv_quad(const char *name, quad_t *data);
void setenv(const char *name, const char *value);
int unsetenv(const char *name);
int testenv(const char *name);
#ifdef APM_FIXUP_CALLTODO
struct timeval;