Blame SOURCES/autofs-5.0.7-setup-program-map-env-from-macro-table.patch

306fa1
autofs-5.0.7 - setup program map env from macro table
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
The ability to pass parameters to program maps, in some way, is needed.
306fa1
Standard autofs specifies that program maps have one argument so passing
306fa1
parameters as arguments shouldn't be done.
306fa1
306fa1
This patch sets the existing macro table definitions (for both global and
306fa1
local table) as environment variables before calling the map. The values
306fa1
are not checked after return so, at this stage, program maps can't change
306fa1
macro definitions.
306fa1
---
306fa1
 CHANGELOG                |    1 +
306fa1
 include/macros.h         |    1 +
306fa1
 lib/macros.c             |   28 ++++++++++++++++++++++++++++
306fa1
 modules/lookup_program.c |   20 ++++++++++++++++++++
306fa1
 4 files changed, 50 insertions(+)
306fa1
306fa1
--- autofs-5.0.7.orig/CHANGELOG
306fa1
+++ autofs-5.0.7/CHANGELOG
306fa1
@@ -76,6 +76,7 @@
306fa1
 - fix options compare.
306fa1
 - fix fix options compare.
306fa1
 - fix max() declaration.
306fa1
+- setup program map env from macro table.
306fa1
 
306fa1
 25/07/2012 autofs-5.0.7
306fa1
 =======================
306fa1
--- autofs-5.0.7.orig/include/macros.h
306fa1
+++ autofs-5.0.7/include/macros.h
306fa1
@@ -40,5 +40,6 @@ void macro_free_global_table(void);
306fa1
 void macro_free_table(struct substvar *table);
306fa1
 const struct substvar *
306fa1
 macro_findvar(const struct substvar *table, const char *str, int len);
306fa1
+void macro_setenv(struct substvar *table);
306fa1
 
306fa1
 #endif
306fa1
--- autofs-5.0.7.orig/lib/macros.c
306fa1
+++ autofs-5.0.7/lib/macros.c
306fa1
@@ -421,3 +421,31 @@ macro_findvar(const struct substvar *tab
306fa1
 	return NULL;
306fa1
 }
306fa1
 
306fa1
+/* Set environment from macro variable table */
306fa1
+void macro_setenv(struct substvar *table)
306fa1
+{
306fa1
+	const struct substvar *sv = system_table;
306fa1
+	const struct substvar *lv = table;
306fa1
+
306fa1
+	/*
306fa1
+	 * First set environment from global table, matching local
306fa1
+	 * variables will overwrite these.
306fa1
+	 */
306fa1
+	while (sv) {
306fa1
+		if (sv->def)
306fa1
+			setenv(sv->def, sv->val, 1);
306fa1
+		sv = sv->next;
306fa1
+	}
306fa1
+
306fa1
+	error(LOGOPT_ANY, "table %p", table);
306fa1
+	dump_table(table);
306fa1
+
306fa1
+	/* Next set environment from the local table */
306fa1
+	while (lv) {
306fa1
+		if (lv->def)
306fa1
+			setenv(lv->def, lv->val, 1);
306fa1
+		lv = lv->next;
306fa1
+	}
306fa1
+
306fa1
+	return;
306fa1
+}
306fa1
--- autofs-5.0.7.orig/modules/lookup_program.c
306fa1
+++ autofs-5.0.7/modules/lookup_program.c
306fa1
@@ -36,9 +36,17 @@
306fa1
 
306fa1
 struct lookup_context {
306fa1
 	const char *mapname;
306fa1
+	char *mapfmt;
306fa1
 	struct parse_mod *parse;
306fa1
 };
306fa1
 
306fa1
+struct parse_context {
306fa1
+	char *optstr;		/* Mount options */
306fa1
+	char *macros;		/* Map wide macro defines */
306fa1
+	struct substvar *subst;	/* $-substitutions */
306fa1
+	int slashify_colons;	/* Change colons to slashes? */
306fa1
+};
306fa1
+
306fa1
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
306fa1
 
306fa1
 int lookup_init(const char *mapfmt, int argc, const char *const *argv, void **context)
306fa1
@@ -79,6 +87,8 @@ int lookup_init(const char *mapfmt, int
306fa1
 	if (!mapfmt)
306fa1
 		mapfmt = MAPFMT_DEFAULT;
306fa1
 
306fa1
+	ctxt->mapfmt = strdup(mapfmt);
306fa1
+
306fa1
 	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
306fa1
 	if (!ctxt->parse) {
306fa1
 		logmsg(MODPREFIX "failed to open parse context");
306fa1
@@ -255,6 +265,14 @@ int lookup_mount(struct autofs_point *ap
306fa1
 			warn(ap->logopt,
306fa1
 			     MODPREFIX "failed to set PWD to %s for map %s",
306fa1
 			     ap->path, ctxt->mapname);
306fa1
+		/*
306fa1
+		 * MAPFMT_DEFAULT must be "sun" for ->parse_init() to have setup
306fa1
+		 * the macro table.
306fa1
+		 */
306fa1
+		if (ctxt->mapfmt && strcmp(ctxt->mapfmt, "MAPFMT_DEFAULT")) {
306fa1
+			struct parse_context *pctxt = (struct parse_context *) ctxt->parse->context;
306fa1
+			macro_setenv(pctxt->subst);
306fa1
+		}
306fa1
 		execl(ctxt->mapname, ctxt->mapname, name, NULL);
306fa1
 		_exit(255);	/* execl() failed */
306fa1
 	}
306fa1
@@ -448,6 +466,8 @@ int lookup_done(void *context)
306fa1
 {
306fa1
 	struct lookup_context *ctxt = (struct lookup_context *) context;
306fa1
 	int rv = close_parse(ctxt->parse);
306fa1
+	if (ctxt->mapfmt)
306fa1
+		free(ctxt->mapfmt);
306fa1
 	free(ctxt);
306fa1
 	return rv;
306fa1
 }