Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Dec 2018 19:18:52 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r341644 - stable/11/sys/kern
Message-ID:  <201812061918.wB6JIq8P010552@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Thu Dec  6 19:18:51 2018
New Revision: 341644
URL: https://svnweb.freebsd.org/changeset/base/341644

Log:
  Fix kenv handling in stable/11 following r337333
  
  The aforementioned commit merged revised static_env/static_hint handling to
  allow static_env and loader env to coexist with the variable
  loader_env.disabled=0. init_static_kenv had been rewritten slighly in an
  attempt to maintain historical behavior: the static environment and loader
  environment are mutually exclusive, unless the latter disables the former.
  
  The rewritten version botched this by only setting up the loader environment
  if the static environment was empty or if the loader environment was
  specifically enabled. It was never given a chance to disable the static
  environment, so the default behavior was broken unless the loader
  environment was specifically enabled by the static environment.
  
  Rewrite this again to do the right thing:
  - Setup the static environment and check loader_env.disabled; if it's
    explicitly enabled, we're done.
  - Check static_{env,hints}.disabled and "empty out" the respective
    environments as needed
  - Finally, check: if the static environment is not empty and we've not
    explicitly re-enabled the static environment with loader_env.disabled=0,
    we tear the loader environment (which was setup to 'keep things simple')
    down again.
  
  Future commits to head (and subsequently MFC'd) will likely zero these
  environments out if they're disabled since this normally happens when
  they're merged into the dynamic environment.
  
  This is a direct commit to stable/11 because this particular bug does not
  apply to head.
  
  Fixes:		r337333
  Reported by:	bde

Modified:
  stable/11/sys/kern/kern_environment.c

Modified: stable/11/sys/kern/kern_environment.c
==============================================================================
--- stable/11/sys/kern/kern_environment.c	Thu Dec  6 18:59:33 2018	(r341643)
+++ stable/11/sys/kern/kern_environment.c	Thu Dec  6 19:18:51 2018	(r341644)
@@ -245,7 +245,7 @@ done:
 void
 init_static_kenv(char *buf, size_t len)
 {
-	char *eval;
+	char *eval, *loader_eval;
 
 	KASSERT(!dynamic_kenv, ("kenv: dynamic_kenv already initialized"));
 	/*
@@ -264,21 +264,43 @@ init_static_kenv(char *buf, size_t len)
 	 *
 	 * As a warning, the static environment may not be disabled in any way
 	 * if the static environment has disabled the loader environment.
+	 *
+	 * We're setting up the static environment early here because it will
+	 * either be used or empty.
 	 */
 	kern_envp = static_env;
-	eval = kern_getenv("loader_env.disabled");
-	if (*kern_envp == '\0' || (eval != NULL && strcmp(eval, "0") == 0)) {
-		md_envp = buf;
-		md_env_len = len;
-		md_env_pos = 0;
+	loader_eval = kern_getenv("loader_env.disabled");
+	if (loader_eval != NULL && strcmp(loader_eval, "1") == 0)
+		/* Bail out early, the loader environment is disabled. */
+		return;
 
-		eval = kern_getenv("static_env.disabled");
-		if (eval != NULL && strcmp(eval, "1") == 0)
-			*kern_envp = '\0';
-	}
+	/*
+	 * Next, the loader env is checked for the status of the static env.  We
+	 * are allowing static_env and static_hints to disable themselves here for
+	 * the sake of simplicity.
+	 */
+	md_envp = buf;
+	md_env_len = len;
+	md_env_pos = 0;
+
+	eval = kern_getenv("static_env.disabled");
+	if (eval != NULL && strcmp(eval, "1") == 0)
+		*static_env = '\0';
+
 	eval = kern_getenv("static_hints.disabled");
 	if (eval != NULL && strcmp(eval, "1") == 0)
 		*static_hints = '\0';
+
+	/*
+	 * Now we see if we need to tear the loader environment back down due
+	 * to the presence of a non-empty static environment and lack of request
+	 * to keep it enabled.
+	 */
+	if (*static_env != '\0' &&
+	    (loader_eval == NULL || strcmp(loader_eval, "0") != 0)) {
+		md_envp = NULL;
+		md_env_len = 0;
+	}
 }
 
 static void



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201812061918.wB6JIq8P010552>