<p>George Joseph <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10617">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Kevin Harwell: Looks good to me, but someone else must approve
Benjamin Keith Ford: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip: Patch for res_pjsip_* module load/reload crash<br><br>The session_supplements for the pjsip makes crashes when the module<br>load/unload.<br><br>ASTERISK-28157<br><br>Change-Id: I5b82be3a75d702cf1933d8d1417f44aa10ad1029<br>---<br>M include/asterisk/res_pjsip_session.h<br>M res/res_pjsip/pjsip_message_filter.c<br>M res/res_pjsip/pjsip_session.c<br>M res/res_pjsip_session.c<br>4 files changed, 94 insertions(+), 22 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h</span><br><span>index ccee8c9..34f4783 100644</span><br><span>--- a/include/asterisk/res_pjsip_session.h</span><br><span>+++ b/include/asterisk/res_pjsip_session.h</span><br><span>@@ -258,6 +258,8 @@</span><br><span> * processing to incoming and outgoing SIP requests and responses</span><br><span> */</span><br><span> struct ast_sip_session_supplement {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Reference module info */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_module *module;</span><br><span> /*! Method on which to call the callbacks. If NULL, call on all methods */</span><br><span> const char *method;</span><br><span> /*! Priority for this supplement. Lower numbers are visited before higher numbers */</span><br><span>@@ -580,9 +582,13 @@</span><br><span> * set channel data based on headers in an incoming message. Similarly,</span><br><span> * a module could reject an incoming request if desired.</span><br><span> *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param module Referenced module(NULL safe)</span><br><span> * \param supplement The supplement to register</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-void ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement);</span><br><span style="color: hsl(120, 100%, 40%);">+void ast_sip_session_register_supplement_with_module(struct ast_module *module, struct ast_sip_session_supplement *supplement);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define ast_sip_session_register_supplement(supplement) \</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_register_supplement_with_module(AST_MODULE_SELF, supplement)</span><br><span> </span><br><span> /*!</span><br><span> * \brief Unregister a an supplement to SIP session processing</span><br><span>@@ -599,6 +605,13 @@</span><br><span> int ast_sip_session_add_supplements(struct ast_sip_session *session);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Remove supplements from a SIP session</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param session The session to remove</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void ast_sip_session_remove_supplements(struct ast_sip_session *session);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Alternative for ast_datastore_alloc()</span><br><span> *</span><br><span> * There are two major differences between this and ast_datastore_alloc()</span><br><span>diff --git a/res/res_pjsip/pjsip_message_filter.c b/res/res_pjsip/pjsip_message_filter.c</span><br><span>index f948c44..4b0f60d 100644</span><br><span>--- a/res/res_pjsip/pjsip_message_filter.c</span><br><span>+++ b/res/res_pjsip/pjsip_message_filter.c</span><br><span>@@ -24,6 +24,7 @@</span><br><span> #include "asterisk/res_pjsip.h"</span><br><span> #include "asterisk/res_pjsip_session.h"</span><br><span> #include "include/res_pjsip_private.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/module.h"</span><br><span> </span><br><span> #define MOD_DATA_RESTRICTIONS "restrictions"</span><br><span> </span><br><span>diff --git a/res/res_pjsip/pjsip_session.c b/res/res_pjsip/pjsip_session.c</span><br><span>index f3f3a4d..7dc6221 100644</span><br><span>--- a/res/res_pjsip/pjsip_session.c</span><br><span>+++ b/res/res_pjsip/pjsip_session.c</span><br><span>@@ -32,12 +32,16 @@</span><br><span> </span><br><span> AST_RWLIST_HEAD_STATIC(session_supplements, ast_sip_session_supplement);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-void ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement)</span><br><span style="color: hsl(120, 100%, 40%);">+void ast_sip_session_register_supplement_with_module(struct ast_module *module, struct ast_sip_session_supplement *supplement)</span><br><span> {</span><br><span> struct ast_sip_session_supplement *iter;</span><br><span> int inserted = 0;</span><br><span> SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ ast_assert(supplement != NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ supplement->module = module;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (!supplement->response_priority) {</span><br><span> supplement->response_priority = AST_SIP_SESSION_BEFORE_MEDIA;</span><br><span> }</span><br><span>@@ -94,8 +98,34 @@</span><br><span> if (!copy) {</span><br><span> return -1;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* referenced session created. increasing module reference. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_module_ref(copy->module);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> AST_LIST_INSERT_TAIL(&session->supplements, copy, next);</span><br><span> }</span><br><span> </span><br><span> return 0;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void ast_sip_session_remove_supplements(struct ast_sip_session *session)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_supplement *iter;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!session) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* free the supplements */</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((iter = AST_LIST_REMOVE_HEAD(&session->supplements, next))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (iter->module) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* referenced session closed. decreasing modue reference. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_module_unref(iter->module);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(iter);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c</span><br><span>index 94240d3..99e0ca4 100644</span><br><span>--- a/res/res_pjsip_session.c</span><br><span>+++ b/res/res_pjsip_session.c</span><br><span>@@ -58,6 +58,9 @@</span><br><span> #define DEFAULT_NUM_SESSION_MEDIA 2</span><br><span> </span><br><span> /* Some forward declarations */</span><br><span style="color: hsl(120, 100%, 40%);">+static void handle_session_begin(struct ast_sip_session *session);</span><br><span style="color: hsl(120, 100%, 40%);">+static void handle_session_end(struct ast_sip_session *session);</span><br><span style="color: hsl(120, 100%, 40%);">+static void handle_session_destroy(struct ast_sip_session *session);</span><br><span> static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata);</span><br><span> static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,</span><br><span> enum ast_sip_session_response_priority response_priority);</span><br><span>@@ -2088,7 +2091,6 @@</span><br><span> static void session_destructor(void *obj)</span><br><span> {</span><br><span> struct ast_sip_session *session = obj;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_session_supplement *supplement;</span><br><span> struct ast_sip_session_delayed_request *delay;</span><br><span> const char *endpoint_name = session->endpoint ?</span><br><span> ast_sorcery_object_get_id(session->endpoint) : "<none>";</span><br><span>@@ -2104,19 +2106,18 @@</span><br><span> , session->contact ? ast_sorcery_object_get_id(session->contact) : "<none>"</span><br><span> );</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- while ((supplement = AST_LIST_REMOVE_HEAD(&session->supplements, next))) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (supplement->session_destroy) {</span><br><span style="color: hsl(0, 100%, 40%);">- supplement->session_destroy(session);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(supplement);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* fire session destroy handler */</span><br><span style="color: hsl(120, 100%, 40%);">+ handle_session_destroy(session);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* remove all registered supplements */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_remove_supplements(session);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_DESTROY(&session->supplements);</span><br><span> </span><br><span> ast_taskprocessor_unreference(session->serializer);</span><br><span> ao2_cleanup(session->datastores);</span><br><span> ast_sip_session_media_state_free(session->active_media_state);</span><br><span> ast_sip_session_media_state_free(session->pending_media_state);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_DESTROY(&session->supplements);</span><br><span> while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {</span><br><span> delayed_request_free(delay);</span><br><span> }</span><br><span>@@ -2165,7 +2166,6 @@</span><br><span> {</span><br><span> RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);</span><br><span> struct ast_sip_session *ret_session;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_session_supplement *iter;</span><br><span> int dsp_features = 0;</span><br><span> </span><br><span> session = ao2_alloc(sizeof(*session), session_destructor);</span><br><span>@@ -2247,11 +2247,9 @@</span><br><span> ao2_ref(session, -1);</span><br><span> return NULL;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&session->supplements, iter, next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (iter->session_begin) {</span><br><span style="color: hsl(0, 100%, 40%);">- iter->session_begin(session);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Fire seesion begin handlers */</span><br><span style="color: hsl(120, 100%, 40%);">+ handle_session_begin(session);</span><br><span> </span><br><span> /* Avoid unnecessary ref manipulation to return a session */</span><br><span> ret_session = session;</span><br><span>@@ -3316,6 +3314,40 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void handle_session_begin(struct ast_sip_session *session)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_supplement *iter;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&session->supplements, iter, next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (iter->session_begin) {</span><br><span style="color: hsl(120, 100%, 40%);">+ iter->session_begin(session);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void handle_session_destroy(struct ast_sip_session *session)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_supplement *iter;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&session->supplements, iter, next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (iter->session_destroy) {</span><br><span style="color: hsl(120, 100%, 40%);">+ iter->session_destroy(session);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void handle_session_end(struct ast_sip_session *session)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_session_supplement *iter;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Session is dead. Notify the supplements. */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&session->supplements, iter, next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (iter->session_end) {</span><br><span style="color: hsl(120, 100%, 40%);">+ iter->session_end(session);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,</span><br><span> enum ast_sip_session_response_priority response_priority)</span><br><span> {</span><br><span>@@ -3393,17 +3425,13 @@</span><br><span> static int session_end(void *vsession)</span><br><span> {</span><br><span> struct ast_sip_session *session = vsession;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_session_supplement *iter;</span><br><span> </span><br><span> /* Stop the scheduled termination */</span><br><span> sip_session_defer_termination_stop_timer(session);</span><br><span> </span><br><span> /* Session is dead. Notify the supplements. */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&session->supplements, iter, next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (iter->session_end) {</span><br><span style="color: hsl(0, 100%, 40%);">- iter->session_end(session);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ handle_session_end(session);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10617">change 10617</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10617"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I5b82be3a75d702cf1933d8d1417f44aa10ad1029 </div>
<div style="display:none"> Gerrit-Change-Number: 10617 </div>
<div style="display:none"> Gerrit-PatchSet: 11 </div>
<div style="display:none"> Gerrit-Owner: sungtae kim <pchero21@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation (1000185) </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua C. Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: sungtae kim <pchero21@gmail.com> </div>