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

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