Blame SOURCES/autofs-5.1.1-factor-out-alloc-multi-map-context.patch

306fa1
autofs-5.1.1 - factor out alloc multi map context
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
Seperate out the context allocation function for the multi map module.
306fa1
306fa1
Signed-off-by: Ian Kent <raven@themaw.net>
306fa1
---
306fa1
 modules/lookup_multi.c |  161 +++++++++++++++++++++++++-----------------------
306fa1
 1 file changed, 85 insertions(+), 76 deletions(-)
306fa1
306fa1
diff --git a/modules/lookup_multi.c b/modules/lookup_multi.c
306fa1
index 36ace11..433b424 100644
306fa1
--- a/modules/lookup_multi.c
306fa1
+++ b/modules/lookup_multi.c
306fa1
@@ -40,6 +40,84 @@ struct lookup_context {
306fa1
 
306fa1
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
306fa1
 
306fa1
+static int free_multi_context(struct lookup_context *);
306fa1
+
306fa1
+static struct lookup_context *alloc_context(const char *format,
306fa1
+					    int argc, const char *const *argv)
306fa1
+{
306fa1
+	struct lookup_context *ctxt;
306fa1
+	char buf[MAX_ERR_BUF];
306fa1
+	char **args;
306fa1
+	int i, an;
306fa1
+	char *estr;
306fa1
+
306fa1
+	ctxt = malloc(sizeof(struct lookup_context));
306fa1
+	if (!ctxt)
306fa1
+		goto nomem;
306fa1
+
306fa1
+	memset(ctxt, 0, sizeof(struct lookup_context));
306fa1
+
306fa1
+	if (argc < 1) {
306fa1
+		logerr(MODPREFIX "No map list");
306fa1
+		goto error_out;
306fa1
+	}
306fa1
+
306fa1
+	ctxt->n = 1;				/* Always at least one map */
306fa1
+	for (i = 0; i < argc; i++) {
306fa1
+		if (!strcmp(argv[i], "--"))	/* -- separates maps */
306fa1
+			ctxt->n++;
306fa1
+	}
306fa1
+
306fa1
+	if (!(ctxt->m = malloc(ctxt->n * sizeof(struct module_info))) ||
306fa1
+	    !(ctxt->argl = malloc((argc + 1) * sizeof(const char *))))
306fa1
+		goto nomem;
306fa1
+
306fa1
+	memset(ctxt->m, 0, ctxt->n * sizeof(struct module_info));
306fa1
+
306fa1
+	memcpy(ctxt->argl, argv, (argc + 1) * sizeof(const char *));
306fa1
+
306fa1
+	args = NULL;
306fa1
+	for (i = an = 0; ctxt->argl[an]; an++) {
306fa1
+		if (ctxt->m[i].argc == 0)
306fa1
+			args = (char **) &ctxt->argl[an];
306fa1
+
306fa1
+		if (strcmp(ctxt->argl[an], "--"))
306fa1
+			ctxt->m[i].argc++;
306fa1
+		else {
306fa1
+			ctxt->argl[an] = NULL;
306fa1
+			if (!args) {
306fa1
+				logerr(MODPREFIX "error assigning map args");
306fa1
+				goto error_out;
306fa1
+			}
306fa1
+			ctxt->m[i].argv = copy_argv(ctxt->m[i].argc,
306fa1
+						    (const char **) args);
306fa1
+			if (!ctxt->m[i].argv)
306fa1
+				goto nomem;
306fa1
+			args = NULL;
306fa1
+			i++;
306fa1
+		}
306fa1
+	}
306fa1
+
306fa1
+	/* catch the last one */
306fa1
+	if (args) {
306fa1
+		ctxt->m[i].argv = copy_argv(ctxt->m[i].argc, (const char **) args);
306fa1
+		if (!ctxt->m[i].argv)
306fa1
+			goto nomem;
306fa1
+	}
306fa1
+
306fa1
+	return ctxt;
306fa1
+
306fa1
+nomem:
306fa1
+	estr = strerror_r(errno, buf, MAX_ERR_BUF);
306fa1
+	logerr(MODPREFIX "error: %s", estr);
306fa1
+
306fa1
+error_out:
306fa1
+	free_multi_context(ctxt);
306fa1
+	free(ctxt);
306fa1
+
306fa1
+	return NULL;
306fa1
+}
306fa1
+
306fa1
 static int free_multi_context(struct lookup_context *ctxt)
306fa1
 {
306fa1
 	int rv;
306fa1
@@ -180,95 +258,26 @@ int lookup_init(const char *my_mapfmt,
306fa1
 		int argc, const char *const *argv, void **context)
306fa1
 {
306fa1
 	struct lookup_context *ctxt;
306fa1
-	char buf[MAX_ERR_BUF];
306fa1
-	char **args;
306fa1
-	int i, an;
306fa1
-	char *estr;
306fa1
+	int i;
306fa1
 
306fa1
-	ctxt = malloc(sizeof(struct lookup_context));
306fa1
+	ctxt = alloc_context(my_mapfmt, argc, argv);
306fa1
 	if (!ctxt)
306fa1
-		goto nomem;
306fa1
-
306fa1
-	memset(ctxt, 0, sizeof(struct lookup_context));
306fa1
-
306fa1
-	if (argc < 1) {
306fa1
-		logerr(MODPREFIX "No map list");
306fa1
-		goto error_out;
306fa1
-	}
306fa1
-
306fa1
-	ctxt->n = 1;				/* Always at least one map */
306fa1
-	for (i = 0; i < argc; i++) {
306fa1
-		if (!strcmp(argv[i], "--"))	/* -- separates maps */
306fa1
-			ctxt->n++;
306fa1
-	}
306fa1
-
306fa1
-	if (!(ctxt->m = malloc(ctxt->n * sizeof(struct module_info))) ||
306fa1
-	    !(ctxt->argl = malloc((argc + 1) * sizeof(const char *))))
306fa1
-		goto nomem;
306fa1
-
306fa1
-	memset(ctxt->m, 0, ctxt->n * sizeof(struct module_info));
306fa1
-
306fa1
-	memcpy(ctxt->argl, argv, (argc + 1) * sizeof(const char *));
306fa1
-
306fa1
-	args = NULL;
306fa1
-	for (i = an = 0; ctxt->argl[an]; an++) {
306fa1
-		if (ctxt->m[i].argc == 0) {
306fa1
-			args = (char **) &ctxt->argl[an];
306fa1
-		}
306fa1
-		if (!strcmp(ctxt->argl[an], "--")) {
306fa1
-			ctxt->argl[an] = NULL;
306fa1
-			if (!args) {
306fa1
-				logerr(MODPREFIX "error assigning map args");
306fa1
-				goto error_out;
306fa1
-			}
306fa1
-			ctxt->m[i].argv = copy_argv(ctxt->m[i].argc, (const char **) args);
306fa1
-			if (!ctxt->m[i].argv)
306fa1
-				goto nomem;
306fa1
-			args = NULL;
306fa1
-			i++;
306fa1
-		} else {
306fa1
-			ctxt->m[i].argc++;
306fa1
-		}
306fa1
-	}
306fa1
-
306fa1
-	/* catch the last one */
306fa1
-	if (args) {
306fa1
-		ctxt->m[i].argv = copy_argv(ctxt->m[i].argc, (const char **) args);
306fa1
-		if (!ctxt->m[i].argv)
306fa1
-			goto nomem;
306fa1
-	}
306fa1
+		return 1;
306fa1
 
306fa1
 	for (i = 0; i < ctxt->n; i++) {
306fa1
 		ctxt->m[i].mod = nss_open_lookup(my_mapfmt,
306fa1
 				 ctxt->m[i].argc, ctxt->m[i].argv);
306fa1
 		if (!ctxt->m[i].mod) {
306fa1
 			logerr(MODPREFIX "error opening module");
306fa1
-			goto error_out;
306fa1
+			free_multi_context(ctxt);
306fa1
+			free(ctxt);
306fa1
+			return 1;
306fa1
 		}
306fa1
 	}
306fa1
 
306fa1
 	*context = ctxt;
306fa1
-	return 0;
306fa1
 
306fa1
-nomem:
306fa1
-	estr = strerror_r(errno, buf, MAX_ERR_BUF);
306fa1
-	logerr(MODPREFIX "error: %s", estr);
306fa1
-error_out:
306fa1
-	if (ctxt) {
306fa1
-		if (ctxt->m) {
306fa1
-			for (i = 0; i < ctxt->n; i++) {
306fa1
-				if (ctxt->m[i].mod)
306fa1
-					close_lookup(ctxt->m[i].mod);
306fa1
-				if (ctxt->m[i].argv)
306fa1
-					free_argv(ctxt->m[i].argc, ctxt->m[i].argv);
306fa1
-			}
306fa1
-			free(ctxt->m);
306fa1
-		}
306fa1
-		if (ctxt->argl)
306fa1
-			free(ctxt->argl);
306fa1
-		free(ctxt);
306fa1
-	}
306fa1
-	return 1;
306fa1
+	return 0;
306fa1
 }
306fa1
 
306fa1
 int lookup_reinit(const char *my_mapfmt,