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

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