[asterisk-commits] tilghman: branch 1.6.1 r187486 - in /branches/1.6.1: ./ include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 9 13:54:11 CDT 2009


Author: tilghman
Date: Thu Apr  9 13:54:07 2009
New Revision: 187486

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=187486
Log:
Merged revisions 187483 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/trunk

................
  r187483 | tilghman | 2009-04-09 13:40:01 -0500 (Thu, 09 Apr 2009) | 15 lines
  
  Merged revisions 187428 via svnmerge from 
  https://origsvn.digium.com/svn/asterisk/branches/1.4
  
  ........
    r187428 | tilghman | 2009-04-09 13:08:20 -0500 (Thu, 09 Apr 2009) | 8 lines
    
    Race condition between ast_cli_command() and 'module unload' could cause a deadlock.
    Add lock timeouts to avoid this potential deadlock.
    (closes issue #14705)
     Reported by: jamessan
     Patches: 
           20090320__bug14705.diff.txt uploaded by tilghman (license 14)
     Tested by: jamessan
  ........
................

Modified:
    branches/1.6.1/   (props changed)
    branches/1.6.1/include/asterisk/linkedlists.h
    branches/1.6.1/include/asterisk/lock.h
    branches/1.6.1/main/manager.c

Propchange: branches/1.6.1/
------------------------------------------------------------------------------
Binary property 'trunk-merged' - no diff available.

Modified: branches/1.6.1/include/asterisk/linkedlists.h
URL: http://svn.digium.com/svn-view/asterisk/branches/1.6.1/include/asterisk/linkedlists.h?view=diff&rev=187486&r1=187485&r2=187486
==============================================================================
--- branches/1.6.1/include/asterisk/linkedlists.h (original)
+++ branches/1.6.1/include/asterisk/linkedlists.h Thu Apr  9 13:54:07 2009
@@ -52,6 +52,18 @@
         ast_rwlock_wrlock(&(head)->lock)
 
 /*!
+  \brief Write locks a list, with timeout.
+  \param head This is a pointer to the list head structure
+  \param tv Pointer to a timeval structure
+
+  This macro attempts to place an exclusive write lock in the
+  list head structure pointed to by head.
+  \retval 0 on success
+  \retval non-zero on failure
+*/
+#define	AST_RWLIST_TIMEDWRLOCK(head,tv)	ast_rwlock_timedwrlock(&(head)->lock, tv)
+
+/*!
   \brief Read locks a list.
   \param head This is a pointer to the list head structure
 
@@ -62,6 +74,19 @@
 */
 #define AST_RWLIST_RDLOCK(head)                                         \
         ast_rwlock_rdlock(&(head)->lock)
+
+/*!
+  \brief Read locks a list, with timeout.
+  \param head This is a pointer to the list head structure
+  \param tv Pointer to a timeval structure
+
+  This macro attempts to place a read lock in the
+  list head structure pointed to by head.
+  \retval 0 on success
+  \retval non-zero on failure
+*/
+#define AST_RWLIST_TIMEDRDLOCK(head,tv)                                 \
+        ast_rwlock_timedrdlock(&(head)->lock, tv)
 
 /*!
   \brief Locks a list, without blocking if the list is locked.

Modified: branches/1.6.1/include/asterisk/lock.h
URL: http://svn.digium.com/svn-view/asterisk/branches/1.6.1/include/asterisk/lock.h?view=diff&rev=187486&r1=187485&r2=187486
==============================================================================
--- branches/1.6.1/include/asterisk/lock.h (original)
+++ branches/1.6.1/include/asterisk/lock.h Thu Apr  9 13:54:07 2009
@@ -1353,15 +1353,18 @@
 	return res;
 }
 
-static inline int _ast_rwlock_tryrdlock(ast_rwlock_t *t, const char *name,
-	const char *filename, int line, const char *func)
+#define ast_rwlock_timedrdlock(a,b) \
+	_ast_rwlock_timedrdlock(a, # a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+static inline int _ast_rwlock_timedrdlock(ast_rwlock_t *t, const char *name,
+	const struct timespec *abs_timeout, const char *filename, int line, const char *func)
 {
 	int res;
 	struct ast_lock_track *lt = &t->track;
+	int canlog = strcmp(filename, "logger.c") & t->tracking;
 #ifdef HAVE_BKTR
 	struct ast_bt *bt = NULL;
 #endif
-
 
 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
 	if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
@@ -1386,13 +1389,13 @@
 			bt = &lt->backtrace[lt->reentrancy];
 		}
 		ast_reentrancy_unlock(lt);
-		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
-#else
-		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
-#endif
-	}
-
-	if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
+		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
+#else
+		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
+#endif
+	}
+	res = pthread_rwlock_timedrdlock(&t->lock, abs_timeout);
+	if (!res) {
 		ast_reentrancy_lock(lt);
 		if (lt->reentrancy < AST_MAX_REENTRANCY) {
 			lt->file[lt->reentrancy] = filename;
@@ -1405,21 +1408,41 @@
 		if (t->tracking) {
 			ast_mark_lock_acquired(t);
 		}
-	} else if (t->tracking) {
-		ast_mark_lock_failed(t);
+	} else {
+#ifdef HAVE_BKTR
+		if (lt->reentrancy) {
+			ast_reentrancy_lock(lt);
+			bt = &lt->backtrace[lt->reentrancy-1];
+		} else {
+			bt = NULL;
+		}
+		if (t->tracking) {
+			ast_remove_lock_info(t, bt);
+		}
+#else
+		if (t->tracking) {
+			ast_remove_lock_info(t);
+		}
+#endif
+		__ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
+				filename, line, func, strerror(res));
+		DO_THREAD_CRASH;
 	}
 	return res;
 }
 
-static inline int _ast_rwlock_trywrlock(ast_rwlock_t *t, const char *name,
-	const char *filename, int line, const char *func)
+#define ast_rwlock_timedwrlock(a,b) \
+	_ast_rwlock_timedwrlock(a, # a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+static inline int _ast_rwlock_timedwrlock(ast_rwlock_t *t, const char *name,
+	const struct timespec *abs_timeout, const char *filename, int line, const char *func)
 {
 	int res;
-	struct ast_lock_track *lt= &t->track;
+	struct ast_lock_track *lt = &t->track;
+	int canlog = strcmp(filename, "logger.c") & t->tracking;
 #ifdef HAVE_BKTR
 	struct ast_bt *bt = NULL;
 #endif
-
 
 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
 	if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
@@ -1449,6 +1472,139 @@
 		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
 #endif
 	}
+	res = pthread_rwlock_timedwrlock(&t->lock, abs_timeout);
+	if (!res) {
+		ast_reentrancy_lock(lt);
+		if (lt->reentrancy < AST_MAX_REENTRANCY) {
+			lt->file[lt->reentrancy] = filename;
+			lt->lineno[lt->reentrancy] = line;
+			lt->func[lt->reentrancy] = func;
+			lt->thread[lt->reentrancy] = pthread_self();
+			lt->reentrancy++;
+		}
+		ast_reentrancy_unlock(lt);
+		if (t->tracking) {
+			ast_mark_lock_acquired(t);
+		}
+	} else {
+#ifdef HAVE_BKTR
+		if (lt->reentrancy) {
+			ast_reentrancy_lock(lt);
+			bt = &lt->backtrace[lt->reentrancy-1];
+		} else {
+			bt = NULL;
+		}
+		if (t->tracking) {
+			ast_remove_lock_info(t, bt);
+		}
+#else
+		if (t->tracking) {
+			ast_remove_lock_info(t);
+		}
+#endif
+		__ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
+				filename, line, func, strerror(res));
+		DO_THREAD_CRASH;
+	}
+	return res;
+}
+
+static inline int _ast_rwlock_tryrdlock(ast_rwlock_t *t, const char *name,
+	const char *filename, int line, const char *func)
+{
+	int res;
+	struct ast_lock_track *lt = &t->track;
+#ifdef HAVE_BKTR
+	struct ast_bt *bt = NULL;
+#endif
+
+
+#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
+	if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
+		 /* Don't warn abount uninitialized lock.
+		  * Simple try to initialize it.
+		  * May be not needed in linux system.
+		  */
+		res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
+		if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
+			__ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
+					filename, line, func, name);
+			return res;
+		}
+	}
+#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
+
+	if (t->tracking) {
+#ifdef HAVE_BKTR
+		ast_reentrancy_lock(lt);
+		if (lt->reentrancy != AST_MAX_REENTRANCY) {
+			ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
+			bt = &lt->backtrace[lt->reentrancy];
+		}
+		ast_reentrancy_unlock(lt);
+		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
+#else
+		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
+#endif
+	}
+
+	if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
+		ast_reentrancy_lock(lt);
+		if (lt->reentrancy < AST_MAX_REENTRANCY) {
+			lt->file[lt->reentrancy] = filename;
+			lt->lineno[lt->reentrancy] = line;
+			lt->func[lt->reentrancy] = func;
+			lt->thread[lt->reentrancy] = pthread_self();
+			lt->reentrancy++;
+		}
+		ast_reentrancy_unlock(lt);
+		if (t->tracking) {
+			ast_mark_lock_acquired(t);
+		}
+	} else if (t->tracking) {
+		ast_mark_lock_failed(t);
+	}
+	return res;
+}
+
+static inline int _ast_rwlock_trywrlock(ast_rwlock_t *t, const char *name,
+	const char *filename, int line, const char *func)
+{
+	int res;
+	struct ast_lock_track *lt= &t->track;
+#ifdef HAVE_BKTR
+	struct ast_bt *bt = NULL;
+#endif
+
+
+#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
+	if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
+		 /* Don't warn abount uninitialized lock.
+		  * Simple try to initialize it.
+		  * May be not needed in linux system.
+		  */
+		res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
+		if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
+			__ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
+					filename, line, func, name);
+			return res;
+		}
+	}
+#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
+
+	if (t->tracking) {
+#ifdef HAVE_BKTR
+		ast_reentrancy_lock(lt);
+		if (lt->reentrancy != AST_MAX_REENTRANCY) {
+			ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
+			bt = &lt->backtrace[lt->reentrancy];
+		}
+		ast_reentrancy_unlock(lt);
+		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
+#else
+		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
+#endif
+	}
 
 	if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
 		ast_reentrancy_lock(lt);
@@ -1601,6 +1757,11 @@
 	return pthread_rwlock_rdlock(prwlock);
 }
 
+static inline int ast_rwlock_timedrdlock(ast_rwlock_t *prwlock, const struct timespec *abs_timeout)
+{
+	return pthread_rwlock_timedrdlock(prwlock, abs_timeout);
+}
+
 static inline int ast_rwlock_tryrdlock(ast_rwlock_t *prwlock)
 {
 	return pthread_rwlock_tryrdlock(prwlock);
@@ -1609,6 +1770,11 @@
 static inline int ast_rwlock_wrlock(ast_rwlock_t *prwlock)
 {
 	return pthread_rwlock_wrlock(prwlock);
+}
+
+static inline int ast_rwlock_timedwrlock(ast_rwlock_t *prwlock, const struct timespec *abs_timeout)
+{
+	return pthread_rwlock_timedwrlock(prwlock, abs_timeout);
 }
 
 static inline int ast_rwlock_trywrlock(ast_rwlock_t *prwlock)

Modified: branches/1.6.1/main/manager.c
URL: http://svn.digium.com/svn-view/asterisk/branches/1.6.1/main/manager.c?view=diff&rev=187486&r1=187485&r2=187486
==============================================================================
--- branches/1.6.1/main/manager.c (original)
+++ branches/1.6.1/main/manager.c Thu Apr  9 13:54:07 2009
@@ -3338,8 +3338,12 @@
 int ast_manager_unregister(char *action)
 {
 	struct manager_action *cur;
-
-	AST_RWLIST_WRLOCK(&actions);
+	struct timespec tv = { 5, };
+
+	if (AST_RWLIST_TIMEDWRLOCK(&actions, &tv)) {
+		ast_log(LOG_ERROR, "Could not obtain lock on manager list\n");
+		return -1;
+	}
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&actions, cur, list) {
 		if (!strcasecmp(action, cur->action)) {
 			AST_RWLIST_REMOVE_CURRENT(list);
@@ -3348,7 +3352,7 @@
 			break;
 		}
 	}
-	AST_RWLIST_TRAVERSE_SAFE_END;
+	AST_RWLIST_TRAVERSE_SAFE_END
 	AST_RWLIST_UNLOCK(&actions);
 
 	return 0;
@@ -3367,8 +3371,12 @@
 static int ast_manager_register_struct(struct manager_action *act)
 {
 	struct manager_action *cur, *prev = NULL;
-
-	AST_RWLIST_WRLOCK(&actions);
+	struct timespec tv = { 5, };
+
+	if (AST_RWLIST_TIMEDWRLOCK(&actions, &tv)) {
+		ast_log(LOG_ERROR, "Could not obtain lock on manager list\n");
+		return -1;
+	}
 	AST_RWLIST_TRAVERSE(&actions, cur, list) {
 		int ret = strcasecmp(cur->action, act->action);
 		if (ret == 0) {
@@ -3381,8 +3389,8 @@
 			break;
 		}
 	}
-	
-	if (prev)	
+
+	if (prev)
 		AST_RWLIST_INSERT_AFTER(&actions, prev, act, list);
 	else
 		AST_RWLIST_INSERT_HEAD(&actions, act, list);
@@ -3409,7 +3417,10 @@
 	cur->synopsis = synopsis;
 	cur->description = description;
 
-	ast_manager_register_struct(cur);
+	if (ast_manager_register_struct(cur)) {
+		ast_free(cur);
+		return -1;
+	}
 
 	return 0;
 }




More information about the asterisk-commits mailing list