Changeset View
Standalone View
sys/kern/kern_environment.c
Show First 20 Lines • Show All 353 Lines • ▼ Show 20 Lines | if (!getenv_is_true("loader_env.disabled")) { | ||||
} | } | ||||
} | } | ||||
if (getenv_is_true("static_hints.disabled")) { | if (getenv_is_true("static_hints.disabled")) { | ||||
static_hints[0] = '\0'; | static_hints[0] = '\0'; | ||||
static_hints[1] = '\0'; | static_hints[1] = '\0'; | ||||
} | } | ||||
} | } | ||||
/* Maximum suffix number appended for duplicate environment variable names. */ | |||||
#define MAXSUFFIX 9999 | |||||
#define SUFFIXLEN strlen("_" __XSTRING(MAXSUFFIX)) | |||||
static void | static void | ||||
getfreesuffix(char *cp, size_t *n) | |||||
{ | |||||
size_t len = strlen(cp); | |||||
char * ncp; | |||||
kevans: I'd maybe like to see `9999` spelled here and below as a `#define` (perhaps just thrown right… | |||||
ncp = malloc(len + SUFFIXLEN + 1, M_KENV, M_WAITOK); | |||||
memcpy(ncp, cp, len); | |||||
for (*n = 1; *n <= MAXSUFFIX; (*n)++) { | |||||
sprintf(&ncp[len], "_%zu", *n); | |||||
if (!_getenv_dynamic_locked(ncp, NULL)) | |||||
break; | |||||
} | |||||
free(ncp, M_KENV); | |||||
if (*n > MAXSUFFIX) | |||||
panic("Too many duplicate kernel environment values: %s", cp); | |||||
} | |||||
static void | |||||
init_dynamic_kenv_from(char *init_env, int *curpos) | init_dynamic_kenv_from(char *init_env, int *curpos) | ||||
{ | { | ||||
char *cp, *cpnext, *eqpos, *found; | char *cp, *cpnext, *eqpos, *found; | ||||
size_t len; | size_t len, n; | ||||
int i; | int i; | ||||
if (init_env && *init_env != '\0') { | if (init_env && *init_env != '\0') { | ||||
found = NULL; | found = NULL; | ||||
i = *curpos; | i = *curpos; | ||||
for (cp = init_env; cp != NULL; cp = cpnext) { | for (cp = init_env; cp != NULL; cp = cpnext) { | ||||
cpnext = kernenv_next(cp); | cpnext = kernenv_next(cp); | ||||
len = strlen(cp) + 1; | len = strlen(cp) + 1; | ||||
if (i > KENV_SIZE) { | |||||
printf( | |||||
"WARNING: too many kenv strings, ignoring %s\n", | |||||
cp); | |||||
goto sanitize; | |||||
} | |||||
if (len > KENV_MNAMELEN + 1 + kenv_mvallen + 1) { | if (len > KENV_MNAMELEN + 1 + kenv_mvallen + 1) { | ||||
printf( | printf( | ||||
"WARNING: too long kenv string, ignoring %s\n", | "WARNING: too long kenv string, ignoring %s\n", | ||||
cp); | cp); | ||||
goto sanitize; | goto sanitize; | ||||
} | } | ||||
eqpos = strchr(cp, '='); | eqpos = strchr(cp, '='); | ||||
if (eqpos == NULL) { | if (eqpos == NULL) { | ||||
printf( | printf( | ||||
"WARNING: malformed static env value, ignoring %s\n", | "WARNING: malformed static env value, ignoring %s\n", | ||||
cp); | cp); | ||||
goto sanitize; | goto sanitize; | ||||
} | } | ||||
*eqpos = 0; | *eqpos = 0; | ||||
/* | /* | ||||
* De-dupe the environment as we go. We don't add the | * Handle duplicates in the environment as we go; we | ||||
* duplicated assignments because config(8) will flip | * add the duplicated assignments with _N suffixes. | ||||
* the order of the static environment around to make | * This ensures that (a) if a variable is set in the | ||||
* kernel processing match the order of specification | * static environment and in the "loader" environment | ||||
Not Done Inline Actionss/environement/environment/? imp: s/environement/environment/? | |||||
* in the kernel config. | * provided by MD code, the value from the loader will | ||||
* have the expected variable name and the value from | |||||
* the static environment will have the suffix; and (b) | |||||
* if the "loader" environment has the same variable | |||||
* set multiple times (as is possible with values being | |||||
* passed via the kernel "command line") the extra | |||||
* values are visible to code which knows where to look | |||||
* for them. | |||||
Not Done Inline ActionsThis comment doesn't match what the code does now, I don't think... imp:
This comment doesn't match what the code does now, I don't think... | |||||
*/ | */ | ||||
found = _getenv_dynamic_locked(cp, NULL); | found = _getenv_dynamic_locked(cp, NULL); | ||||
*eqpos = '='; | if (found != NULL) { | ||||
if (found != NULL) | getfreesuffix(cp, &n); | ||||
goto sanitize; | kenvp[i] = malloc(len + SUFFIXLEN, | ||||
if (i > KENV_SIZE) { | M_KENV, M_WAITOK); | ||||
printf( | sprintf(kenvp[i++], "%s_%zu=%s", cp, n, | ||||
"WARNING: too many kenv strings, ignoring %s\n", | &eqpos[1]); | ||||
cp); | } else { | ||||
goto sanitize; | |||||
} | |||||
kenvp[i] = malloc(len, M_KENV, M_WAITOK); | kenvp[i] = malloc(len, M_KENV, M_WAITOK); | ||||
*eqpos = '='; | |||||
strcpy(kenvp[i++], cp); | strcpy(kenvp[i++], cp); | ||||
Not Done Inline ActionsSo how do we replace env vars that were in the loader env with ones in the kernenv? imp: So how do we replace env vars that were in the loader env with ones in the kernenv?
This seems… | |||||
Not Done Inline ActionsMost kenv manipulation will still work like you'd expect, since this is a one-time thing after kmem comes online to create the dynamic environment. It does break overriding static env vars with loader env vars, but I'm not convinced anyone else actually takes advantage of those being able to co-exist since I made it work... and I don't specify anything in the static env that loader may care about. kevans: Most kenv manipulation will still work like you'd expect, since this is a one-time thing after… | |||||
Not Done Inline ActionsOh, no, it doesn't really break that, either. We process them in reverse order so that loader env does override static env, so static env just unintuitively gets the indexed names. kevans: Oh, no, it doesn't really break that, either. We process them in reverse order so that loader… | |||||
} | |||||
sanitize: | sanitize: | ||||
#ifdef PRESERVE_EARLY_KENV | #ifdef PRESERVE_EARLY_KENV | ||||
continue; | continue; | ||||
#else | #else | ||||
explicit_bzero(cp, len - 1); | explicit_bzero(cp, len - 1); | ||||
#endif | #endif | ||||
} | } | ||||
*curpos = i; | *curpos = i; | ||||
▲ Show 20 Lines • Show All 710 Lines • Show Last 20 Lines |
I'd maybe like to see 9999 spelled here and below as a #define (perhaps just thrown right above that allocation so it draws attention to the 4 that's derived from the limit if someone gets a wild hair and finds a reason to change it)