[asterisk-commits] file: branch anthonyl/5240-testing r43293 - in /team/anthonyl/5240-testing: i...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Sep 19 14:06:27 MST 2006


Author: file
Date: Tue Sep 19 16:06:27 2006
New Revision: 43293

URL: http://svn.digium.com/view/asterisk?rev=43293&view=rev
Log:
Yeah...

Modified:
    team/anthonyl/5240-testing/include/asterisk/manager.h
    team/anthonyl/5240-testing/main/manager.c

Modified: team/anthonyl/5240-testing/include/asterisk/manager.h
URL: http://svn.digium.com/view/asterisk/team/anthonyl/5240-testing/include/asterisk/manager.h?rev=43293&r1=43292&r2=43293&view=diff
==============================================================================
--- team/anthonyl/5240-testing/include/asterisk/manager.h (original)
+++ team/anthonyl/5240-testing/include/asterisk/manager.h Tue Sep 19 16:06:27 2006
@@ -106,12 +106,6 @@
 */
 int ast_manager_unregister( char *action );
 
-/*! Add a manager_user to current list of manager */
-int *ast_manager_add(struct ast_manager_user *amu);
-
-/*! Get an manager by his name */
-struct ast_manager_user *ast_get_manager_by_name_locked(const char *name);
-
 /*! External routines may send asterisk manager events this way */
 /*! 	\param category	Event category, matches manager authorization
 	\param event	Event name
@@ -139,11 +133,5 @@
 int init_manager(void);
 /*! Called by Asterisk initialization */
 int reload_manager(void);
-/*! Add a manager_user to current list of manager */
-int *ast_manager_add(struct ast_manager_user *amu);
-int *ast_manager_user_add(struct ast_manager_user *amu);
-/*! Get an manager by his name */
-struct ast_manager_user *ast_get_manager_by_name_locked(const char *name);
-
 
 #endif /* _ASTERISK_MANAGER_H */

Modified: team/anthonyl/5240-testing/main/manager.c
URL: http://svn.digium.com/view/asterisk/team/anthonyl/5240-testing/main/manager.c?rev=43293&r1=43292&r2=43293&view=diff
==============================================================================
--- team/anthonyl/5240-testing/main/manager.c (original)
+++ team/anthonyl/5240-testing/main/manager.c Tue Sep 19 16:06:27 2006
@@ -103,12 +103,9 @@
 static int block_sockets = 0;
 static int num_sessions = 0;
 
-static struct ast_manager_user *amus =NULL;
-
 /* Protected by the sessions list lock */
 struct eventqent *master_eventq = NULL;
 
-AST_MUTEX_DEFINE_STATIC(amulock);
 AST_THREADSTORAGE(manager_event_buf, manager_event_buf_init);
 #define MANAGER_EVENT_BUF_INITSIZE   256
 
@@ -178,6 +175,8 @@
 	AST_LIST_ENTRY(mansession) list;
 };
 
+static AST_LIST_HEAD_STATIC(sessions, mansession);
+
 struct ast_manager_user {
 	char username[80];
 	char *secret;
@@ -186,10 +185,11 @@
 	char *read;
 	char *write;
 	unsigned int displayconnects:1;
-	struct ast_manager_user *next;
+	int keep;
+	AST_LIST_ENTRY(ast_manager_user) list;
 };
 
-static AST_LIST_HEAD_STATIC(sessions, mansession);
+static AST_LIST_HEAD_STATIC(users, ast_manager_user);
 
 static struct manager_action *first_action = NULL;
 AST_MUTEX_DEFINE_STATIC(actionlock);
@@ -396,32 +396,14 @@
 
 
 
-struct ast_manager_user *ast_get_manager_by_name_locked(const char *name)
-{
-	struct ast_manager_user *tmp = NULL;
-	tmp=amus;
-	while (tmp) {
-		if (!strcasecmp(tmp->username, name))
+static struct ast_manager_user *ast_get_manager_by_name_locked(const char *name)
+{
+	struct ast_manager_user *user = NULL;
+
+	AST_LIST_TRAVERSE(&users, user, list)
+		if (!strcasecmp(user->username, name))
 			break;
-		tmp = tmp->next;
-	}
-	if ( tmp)
-		return tmp;
-	return NULL;
-}
-
-
-
-int *ast_manager_user_add(struct ast_manager_user *amu) {
-	if (!amu) {
-		ast_log(LOG_DEBUG, "You cant pass NULL to that function");
-		return NULL;
-	}
-	ast_mutex_lock(&amulock);
-	amu->next = amus;
-	amus = amu;
-	ast_mutex_unlock(&amulock);
-	return NULL;
+	return user;
 }
 
 void astman_append(struct mansession *s, const char *fmt, ...)
@@ -470,23 +452,19 @@
 
 static int handle_showmanager(int fd, int argc, char *argv[])
 {
-	struct ast_manager_user *tmp;
-	if (argc != 4 )
+	struct ast_manager_user *user = NULL;
+
+	if (argc != 4)
 		return RESULT_SHOWUSAGE;
 
-	/* try to lock manager_user list ... */
-	if (ast_mutex_lock(&amulock)) {
-		ast_log(LOG_ERROR, "Unable to lock manager_user list\n");
+	AST_LIST_LOCK(&users);
+
+	if (!(user = ast_get_manager_by_name_locked(argv[3]))) {
+		ast_cli(fd, "There is no manager called %s\n", argv[3]);
+		AST_LIST_UNLOCK(&users);
 		return -1;
 	}
 
-	tmp = ast_get_manager_by_name_locked(argv[3]);
-
-	if (!tmp) {
-		ast_cli(fd, "There are no manager called %s\n",argv[3]);
-		ast_mutex_unlock(&amulock);
-		return -1;
-	}
 	ast_cli(fd,"\n");
 	ast_cli(fd,
 		"       username: %s\n"
@@ -496,47 +474,49 @@
 		"           read: %s\n"
 		"          write: %s\n"
 		"displayconnects: %s\n",
-		(tmp->username ? tmp->username : "(N/A)"),
-		(tmp->secret ? tmp->secret : "(N/A)"),
-		(tmp->deny ? tmp->deny : "(N/A)"),
-		(tmp->permit ? tmp->permit : "(N/A)"),
-		(tmp->read ? tmp->read : "(N/A)"),
-		(tmp->write ? tmp->write : "(N/A)"),
-		(tmp->displayconnects ? "yes" : "no"));
-	ast_mutex_unlock(&amulock);
+		(user->username ? user->username : "(N/A)"),
+		(user->secret ? user->secret : "(N/A)"),
+		(user->deny ? user->deny : "(N/A)"),
+		(user->permit ? user->permit : "(N/A)"),
+		(user->read ? user->read : "(N/A)"),
+		(user->write ? user->write : "(N/A)"),
+		(user->displayconnects ? "yes" : "no"));
+
+	AST_LIST_UNLOCK(&users);
 
 	return RESULT_SUCCESS;
 }
 
 
-static int handle_showmanagers(int fd, int argc, char *argv[]) {
-	struct ast_manager_user *tmp;
+static int handle_showmanagers(int fd, int argc, char *argv[])
+{
+	struct ast_manager_user *user = NULL;
 	int count_amu = 0;
 
-	if ( argc > 4 )
+	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 
-	/* try to lock manager_user list ... */
-	if (ast_mutex_lock(&amulock)) {
-		ast_log(LOG_ERROR, "Unable to lock manager_user list\n");
-		return -1;
-	}
-
-	tmp=amus;
-	if (!tmp) {
-		ast_cli(fd, "There are no manager user.\n");
-		ast_mutex_unlock(&amulock);
-		return -1;
-	}
+	AST_LIST_LOCK(&users);
+
+	/* If there are no users, print out something along those lines */
+	if (AST_LIST_EMPTY(&users)) {
+		ast_cli(fd, "There are no manager users.\n");
+		AST_LIST_UNLOCK(&users);
+		return RESULT_SUCCESS;
+	}
+
 	ast_cli(fd, "\nusername\n--------\n");
-	while (tmp) {
-		ast_cli(fd, "%s\n", tmp->username);
-		tmp = tmp->next;
+
+	AST_LIST_TRAVERSE(&users, user, list) {
+		ast_cli(fd, "%s\n", user->username);
 		count_amu++;
 	}
-	ast_mutex_unlock(&amulock);
+
+	AST_LIST_UNLOCK(&users);
+
 	ast_cli(fd,"-------------------\n");
 	ast_cli(fd,"%d manager users configured.\n", count_amu);
+
 	return RESULT_SUCCESS;
 }
 
@@ -613,13 +593,13 @@
 "event queue.\n";
 
 static char showmanagers_help[] =
-"Usage: show managers\n"
+"Usage: manager show users\n"
 "       Prints a listing of all managers that are currently configured on that\n"
 " system.\n";
 
 static char showmanager_help[] =
-" Usage: show manager foobar\n"
-" Display all the infos related to the manager foobar.\n";
+" Usage: manager show user <user>\n"
+" Display all information related to the manager user specified.\n";
 
 static struct ast_cli_entry show_managers_cli =
 	{ { "manager", "show", "users" },
@@ -2583,15 +2563,16 @@
 
 int init_manager(void)
 {
-	struct ast_config *cfg;
-	char *val,*cat;
+	struct ast_config *cfg = NULL;
+	char *val, *cat = NULL;
 	int oldportno = portno;
 	static struct sockaddr_in ba;
 	int x = 1;
 	int flags;
 	int webenabled = 0;
 	int newhttptimeout = 60;
-	amus = NULL;
+	struct ast_manager_user *user = NULL;
+
 	if (!registered) {
 		/* Register default actions */
 		ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping);
@@ -2683,58 +2664,81 @@
 #endif
 	}
 
-
-
-		cat = ast_category_browse(cfg, NULL);
-		amus=NULL; /* Resetting the boss */
-		while(cat) {
-			if (!strcasecmp(cat, "general")) {
-				ast_log(LOG_NOTICE, "ignoring the cat general\n");
-				cat = ast_category_browse(cfg, cat);
-				continue;
-			}
-
-			struct ast_manager_user *amu = malloc(sizeof(struct ast_manager_user));
-			memset(amu, 0, sizeof(struct ast_manager_user));
-			struct ast_variable *var;
-			var = ast_variable_browse(cfg, cat);
-
-			while (var) {
-				if (!strcasecmp(var->name, "secret")) {
-					if (amu->secret)
-						free(amu->secret);
-					amu->secret=strdup(var->value);
-				} else if (!strcasecmp(var->name, "deny") ) {
-					if (amu->deny)
-						free(amu->deny);
-					amu->deny=strdup(var->value);
-				} else if (!strcasecmp(var->name, "permit") ) {
-					if (amu->permit)
-						free(amu->permit);
-					amu->permit=strdup(var->value);
-				}  else if (!strcasecmp(var->name, "read") ) {
-					if (amu->read)
-						free(amu->read);
-					amu->read=strdup(var->value);
-				}  else if (!strcasecmp(var->name, "write") ) {
-					if (amu->write)
-						free(amu->write);
-					amu->write=strdup(var->value);
-				}  else if (!strcasecmp(var->name, "displayconnects") ) {
-					amu->displayconnects=ast_true(var->value);
-				} else {
-					ast_log(LOG_DEBUG, "%s is unknown.\n",var->name);
-				}
-				var = var->next;
-			}
-
-			ast_copy_string(amu->username,cat,sizeof(amu->username));
-
-			ast_log(LOG_DEBUG, "Adding %s\n",amu->username);
-			ast_manager_user_add(amu);
-			amu=NULL;
-			cat = ast_category_browse(cfg, cat);
-		}
+	AST_LIST_LOCK(&users);
+
+	while ((cat = ast_category_browse(cfg, cat))) {
+		struct ast_variable *var = NULL;
+
+		if (!strcasecmp(cat, "general"))
+			continue;
+
+		/* Look for an existing entry, if none found - create one and add it to the list */
+		if (!(user = ast_get_manager_by_name_locked(cat))) {
+			if (!(user = ast_calloc(1, sizeof(*user))))
+				break;
+			/* Copy name over */
+			ast_copy_string(user->username, cat, sizeof(user->username));
+			/* Insert into list */
+			AST_LIST_INSERT_TAIL(&users, user, list);
+		}
+
+		/* Make sure we keep this user and don't destroy it during cleanup */
+		user->keep = 1;
+
+		var = ast_variable_browse(cfg, cat);
+		while (var) {
+			if (!strcasecmp(var->name, "secret")) {
+				if (user->secret)
+					free(user->secret);
+				user->secret = ast_strdup(var->value);
+			} else if (!strcasecmp(var->name, "deny") ) {
+				if (user->deny)
+					free(user->deny);
+				user->deny = ast_strdup(var->value);
+			} else if (!strcasecmp(var->name, "permit") ) {
+				if (user->permit)
+					free(user->permit);
+				user->permit = ast_strdup(var->value);
+			}  else if (!strcasecmp(var->name, "read") ) {
+				if (user->read)
+					free(user->read);
+				user->read = ast_strdup(var->value);
+			}  else if (!strcasecmp(var->name, "write") ) {
+				if (user->write)
+					free(user->write);
+				user->write = ast_strdup(var->value);
+			}  else if (!strcasecmp(var->name, "displayconnects") )
+				user->displayconnects = ast_true(var->value);
+			else
+				ast_log(LOG_DEBUG, "%s is an unknown option.\n", var->name);
+			var = var->next;
+		}
+	}
+
+	/* Perform cleanup - essentially prune out old users that no longer exist */
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&users, user, list) {
+		if (user->keep) {
+			user->keep = 0;
+			continue;
+		}
+		/* We do not need to keep this user so take them out of the list */
+		AST_LIST_REMOVE_CURRENT(&users, list);
+		/* Free their memory now */
+		if (user->secret)
+			free(user->secret);
+		if (user->deny)
+			free(user->deny);
+		if (user->permit)
+			free(user->permit);
+		if (user->read)
+			free(user->read);
+		if (user->write)
+			free(user->write);
+		free(user);
+	}
+	AST_LIST_TRAVERSE_SAFE_END
+
+	AST_LIST_UNLOCK(&users);
 
 	ast_config_destroy(cfg);
 



More information about the asterisk-commits mailing list