[svn-commits] murf: branch 1.4 r59225 - /branches/1.4/main/config.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Mon Mar 26 13:06:12 MST 2007


Author: murf
Date: Mon Mar 26 15:06:12 2007
New Revision: 59225

URL: http://svn.digium.com/view/asterisk?view=rev&rev=59225
Log:
Fix for 9257; by eliminating the globals in main/config.c, we make it thread-safe, which is a minimum requirement.

Modified:
    branches/1.4/main/config.c

Modified: branches/1.4/main/config.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/config.c?view=diff&rev=59225&r1=59224&r2=59225
==============================================================================
--- branches/1.4/main/config.c (original)
+++ branches/1.4/main/config.c Mon Mar 26 15:06:12 2007
@@ -62,14 +62,8 @@
 
 static char *extconfig_conf = "extconfig.conf";
 
-/*! Growable string buffer */
-static char *comment_buffer;   /*!< this will be a comment collector.*/
-static int   comment_buffer_size;  /*!< the amount of storage so far alloc'd for the comment_buffer */
-
-static char *lline_buffer;    /*!< A buffer for stuff behind the ; */
-static int  lline_buffer_size;
-
-
+
+/*! \brief Structure to keep comments for rewriting configuration files */
 struct ast_comment {
 	struct ast_comment *next;
 	char cmt[0];
@@ -77,71 +71,70 @@
 
 #define CB_INCR 250
 
-static void CB_INIT(void)
-{
-	if (!comment_buffer) {
-		comment_buffer = ast_malloc(CB_INCR);
-		if (!comment_buffer)
+static void CB_INIT(char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
+{
+	if (!(*comment_buffer)) {
+		*comment_buffer = ast_malloc(CB_INCR);
+		if (!(*comment_buffer))
 			return;
-		comment_buffer[0] = 0;
-		comment_buffer_size = CB_INCR;
-		lline_buffer = ast_malloc(CB_INCR);
-		if (!lline_buffer)
+		(*comment_buffer)[0] = 0;
+		*comment_buffer_size = CB_INCR;
+		*lline_buffer = ast_malloc(CB_INCR);
+		if (!(*lline_buffer))
 			return;
-		lline_buffer[0] = 0;
-		lline_buffer_size = CB_INCR;
+		(*lline_buffer)[0] = 0;
+		*lline_buffer_size = CB_INCR;
 	} else {
-		comment_buffer[0] = 0;
-		lline_buffer[0] = 0;
-	}
-}
-
-static void  CB_ADD(char *str)
-{
-	int rem = comment_buffer_size - strlen(comment_buffer) - 1;
+		(*comment_buffer)[0] = 0;
+		(*lline_buffer)[0] = 0;
+	}
+}
+
+static void  CB_ADD(char **comment_buffer, int *comment_buffer_size, char *str)
+{
+	int rem = *comment_buffer_size - strlen(*comment_buffer) - 1;
 	int siz = strlen(str);
 	if (rem < siz+1) {
-		comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + siz + 1);
-		if (!comment_buffer)
+		*comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + siz + 1);
+		if (!(*comment_buffer))
 			return;
-		comment_buffer_size += CB_INCR+siz+1;
-	}
-	strcat(comment_buffer,str);
-}
-
-static void  CB_ADD_LEN(char *str, int len)
-{
-	int cbl = strlen(comment_buffer) + 1;
-	int rem = comment_buffer_size - cbl;
+		*comment_buffer_size += CB_INCR+siz+1;
+	}
+	strcat(*comment_buffer,str);
+}
+
+static void  CB_ADD_LEN(char **comment_buffer, int *comment_buffer_size, char *str, int len)
+{
+	int cbl = strlen(*comment_buffer) + 1;
+	int rem = *comment_buffer_size - cbl;
 	if (rem < len+1) {
-		comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + len + 1);
-		if (!comment_buffer)
+		*comment_buffer = ast_realloc(*comment_buffer, *comment_buffer_size + CB_INCR + len + 1);
+		if (!(*comment_buffer))
 			return;
-		comment_buffer_size += CB_INCR+len+1;
-	}
-	strncat(comment_buffer,str,len);
-	comment_buffer[cbl+len-1] = 0;
-}
-
-static void  LLB_ADD(char *str)
-{
-	int rem = lline_buffer_size - strlen(lline_buffer) - 1;
+		*comment_buffer_size += CB_INCR+len+1;
+	}
+	strncat(*comment_buffer,str,len);
+	(*comment_buffer)[cbl+len-1] = 0;
+}
+
+static void  LLB_ADD(char **lline_buffer, int *lline_buffer_size, char *str)
+{
+	int rem = *lline_buffer_size - strlen(*lline_buffer) - 1;
 	int siz = strlen(str);
 	if (rem < siz+1) {
-		lline_buffer = ast_realloc(lline_buffer, lline_buffer_size + CB_INCR + siz + 1);
-		if (!lline_buffer) 
+		*lline_buffer = ast_realloc(*lline_buffer, *lline_buffer_size + CB_INCR + siz + 1);
+		if (!(*lline_buffer)) 
 			return;
-		lline_buffer_size += CB_INCR + siz + 1;
-	}
-	strcat(lline_buffer,str);
-}
-
-static void CB_RESET(void )  
+		*lline_buffer_size += CB_INCR + siz + 1;
+	}
+	strcat(*lline_buffer,str);
+}
+
+static void CB_RESET(char **comment_buffer, char **lline_buffer)  
 { 
-	comment_buffer[0] = 0; 
-	lline_buffer[0] = 0;
-}
-		
+	(*comment_buffer)[0] = 0; 
+	(*lline_buffer)[0] = 0;
+}
 
 
 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
@@ -599,7 +592,8 @@
 	cfg->current = (struct ast_category *) cat;
 }
 
-static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments)
+static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
+				char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
 {
 	char *c;
 	char *cur = buf;
@@ -627,14 +621,14 @@
 			return -1;
 		}
 		/* add comments */
-		if (withcomments && comment_buffer && comment_buffer[0] ) {
-			newcat->precomments = ALLOC_COMMENT(comment_buffer);
-		}
-		if (withcomments && lline_buffer && lline_buffer[0] ) {
-			newcat->sameline = ALLOC_COMMENT(lline_buffer);
+		if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
+			newcat->precomments = ALLOC_COMMENT(*comment_buffer);
+		}
+		if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
+			newcat->sameline = ALLOC_COMMENT(*lline_buffer);
 		}
 		if( withcomments )
-			CB_RESET();
+			CB_RESET(comment_buffer, lline_buffer);
 		
  		/* If there are options or categories to inherit from, process them now */
  		if (c) {
@@ -648,7 +642,7 @@
 					(*cat)->ignored = 1;
 				} else if (!strcasecmp(cur, "+")) {
 					*cat = category_get(cfg, catname, 1);
-					if (!*cat) {
+					if (!(*cat)) {
 						ast_config_destroy(cfg);
 						if (newcat)
 							ast_category_destroy(newcat);
@@ -683,7 +677,7 @@
 			*c = '\0';
 			/* Find real argument */
 			c = ast_skip_blanks(c + 1);
-			if (!*c)
+			if (!(*c))
 				c = NULL;
 		} else 
 			c = NULL;
@@ -737,7 +731,7 @@
 			ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
 	} else {
 		/* Just a line (variable = value) */
-		if (!*cat) {
+		if (!(*cat)) {
 			ast_log(LOG_WARNING,
 				"parse error: No category context for line %d of %s\n", lineno, configfile);
 			return -1;
@@ -759,14 +753,14 @@
 				v->blanklines = 0;
 				ast_variable_append(*cat, v);
 				/* add comments */
-				if (withcomments && comment_buffer && comment_buffer[0] ) {
-					v->precomments = ALLOC_COMMENT(comment_buffer);
+				if (withcomments && *comment_buffer && (*comment_buffer)[0] ) {
+					v->precomments = ALLOC_COMMENT(*comment_buffer);
 				}
-				if (withcomments && lline_buffer && lline_buffer[0] ) {
-					v->sameline = ALLOC_COMMENT(lline_buffer);
+				if (withcomments && *lline_buffer && (*lline_buffer)[0] ) {
+					v->sameline = ALLOC_COMMENT(*lline_buffer);
 				}
 				if( withcomments )
-					CB_RESET();
+					CB_RESET(comment_buffer, lline_buffer);
 				
 			} else {
 				return -1;
@@ -789,6 +783,13 @@
 	struct ast_category *cat = NULL;
 	int count = 0;
 	struct stat statbuf;
+	/*! Growable string buffer */
+	char *comment_buffer=0;   /*!< this will be a comment collector.*/
+	int   comment_buffer_size=0;  /*!< the amount of storage so far alloc'd for the comment_buffer */
+
+	char *lline_buffer=0;    /*!< A buffer for stuff behind the ; */
+	int  lline_buffer_size=0;
+
 	
 	cat = ast_config_get_current_category(cfg);
 
@@ -799,7 +800,7 @@
 	}
 
 	if (withcomments) {
-		CB_INIT();
+		CB_INIT(&comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size);
 		if (!lline_buffer || !comment_buffer) {
 			ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
 			return NULL;
@@ -855,7 +856,7 @@
 			lineno++;
 			if (fgets(buf, sizeof(buf), f)) {
 				if ( withcomments ) {
-					CB_ADD(lline_buffer);       /* add the current lline buffer to the comment buffer */
+					CB_ADD(&comment_buffer, &comment_buffer_size, lline_buffer);       /* add the current lline buffer to the comment buffer */
 					lline_buffer[0] = 0;        /* erase the lline buffer */
 				}
 				
@@ -893,8 +894,8 @@
 								char *oldptr;
 								oldptr = process_buf + strlen(process_buf);
 								if ( withcomments ) {
-									CB_ADD(";");
-									CB_ADD_LEN(oldptr+1,new_buf-oldptr-1);
+									CB_ADD(&comment_buffer, &comment_buffer_size, ";");
+									CB_ADD_LEN(&comment_buffer, &comment_buffer_size, oldptr+1, new_buf-oldptr-1);
 								}
 								
 								memmove(oldptr, new_buf, strlen(new_buf) + 1);
@@ -907,7 +908,7 @@
 							/* If ; is found, and we are not nested in a comment, 
 							   we immediately stop all comment processing */
 							if ( withcomments ) {
-								LLB_ADD(comment_p);
+								LLB_ADD(&lline_buffer, &lline_buffer_size, comment_p);
 							}
 							*comment_p = '\0'; 
 							new_buf = comment_p;
@@ -917,13 +918,13 @@
 				}
 				if( withcomments && comment && !process_buf )
 				{
-					CB_ADD(buf);  /* the whole line is a comment, store it */
+					CB_ADD(&comment_buffer, &comment_buffer_size, buf);  /* the whole line is a comment, store it */
 				}
 				
 				if (process_buf) {
 					char *buf = ast_strip(process_buf);
 					if (!ast_strlen_zero(buf)) {
-						if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments)) {
+						if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
 							cfg = NULL;
 							break;
 						}



More information about the svn-commits mailing list