chan_pjsip: Fix deadlock when masquerading PJSIP channels.

Performing a directed call pickup resulted in a deadlock when PJSIP
channels were involved.

A masquerade needs to hold onto the channel locks while it swaps channel
information between the two channels involved in the masquerade.  With
PJSIP channels, the fixup routine needed to push a fixup task onto the
PJSIP channel's serializer.  Unfortunately, if the serializer was also
processing a task that needed to lock the channel, you get deadlock.

* Added a new control frame that is used to notify the channels that a
masquerade is about to start and when it has completed.

* Added the ability to query taskprocessors if the current thread is the
taskprocessor thread.

* Added the ability to suspend/unsuspend the PJSIP serializer thread so a
masquerade could fixup the PJSIP channel without using the serializer.

ASTERISK-24356 #close
Reported by: rmudgett

Review: https://reviewboard.asterisk.org/r/4034/
........

Merged revisions 424471 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@424472 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Richard Mudgett
2014-10-03 17:39:50 +00:00
parent b67094624d
commit 6a844be566
18 changed files with 253 additions and 49 deletions

View File

@@ -701,40 +701,24 @@ static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
return res;
}
struct fixup_data {
struct ast_sip_session *session;
struct ast_channel *chan;
};
static int fixup(void *data)
{
struct fixup_data *fix_data = data;
struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(fix_data->chan);
struct chan_pjsip_pvt *pvt = channel->pvt;
channel->session->channel = fix_data->chan;
set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(fix_data->chan));
return 0;
}
/*! \brief Function called by core to change the underlying owner channel */
static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{
struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
struct fixup_data fix_data;
fix_data.session = channel->session;
fix_data.chan = newchan;
struct chan_pjsip_pvt *pvt = channel->pvt;
if (channel->session->channel != oldchan) {
return -1;
}
if (ast_sip_push_task_synchronous(channel->session->serializer, fixup, &fix_data)) {
ast_log(LOG_WARNING, "Unable to perform channel fixup\n");
return -1;
}
/*
* The masquerade has suspended the channel's session
* serializer so we can safely change it outside of
* the serializer thread.
*/
channel->session->channel = newchan;
set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(newchan));
return 0;
}
@@ -1211,6 +1195,24 @@ static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const voi
case AST_CONTROL_PVT_CAUSE_CODE:
res = -1;
break;
case AST_CONTROL_MASQUERADE_NOTIFY:
ast_assert(datalen == sizeof(int));
if (*(int *) data) {
/*
* Masquerade is beginning:
* Wait for session serializer to get suspended.
*/
ast_channel_unlock(ast);
ast_sip_session_suspend(channel->session);
ast_channel_lock(ast);
} else {
/*
* Masquerade is complete:
* Unsuspend the session serializer.
*/
ast_sip_session_unsuspend(channel->session);
}
break;
case AST_CONTROL_HOLD:
chan_pjsip_add_hold(ast_channel_uniqueid(ast));
device_buf_size = strlen(ast_channel_name(ast)) + 1;