]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
chan_pjsip: Fix deadlock when masquerading PJSIP channels.
authorRichard Mudgett <rmudgett@digium.com>
Fri, 3 Oct 2014 17:30:07 +0000 (17:30 +0000)
committerRichard Mudgett <rmudgett@digium.com>
Fri, 3 Oct 2014 17:30:07 +0000 (17:30 +0000)
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/

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/12@424471 65c4cc65-6c06-0410-ace0-fbb531ad65f3

18 files changed:
addons/chan_ooh323.c
channels/chan_iax2.c
channels/chan_misdn.c
channels/chan_motif.c
channels/chan_pjsip.c
channels/chan_sip.c
channels/chan_skinny.c
channels/chan_unistim.c
funcs/func_frame_trace.c
include/asterisk/frame.h
include/asterisk/res_pjsip_session.h
include/asterisk/taskprocessor.h
main/bridge_channel.c
main/channel.c
main/core_unreal.c
main/taskprocessor.c
res/res_pjsip_session.c
res/res_pjsip_session.exports.in

index 55ef3e1f89c1f27734a408f02f41b746e3ee2f39..2c8d14ba9e5cbd36056b6a51f83cebfd9c51b97a 100644 (file)
@@ -1391,6 +1391,7 @@ static int ooh323_indicate(struct ast_channel *ast, int condition, const void *d
                break;
        case AST_CONTROL_PROCEEDING:
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
        case -1:
                break;
        default:
index 0edff80e4346ff20ffe6160241a7877fad40de2d..ef0c0965634dbac11c1682da17e1168ed096967f 100644 (file)
@@ -1415,6 +1415,8 @@ static int iax2_is_control_frame_allowed(int subtype)
                /* Only meaningful across a bridge on this machine for direct-media exchange. */
        case AST_CONTROL_PVT_CAUSE_CODE:
                /* Intended only for the sending machine's local channel structure. */
+       case AST_CONTROL_MASQUERADE_NOTIFY:
+               /* Intended only for masquerades when calling ast_indicate_data(). */
        case AST_CONTROL_STREAM_STOP:
        case AST_CONTROL_STREAM_SUSPEND:
        case AST_CONTROL_STREAM_RESTART:
@@ -5723,6 +5725,7 @@ static int iax2_indicate(struct ast_channel *c, int condition, const void *data,
                }
                break;
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
                res = -1;
                goto done;
        }
index 47d5947c3bf57f28fb60790a7904bd6f472cbaeb..dbc48a7f0e0ffcc80895e3b0298c08e4f990b98a 100644 (file)
@@ -7110,6 +7110,7 @@ static int misdn_indication(struct ast_channel *ast, int cond, const void *data,
                chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc->pid);
                /* fallthrough */
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
                return -1;
        }
 
index e3f5b75a5236376eecc0de226111914266ac1118..d222a0316b7f268c9abb562e48aee2cd4b379f89 100644 (file)
@@ -1795,6 +1795,7 @@ static int jingle_indicate(struct ast_channel *ast, int condition, const void *d
        case AST_CONTROL_CONNECTED_LINE:
                break;
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
        case -1:
                res = -1;
                break;
index ffeb960a4d6d4d5692a06c5f373a4d7e4e6805de..c58f57233b736b91bcc9db9a445dc75df299463a 100644 (file)
@@ -663,40 +663,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;
 }
@@ -1178,6 +1162,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;
index 0bb5cb19844728ee9ffa506059b0488e7bc7717c..8d3816ec02366bf0add5d8367e95c9e5dc1c0f9c 100644 (file)
@@ -7880,6 +7880,7 @@ static int sip_indicate(struct ast_channel *ast, int condition, const void *data
                res = -1;
                break;
        case AST_CONTROL_PVT_CAUSE_CODE: /* these should be handled by the code in channel.c */
+       case AST_CONTROL_MASQUERADE_NOTIFY:
        case -1:
                res = -1;
                break;
index 01bca4f23e1ff8f583afac3647ce1480a97b416a..8e98cbabe69615b2a2d5065a0d00be77e9c8b780 100644 (file)
@@ -5381,6 +5381,7 @@ static int skinny_indicate(struct ast_channel *ast, int ind, const void *data, s
                ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
                /* fallthrough */
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
                return -1; /* Tell asterisk to provide inband signalling */
        }
        return 0;
index 784bd33079b9f0d7a10bd273301a4b4012dfdf73..eb2588c64aa137d65b548d1bc31518141bbb1651 100644 (file)
@@ -5231,6 +5231,7 @@ static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
                ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
                /* fallthrough */
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
                return -1;
        }
 
index 36a98d649d888d2e3f4b2bb5c8b93d679c564b52..3d50b87916671a2782a7118c45e795baa91805bf 100644 (file)
@@ -334,6 +334,10 @@ static void print_frame(struct ast_frame *frame)
                case AST_CONTROL_PVT_CAUSE_CODE:
                        ast_verbose("SubClass: PVT_CAUSE_CODE\n");
                        break;
+               case AST_CONTROL_MASQUERADE_NOTIFY:
+                       /* Should never happen. */
+                       ast_assert(0);
+                       break;
                case AST_CONTROL_STREAM_STOP:
                        ast_verbose("SubClass: STREAM_STOP\n");
                        break;
index 846832aff743379d3a0087f72d76954b5cbfbbba..a190bd00284f96d508c56a44c193dcc2e355ad31 100644 (file)
@@ -287,6 +287,7 @@ enum ast_control_frame_type {
        AST_CONTROL_MCID = 31,                  /*!< Indicate that the caller is being malicious. */
        AST_CONTROL_UPDATE_RTP_PEER = 32, /*!< Interrupt the bridge and have it update the peer */
        AST_CONTROL_PVT_CAUSE_CODE = 33, /*!< Contains an update to the protocol-specific cause-code stored for branching dials */
+       AST_CONTROL_MASQUERADE_NOTIFY = 34,     /*!< A masquerade is about to begin/end. (Never sent as a frame but directly with ast_indicate_data().) */
 
        /*
         * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
index 5c88896ebb0a90bb11ec92855039621bda1cfe6a..33afb797e0ff41687601685e55dc37439aa3de32 100644 (file)
@@ -87,6 +87,9 @@ struct ast_sip_session_media {
  */
 struct ast_sip_session_delayed_request;
 
+/*! \brief Opaque struct controlling the suspension of the session's serializer. */
+struct ast_sip_session_suspender;
+
 /*!
  * \brief A structure describing a SIP session
  *
@@ -96,45 +99,47 @@ struct ast_sip_session_delayed_request;
  * to use the term "SIP session" to refer to the INVITE dialog itself.
  */
 struct ast_sip_session {
-       /* Dialplan extension where incoming call is destined */
+       /*! Dialplan extension where incoming call is destined */
        char exten[AST_MAX_EXTENSION];
-       /* The endpoint with which Asterisk is communicating */
+       /*! The endpoint with which Asterisk is communicating */
        struct ast_sip_endpoint *endpoint;
-       /* The contact associated with this session */
+       /*! The contact associated with this session */
        struct ast_sip_contact *contact;
-       /* The PJSIP details of the session, which includes the dialog */
+       /*! The PJSIP details of the session, which includes the dialog */
        struct pjsip_inv_session *inv_session;
-       /* The Asterisk channel associated with the session */
+       /*! The Asterisk channel associated with the session */
        struct ast_channel *channel;
-       /* Registered session supplements */
+       /*! Registered session supplements */
        AST_LIST_HEAD(, ast_sip_session_supplement) supplements;
-       /* Datastores added to the session by supplements to the session */
+       /*! Datastores added to the session by supplements to the session */
        struct ao2_container *datastores;
-       /* Media streams */
+       /*! Media streams */
        struct ao2_container *media;
-       /* Serializer for tasks relating to this SIP session */
+       /*! Serializer for tasks relating to this SIP session */
        struct ast_taskprocessor *serializer;
-       /* Requests that could not be sent due to current inv_session state */
+       /*! Non-null if the session serializer is suspended or being suspended. */
+       struct ast_sip_session_suspender *suspended;
+       /*! Requests that could not be sent due to current inv_session state */
        AST_LIST_HEAD_NOLOCK(, ast_sip_session_delayed_request) delayed_requests;
-       /* When we need to reschedule a reinvite, we use this structure to do it */
+       /*! When we need to reschedule a reinvite, we use this structure to do it */
        pj_timer_entry rescheduled_reinvite;
-       /* Format capabilities pertaining to direct media */
+       /*! Format capabilities pertaining to direct media */
        struct ast_format_cap *direct_media_cap;
-       /* When we need to forcefully end the session */
+       /*! When we need to forcefully end the session */
        pj_timer_entry scheduled_termination;
-       /* Identity of endpoint this session deals with */
+       /*! Identity of endpoint this session deals with */
        struct ast_party_id id;
-       /* Requested capabilities */
+       /*! Requested capabilities */
        struct ast_format_cap *req_caps;
-       /* Codecs overriden by dialplan on an outgoing request */
+       /*! Codecs overriden by dialplan on an outgoing request */
        struct ast_codec_pref override_prefs;
-       /* Optional DSP, used only for inband DTMF detection if configured */
+       /*! Optional DSP, used only for inband DTMF detection if configured */
        struct ast_dsp *dsp;
-       /* Whether the termination of the session should be deferred */
+       /*! Whether the termination of the session should be deferred */
        unsigned int defer_terminate:1;
-       /* Deferred incoming re-invite */
+       /*! Deferred incoming re-invite */
        pjsip_rx_data *deferred_reinvite;
-       /* Current T.38 state */
+       /*! Current T.38 state */
        enum ast_sip_session_t38state t38state;
 };
 
@@ -389,6 +394,28 @@ struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_
 struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
        struct ast_sip_contact *contact, pjsip_inv_session *inv);
 
+/*!
+ * \brief Request and wait for the session serializer to be suspended.
+ * \since 12.7.0
+ *
+ * \param session Which session to suspend the serializer.
+ *
+ * \note No channel locks can be held while calling without risk of deadlock.
+ *
+ * \return Nothing
+ */
+void ast_sip_session_suspend(struct ast_sip_session *session);
+
+/*!
+ * \brief Request the session serializer be unsuspended.
+ * \since 12.7.0
+ *
+ * \param session Which session to unsuspend the serializer.
+ *
+ * \return Nothing
+ */
+void ast_sip_session_unsuspend(struct ast_sip_session *session);
+
 /*!
  * \brief Create a new outgoing SIP session
  *
index ab523290c8f7f8053cf0da3b3507836b8839ddfd..f16f144cb558ef9fcdc130a4ce91da89d8987dfc 100644 (file)
@@ -246,6 +246,16 @@ int ast_taskprocessor_push_local(struct ast_taskprocessor *tps,
  */
 int ast_taskprocessor_execute(struct ast_taskprocessor *tps);
 
+/*!
+ * \brief Am I the given taskprocessor's current task.
+ * \since 12.7.0
+ *
+ * \param tps Taskprocessor to check.
+ *
+ * \retval non-zero if current thread is the taskprocessor thread.
+ */
+int ast_taskprocessor_is_task(struct ast_taskprocessor *tps);
+
 /*!
  * \brief Return the name of the taskprocessor singleton
  * \since 1.6.1
index badb6b2e1738c493c51add0b7451153e34eea52d..db25f09f1e42dd9fcf46d629b8ac6634189ad6e1 100644 (file)
@@ -2049,6 +2049,10 @@ static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_chan
                        ast_indicate(chan, -1);
                }
                break;
+       case AST_CONTROL_MASQUERADE_NOTIFY:
+               /* Should never happen. */
+               ast_assert(0);
+               break;
        default:
                ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
                break;
index c17e28c7a544e464f389340d0ef27537e5707897..e55ec7350088e457a111f6ea7d0ddd68fe272fd9 100644 (file)
@@ -4347,6 +4347,7 @@ static int attribute_const is_visible_indication(enum ast_control_frame_type con
        case AST_CONTROL_MCID:
        case AST_CONTROL_UPDATE_RTP_PEER:
        case AST_CONTROL_PVT_CAUSE_CODE:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
        case AST_CONTROL_STREAM_STOP:
        case AST_CONTROL_STREAM_SUSPEND:
        case AST_CONTROL_STREAM_REVERSE:
@@ -4513,7 +4514,9 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
        ast_channel_lock(chan);
 
        /* Don't bother if the channel is about to go away, anyway. */
-       if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
+       if ((ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
+                       || ast_check_hangup(chan))
+               && condition != AST_CONTROL_MASQUERADE_NOTIFY) {
                res = -1;
                goto indicate_cleanup;
        }
@@ -4661,6 +4664,7 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
        case AST_CONTROL_AOC:
        case AST_CONTROL_END_OF_Q:
        case AST_CONTROL_MCID:
+       case AST_CONTROL_MASQUERADE_NOTIFY:
        case AST_CONTROL_UPDATE_RTP_PEER:
        case AST_CONTROL_STREAM_STOP:
        case AST_CONTROL_STREAM_SUSPEND:
@@ -6410,6 +6414,11 @@ static void channel_do_masquerade(struct ast_channel *original, struct ast_chann
         * original channel's backend.  While the features are nice, which is the
         * reason we're keeping it, it's still awesomely weird. XXX */
 
+       /* Indicate to each channel that a masquerade is about to begin. */
+       x = 1;
+       ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
+       ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
+
        /*
         * The container lock is necessary for proper locking order
         * because the channels must be unlinked to change their
@@ -6450,8 +6459,9 @@ static void channel_do_masquerade(struct ast_channel *original, struct ast_chann
        /* Start the masquerade channel contents rearangement. */
        ast_channel_lock_both(original, clonechan);
 
-       ast_debug(4, "Actually Masquerading %s(%u) into the structure of %s(%u)\n",
-               ast_channel_name(clonechan), ast_channel_state(clonechan), ast_channel_name(original), ast_channel_state(original));
+       ast_debug(1, "Actually Masquerading %s(%u) into the structure of %s(%u)\n",
+               ast_channel_name(clonechan), ast_channel_state(clonechan),
+               ast_channel_name(original), ast_channel_state(original));
 
        /*
         * Remember the original read/write formats.  We turn off any
@@ -6717,6 +6727,19 @@ static void channel_do_masquerade(struct ast_channel *original, struct ast_chann
        ast_channel_unlock(original);
        ast_channel_unlock(clonechan);
 
+       /*
+        * Indicate to each channel that a masquerade is complete.
+        *
+        * We can still do this to clonechan even though it is a
+        * zombie because ast_indicate_data() will explicitly pass
+        * this control and ast_hangup() is held off until the
+        * ast_channel_masq() and ast_channel_masqr() pointers are
+        * cleared.
+        */
+       x = 0;
+       ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
+       ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
+
        ast_bridge_notify_masquerade(original);
 
        if (clone_hold_state == AST_CONTROL_HOLD) {
index c9afa5194e355bebc005bada5fdc2159f0e4675a..814c7bf898fda34e8b1e245862e531b12f30048e 100644 (file)
@@ -519,6 +519,12 @@ int ast_unreal_indicate(struct ast_channel *ast, int condition, const void *data
        ao2_ref(p, 1); /* ref for unreal_queue_frame */
 
        switch (condition) {
+       case AST_CONTROL_MASQUERADE_NOTIFY:
+               /*
+                * Always block this because this is the channel being
+                * masqueraded; not anything down the chain.
+                */
+               break;
        case AST_CONTROL_CONNECTED_LINE:
        case AST_CONTROL_REDIRECTING:
                res = unreal_colp_redirect_indicate(p, ast, condition);
index 0582f76fa86c9ad00c2a0909b738080d68b10410..5fbbca571d2d5171193263d4bd43f515c12ce3f8 100644 (file)
@@ -78,9 +78,9 @@ struct ast_taskprocessor {
        long tps_queue_size;
        /*! \brief Taskprocessor queue */
        AST_LIST_HEAD_NOLOCK(tps_queue, tps_task) tps_queue;
-       /*! \brief Taskprocessor singleton list entry */
-       AST_LIST_ENTRY(ast_taskprocessor) list;
        struct ast_taskprocessor_listener *listener;
+       /*! Current thread executing the tasks */
+       pthread_t thread;
        /*! Indicates if the taskprocessor is currently executing a task */
        unsigned int executing:1;
 };
@@ -600,6 +600,8 @@ static struct ast_taskprocessor *__allocate_taskprocessor(const char *name, stru
        ao2_ref(listener, +1);
        p->listener = listener;
 
+       p->thread = AST_PTHREADT_NULL;
+
        ao2_ref(p, +1);
        listener->tps = p;
 
@@ -752,6 +754,7 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
                return 0;
        }
 
+       tps->thread = pthread_self();
        tps->executing = 1;
 
        if (t->wants_local) {
@@ -768,6 +771,7 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
        tps_task_free(t);
 
        ao2_lock(tps);
+       tps->thread = AST_PTHREADT_NULL;
        /* We need to check size in the same critical section where we reset the
         * executing bit. Avoids a race condition where a task is pushed right
         * after we pop an empty stack.
@@ -789,3 +793,13 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
        }
        return size > 0;
 }
+
+int ast_taskprocessor_is_task(struct ast_taskprocessor *tps)
+{
+       int is_task;
+
+       ao2_lock(tps);
+       is_task = pthread_equal(tps->thread, pthread_self());
+       ao2_unlock(tps);
+       return is_task;
+}
index 832fa95edaea9fe9929485116a6988d29bf4fbb0..d5aee9b2db73f55c99b98cbf10de5eb64e663b6d 100644 (file)
@@ -1223,6 +1223,108 @@ struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
        return session;
 }
 
+/*! \brief struct controlling the suspension of the session's serializer. */
+struct ast_sip_session_suspender {
+       ast_cond_t cond_suspended;
+       ast_cond_t cond_complete;
+       int suspended;
+       int complete;
+};
+
+static void sip_session_suspender_dtor(void *vdoomed)
+{
+       struct ast_sip_session_suspender *doomed = vdoomed;
+
+       ast_cond_destroy(&doomed->cond_suspended);
+       ast_cond_destroy(&doomed->cond_complete);
+}
+
+/*!
+ * \internal
+ * \brief Block the session serializer thread task.
+ *
+ * \param data Pushed serializer task data for suspension.
+ *
+ * \retval 0
+ */
+static int sip_session_suspend_task(void *data)
+{
+       struct ast_sip_session_suspender *suspender = data;
+
+       ao2_lock(suspender);
+
+       /* Signal that the serializer task is now suspended. */
+       suspender->suspended = 1;
+       ast_cond_signal(&suspender->cond_suspended);
+
+       /* Wait for the the serializer suspension to be completed. */
+       while (!suspender->complete) {
+               ast_cond_wait(&suspender->cond_complete, ao2_object_get_lockaddr(suspender));
+       }
+
+       ao2_unlock(suspender);
+       ao2_ref(suspender, -1);
+
+       return 0;
+}
+
+void ast_sip_session_suspend(struct ast_sip_session *session)
+{
+       struct ast_sip_session_suspender *suspender;
+       int res;
+
+       ast_assert(session->suspended == NULL);
+
+       if (ast_taskprocessor_is_task(session->serializer)) {
+               /* I am the session's serializer thread so I cannot suspend. */
+               return;
+       }
+
+       suspender = ao2_alloc(sizeof(*suspender), sip_session_suspender_dtor);
+       if (!suspender) {
+               /* We will just have to hope that the system does not deadlock */
+               return;
+       }
+       ast_cond_init(&suspender->cond_suspended, NULL);
+       ast_cond_init(&suspender->cond_complete, NULL);
+
+       ao2_ref(suspender, +1);
+       res = ast_sip_push_task(session->serializer, sip_session_suspend_task, suspender);
+       if (res) {
+               /* We will just have to hope that the system does not deadlock */
+               ao2_ref(suspender, -2);
+               return;
+       }
+
+       session->suspended = suspender;
+
+       /* Wait for the serializer to get suspended. */
+       ao2_lock(suspender);
+       while (!suspender->suspended) {
+               ast_cond_wait(&suspender->cond_suspended, ao2_object_get_lockaddr(suspender));
+       }
+       ao2_unlock(suspender);
+}
+
+void ast_sip_session_unsuspend(struct ast_sip_session *session)
+{
+       struct ast_sip_session_suspender *suspender = session->suspended;
+
+       if (!suspender) {
+               /* Nothing to do */
+               return;
+       }
+       session->suspended = NULL;
+
+       /* Signal that the serializer task suspension is now complete. */
+       ao2_lock(suspender);
+       suspender->complete = 1;
+       ast_cond_signal(&suspender->cond_complete);
+       ao2_unlock(suspender);
+
+       ao2_ref(suspender, -1);
+}
+
 static int session_outbound_auth(pjsip_dialog *dlg, pjsip_tx_data *tdata, void *user_data)
 {
        pjsip_inv_session *inv = pjsip_dlg_get_inv_session(dlg);
index 55274f9cb014414ed00d7433f3871158cde47a3b..07f26b7e9bc8bb178e2bbf7a38309c0c70610637 100644 (file)
@@ -15,6 +15,8 @@
                LINKER_SYMBOL_PREFIXast_sip_session_send_request;
                LINKER_SYMBOL_PREFIXast_sip_session_create_invite;
                LINKER_SYMBOL_PREFIXast_sip_session_create_outgoing;
+               LINKER_SYMBOL_PREFIXast_sip_session_suspend;
+               LINKER_SYMBOL_PREFIXast_sip_session_unsuspend;
                LINKER_SYMBOL_PREFIXast_sip_dialog_get_session;
                LINKER_SYMBOL_PREFIXast_sip_session_resume_reinvite;
                LINKER_SYMBOL_PREFIXast_sip_channel_pvt_alloc;