[asterisk-commits] file: trunk r78521 - in /trunk: include/asterisk/manager.h main/manager.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Aug 7 17:13:52 CDT 2007


Author: file
Date: Tue Aug  7 17:13:40 2007
New Revision: 78521

URL: http://svn.digium.com/view/asterisk?view=rev&rev=78521
Log:
Use the linkedlists.h macros for the manager action list.

Modified:
    trunk/include/asterisk/manager.h
    trunk/main/manager.c

Modified: trunk/include/asterisk/manager.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/manager.h?view=diff&rev=78521&r1=78520&r2=78521
==============================================================================
--- trunk/include/asterisk/manager.h (original)
+++ trunk/include/asterisk/manager.h Tue Aug  7 17:13:40 2007
@@ -113,7 +113,7 @@
 	/*! Function to be called */
 	int (*func)(struct mansession *s, const struct message *m);
 	/*! For easy linking */
-	struct manager_action *next;
+	AST_RWLIST_ENTRY(manager_action) list;
 };
 
 /* External routines may register/unregister manager callbacks this way */

Modified: trunk/main/manager.c
URL: http://svn.digium.com/view/asterisk/trunk/main/manager.c?view=diff&rev=78521&r1=78520&r2=78521
==============================================================================
--- trunk/main/manager.c (original)
+++ trunk/main/manager.c Tue Aug  7 17:13:40 2007
@@ -188,9 +188,9 @@
 static AST_RWLIST_HEAD_STATIC(users, ast_manager_user);
 
 /*! \brief list of actions registered */
-static struct manager_action *first_action;
-AST_RWLOCK_DEFINE_STATIC(actionlock);
-
+static AST_RWLIST_HEAD_STATIC(actions, manager_action);
+
+/*! \brief list of hooks registered */
 static AST_RWLIST_HEAD_STATIC(manager_hooks, manager_custom_hook);
 
 /*! \brief Add a custom hook to be called when an event is fired */
@@ -412,14 +412,14 @@
 	int l = strlen(word), which = 0;
 	char *ret = NULL;
 
-	ast_rwlock_rdlock(&actionlock);
-	for (cur = first_action; cur; cur = cur->next) { /* Walk the list of actions */
+	AST_RWLIST_RDLOCK(&actions);
+	AST_RWLIST_TRAVERSE(&actions, cur, list) {
 		if (!strncasecmp(word, cur->action, l) && ++which > state) {
 			ret = ast_strdup(cur->action);
 			break;	/* make sure we exit even if ast_strdup() returns NULL */
 		}
 	}
-	ast_rwlock_unlock(&actionlock);
+	AST_RWLIST_UNLOCK(&actions);
 
 	return ret;
 }
@@ -500,8 +500,8 @@
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
 
-	ast_rwlock_rdlock(&actionlock);
-	for (cur = first_action; cur; cur = cur->next) { /* Walk the list of actions */
+	AST_RWLIST_RDLOCK(&actions);
+	AST_RWLIST_TRAVERSE(&actions, cur, list) {
 		for (num = 3; num < argc; num++) {
 			if (!strcasecmp(cur->action, argv[num])) {
 				ast_cli(fd, "Action: %s\nSynopsis: %s\nPrivilege: %s\n%s\n",
@@ -511,7 +511,7 @@
 			}
 		}
 	}
-	ast_rwlock_unlock(&actionlock);
+	AST_RWLIST_UNLOCK(&actions);
 
 	return RESULT_SUCCESS;
 }
@@ -612,10 +612,10 @@
 	ast_cli(fd, format, "Action", "Privilege", "Synopsis");
 	ast_cli(fd, format, "------", "---------", "--------");
 
-	ast_rwlock_rdlock(&actionlock);
-	for (cur = first_action; cur; cur = cur->next) /* Walk the list of actions */
+	AST_RWLIST_RDLOCK(&actions);
+	AST_RWLIST_TRAVERSE(&actions, cur, list)
 		ast_cli(fd, format, cur->action, authority_to_str(cur->authority, &authority), cur->synopsis);
-	ast_rwlock_unlock(&actionlock);
+	AST_RWLIST_UNLOCK(&actions);
 
 	return RESULT_SUCCESS;
 }
@@ -1439,7 +1439,7 @@
 	struct ast_str *temp = ast_str_alloca(BUFSIZ); /* XXX very large ? */
 
 	astman_start_ack(s, m);
-	for (cur = first_action; cur; cur = cur->next) { /* Walk the list of actions */
+	AST_RWLIST_TRAVERSE(&actions, cur, list) {
 		if ((s->writeperm & cur->authority) == cur->authority)
 			astman_append(s, "%s: %s (Priv: %s)\r\n",
 				cur->action, cur->synopsis, authority_to_str(cur->authority, &temp));
@@ -2361,8 +2361,8 @@
 		}
 	}
 
-	ast_rwlock_rdlock(&actionlock);	
-	for (tmp = first_action ; tmp; tmp = tmp->next) {
+	AST_RWLIST_RDLOCK(&actions);
+	AST_RWLIST_TRAVERSE(&actions, tmp, list) {
 		if (strcasecmp(action, tmp->action))
 			continue;
 		if ((s->writeperm & tmp->authority) == tmp->authority)
@@ -2371,7 +2371,8 @@
 			astman_send_error(s, m, "Permission denied");
 		break;
 	}
-	ast_rwlock_unlock(&actionlock);
+	AST_RWLIST_UNLOCK(&actions);
+
 	if (!tmp) {
 		ast_mutex_lock(&s->__lock);
 		astman_send_error(s, m, "Invalid/unknown command. Use Action: ListCommands to show available commands.");
@@ -2670,21 +2671,20 @@
  */
 int ast_manager_unregister(char *action)
 {
-	struct manager_action *cur, *prev;
-
-	ast_rwlock_wrlock(&actionlock);
-	for (cur = first_action, prev = NULL; cur; prev = cur, cur = cur->next) {
+	struct manager_action *cur;
+
+	AST_RWLIST_WRLOCK(&actions);
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&actions, cur, list) {
 		if (!strcasecmp(action, cur->action)) {
-			if (prev)
-				prev->next = cur->next;
-			else
-				first_action = cur->next;
+			AST_RWLIST_REMOVE_CURRENT(&actions, list);
 			ast_free(cur);
 			ast_verb(2, "Manager unregistered action %s\n", action);
 			break;
 		}
 	}
-	ast_rwlock_unlock(&actionlock);
+	AST_RWLIST_TRAVERSE_SAFE_END
+	AST_RWLIST_UNLOCK(&actions);
+
 	return 0;
 }
 
@@ -2701,27 +2701,30 @@
 static int ast_manager_register_struct(struct manager_action *act)
 {
 	struct manager_action *cur, *prev = NULL;
-	int ret;
-
-	ast_rwlock_wrlock(&actionlock);
-	for (cur = first_action; cur; prev = cur, cur = cur->next) {
-		ret = strcasecmp(cur->action, act->action);
+
+	AST_RWLIST_WRLOCK(&actions);
+	AST_RWLIST_TRAVERSE(&actions, cur, list) {
+		int ret = strcasecmp(cur->action, act->action);
 		if (ret == 0) {
 			ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", act->action);
-			ast_rwlock_unlock(&actionlock);
+			AST_RWLIST_UNLOCK(&actions);
 			return -1;
 		}
-		if (ret > 0)	/* Insert these alphabetically */
+		if (ret > 0) { /* Insert these alphabetically */
+			prev = cur;
 			break;
-	}
-	if (prev)
-		prev->next = act;
+		}
+	}
+	
+	if (prev)	
+		AST_RWLIST_INSERT_AFTER(&actions, prev, act, list);
 	else
-		first_action = act;
-	act->next = cur;
+		AST_RWLIST_INSERT_HEAD(&actions, act, list);
 
 	ast_verb(2, "Manager registered action %s\n", act->action);
-	ast_rwlock_unlock(&actionlock);
+
+	AST_RWLIST_UNLOCK(&actions);
+
 	return 0;
 }
 
@@ -2729,10 +2732,9 @@
 	the preferred way to register a manager command */
 int ast_manager_register2(const char *action, int auth, int (*func)(struct mansession *s, const struct message *m), const char *synopsis, const char *description)
 {
-	struct manager_action *cur;
-
-	cur = ast_malloc(sizeof(*cur));
-	if (!cur)
+	struct manager_action *cur = NULL;
+
+	if (!(cur = ast_calloc(1, sizeof(*cur))))
 		return -1;
 
 	cur->action = action;
@@ -2740,7 +2742,6 @@
 	cur->func = func;
 	cur->synopsis = synopsis;
 	cur->description = description;
-	cur->next = NULL;
 
 	ast_manager_register_struct(cur);
 




More information about the asterisk-commits mailing list