Index: app.c
===================================================================
RCS file: /usr/cvsroot/asterisk/app.c,v
retrieving revision 1.60
diff -u -r1.60 app.c
--- app.c	4 May 2005 15:26:03 -0000	1.60
+++ app.c	14 May 2005 12:50:11 -0000
@@ -1011,24 +1011,21 @@
 	int count = 0;
 	char *test;
 	char cat[80] = "";
+	char *s;
 
-	if (category && !ast_strlen_zero(category)) {
-		strncpy(cat, category, sizeof(cat) - 1);
-	} else {
-		strncpy(cat, GROUP_CATEGORY_PREFIX, sizeof(cat) - 1);
-	}
+	if (group == NULL || ast_strlen_zero(group));
+		return 0;
 
-	if (group && !ast_strlen_zero(group)) {
-		chan = ast_channel_walk_locked(NULL);
-		while(chan) {
-			test = pbx_builtin_getvar_helper(chan, cat);
-			if (test && !strcasecmp(test, group))
-				count++;
-			ast_mutex_unlock(&chan->lock);
-			chan = ast_channel_walk_locked(chan);
-		}
-	}
+	s = (category && !ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
+	strncpy(cat, s, sizeof(cat) - 1);
 
+	chan = NULL;
+	while ( (chan = ast_channel_walk_locked(chan)) != NULL) {
+		test = pbx_builtin_getvar_helper(chan, cat);
+		if (test && !strcasecmp(test, group))
+			count++;
+		ast_mutex_unlock(&chan->lock);
+	}
 	return count;
 }
 
@@ -1039,6 +1036,7 @@
 	int count = 0;
 	char *test;
 	char cat[80] = "";
+	char *s;
 
 	if (!groupmatch || ast_strlen_zero(groupmatch))
 		return 0;
@@ -1047,19 +1045,15 @@
 	if (regcomp(&regexbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
 		return 0;
 
-	if (category && !ast_strlen_zero(category)) {
-		strncpy(cat, category, sizeof(cat) - 1);
-	} else {
-		strncpy(cat, GROUP_CATEGORY_PREFIX, sizeof(cat) - 1);
-	}
+	s = (category && !ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
+	strncpy(cat, s, sizeof(cat) - 1);
 
-	chan = ast_channel_walk_locked(NULL);
-	while(chan) {
+	chan = NULL;
+	while ( (chan = ast_channel_walk_locked(chan)) != NULL) {
 		test = pbx_builtin_getvar_helper(chan, cat);
 		if (test && !regexec(&regexbuf, test, 0, NULL, 0))
 			count++;
 		ast_mutex_unlock(&chan->lock);
-		chan = ast_channel_walk_locked(chan);
 	}
 
 	regfree(&regexbuf);
Index: channel.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channel.c,v
retrieving revision 1.193
diff -u -r1.193 channel.c
--- channel.c	29 Apr 2005 17:00:33 -0000	1.193
+++ channel.c	14 May 2005 12:50:16 -0000
@@ -61,7 +61,11 @@
 #define MONITOR_DELAY	150 * 8		/* 150 ms of MONITORING DELAY */
 #endif
 
+/*
+ * Prevent new channel allocation if shutting down.
+ */
 static int shutting_down = 0;
+
 static int uniqueint = 0;
 
 unsigned long global_fin = 0, global_fout = 0;
@@ -71,12 +75,17 @@
 struct chanlist {
 	const struct ast_channel_tech *tech;
 	struct chanlist *next;
-} *backends = NULL;
-struct ast_channel *channels = NULL;
+};
 
-/* Protect the channel list (highly unlikely that two things would change
-   it at the same time, but still! */
-   
+static struct chanlist *backends = NULL;
+
+/*
+ * the list of channels we have
+ */
+static struct ast_channel *channels = NULL;
+
+/* Protect the channel list, both backends and channels.
+ */
 AST_MUTEX_DEFINE_STATIC(chlock);
 
 static int show_channeltypes(int fd, int argc, char *argv[])
@@ -489,70 +508,77 @@
 		ast_clear_flag(chan, AST_FLAG_DEFER_DTMF);
 }
 
-struct ast_channel *ast_channel_walk_locked(struct ast_channel *prev)
-{
-	/* Returns next channel (locked) */
-	struct ast_channel *l, *ret;
-	int retries = 0;	
-retry:
-	ret=NULL;
-	ast_mutex_lock(&chlock);
-	l = channels;
-	if (!prev) {
-		if (l) {
-			if (ast_mutex_trylock(&l->lock)) {
-				if (retries < 10)
-					ast_log(LOG_DEBUG, "Avoiding initial deadlock for '%s'\n", l->name);
-				else
-					ast_log(LOG_WARNING, "Avoided initial deadlock for '%s', %d retries!\n", l->name, retries);
-				ast_mutex_unlock(&chlock);
-				if (retries < 10) {
-					usleep(1);
-					retries++;
-					goto retry;
-				} else
-					return NULL;
+/*
+ * Helper function to return the channel after prev, or the one matching name,
+ * with the individual lock held. If getting the individual lock fails,
+ * unlock and retry quickly up to 10 times, then give up.
+ * 
+ * This is only an internal interface, applications should use
+ * ast_channel_walk_locked(prev) or ast_get_channel_by_name_locked(name).
+ *
+ * XXX Note that this code has cost O(N) because of the need to verify
+ * that the object is still on the global list.
+ *
+ * XXX also note that accessing fields (e.g. c->name in ast_log())
+ * can only be done with the lock held or someone could delete the
+ * object while we work on it. This causes some ugliness in the code.
+ * Note that removing the first ast_log() may be harmful, as it would
+ * shorten the retry period and possibly cause failures.
+ * We should definitely go for a better scheme that is deadlock-free.
+ */
+static struct ast_channel *ast_channel_find_locked(struct ast_channel *prev,
+	const char *name)
+{
+	/* The original code had a variant msg depending on 'prev' */
+	char *msg = prev ? "initial deadlock" : "deadlock";
+	int retries, done;
+	struct ast_channel *c;
+
+	for (retries =0; retries < 10; retries++) {
+		ast_mutex_lock(&chlock);
+		for (c = channels; c; c = c->next) {
+			if (prev == NULL) {
+				/* want either head of list or match by name */
+				if (name == NULL || !strcasecmp(name, c->name))
+					break;
+			} else if (c == prev) { /* found, return c->next */
+				c = c->next;
+				break;
 			}
 		}
+		/* exit if chan not found or mutex acquired successfully */
+		done = (c == NULL) || (ast_mutex_trylock(&c->lock) == 0);
+		/* XXX we need to hold the lock to access c->name */
+		if (!done)
+			ast_log(LOG_DEBUG, "Avoiding %s for '%s'\n", msg, c->name);
 		ast_mutex_unlock(&chlock);
-		return l;
-	}
-	while(l) {
-		if (l == prev)
-			ret = l->next;
-		l = l->next;
-	}
-	if (ret) {
-		if (ast_mutex_trylock(&ret->lock)) {
-			if (retries < 10)
-				ast_log(LOG_DEBUG, "Avoiding deadlock for '%s'\n", ret->name);
-			else
-				ast_log(LOG_WARNING, "Avoided deadlock for '%s', %d retries!\n", ret->name, retries);
-			ast_mutex_unlock(&chlock);
-			if (retries < 10) {
-				usleep(1);
-				retries++;
-				goto retry;
-			} else
-				return NULL;
-		}
+		if (done)
+			return c;
+		usleep(1);
 	}
-	ast_mutex_unlock(&chlock);
-	return ret;
-	
+	/*
+	 * c is surely not null, but we don't have the lock so cannot
+	 * access c->name
+	 */
+	ast_log(LOG_WARNING, "Avoided %s for '%p', %d retries!\n",
+			msg, c, retries);
+	return NULL;
 }
 
+struct ast_channel *ast_channel_walk_locked(struct ast_channel *prev)
+{
+    return ast_channel_find_locked(prev, NULL);
+}
+
+
+
+
+
+
+
 struct ast_channel *ast_get_channel_by_name_locked(char *channame)
 {
-	struct ast_channel *chan;
-	chan = ast_channel_walk_locked(NULL);
-	while(chan) {
-		if (!strcasecmp(chan->name, channame))
-			return chan;
-		ast_mutex_unlock(&chan->lock);
-		chan = ast_channel_walk_locked(chan);
-	}
-	return NULL;
+    return ast_channel_find_locked(NULL, channame);
 }
 
 int ast_safe_sleep_conditional(	struct ast_channel *chan, int ms,
Index: cli.c
===================================================================
RCS file: /usr/cvsroot/asterisk/cli.c,v
retrieving revision 1.79
diff -u -r1.79 cli.c
--- cli.c	29 Apr 2005 17:00:33 -0000	1.79
+++ cli.c	14 May 2005 12:50:17 -0000
@@ -412,10 +412,9 @@
 		return RESULT_SHOWUSAGE;
 	
 	concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
-	c = ast_channel_walk_locked(NULL);
 	if(!concise)
 		ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
-	while(c) {
+	while ( (c = ast_channel_walk_locked(c)) != NULL) {
 		if(concise)
 			ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
 					c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
@@ -427,7 +426,6 @@
 
 		numchans++;
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
 	}
 	if(!concise)
 		ast_cli(fd, "%d active channel(s)\n", numchans);
@@ -471,18 +469,12 @@
 	struct ast_channel *c=NULL;
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, argv[2])) {
-			ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
-			ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
-			ast_mutex_unlock(&c->lock);
-			break;
-		}
+	c = ast_get_channel_by_name_locked(argv[2]);
+	if (c) {
+		ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
+		ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
-	if (!c) 
+	} else
 		ast_cli(fd, "%s is not a known channel\n", argv[2]);
 	return RESULT_SUCCESS;
 }
@@ -596,6 +588,8 @@
 	return RESULT_SUCCESS;
 }
 
+#define	DEBUGCHAN_FLAG	0x80000000
+/* XXX todo: merge next two functions!!! */
 static int handle_debugchan(int fd, int argc, char *argv[])
 {
 	struct ast_channel *c=NULL;
@@ -605,31 +599,26 @@
 
 	is_all = !strcasecmp("all", argv[2]);
 	if (is_all) {
-		global_fin |= 0x80000000;
-		global_fout |= 0x80000000;
+		global_fin |= DEBUGCHAN_FLAG;
+		global_fout |= DEBUGCHAN_FLAG;
+		c = ast_channel_walk_locked(NULL);
+	} else {
+		c = ast_get_channel_by_name_locked(argv[2]);
+		if (c == NULL)
+			ast_cli(fd, "No such channel %s\n", argv[2]);
 	}
-	c = ast_channel_walk_locked(NULL);
 	while(c) {
-		if (is_all || !strcasecmp(c->name, argv[2])) {
-			if (!(c->fin & 0x80000000) || !(c->fout & 0x80000000)) {
-				c->fin |= 0x80000000;
-				c->fout |= 0x80000000;
-				ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
-			}
-			if (!is_all)
-				break;
+		if (!(c->fin & DEBUGCHAN_FLAG) || !(c->fout & DEBUGCHAN_FLAG)) {
+			c->fin |= DEBUGCHAN_FLAG;
+			c->fout |= DEBUGCHAN_FLAG;
+			ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
 		}
 		ast_mutex_unlock(&c->lock);
+		if (!is_all)
+			break;
 		c = ast_channel_walk_locked(c);
 	}
-	if (!is_all) {
-		if (c)
-			ast_mutex_unlock(&c->lock);
-		else
-			ast_cli(fd, "No such channel %s\n", argv[2]);
-	}
-	else
-		ast_cli(fd, "Debugging on new channels is enabled\n");
+	ast_cli(fd, "Debugging on new channels is enabled\n");
 	return RESULT_SUCCESS;
 }
 
@@ -641,31 +630,26 @@
 		return RESULT_SHOWUSAGE;
 	is_all = !strcasecmp("all", argv[3]);
 	if (is_all) {
-		global_fin &= ~0x80000000;
-		global_fout &= ~0x80000000;
-	}
-	c = ast_channel_walk_locked(NULL);
+		global_fin &= ~DEBUGCHAN_FLAG;
+		global_fout &= ~DEBUGCHAN_FLAG;
+		c = ast_channel_walk_locked(NULL);
+	} else {
+		c = ast_get_channel_by_name_locked(argv[3]);
+		if (c == NULL)
+			ast_cli(fd, "No such channel %s\n", argv[3]);
+    }
 	while(c) {
-		if (is_all || !strcasecmp(c->name, argv[3])) {
-			if ((c->fin & 0x80000000) || (c->fout & 0x80000000)) {
-				c->fin &= 0x7fffffff;
-				c->fout &= 0x7fffffff;
-				ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
-			}
-			if (!is_all)
-				break;
+		if ((c->fin & DEBUGCHAN_FLAG) || (c->fout & DEBUGCHAN_FLAG)) {
+			c->fin &= ~DEBUGCHAN_FLAG;
+			c->fout &= ~DEBUGCHAN_FLAG;
+			ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
 		}
 		ast_mutex_unlock(&c->lock);
+		if (!is_all)
+			break;
 		c = ast_channel_walk_locked(c);
 	}
-	if (!is_all) {
-		if (c)
-			ast_mutex_unlock(&c->lock);
-		else
-			ast_cli(fd, "No such channel %s\n", argv[3]);
-	}
-	else
-		ast_cli(fd, "Debugging on new channels is disabled\n");
+	ast_cli(fd, "Debugging on new channels is disabled\n");
 	return RESULT_SUCCESS;
 }
 		
@@ -683,9 +667,11 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	gettimeofday(&now, NULL);
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, argv[2])) {
+	c = ast_get_channel_by_name_locked(argv[2]);
+	if (!c) {
+		ast_cli(fd, "%s is not a known channel\n", argv[2]);
+		return RESULT_SUCCESS;
+	}
 			if(c->cdr) {
 				elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
 				hour = elapsed_seconds / 3600;
@@ -740,37 +726,27 @@
 					ast_cli(fd,"  CDR Variables:\n%s\n",buf);
 
 			ast_mutex_unlock(&c->lock);
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
-	if (!c) 
-		ast_cli(fd, "%s is not a known channel\n", argv[2]);
 	return RESULT_SUCCESS;
 }
 
 static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos)
 {
-	struct ast_channel *c;
+	struct ast_channel *c = NULL;
 	int which=0;
-	char *ret;
+	char *ret = NULL;
+
 	if (pos != rpos)
 		return NULL;
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
+	while ( (c = ast_channel_walk_locked(c)) != NULL) {
 		if (!strncasecmp(word, c->name, strlen(word))) {
-			if (++which > state)
+			if (++which > state) {
+				ret = strdup(c->name);
+				ast_mutex_unlock(&c->lock);
 				break;
+			}
 		}
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
 	}
-	if (c) {
-		ret = strdup(c->name);
-		ast_mutex_unlock(&c->lock);
-	} else
-		ret = NULL;
 	return ret;
 }
 
Index: manager.c
===================================================================
RCS file: /usr/cvsroot/asterisk/manager.c,v
retrieving revision 1.94
diff -u -r1.94 manager.c
--- manager.c	29 Apr 2005 17:00:33 -0000	1.94
+++ manager.c	14 May 2005 12:50:19 -0000
@@ -570,14 +570,7 @@
 		astman_send_error(s, m, "No channel specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -611,14 +604,7 @@
 		return 0;
 	}
 
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -656,14 +642,7 @@
 		return 0;
 	}
 
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -698,25 +677,22 @@
 	char bridge[256];
 	struct timeval now;
 	long elapsed_seconds=0;
+	int all = !name || ast_strlen_zero(name); /* set if we want all channels */
 
 	gettimeofday(&now, NULL);
 	astman_send_ack(s, m, "Channel status will follow");
-	c = ast_channel_walk_locked(NULL);
         if (id && !ast_strlen_zero(id))
                 snprintf(idText,256,"ActionID: %s\r\n",id);
-	if (name && !ast_strlen_zero(name)) {
-		while (c) {
-			if (!strcasecmp(c->name, name)) {
-				break;
-			}
-			ast_mutex_unlock(&c->lock);
-			c = ast_channel_walk_locked(c);
-		}
+	if (all)
+		c = ast_channel_walk_locked(NULL);
+	else {
+		c = ast_get_channel_by_name_locked(name);
 		if (!c) {
 			astman_send_error(s, m, "No such channel");
 			return 0;
 		}
 	}
+	/* if we look by name, we break after the first iteration */
 	while(c) {
 		if (c->_bridge)
 			snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->_bridge->name);
@@ -769,10 +745,8 @@
 			ast_state2str(c->_state), bridge, c->uniqueid, idText);
 		}
 		ast_mutex_unlock(&s->lock);
-		ast_mutex_unlock(&c->lock);
-		if (name && !ast_strlen_zero(name)) {
+		if (!all)
 			break;
-		}
 		c = ast_channel_walk_locked(c);
 	}
 	ast_mutex_lock(&s->lock);
@@ -1171,14 +1145,7 @@
 		astman_send_error(s, m, "No timeout specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.241
diff -u -r1.241 pbx.c
--- pbx.c	8 May 2005 17:10:58 -0000	1.241
+++ pbx.c	14 May 2005 12:50:25 -0000
@@ -4424,14 +4314,7 @@
 	struct ast_channel *chan;
 	int res = -1;
 
-	chan = ast_channel_walk_locked(NULL);
-	while(chan) {
-		if (!strcasecmp(channame, chan->name))
-			break;
-		ast_mutex_unlock(&chan->lock);
-		chan = ast_channel_walk_locked(chan);
-	}
-	
+	chan = ast_get_channel_by_name_locked(channame);
 	if (chan) {
 		res = ast_async_goto(chan, context, exten, priority);
 		ast_mutex_unlock(&chan->lock);
@@ -5726,7 +5643,7 @@
 	char *value;
 	char *stringp=NULL;
 	char *channel;
-	struct ast_channel *chan2=NULL;
+	struct ast_channel *chan2;
 	char tmp[4096]="";
 	char *s;
 
@@ -5740,11 +5657,7 @@
 	channel = strsep(&stringp,"|"); 
 	value = strsep(&stringp,"\0");
 	if (channel && value && name) {
-		while((chan2 = ast_channel_walk_locked(chan2))) {
-			if (!strcmp(chan2->name, channel))
-				break;
-			ast_mutex_unlock(&chan2->lock);
-		}
+		chan2 = ast_get_channel_by_name_locked(channel);
 		if (chan2) {
 			s = alloca(strlen(value) + 4);
 			if (s) {
Index: apps/app_groupcount.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_groupcount.c,v
retrieving revision 1.14
diff -u -r1.14 app_groupcount.c
--- apps/app_groupcount.c	8 May 2005 17:17:34 -0000	1.14
+++ apps/app_groupcount.c	14 May 2005 12:50:31 -0000
@@ -174,9 +174,8 @@
 		havepattern = 1;
 	}
 
-	c = ast_channel_walk_locked(NULL);
 	ast_cli(fd, FORMAT_STRING, "Channel", "Group", "Category");
-	while(c) {
+	while ( (c = ast_channel_walk_locked(c)) != NULL) {
 		headp=&c->varshead;
 		AST_LIST_TRAVERSE(headp,current,entries) {
 			if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
@@ -194,7 +193,6 @@
 		}
 		numchans++;
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
 	}
 
 	if (havepattern)
Index: apps/app_setcdruserfield.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_setcdruserfield.c,v
retrieving revision 1.6
diff -u -r1.6 app_setcdruserfield.c
--- apps/app_setcdruserfield.c	21 Apr 2005 06:02:43 -0000	1.6
+++ apps/app_setcdruserfield.c	14 May 2005 12:50:31 -0000
@@ -76,13 +76,7 @@
 		astman_send_error(s, m, "No UserField specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while (c) {
-		if (!strcasecmp(c->name, channel))
-			break;
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(NULL);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
Index: apps/app_softhangup.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_softhangup.c,v
retrieving revision 1.6
diff -u -r1.6 app_softhangup.c
--- apps/app_softhangup.c	21 Apr 2005 06:02:43 -0000	1.6
+++ apps/app_softhangup.c	14 May 2005 12:50:31 -0000
@@ -60,6 +60,7 @@
 	while (c) {
 		strncpy(name, c->name, sizeof(name)-1);
 		ast_mutex_unlock(&c->lock);
+		/* XXX watch out, i think it is wrong to access c-> after unlocking! */
 		if (all) {
 			/* CAPI is set up like CAPI[foo/bar]/clcnt */ 
 			if (!strcmp(c->type,"CAPI")) 
Index: apps/app_zapscan.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_zapscan.c,v
retrieving revision 1.16
diff -u -r1.16 app_zapscan.c
--- apps/app_zapscan.c	21 Apr 2005 06:02:43 -0000	1.16
+++ apps/app_zapscan.c	14 May 2005 12:50:32 -0000
@@ -64,15 +64,7 @@
 	char name[80];
 	
 	snprintf(name,sizeof(name),"Zap/%d-1",num);
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
-	return c;
+	return ast_get_channel_by_name_locked(name);
 }
 
 static int careful_write(int fd, unsigned char *data, int len)
Index: res/res_agi.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_agi.c,v
retrieving revision 1.34
diff -u -r1.34 res_agi.c
--- res/res_agi.c	27 Apr 2005 01:42:33 -0000	1.34
+++ res/res_agi.c	14 May 2005 12:50:51 -0000
@@ -956,19 +956,15 @@
 		ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT);
 		fdprintf(agi->fd, "200 result=1\n");
 		return RESULT_SUCCESS;
-        } else if (argc == 2) {
+	} else if (argc == 2) {
 		/* one argument: look for info on the specified channel */
-		c = ast_channel_walk_locked(NULL);
-		while (c) {
-			if (strcasecmp(argv[1], c->name) == 0) {
-				/* we have a matching channel */
-				ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
-				fdprintf(agi->fd, "200 result=1\n");
-				ast_mutex_unlock(&c->lock);
-       				return RESULT_SUCCESS;
-			}
+		c = ast_get_channel_by_name_locked(argv[1]);
+		if (c) {
+			/* we have a matching channel */
+			ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
+			fdprintf(agi->fd, "200 result=1\n");
 			ast_mutex_unlock(&c->lock);
-			c = ast_channel_walk_locked(c);
+			return RESULT_SUCCESS;
 		}
 		/* if we get this far no channel name matched the argument given */
 		fdprintf(agi->fd, "200 result=-1\n");
@@ -1032,15 +1028,11 @@
 		return RESULT_SUCCESS;
 	} else if (argc == 3) {
 		/* one argument: look for info on the specified channel */
-		c = ast_channel_walk_locked(NULL);
-		while (c) {
-			if (strcasecmp(argv[2],c->name)==0) {
-				fdprintf(agi->fd, "200 result=%d\n", c->_state);
-				ast_mutex_unlock(&c->lock);
-				return RESULT_SUCCESS;
-			}
+		c = ast_get_channel_by_name_locked(argv[2]);
+		if (c) {
+			fdprintf(agi->fd, "200 result=%d\n", c->_state);
 			ast_mutex_unlock(&c->lock);
-			c = ast_channel_walk_locked(c);
+			return RESULT_SUCCESS;
 		}
 		/* if we get this far no channel name matched the argument given */
 		fdprintf(agi->fd, "200 result=-1\n");
@@ -1083,15 +1075,11 @@
 	if ((argc != 4) && (argc != 5))
 		return RESULT_SHOWUSAGE;
 	if (argc == 5) {
-		while((chan2 = ast_channel_walk_locked(chan2))) {
-			if (!strcmp(chan2->name, argv[4]))
-				break;
-			ast_mutex_unlock(&chan2->lock);
-		}
+		chan2 = ast_get_channel_by_name_locked(argv[4]);
 	} else {
 		chan2 = chan;
 	}
-	if (chan) {
+	if (chan) { /* XXX isn't this chan2 ? */
 		pbx_substitute_variables_helper(chan2, argv[3], tmp, sizeof(tmp) - 1);
 		fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
 	} else {
Index: res/res_features.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_features.c,v
retrieving revision 1.52
diff -u -r1.52 res_features.c
--- res/res_features.c	9 May 2005 14:28:53 -0000	1.52
+++ res/res_features.c	14 May 2005 12:50:53 -0000
@@ -1657,10 +1657,9 @@
 
 int ast_pickup_call(struct ast_channel *chan)
 {
-	struct ast_channel *cur;
+	struct ast_channel *cur = NULL;
 	int res = -1;
-	cur = ast_channel_walk_locked(NULL);
-	while(cur) {
+	while ( (cur = ast_channel_walk_locked(cur)) != NULL) {
 		if (!cur->pbx && 
 			(cur != chan) &&
 			(chan->pickupgroup & cur->callgroup) &&
@@ -1669,7 +1668,6 @@
 			 	break;
 		}
 		ast_mutex_unlock(&cur->lock);
-		cur = ast_channel_walk_locked(cur);
 	}
 	if (cur) {
 		if (option_debug)
Index: res/res_monitor.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_monitor.c,v
retrieving revision 1.33
diff -u -r1.33 res_monitor.c
--- res/res_monitor.c	6 May 2005 07:15:08 -0000	1.33
+++ res/res_monitor.c	14 May 2005 12:50:53 -0000
@@ -413,14 +413,7 @@
 		astman_send_error(s, m, "No channel specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while (c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -471,14 +464,7 @@
 		astman_send_error(s, m, "No channel specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -514,14 +500,7 @@
 		astman_send_error(s, m, "No filename specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
Index: res/res_musiconhold.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_musiconhold.c,v
retrieving revision 1.58
diff -u -r1.58 res_musiconhold.c
--- res/res_musiconhold.c	21 Apr 2005 06:02:44 -0000	1.58
+++ res/res_musiconhold.c	14 May 2005 12:50:54 -0000
@@ -962,10 +962,11 @@
 	ast_mutex_unlock(&moh_lock);
 }
 
-static void moh_on_off(int on) {
-	struct ast_channel *chan = ast_channel_walk_locked(NULL);
+static void moh_on_off(int on)
+{
+	struct ast_channel *chan = NULL;
 
-	while (chan) {
+	while ( (chan = ast_channel_walk_locked(chan)) != NULL) {
 		if (ast_test_flag(chan, AST_FLAG_MOH)) {
 			if (on)
 				local_ast_moh_start(chan, NULL);
@@ -973,7 +974,6 @@
 				ast_deactivate_generator(chan);
 		}
 		ast_mutex_unlock(&chan->lock);
-		chan = ast_channel_walk_locked(chan);
 	}
 }
 
