diff options
author | mux <mux@FreeBSD.org> | 2002-04-17 13:06:36 +0000 |
---|---|---|
committer | mux <mux@FreeBSD.org> | 2002-04-17 13:06:36 +0000 |
commit | a207e41bef089b8849a230f44088a562d0b5d19f (patch) | |
tree | cb447f9f2a04a764ece9fcefc6fa6b52d19870b8 /sys/kern | |
parent | c79270302c4767b589a4fe70da3ba9866936358f (diff) | |
download | FreeBSD-src-a207e41bef089b8849a230f44088a562d0b5d19f.zip FreeBSD-src-a207e41bef089b8849a230f44088a562d0b5d19f.tar.gz |
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
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/init_main.c | 5 | ||||
-rw-r--r-- | sys/kern/init_sysent.c | 1 | ||||
-rw-r--r-- | sys/kern/kern_environment.c | 367 | ||||
-rw-r--r-- | sys/kern/subr_hints.c | 72 | ||||
-rw-r--r-- | sys/kern/syscalls.c | 1 | ||||
-rw-r--r-- | sys/kern/vfs_conf.c | 11 | ||||
-rw-r--r-- | sys/kern/vfs_mount.c | 11 |
7 files changed, 384 insertions, 84 deletions
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 4f0aa45..6904392 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -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 == ':') diff --git a/sys/kern/init_sysent.c b/sys/kern/init_sysent.c index ead3758..b212aaf 100644 --- a/sys/kern/init_sysent.c +++ b/sys/kern/init_sysent.c @@ -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 */ }; diff --git a/sys/kern/kern_environment.c b/sys/kern/kern_environment.c index 6ebc017..f507df2 100644 --- a/sys/kern/kern_environment.c +++ b/sys/kern/kern_environment.c @@ -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,50 +396,17 @@ 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. */ diff --git a/sys/kern/subr_hints.c b/sys/kern/subr_hints.c index 3ca9d5b..ddcba4a 100644 --- a/sys/kern/subr_hints.c +++ b/sys/kern/subr_hints.c @@ -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; diff --git a/sys/kern/syscalls.c b/sys/kern/syscalls.c index a7d4102..34a1382 100644 --- a/sys/kern/syscalls.c +++ b/sys/kern/syscalls.c @@ -397,4 +397,5 @@ char *syscallnames[] = { "#387", /* 387 = __mac_get_file */ "#388", /* 388 = __mac_set_fd */ "#389", /* 389 = __mac_set_file */ + "kenv", /* 390 = kenv */ }; diff --git a/sys/kern/vfs_conf.c b/sys/kern/vfs_conf.c index 2e5360a5..20d9b90 100644 --- a/sys/kern/vfs_conf.c +++ b/sys/kern/vfs_conf.c @@ -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 diff --git a/sys/kern/vfs_mount.c b/sys/kern/vfs_mount.c index 2e5360a5..20d9b90 100644 --- a/sys/kern/vfs_mount.c +++ b/sys/kern/vfs_mount.c @@ -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 |