Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 16 Jul 2018 14:38:57 +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: r336343 - stable/11/usr.sbin/config
Message-ID:  <201807161438.w6GEcv4D037248@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Mon Jul 16 14:38:57 2018
New Revision: 336343
URL: https://svnweb.freebsd.org/changeset/base/336343

Log:
  MFC r335652-r335654
  
  r335652:
  config(8): Make 'env' files consistent with other file-accepting options
  
  Previously, only one 'env' file could be specified. Later 'env' directives
  would overwrite earlier 'env' directives. This is inconsistent with every
  other file-accepting directives which process files in order, including
  hints.
  
  A caveat applies to both hints and env that isn't mentioned: they're
  concatenated in the order of appearance, so they're not actually applied in
  the way one might think by supplying:
  
  hints x
  hints y
  
  Hints in x will take precedence over same-name hints in y due to how
  the kernel processes them, stopping at the first line that matches the hint
  we're searching for. Future work will flip the order of concatenation so
  that later files may still properly override earlier files.
  
  In practice, this likely doesn't matter at all due to the nature of the
  beast.
  
  r335653:
  config(8): Flip the order of concatenation for `hints` and `env`
  
  As previously noted, kernel's processing of these means that the first
  appearance of a hint/variable wins. Flipping the order of concatenation
  means that later variables override earlier variables, as expected when one
  does:
  
  hints x
  hints y
  
  Where perhaps x is:
  
  hint.aw_sid.0.disable=1
  
  and y is:
  
  hint.aw_sid.0.disable=0
  
  The expectation would be that a later appearing variable would override an
  earlier appearing variable, such as with `device`/`nodevice`, device.hints,
  and other similarly structured data files.
  
  r335654:
  config(8): part of patch disappeared, don't close ifp at the end

Modified:
  stable/11/usr.sbin/config/config.5
  stable/11/usr.sbin/config/config.h
  stable/11/usr.sbin/config/config.y
  stable/11/usr.sbin/config/mkmakefile.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.sbin/config/config.5
==============================================================================
--- stable/11/usr.sbin/config/config.5	Mon Jul 16 14:34:25 2018	(r336342)
+++ stable/11/usr.sbin/config/config.5	Mon Jul 16 14:38:57 2018	(r336343)
@@ -23,7 +23,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd June 22, 2018
+.Dd June 26, 2018
 .Dt CONFIG 5
 .Os
 .Sh NAME
@@ -124,6 +124,19 @@ the compiled-in environment instead, unless the boot e
 This directive is useful for setting kernel tunables in
 embedded environments that do not start from
 .Xr loader 8 .
+.Pp
+All
+.Ic env
+and
+.Ic envvar
+directives will be processed and added to the static environment in reversed
+order of appearance so that later specified variables properly override earlier
+specified variables.
+Note that within
+.Ar filename ,
+the first appearance of a given variable will be the first one seen by the
+kernel, effectively shadowing any later appearances of the same variable within
+.Ar filename .
 .\" -------- ENVVAR --------
 .Pp
 .It Ic envvar Ar setting
@@ -133,11 +146,14 @@ compiled-in environment.
 must be of the form
 .Dq Va name=value .
 Optional quotes are supported in both name and value.
-All environment variables specified with
-.Ic envvar
-will be set after any
+.Pp
+All
 .Ic env
-files are included.
+and
+.Ic envvar
+directives will be processed and added to the static environment in reversed
+order of appearance so that later specified variables properly override earlier
+specified variables.
 .\" -------- FILES --------
 .Pp
 .It Ic files Ar filename
@@ -164,7 +180,9 @@ The file
 must conform to the syntax specified by
 .Xr device.hints 5 .
 Multiple hints lines are allowed.
-The resulting hints will be the files concatenated in the order of appearance.
+The resulting hints will be the files concatenated in reverse order of
+appearance so that hints in later files properly override hints in earlier
+files.
 .\" -------- IDENT --------
 .Pp
 .It Ic ident Ar name

Modified: stable/11/usr.sbin/config/config.h
==============================================================================
--- stable/11/usr.sbin/config/config.h	Mon Jul 16 14:34:25 2018	(r336342)
+++ stable/11/usr.sbin/config/config.h	Mon Jul 16 14:38:57 2018	(r336343)
@@ -35,6 +35,7 @@
  */
 #include <sys/types.h>
 #include <sys/queue.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -139,6 +140,7 @@ SLIST_HEAD(, opt_list) otab;
 
 struct envvar {
 	char	*env_str;
+	bool	env_is_file;
 	STAILQ_ENTRY(envvar) envvar_next;
 };
 
@@ -172,7 +174,6 @@ SLIST_HEAD(, includepath) includepath;
 #define OPT_AUTOGEN	"CONFIG_AUTOGENERATED"
 
 extern char	*ident;
-extern char	*env;
 extern char	kernconfstr[];
 extern int	do_trace;
 extern int	envmode;

Modified: stable/11/usr.sbin/config/config.y
==============================================================================
--- stable/11/usr.sbin/config/config.y	Mon Jul 16 14:34:25 2018	(r336342)
+++ stable/11/usr.sbin/config/config.y	Mon Jul 16 14:38:57 2018	(r336343)
@@ -80,7 +80,6 @@
 
 struct	device_head dtab;
 char	*ident;
-char	*env;
 int	envmode;
 int	hintmode;
 int	yyline;
@@ -97,6 +96,7 @@ int yywrap(void);
 
 static void newdev(char *name);
 static void newfile(char *name);
+static void newenvvar(char *name, bool is_file);
 static void rmdev_schedule(struct device_head *dh, char *name);
 static void newopt(struct opt_head *list, char *name, char *value, int append);
 static void rmopt_schedule(struct opt_head *list, char *name);
@@ -189,20 +189,8 @@ Config_spec:
 		|
 	MAXUSERS NUMBER { maxusers = $2; } |
 	PROFILE NUMBER { profiling = $2; } |
-	ENV ID {
-		env = $2;
-		envmode = 1;
-		} |
-	ENVVAR ENVLINE {
-		struct envvar *envvar;
-
-		envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
-		if (envvar == NULL)
-			err(EXIT_FAILURE, "calloc");
-		envvar->env_str = $2;
-		STAILQ_INSERT_TAIL(&envvars, envvar, envvar_next);
-		envmode = 1;
-	        } |
+	ENV ID { newenvvar($2, true); } |
+	ENVVAR ENVLINE { newenvvar($2, false); } |
 	HINTS ID {
 		struct hint *hint;
 
@@ -210,7 +198,7 @@ Config_spec:
 		if (hint == NULL)
 			err(EXIT_FAILURE, "calloc");	
 		hint->hint_name = $2;
-		STAILQ_INSERT_TAIL(&hints, hint, hint_next);
+		STAILQ_INSERT_HEAD(&hints, hint, hint_next);
 		hintmode = 1;
 	        }
 
@@ -359,7 +347,21 @@ newfile(char *name)
 	nl->f_name = name;
 	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
 }
-	
+
+static void
+newenvvar(char *name, bool is_file)
+{
+	struct envvar *envvar;
+
+	envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
+	if (envvar == NULL)
+		err(EXIT_FAILURE, "calloc");
+	envvar->env_str = name;
+	envvar->env_is_file = is_file;
+	STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next);
+	envmode = 1;
+}
+
 /*
  * Find a device in the list of devices.
  */

Modified: stable/11/usr.sbin/config/mkmakefile.c
==============================================================================
--- stable/11/usr.sbin/config/mkmakefile.c	Mon Jul 16 14:34:25 2018	(r336342)
+++ stable/11/usr.sbin/config/mkmakefile.c	Mon Jul 16 14:38:57 2018	(r336343)
@@ -304,13 +304,6 @@ makeenv(void)
 	char line[BUFSIZ], result[BUFSIZ], *linep;
 	struct envvar *envvar;
 
-	if (env) {
-		ifp = fopen(env, "r");
-		if (ifp == NULL)
-			err(1, "%s", env);
-	} else {
-		ifp = NULL;
-	}
 	ofp = fopen(path("env.c.new"), "w");
 	if (ofp == NULL)
 		err(1, "%s", path("env.c.new"));
@@ -319,25 +312,28 @@ makeenv(void)
 	fprintf(ofp, "\n");
 	fprintf(ofp, "int envmode = %d;\n", envmode);
 	fprintf(ofp, "char static_env[] = {\n");
-	if (ifp) {
-		while (fgets(line, BUFSIZ, ifp) != NULL) {
-			sanitize_envline(result, line);
-			/* anything left? */
+	STAILQ_FOREACH(envvar, &envvars, envvar_next) {
+		if (envvar->env_is_file) {
+			ifp = fopen(envvar->env_str, "r");
+			if (ifp == NULL)
+				err(1, "%s", envvar->env_str);
+			while (fgets(line, BUFSIZ, ifp) != NULL) {
+				sanitize_envline(result, line);
+				/* anything left? */
+				if (*result == '\0')
+					continue;
+				fprintf(ofp, "\"%s\\0\"\n", result);
+			}
+			fclose(ifp);
+		} else {
+			linep = envvar->env_str;
+			sanitize_envline(result, linep);
 			if (*result == '\0')
 				continue;
 			fprintf(ofp, "\"%s\\0\"\n", result);
 		}
 	}
-	STAILQ_FOREACH(envvar, &envvars, envvar_next) {
-		linep = envvar->env_str;
-		sanitize_envline(result, linep);
-		if (*result == '\0')
-			continue;
-		fprintf(ofp, "\"%s\\0\"\n", result);
-	}
 	fprintf(ofp, "\"\\0\"\n};\n");
-	if (ifp)
-		fclose(ifp);
 	fclose(ofp);
 	moveifchanged(path("env.c.new"), path("env.c"));
 }



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