1998-10-09 00:31:29 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith
|
|
|
|
* 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.
|
1998-10-09 11:03:46 +00:00
|
|
|
*/
|
1998-10-09 00:31:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The unified bootloader passes us a pointer to a preserved copy of
|
2002-04-17 13:06:36 +00:00
|
|
|
* bootstrap/kernel environment variables. We convert them to a
|
|
|
|
* dynamic array of strings later when the VM subsystem is up.
|
1998-10-09 00:31:29 +00:00
|
|
|
*
|
2002-04-17 13:06:36 +00:00
|
|
|
* We make these available through the kenv(2) syscall for userland
|
|
|
|
* and through getenv()/freeenv() setenv() unsetenv() testenv() for
|
|
|
|
* the kernel.
|
1998-10-09 00:31:29 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
#include <sys/types.h>
|
1998-10-09 00:31:29 +00:00
|
|
|
#include <sys/param.h>
|
2002-04-17 13:06:36 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
1998-10-09 00:31:29 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
2002-04-17 13:06:36 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/sysproto.h>
|
1998-10-09 00:31:29 +00:00
|
|
|
#include <sys/libkern.h>
|
2002-04-17 13:06:36 +00:00
|
|
|
#include <sys/kenv.h>
|
1998-10-09 00:31:29 +00:00
|
|
|
|
2006-10-22 11:52:19 +00:00
|
|
|
#include <security/mac/mac_framework.h>
|
|
|
|
|
2005-02-10 12:02:37 +00:00
|
|
|
static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
|
1998-10-09 00:31:29 +00:00
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
#define KENV_SIZE 512 /* Maximum number of environment strings */
|
|
|
|
|
|
|
|
/* pointer to the static environment */
|
|
|
|
char *kern_envp;
|
2010-01-10 22:34:18 +00:00
|
|
|
static int env_len;
|
|
|
|
static int env_pos;
|
2002-04-17 13:06:36 +00:00
|
|
|
static char *kernenv_next(char *);
|
|
|
|
|
|
|
|
/* dynamic environment variables */
|
|
|
|
char **kenvp;
|
2006-07-09 21:42:58 +00:00
|
|
|
struct mtx kenv_lock;
|
2002-04-17 13:06:36 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-05 13:10:58 +00:00
|
|
|
* No need to protect this with a mutex since SYSINITS are single threaded.
|
2002-04-17 13:06:36 +00:00
|
|
|
*/
|
|
|
|
int dynamic_kenv = 0;
|
|
|
|
|
|
|
|
#define KENV_CHECK if (!dynamic_kenv) \
|
|
|
|
panic("%s: called before SI_SUB_KMEM", __func__)
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kenv(td, uap)
|
2002-04-17 13:06:36 +00:00
|
|
|
struct thread *td;
|
|
|
|
struct kenv_args /* {
|
2002-12-14 02:07:32 +00:00
|
|
|
int what;
|
|
|
|
const char *name;
|
|
|
|
char *value;
|
|
|
|
int len;
|
2002-04-17 13:06:36 +00:00
|
|
|
} */ *uap;
|
|
|
|
{
|
2006-07-09 21:42:58 +00:00
|
|
|
char *name, *value, *buffer = NULL;
|
2009-03-23 00:00:50 +00:00
|
|
|
size_t len, done, needed, buflen;
|
2002-04-17 13:06:36 +00:00
|
|
|
int error, i;
|
2002-05-01 02:51:50 +00:00
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
|
|
|
|
|
|
|
|
error = 0;
|
2002-12-14 01:56:26 +00:00
|
|
|
if (uap->what == KENV_DUMP) {
|
2002-11-01 20:46:53 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kenv_check_dump(td->td_ucred);
|
2002-11-01 20:46:53 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
2004-04-28 01:27:33 +00:00
|
|
|
done = needed = 0;
|
2009-03-23 00:00:50 +00:00
|
|
|
buflen = uap->len;
|
|
|
|
if (buflen > KENV_SIZE * (KENV_MNAMELEN + KENV_MVALLEN + 2))
|
|
|
|
buflen = KENV_SIZE * (KENV_MNAMELEN +
|
|
|
|
KENV_MVALLEN + 2);
|
2006-07-09 21:42:58 +00:00
|
|
|
if (uap->len > 0 && uap->value != NULL)
|
2009-03-23 00:00:50 +00:00
|
|
|
buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_lock(&kenv_lock);
|
2004-04-28 01:27:33 +00:00
|
|
|
for (i = 0; kenvp[i] != NULL; i++) {
|
|
|
|
len = strlen(kenvp[i]) + 1;
|
|
|
|
needed += len;
|
2009-03-23 00:00:50 +00:00
|
|
|
len = min(len, buflen - done);
|
2004-04-28 01:27:33 +00:00
|
|
|
/*
|
|
|
|
* If called with a NULL or insufficiently large
|
|
|
|
* buffer, just keep computing the required size.
|
|
|
|
*/
|
2006-07-09 21:42:58 +00:00
|
|
|
if (uap->value != NULL && buffer != NULL && len > 0) {
|
|
|
|
bcopy(kenvp[i], buffer + done, len);
|
2004-04-28 01:27:33 +00:00
|
|
|
done += len;
|
2002-04-17 13:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
|
|
|
if (buffer != NULL) {
|
|
|
|
error = copyout(buffer, uap->value, done);
|
|
|
|
free(buffer, M_TEMP);
|
|
|
|
}
|
2004-04-28 01:27:33 +00:00
|
|
|
td->td_retval[0] = ((done == needed) ? 0 : needed);
|
|
|
|
return (error);
|
2002-04-17 13:06:36 +00:00
|
|
|
}
|
|
|
|
|
2006-11-06 13:42:10 +00:00
|
|
|
switch (uap->what) {
|
|
|
|
case KENV_SET:
|
|
|
|
error = priv_check(td, PRIV_KENV_SET);
|
2002-04-17 13:06:36 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2006-11-06 13:42:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KENV_UNSET:
|
|
|
|
error = priv_check(td, PRIV_KENV_UNSET);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
break;
|
2002-04-17 13:06:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:16:30 +00:00
|
|
|
name = malloc(KENV_MNAMELEN + 1, M_TEMP, M_WAITOK);
|
2002-04-17 13:06:36 +00:00
|
|
|
|
2012-08-14 19:16:30 +00:00
|
|
|
error = copyinstr(uap->name, name, KENV_MNAMELEN + 1, NULL);
|
2002-04-17 13:06:36 +00:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
|
2002-12-14 01:56:26 +00:00
|
|
|
switch (uap->what) {
|
2002-04-17 13:06:36 +00:00
|
|
|
case KENV_GET:
|
2002-11-01 20:46:53 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kenv_check_get(td->td_ucred, name);
|
2002-11-01 20:46:53 +00:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
#endif
|
2002-04-17 13:06:36 +00:00
|
|
|
value = getenv(name);
|
|
|
|
if (value == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
len = strlen(value) + 1;
|
2002-12-14 01:56:26 +00:00
|
|
|
if (len > uap->len)
|
|
|
|
len = uap->len;
|
|
|
|
error = copyout(value, uap->value, len);
|
2002-04-17 13:06:36 +00:00
|
|
|
freeenv(value);
|
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
td->td_retval[0] = len;
|
|
|
|
break;
|
|
|
|
case KENV_SET:
|
2002-12-14 01:56:26 +00:00
|
|
|
len = uap->len;
|
2002-04-17 13:06:36 +00:00
|
|
|
if (len < 1) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-08-14 19:16:30 +00:00
|
|
|
if (len > KENV_MVALLEN + 1)
|
|
|
|
len = KENV_MVALLEN + 1;
|
2003-02-19 05:47:46 +00:00
|
|
|
value = malloc(len, M_TEMP, M_WAITOK);
|
2002-12-14 01:56:26 +00:00
|
|
|
error = copyinstr(uap->value, value, len, NULL);
|
2002-04-17 13:06:36 +00:00
|
|
|
if (error) {
|
|
|
|
free(value, M_TEMP);
|
|
|
|
goto done;
|
|
|
|
}
|
2002-11-01 20:46:53 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kenv_check_set(td->td_ucred, name, value);
|
2002-11-01 20:46:53 +00:00
|
|
|
if (error == 0)
|
|
|
|
#endif
|
|
|
|
setenv(name, value);
|
2002-04-17 13:06:36 +00:00
|
|
|
free(value, M_TEMP);
|
|
|
|
break;
|
|
|
|
case KENV_UNSET:
|
2002-11-01 20:46:53 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kenv_check_unset(td->td_ucred, name);
|
2002-11-01 20:46:53 +00:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
#endif
|
2002-04-17 13:06:36 +00:00
|
|
|
error = unsetenv(name);
|
|
|
|
if (error)
|
|
|
|
error = ENOENT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
free(name, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2010-01-10 22:34:18 +00:00
|
|
|
void
|
|
|
|
init_static_kenv(char *buf, size_t len)
|
|
|
|
{
|
|
|
|
kern_envp = buf;
|
|
|
|
env_len = len;
|
|
|
|
env_pos = 0;
|
|
|
|
}
|
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
/*
|
|
|
|
* Setup the dynamic kernel environment.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_dynamic_kenv(void *data __unused)
|
|
|
|
{
|
|
|
|
char *cp;
|
2011-05-23 16:40:44 +00:00
|
|
|
size_t len;
|
|
|
|
int i;
|
2002-04-17 13:06:36 +00:00
|
|
|
|
2005-07-31 10:28:35 +00:00
|
|
|
kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
|
|
|
|
M_WAITOK | M_ZERO);
|
2002-04-17 13:06:36 +00:00
|
|
|
i = 0;
|
2013-04-16 22:09:08 +00:00
|
|
|
if (kern_envp && *kern_envp != '\0') {
|
2013-04-12 15:58:53 +00:00
|
|
|
for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
|
|
|
|
len = strlen(cp) + 1;
|
|
|
|
if (len > KENV_MNAMELEN + 1 + KENV_MVALLEN + 1) {
|
|
|
|
printf(
|
|
|
|
"WARNING: too long kenv string, ignoring %s\n",
|
|
|
|
cp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i < KENV_SIZE) {
|
|
|
|
kenvp[i] = malloc(len, M_KENV, M_WAITOK);
|
|
|
|
strcpy(kenvp[i++], cp);
|
|
|
|
} else
|
|
|
|
printf(
|
|
|
|
"WARNING: too many kenv strings, ignoring %s\n",
|
|
|
|
cp);
|
2011-05-23 16:40:44 +00:00
|
|
|
}
|
2002-04-17 13:06:36 +00:00
|
|
|
}
|
|
|
|
kenvp[i] = NULL;
|
|
|
|
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
|
2002-04-17 13:06:36 +00:00
|
|
|
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;
|
|
|
|
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_assert(&kenv_lock, MA_OWNED);
|
2002-04-17 13:06:36 +00:00
|
|
|
len = strlen(name);
|
|
|
|
for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
|
2005-09-26 14:14:55 +00:00
|
|
|
if ((strncmp(cp, name, len) == 0) &&
|
|
|
|
(cp[len] == '=')) {
|
2002-04-17 13:06:36 +00:00
|
|
|
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++)
|
|
|
|
;
|
2002-04-25 20:25:15 +00:00
|
|
|
if (*ep != '=')
|
|
|
|
continue;
|
2002-04-17 13:06:36 +00:00
|
|
|
len = ep - cp;
|
2002-04-25 20:25:15 +00:00
|
|
|
ep++;
|
|
|
|
if (!strncmp(name, cp, len) && name[len] == 0)
|
2002-04-17 13:06:36 +00:00
|
|
|
return (ep);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
1998-10-09 00:31:29 +00:00
|
|
|
|
2001-10-23 22:34:36 +00:00
|
|
|
/*
|
|
|
|
* Look up an environment variable by name.
|
2002-04-17 13:06:36 +00:00
|
|
|
* Return a pointer to the string if found.
|
|
|
|
* The pointer has to be freed with freeenv()
|
|
|
|
* after use.
|
2001-10-23 22:34:36 +00:00
|
|
|
*/
|
1998-10-09 00:31:29 +00:00
|
|
|
char *
|
2001-06-15 07:29:17 +00:00
|
|
|
getenv(const char *name)
|
1998-10-09 00:31:29 +00:00
|
|
|
{
|
2002-04-17 17:51:10 +00:00
|
|
|
char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
|
2013-11-01 10:32:33 +00:00
|
|
|
char *ret;
|
2002-04-17 13:06:36 +00:00
|
|
|
|
|
|
|
if (dynamic_kenv) {
|
2013-11-01 10:32:33 +00:00
|
|
|
if (getenv_string(name, buf, sizeof(buf))) {
|
|
|
|
ret = strdup(buf, M_KENV);
|
2002-04-17 17:51:10 +00:00
|
|
|
} else {
|
2002-04-17 13:06:36 +00:00
|
|
|
ret = NULL;
|
2011-05-07 17:59:07 +00:00
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
|
|
|
|
"getenv");
|
2002-04-17 17:51:10 +00:00
|
|
|
}
|
2002-04-17 13:06:36 +00:00
|
|
|
} else
|
|
|
|
ret = _getenv_static(name);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test if an environment variable is defined.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
testenv(const char *name)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (dynamic_kenv) {
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_lock(&kenv_lock);
|
2002-04-17 13:06:36 +00:00
|
|
|
cp = _getenv_dynamic(name, NULL);
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
2002-04-17 13:06:36 +00:00
|
|
|
} else
|
|
|
|
cp = _getenv_static(name);
|
|
|
|
if (cp != NULL)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-01-10 22:34:18 +00:00
|
|
|
static int
|
|
|
|
setenv_static(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (env_pos >= env_len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* Check space for x=y and two nuls */
|
|
|
|
len = strlen(name) + strlen(value);
|
|
|
|
if (len + 3 < env_len - env_pos) {
|
|
|
|
len = sprintf(&kern_envp[env_pos], "%s=%s", name, value);
|
|
|
|
env_pos += len+1;
|
|
|
|
kern_envp[env_pos] = '\0';
|
|
|
|
return (0);
|
|
|
|
} else
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
/*
|
|
|
|
* Set an environment variable by name.
|
|
|
|
*/
|
2002-04-17 17:51:10 +00:00
|
|
|
int
|
2002-04-17 13:06:36 +00:00
|
|
|
setenv(const char *name, const char *value)
|
|
|
|
{
|
2002-04-17 17:51:10 +00:00
|
|
|
char *buf, *cp, *oldenv;
|
|
|
|
int namelen, vallen, i;
|
2002-04-17 13:06:36 +00:00
|
|
|
|
2010-01-10 22:34:18 +00:00
|
|
|
if (dynamic_kenv == 0 && env_len > 0)
|
|
|
|
return (setenv_static(name, value));
|
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
KENV_CHECK;
|
|
|
|
|
2002-04-17 17:51:10 +00:00
|
|
|
namelen = strlen(name) + 1;
|
2012-08-14 19:16:30 +00:00
|
|
|
if (namelen > KENV_MNAMELEN + 1)
|
2002-04-17 17:51:10 +00:00
|
|
|
return (-1);
|
|
|
|
vallen = strlen(value) + 1;
|
2012-08-14 19:16:30 +00:00
|
|
|
if (vallen > KENV_MVALLEN + 1)
|
2002-04-17 17:51:10 +00:00
|
|
|
return (-1);
|
2003-02-19 05:47:46 +00:00
|
|
|
buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
|
2002-04-17 13:06:36 +00:00
|
|
|
sprintf(buf, "%s=%s", name, value);
|
|
|
|
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_lock(&kenv_lock);
|
2002-04-17 13:06:36 +00:00
|
|
|
cp = _getenv_dynamic(name, &i);
|
|
|
|
if (cp != NULL) {
|
2002-04-17 17:51:10 +00:00
|
|
|
oldenv = kenvp[i];
|
2002-04-17 13:06:36 +00:00
|
|
|
kenvp[i] = buf;
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
2002-04-17 17:51:10 +00:00
|
|
|
free(oldenv, M_KENV);
|
2002-04-17 13:06:36 +00:00
|
|
|
} else {
|
|
|
|
/* We add the option if it wasn't found */
|
|
|
|
for (i = 0; (cp = kenvp[i]) != NULL; i++)
|
|
|
|
;
|
2005-07-31 10:28:35 +00:00
|
|
|
|
|
|
|
/* Bounds checking */
|
|
|
|
if (i < 0 || i >= KENV_SIZE) {
|
|
|
|
free(buf, M_KENV);
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
2005-07-31 10:28:35 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2002-04-17 13:06:36 +00:00
|
|
|
kenvp[i] = buf;
|
|
|
|
kenvp[i + 1] = NULL;
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
2002-04-17 13:06:36 +00:00
|
|
|
}
|
2002-04-17 17:51:10 +00:00
|
|
|
return (0);
|
2002-04-17 13:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unset an environment variable string.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
unsetenv(const char *name)
|
|
|
|
{
|
2002-04-17 17:51:10 +00:00
|
|
|
char *cp, *oldenv;
|
2002-04-17 13:06:36 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
KENV_CHECK;
|
|
|
|
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_lock(&kenv_lock);
|
2002-04-17 13:06:36 +00:00
|
|
|
cp = _getenv_dynamic(name, &i);
|
|
|
|
if (cp != NULL) {
|
2002-04-17 17:51:10 +00:00
|
|
|
oldenv = kenvp[i];
|
2002-04-17 13:06:36 +00:00
|
|
|
for (j = i + 1; kenvp[j] != NULL; j++)
|
|
|
|
kenvp[i++] = kenvp[j];
|
|
|
|
kenvp[i] = NULL;
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
2002-04-17 17:51:10 +00:00
|
|
|
free(oldenv, M_KENV);
|
2002-04-17 13:06:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2006-07-09 21:42:58 +00:00
|
|
|
mtx_unlock(&kenv_lock);
|
2002-04-17 13:06:36 +00:00
|
|
|
return (-1);
|
1998-10-09 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
2001-10-23 22:34:36 +00:00
|
|
|
/*
|
|
|
|
* Return a string value from an environment variable.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getenv_string(const char *name, char *data, int size)
|
|
|
|
{
|
2013-11-01 10:32:33 +00:00
|
|
|
char *cp;
|
2001-10-23 22:34:36 +00:00
|
|
|
|
2013-11-01 10:32:33 +00:00
|
|
|
if (dynamic_kenv) {
|
|
|
|
mtx_lock(&kenv_lock);
|
|
|
|
cp = _getenv_dynamic(name, NULL);
|
|
|
|
if (cp != NULL)
|
|
|
|
strlcpy(data, cp, size);
|
|
|
|
mtx_unlock(&kenv_lock);
|
|
|
|
} else {
|
|
|
|
cp = _getenv_static(name);
|
|
|
|
if (cp != NULL)
|
|
|
|
strlcpy(data, cp, size);
|
|
|
|
}
|
|
|
|
return (cp != NULL);
|
2001-10-23 22:34:36 +00:00
|
|
|
}
|
|
|
|
|
1999-01-15 17:25:02 +00:00
|
|
|
/*
|
|
|
|
* Return an integer value from an environment variable.
|
|
|
|
*/
|
|
|
|
int
|
2001-06-15 07:29:17 +00:00
|
|
|
getenv_int(const char *name, int *data)
|
1999-11-07 01:10:30 +00:00
|
|
|
{
|
2002-05-01 02:51:50 +00:00
|
|
|
quad_t tmp;
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
rval = getenv_quad(name, &tmp);
|
|
|
|
if (rval)
|
|
|
|
*data = (int) tmp;
|
|
|
|
return (rval);
|
1999-11-07 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2007-10-13 11:30:19 +00:00
|
|
|
/*
|
|
|
|
* Return an unsigned integer value from an environment variable.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getenv_uint(const char *name, unsigned int *data)
|
|
|
|
{
|
|
|
|
quad_t tmp;
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
rval = getenv_quad(name, &tmp);
|
|
|
|
if (rval)
|
|
|
|
*data = (unsigned int) tmp;
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
2004-10-31 15:50:33 +00:00
|
|
|
/*
|
|
|
|
* Return a long value from an environment variable.
|
|
|
|
*/
|
2007-10-13 11:30:19 +00:00
|
|
|
int
|
2004-10-31 15:50:33 +00:00
|
|
|
getenv_long(const char *name, long *data)
|
|
|
|
{
|
|
|
|
quad_t tmp;
|
2007-10-13 11:30:19 +00:00
|
|
|
int rval;
|
2004-10-31 15:50:33 +00:00
|
|
|
|
|
|
|
rval = getenv_quad(name, &tmp);
|
|
|
|
if (rval)
|
|
|
|
*data = (long) tmp;
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an unsigned long value from an environment variable.
|
|
|
|
*/
|
2007-10-13 11:30:19 +00:00
|
|
|
int
|
2004-10-31 15:50:33 +00:00
|
|
|
getenv_ulong(const char *name, unsigned long *data)
|
|
|
|
{
|
|
|
|
quad_t tmp;
|
2007-10-13 11:30:19 +00:00
|
|
|
int rval;
|
2004-10-31 15:50:33 +00:00
|
|
|
|
|
|
|
rval = getenv_quad(name, &tmp);
|
|
|
|
if (rval)
|
|
|
|
*data = (unsigned long) tmp;
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
1999-11-07 01:10:30 +00:00
|
|
|
/*
|
|
|
|
* Return a quad_t value from an environment variable.
|
|
|
|
*/
|
2001-10-23 22:34:36 +00:00
|
|
|
int
|
2001-06-15 07:29:17 +00:00
|
|
|
getenv_quad(const char *name, quad_t *data)
|
1999-01-15 17:25:02 +00:00
|
|
|
{
|
2013-11-01 10:32:33 +00:00
|
|
|
char value[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
|
2002-05-01 02:51:50 +00:00
|
|
|
char *vtp;
|
|
|
|
quad_t iv;
|
|
|
|
|
2013-11-01 10:32:33 +00:00
|
|
|
if (!getenv_string(name, value, sizeof(value)))
|
2002-05-01 02:51:50 +00:00
|
|
|
return (0);
|
|
|
|
iv = strtoq(value, &vtp, 0);
|
2013-11-01 10:32:33 +00:00
|
|
|
if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0'))
|
2005-03-05 15:52:12 +00:00
|
|
|
return (0);
|
|
|
|
switch (vtp[0]) {
|
|
|
|
case 't': case 'T':
|
|
|
|
iv *= 1024;
|
|
|
|
case 'g': case 'G':
|
|
|
|
iv *= 1024;
|
|
|
|
case 'm': case 'M':
|
|
|
|
iv *= 1024;
|
|
|
|
case 'k': case 'K':
|
|
|
|
iv *= 1024;
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
default:
|
2002-05-01 02:51:50 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
*data = iv;
|
|
|
|
return (1);
|
1999-01-15 17:25:02 +00:00
|
|
|
}
|
1998-10-09 00:31:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the next entry after the one which (cp) falls within, return a
|
|
|
|
* pointer to its start or NULL if there are no more.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
kernenv_next(char *cp)
|
|
|
|
{
|
2002-05-01 02:51:50 +00:00
|
|
|
|
|
|
|
if (cp != NULL) {
|
|
|
|
while (*cp != 0)
|
|
|
|
cp++;
|
|
|
|
cp++;
|
|
|
|
if (*cp == 0)
|
|
|
|
cp = NULL;
|
|
|
|
}
|
|
|
|
return (cp);
|
1998-10-09 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
2001-06-08 05:24:21 +00:00
|
|
|
void
|
|
|
|
tunable_int_init(void *data)
|
|
|
|
{
|
|
|
|
struct tunable_int *d = (struct tunable_int *)data;
|
|
|
|
|
|
|
|
TUNABLE_INT_FETCH(d->path, d->var);
|
|
|
|
}
|
|
|
|
|
2004-10-31 15:50:33 +00:00
|
|
|
void
|
|
|
|
tunable_long_init(void *data)
|
|
|
|
{
|
|
|
|
struct tunable_long *d = (struct tunable_long *)data;
|
|
|
|
|
|
|
|
TUNABLE_LONG_FETCH(d->path, d->var);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tunable_ulong_init(void *data)
|
|
|
|
{
|
|
|
|
struct tunable_ulong *d = (struct tunable_ulong *)data;
|
|
|
|
|
|
|
|
TUNABLE_ULONG_FETCH(d->path, d->var);
|
|
|
|
}
|
|
|
|
|
2008-07-21 15:05:25 +00:00
|
|
|
void
|
|
|
|
tunable_quad_init(void *data)
|
|
|
|
{
|
|
|
|
struct tunable_quad *d = (struct tunable_quad *)data;
|
|
|
|
|
|
|
|
TUNABLE_QUAD_FETCH(d->path, d->var);
|
|
|
|
}
|
|
|
|
|
2001-06-08 05:24:21 +00:00
|
|
|
void
|
|
|
|
tunable_str_init(void *data)
|
|
|
|
{
|
|
|
|
struct tunable_str *d = (struct tunable_str *)data;
|
|
|
|
|
|
|
|
TUNABLE_STR_FETCH(d->path, d->var, d->size);
|
|
|
|
}
|