mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-10-31 02:37:10 +00:00 
			
		
		
		
	res_pjsip: Patch for res_pjsip_* module load/reload crash
The session_supplements for the pjsip makes crashes when the module load/unload. ASTERISK-28157 Change-Id: I5b82be3a75d702cf1933d8d1417f44aa10ad1029
This commit is contained in:
		
				
					committed by
					
						 Corey Farrell
						Corey Farrell
					
				
			
			
				
	
			
			
			
						parent
						
							0e096aefac
						
					
				
				
					commit
					1b6df87816
				
			| @@ -29,22 +29,50 @@ | ||||
| #include "asterisk/lock.h" | ||||
| #include "asterisk/module.h" | ||||
|  | ||||
|  | ||||
| AST_RWLIST_HEAD_STATIC(session_supplements, ast_sip_session_supplement); | ||||
|  | ||||
| void ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement) | ||||
| /* This structure is used to support module references without | ||||
|  * breaking the ABI of the public struct ast_sip_session_supplement. */ | ||||
| struct private_sip_session_supplement { | ||||
| 	/*! \brief This will be added to the session_supplements list. | ||||
| 	 * | ||||
| 	 * This field must be at offset 0 to enable retrieval of this | ||||
| 	 * structure from session_supplements. | ||||
| 	 */ | ||||
| 	struct ast_sip_session_supplement copy; | ||||
| 	/*! Users of this session supplement will hold a reference to the module. */ | ||||
| 	struct ast_module *module; | ||||
| 	/*! Pointer to the static variable for unregister comparisons. */ | ||||
| 	struct ast_sip_session_supplement *original; | ||||
| }; | ||||
|  | ||||
| void ast_sip_session_register_supplement_with_module(struct ast_module *module, struct ast_sip_session_supplement *supplement) | ||||
| { | ||||
| 	struct ast_sip_session_supplement *iter; | ||||
| 	struct private_sip_session_supplement *priv; | ||||
| 	int inserted = 0; | ||||
| 	SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK); | ||||
|  | ||||
| 	ast_assert(supplement != NULL); | ||||
|  | ||||
| 	if (!supplement->response_priority) { | ||||
| 		supplement->response_priority = AST_SIP_SESSION_BEFORE_MEDIA; | ||||
| 	} | ||||
|  | ||||
| 	priv = ast_calloc(1, sizeof(*priv)); | ||||
| 	if (!priv) { | ||||
| 		/* Really can't do anything here, just assert and return. */ | ||||
| 		ast_assert(priv != NULL); | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	priv->copy = *supplement; | ||||
| 	priv->module = module; | ||||
| 	priv->original = supplement; | ||||
|  | ||||
| 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) { | ||||
| 		if (iter->priority > supplement->priority) { | ||||
| 			AST_RWLIST_INSERT_BEFORE_CURRENT(supplement, next); | ||||
| 			AST_RWLIST_INSERT_BEFORE_CURRENT(&priv->copy, next); | ||||
| 			inserted = 1; | ||||
| 			break; | ||||
| 		} | ||||
| @@ -52,7 +80,7 @@ void ast_sip_session_register_supplement(struct ast_sip_session_supplement *supp | ||||
| 	AST_RWLIST_TRAVERSE_SAFE_END; | ||||
|  | ||||
| 	if (!inserted) { | ||||
| 		AST_RWLIST_INSERT_TAIL(&session_supplements, supplement, next); | ||||
| 		AST_RWLIST_INSERT_TAIL(&session_supplements, &priv->copy, next); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -62,17 +90,20 @@ void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *su | ||||
| 	SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK); | ||||
|  | ||||
| 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) { | ||||
| 		if (supplement == iter) { | ||||
| 		struct private_sip_session_supplement *priv = (void*)iter; | ||||
|  | ||||
| 		if (supplement == priv->original) { | ||||
| 			AST_RWLIST_REMOVE_CURRENT(next); | ||||
| 			ast_free(priv); | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	AST_RWLIST_TRAVERSE_SAFE_END; | ||||
| } | ||||
|  | ||||
| static struct ast_sip_session_supplement *supplement_dup(const struct ast_sip_session_supplement *src) | ||||
| static struct private_sip_session_supplement *supplement_dup(const struct private_sip_session_supplement *src) | ||||
| { | ||||
| 	struct ast_sip_session_supplement *dst = ast_calloc(1, sizeof(*dst)); | ||||
| 	struct private_sip_session_supplement *dst = ast_calloc(1, sizeof(*dst)); | ||||
|  | ||||
| 	if (!dst) { | ||||
| 		return NULL; | ||||
| @@ -89,13 +120,47 @@ int ast_sip_session_add_supplements(struct ast_sip_session *session) | ||||
| 	SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK); | ||||
|  | ||||
| 	AST_RWLIST_TRAVERSE(&session_supplements, iter, next) { | ||||
| 		struct ast_sip_session_supplement *copy = supplement_dup(iter); | ||||
| 		struct private_sip_session_supplement *dup = supplement_dup((void*)iter); | ||||
|  | ||||
| 		if (!copy) { | ||||
| 		if (!dup) { | ||||
| 			return -1; | ||||
| 		} | ||||
| 		AST_LIST_INSERT_TAIL(&session->supplements, copy, next); | ||||
|  | ||||
| 		/* referenced session created. increasing module reference. */ | ||||
| 		ast_module_ref(dup->module); | ||||
|  | ||||
| 		AST_LIST_INSERT_TAIL(&session->supplements, &dup->copy, next); | ||||
| 	} | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| void ast_sip_session_remove_supplements(struct ast_sip_session *session) | ||||
| { | ||||
| 	struct ast_sip_session_supplement *iter; | ||||
|  | ||||
| 	if (!session) { | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	/* free the supplements */ | ||||
| 	while ((iter = AST_LIST_REMOVE_HEAD(&session->supplements, next))) { | ||||
| 		struct private_sip_session_supplement *priv = (void*)iter; | ||||
| 		if (priv->module) { | ||||
| 			/* referenced session closed. decreasing module reference. */ | ||||
| 			ast_module_unref(priv->module); | ||||
| 		} | ||||
|  | ||||
| 		ast_free(iter); | ||||
| 	} | ||||
|  | ||||
| 	return; | ||||
| } | ||||
|  | ||||
| /* This stub is for ABI compatibility. */ | ||||
| #undef ast_sip_session_register_supplement | ||||
| void ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement); | ||||
| void ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement) | ||||
| { | ||||
| 	ast_sip_session_register_supplement_with_module(NULL, supplement); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user