[asterisk-commits] rmudgett: branch 1.4 r307623 - /branches/1.4/channels/chan_dahdi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Feb 10 18:29:21 CST 2011


Author: rmudgett
Date: Thu Feb 10 18:29:17 2011
New Revision: 307623

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=307623
Log:
Reentrancy problem if outgoing call gets different B channel than requested.

The chan_dahdi pri_fixup_principle() routine needs to protect the Asterisk
channel with the channel lock when it changes the technology private
pointer to a new private structure.

* Added lock protection while pri_fixup_principle() moves a call from one
private structure to another.

* Made some pri_fixup_principle() messages more meaningful.

Partial backport from v1.8 -r300714.

Modified:
    branches/1.4/channels/chan_dahdi.c

Modified: branches/1.4/channels/chan_dahdi.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.4/channels/chan_dahdi.c?view=diff&rev=307623&r1=307622&r2=307623
==============================================================================
--- branches/1.4/channels/chan_dahdi.c (original)
+++ branches/1.4/channels/chan_dahdi.c Thu Feb 10 18:29:17 2011
@@ -8996,7 +8996,7 @@
 }
 
 
-#ifdef HAVE_PRI
+#if defined(HAVE_PRI)
 static struct dahdi_pvt *pri_find_crv(struct dahdi_pri *pri, int crv)
 {
 	struct dahdi_pvt *p;
@@ -9008,8 +9008,54 @@
 	}
 	return NULL;
 }
-
-
+#endif	/* defined(HAVE_PRI) */
+
+#if defined(HAVE_PRI)
+/*!
+ * \internal
+ * \brief Obtain the DAHDI owner channel lock if the owner exists.
+ * \since 1.8
+ *
+ * \param pri DAHDI PRI control structure.
+ * \param chanpos Channel position in the span.
+ *
+ * \note Assumes the pri->lock is already obtained.
+ * \note Assumes the pri->pvts[chanpos]->lock is already obtained.
+ *
+ * \return Nothing
+ */
+static void sig_pri_lock_owner(struct dahdi_pri *pri, int chanpos)
+{
+	for (;;) {
+		if (!pri->pvts[chanpos]->owner) {
+			/* There is no owner lock to get. */
+			break;
+		}
+		if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
+			/* We got the lock */
+			break;
+		}
+		/* We must unlock the PRI to avoid the possibility of a deadlock */
+		ast_mutex_unlock(&pri->lock);
+		DEADLOCK_AVOIDANCE(&pri->pvts[chanpos]->lock);
+		ast_mutex_lock(&pri->lock);
+	}
+}
+#endif	/* defined(HAVE_PRI) */
+
+#if defined(HAVE_PRI)
+/*!
+ * \internal
+ * \brief Find the private structure for the libpri call.
+ *
+ * \param pri Span controller structure.
+ * \param channel LibPRI encoded channel ID.
+ *
+ * \note Assumes the pri->lock is already obtained.
+ *
+ * \retval array-index into private pointer array on success.
+ * \retval -1 on error.
+ */
 static int pri_find_principle(struct dahdi_pri *pri, int channel)
 {
 	int x;
@@ -9037,7 +9083,22 @@
 	
 	return principle;
 }
-
+#endif	/* defined(HAVE_PRI) */
+
+#if defined(HAVE_PRI)
+/*!
+ * \internal
+ * \brief Fixup the private structure associated with the libpri call.
+ *
+ * \param pri Span controller structure.
+ * \param principle Array-index into private array to move call to if not already there.
+ * \param c LibPRI opaque call pointer to find if need to move call.
+ *
+ * \note Assumes the pri->lock is already obtained.
+ *
+ * \retval principle on success.
+ * \retval -1 on error.
+ */
 static int pri_fixup_principle(struct dahdi_pri *pri, int principle, q931_call *c)
 {
 	int x;
@@ -9061,14 +9122,30 @@
 			if (principle != x) {
 				struct dahdi_pvt *new = pri->pvts[principle], *old = pri->pvts[x];
 
-				if (option_verbose > 2)
-					ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
+				/* Get locks to safely move to the new private structure. */
+				ast_mutex_lock(&old->lock);
+				sig_pri_lock_owner(pri, x);
+				ast_mutex_lock(&new->lock);
+
+				if (option_verbose > 2) {
+					ast_verbose(VERBOSE_PREFIX_3
+						"Moving call (%s) from channel %d to %d.\n",
+						old->owner ? old->owner->name : "",
 						old->channel, new->channel);
+				}
 				if (new->owner) {
-					ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
-						old->channel, new->channel, new->channel);
+					ast_log(LOG_WARNING,
+						"Can't move call (%s) from channel %d to %d.  It is already in use.\n",
+						old->owner ? old->owner->name : "",
+						old->channel, new->channel);
+					ast_mutex_unlock(&new->lock);
+					if (old->owner) {
+						ast_mutex_unlock(&old->owner->lock);
+					}
+					ast_mutex_unlock(&old->lock);
 					return -1;
 				}
+
 				/* Fix it all up now */
 				new->owner = old->owner;
 				old->owner = NULL;
@@ -9104,6 +9181,12 @@
 				/* More stuff to transfer to the new channel. */
 				new->call_level = old->call_level;
 				old->call_level = DAHDI_CALL_LEVEL_IDLE;
+ 
+				ast_mutex_unlock(&old->lock);
+				if (new->owner) {
+					ast_mutex_unlock(&new->owner->lock);
+				}
+				ast_mutex_unlock(&new->lock);
 			}
 			return principle;
 		}
@@ -9135,7 +9218,9 @@
 	ast_log(LOG_WARNING, "Call specified, but not found?\n");
 	return -1;
 }
-
+#endif	/* defined(HAVE_PRI) */
+
+#if defined(HAVE_PRI)
 static void *do_idle_thread(void *vchan)
 {
 	struct ast_channel *chan = vchan;




More information about the asterisk-commits mailing list