Procházet zdrojové kódy

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

 o "apply" callback function to check whether a variable can be
    overwritten, and possibly immediately apply the changes;
    when NULL, no check is performed.

And a new argument to himport_r():
 o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>
Gerlando Falauto před 12 roky
rodič
revize
c5983592e9
5 změnil soubory, kde provedl 48 přidání a 6 odebrání
  1. 2 1
      common/cmd_nvedit.c
  2. 5 3
      common/env_common.c
  3. 9 0
      include/environment.h
  4. 13 1
      include/search.h
  5. 19 1
      lib/hashtable.c

+ 2 - 1
common/cmd_nvedit.c

@@ -202,6 +202,7 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
  * environment variable, then (if successful) apply the changes to internals so
  * environment variable, then (if successful) apply the changes to internals so
  * to make them effective.  Code for this function was taken out of
  * to make them effective.  Code for this function was taken out of
  * _do_env_set(), which now calls it instead.
  * _do_env_set(), which now calls it instead.
+ * Also called as a callback function by himport_r().
  * Returns 0 in case of success, 1 in case of failure.
  * Returns 0 in case of success, 1 in case of failure.
  * When (flag & H_FORCE) is set, do not print out any error message and force
  * When (flag & H_FORCE) is set, do not print out any error message and force
  * overwriting of write-once variables.
  * overwriting of write-once variables.
@@ -915,7 +916,7 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
 	}
 	}
 
 
 	if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
 	if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
-			0, NULL) == 0) {
+			0, NULL, 0 /* do_apply */) == 0) {
 		error("Environment import failed: errno = %d\n", errno);
 		error("Environment import failed: errno = %d\n", errno);
 		return 1;
 		return 1;
 	}
 	}

+ 5 - 3
common/env_common.c

@@ -136,7 +136,9 @@ const uchar default_environment[] = {
 	"\0"
 	"\0"
 };
 };
 
 
-struct hsearch_data env_htab;
+struct hsearch_data env_htab = {
+	.apply = env_check_apply,
+};
 
 
 static uchar __env_get_char_spec(int index)
 static uchar __env_get_char_spec(int index)
 {
 {
@@ -197,7 +199,7 @@ void set_default_env(const char *s)
 
 
 	if (himport_r(&env_htab, (char *)default_environment,
 	if (himport_r(&env_htab, (char *)default_environment,
 			sizeof(default_environment), '\0', 0,
 			sizeof(default_environment), '\0', 0,
-			0, NULL) == 0)
+			0, NULL, 0 /* do_apply */) == 0)
 		error("Environment import failed: errno = %d\n", errno);
 		error("Environment import failed: errno = %d\n", errno);
 
 
 	gd->flags |= GD_FLG_ENV_READY;
 	gd->flags |= GD_FLG_ENV_READY;
@@ -223,7 +225,7 @@ int env_import(const char *buf, int check)
 	}
 	}
 
 
 	if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
 	if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
-			0, NULL)) {
+			0, NULL, 0 /* do_apply */)) {
 		gd->flags |= GD_FLG_ENV_READY;
 		gd->flags |= GD_FLG_ENV_READY;
 		return 1;
 		return 1;
 	}
 	}

+ 9 - 0
include/environment.h

@@ -184,6 +184,15 @@ void set_default_env(const char *s);
 /* Import from binary representation into hash table */
 /* Import from binary representation into hash table */
 int env_import(const char *buf, int check);
 int env_import(const char *buf, int check);
 
 
+/*
+ * Check if variable "name" can be changed from oldval to newval,
+ * and if so, apply the changes (e.g. baudrate).
+ * When (flag & H_FORCE) is set, it does not print out any error
+ * message and forces overwriting of write-once variables.
+ */
+int env_check_apply(const char *name, const char *oldval,
+			const char *newval, int flag);
+
 #endif /* DO_DEPS_ONLY */
 #endif /* DO_DEPS_ONLY */
 
 
 #endif /* _ENVIRONMENT_H_ */
 #endif /* _ENVIRONMENT_H_ */

+ 13 - 1
include/search.h

@@ -57,6 +57,16 @@ struct hsearch_data {
 	struct _ENTRY *table;
 	struct _ENTRY *table;
 	unsigned int size;
 	unsigned int size;
 	unsigned int filled;
 	unsigned int filled;
+/*
+ * Callback function which will check whether the given change for variable
+ * "name" from "oldval" to "newval" may be applied or not, and possibly apply
+ * such change.
+ * When (flag & H_FORCE) is set, it shall not print out any error message and
+ * shall force overwriting of write-once variables.
+.* Must return 0 for approval, 1 for denial.
+ */
+	int (*apply)(const char *name, const char *oldval,
+			const char *newval, int flag);
 };
 };
 
 
 /* Create a new hashing table which will at most contain NEL elements.  */
 /* Create a new hashing table which will at most contain NEL elements.  */
@@ -97,10 +107,12 @@ extern ssize_t hexport_r(struct hsearch_data *__htab,
 /*
 /*
  * nvars: length of vars array
  * nvars: length of vars array
  * vars: array of strings (variable names) to import (nvars == 0 means all)
  * vars: array of strings (variable names) to import (nvars == 0 means all)
+ * do_apply: whether to call callback function to check the new argument,
+ * and possibly apply changes (false means accept everything)
  */
  */
 extern int himport_r(struct hsearch_data *__htab,
 extern int himport_r(struct hsearch_data *__htab,
 		     const char *__env, size_t __size, const char __sep,
 		     const char *__env, size_t __size, const char __sep,
-		     int __flag, int nvars, char * const vars[]);
+		     int __flag, int nvars, char * const vars[], int do_apply);
 
 
 /* Flags for himport_r() */
 /* Flags for himport_r() */
 #define	H_NOCLEAR	(1 << 0) /* do not clear hash table before importing */
 #define	H_NOCLEAR	(1 << 0) /* do not clear hash table before importing */

+ 19 - 1
lib/hashtable.c

@@ -658,7 +658,7 @@ static int is_var_in_set(const char *name, int nvars, char * const vars[])
 
 
 int himport_r(struct hsearch_data *htab,
 int himport_r(struct hsearch_data *htab,
 		const char *env, size_t size, const char sep, int flag,
 		const char *env, size_t size, const char sep, int flag,
-		int nvars, char * const vars[])
+		int nvars, char * const vars[], int do_apply)
 {
 {
 	char *data, *sp, *dp, *name, *value;
 	char *data, *sp, *dp, *name, *value;
 
 
@@ -772,6 +772,24 @@ int himport_r(struct hsearch_data *htab,
 		e.key = name;
 		e.key = name;
 		e.data = value;
 		e.data = value;
 
 
+		/* if there is an apply function, check what it has to say */
+		if (do_apply && htab->apply != NULL) {
+			debug("searching before calling cb function"
+				" for  %s\n", name);
+			/*
+			 * Search for variable in existing env, so to pass
+			 * its previous value to the apply callback
+			 */
+			hsearch_r(e, FIND, &rv, htab);
+			debug("previous value was %s\n", rv ? rv->data : "");
+			if (htab->apply(name, rv ? rv->data : NULL,
+				value, flag)) {
+				debug("callback function refused to set"
+					" variable %s, skipping it!\n", name);
+				continue;
+			}
+		}
+
 		hsearch_r(e, ENTER, &rv, htab);
 		hsearch_r(e, ENTER, &rv, htab);
 		if (rv == NULL) {
 		if (rv == NULL) {
 			printf("himport_r: can't insert \"%s=%s\" into hash table\n",
 			printf("himport_r: can't insert \"%s=%s\" into hash table\n",