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

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