[sr-dev] git:master: db: Fixing problem with incorrect initialization of db connections

Jan Janak jan at iptel.org
Wed Jun 10 22:47:26 CEST 2009


Module: sip-router
Branch: master
Commit: 82a5ef65ffe69b33accecc67b24376307aa46812
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=82a5ef65ffe69b33accecc67b24376307aa46812

Author: Jan Janak <jan at iptel.org>
Committer: Jan Janak <jan at iptel.org>
Date:   Wed Jun 10 22:41:27 2009 +0200

db: Fixing problem with incorrect initialization of db connections

The sip-router core is based on the ser core and thus has a slightly
different initialization sequence of the main sr process. Namely we
also call the child_init function of all modules in the main process
with rank PROC_INIT. This is a kind of delayed initialization (called
after all mod_init functions have finished but before the main
process started forking children).

There should be no database connections opened in child_init called
with PROC_INIT parameter. This would result in database connections
opened in the main process and inherited by all children. Such
connections could then be used from multiple processes simultaneously
and that can cause race conditions.

This change checks for the value of the rank parameter in child_init
functions and skips database initialization if the value is PROC_INIT,
PROC_MAIN, or PROC_TCP_MAIN.

The problem was reported by Juha and he deserves the credit for helping to
investigate the issue.

---

 modules/carrierroute/carrierroute.c     |    3 +++
 modules/utils/utils.c                   |    3 +++
 modules_k/acc/acc_mod.c                 |    3 +++
 modules_k/alias_db/alias_db.c           |    3 +++
 modules_k/auth_db/authdb_mod.c          |    3 +++
 modules_k/avpops/avpops.c               |    2 +-
 modules_k/cpl-c/cpl.c                   |    3 +++
 modules_k/group/group_mod.c             |    3 +++
 modules_k/imc/imc.c                     |    3 +++
 modules_k/msilo/msilo.c                 |    3 +++
 modules_k/pdt/pdt.c                     |    3 +++
 modules_k/permissions/trusted.c         |    3 +++
 modules_k/presence/presence.c           |    3 +++
 modules_k/presence_xml/presence_xml.c   |    3 +++
 modules_k/pua/pua.c                     |    3 +++
 modules_k/rls/rls.c                     |    3 +++
 modules_k/siptrace/siptrace.c           |    3 +++
 modules_k/speeddial/speeddial.c         |    3 +++
 modules_k/sqlops/sqlops.c               |    2 +-
 modules_k/uri_db/uridb_mod.c            |    3 +++
 modules_k/userblacklist/userblacklist.c |    3 +++
 modules_k/xcap_client/xcap_client.c     |    3 +++
 22 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/modules/carrierroute/carrierroute.c b/modules/carrierroute/carrierroute.c
index 4a7c8d0..63d9677 100644
--- a/modules/carrierroute/carrierroute.c
+++ b/modules/carrierroute/carrierroute.c
@@ -229,6 +229,9 @@ static int mod_init(void) {
 
 
 static int child_init(int rank) {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if(mode == CARRIERROUTE_MODE_DB){
 		return carrierroute_db_open();
 	}
diff --git a/modules/utils/utils.c b/modules/utils/utils.c
index bb15072..34b70c4 100644
--- a/modules/utils/utils.c
+++ b/modules/utils/utils.c
@@ -280,6 +280,9 @@ static int mod_init(void)
 /* Child initialization function */
 static int child_init(int rank)
 {	
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
     return pres_db_open();
 }
 
diff --git a/modules_k/acc/acc_mod.c b/modules_k/acc/acc_mod.c
index b98bfef..5f25647 100644
--- a/modules_k/acc/acc_mod.c
+++ b/modules_k/acc/acc_mod.c
@@ -527,6 +527,9 @@ static int mod_init( void )
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 #ifdef SQL_ACC
 	if(db_url.s && acc_db_init_child(&db_url)<0) {
 		LM_ERR("could not open database connection");
diff --git a/modules_k/alias_db/alias_db.c b/modules_k/alias_db/alias_db.c
index af50987..0606cd9 100644
--- a/modules_k/alias_db/alias_db.c
+++ b/modules_k/alias_db/alias_db.c
@@ -111,6 +111,9 @@ struct module_exports exports = {
  */
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	db_handle = adbf.init(&db_url);
 	if (!db_handle)
 	{
diff --git a/modules_k/auth_db/authdb_mod.c b/modules_k/auth_db/authdb_mod.c
index d5daedf..e3df1f5 100644
--- a/modules_k/auth_db/authdb_mod.c
+++ b/modules_k/auth_db/authdb_mod.c
@@ -156,6 +156,9 @@ struct module_exports exports = {
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+	
 	auth_db_handle = auth_dbf.init(&db_url);
 	if (auth_db_handle == 0){
 		LM_ERR("unable to connect to the database\n");
diff --git a/modules_k/avpops/avpops.c b/modules_k/avpops/avpops.c
index e01fcdf..440c8f6 100644
--- a/modules_k/avpops/avpops.c
+++ b/modules_k/avpops/avpops.c
@@ -201,7 +201,7 @@ static int avpops_child_init(int rank)
 	if (db_url.s==0)
 		return 0;
 	/* skip main process and TCP manager process */
-	if (rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
 		return 0;
 	/* init DB connection */
 	return avpops_db_init(&db_url, &db_table, db_columns);
diff --git a/modules_k/cpl-c/cpl.c b/modules_k/cpl-c/cpl.c
index bbd0db8..d23d4f6 100644
--- a/modules_k/cpl-c/cpl.c
+++ b/modules_k/cpl-c/cpl.c
@@ -398,6 +398,9 @@ error:
 
 static int cpl_child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	return cpl_db_init(&db_url, &db_table);
 }
 
diff --git a/modules_k/group/group_mod.c b/modules_k/group/group_mod.c
index ee0a766..b444f5c 100644
--- a/modules_k/group/group_mod.c
+++ b/modules_k/group/group_mod.c
@@ -180,6 +180,9 @@ struct module_exports exports = {
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	return group_db_init(&db_url);
 }
 
diff --git a/modules_k/imc/imc.c b/modules_k/imc/imc.c
index 687733f..dd042de 100644
--- a/modules_k/imc/imc.c
+++ b/modules_k/imc/imc.c
@@ -415,6 +415,9 @@ static int mod_init(void)
  */
 static int child_init(int rank)
 {	
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if (imc_dbf.init==0)
 	{
 		LM_ERR("database not bound\n");
diff --git a/modules_k/msilo/msilo.c b/modules_k/msilo/msilo.c
index d17e0d7..72f1164 100644
--- a/modules_k/msilo/msilo.c
+++ b/modules_k/msilo/msilo.c
@@ -441,6 +441,9 @@ static int mod_init(void)
  */
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	LM_DBG("rank #%d / pid <%d>\n", rank, getpid());
 	if (msilo_dbf.init==0)
 	{
diff --git a/modules_k/pdt/pdt.c b/modules_k/pdt/pdt.c
index 46d6206..28650d8 100644
--- a/modules_k/pdt/pdt.c
+++ b/modules_k/pdt/pdt.c
@@ -288,6 +288,9 @@ static int child_init(void)
 /* each child get a new connection to the database */
 static int mod_child_init(int r)
 {
+	if (r=PROC_INIT || r==PROC_MAIN || r==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if ( child_init()!=0 )
 		return -1;
 
diff --git a/modules_k/permissions/trusted.c b/modules_k/permissions/trusted.c
index 24327e2..79a2d47 100644
--- a/modules_k/permissions/trusted.c
+++ b/modules_k/permissions/trusted.c
@@ -226,6 +226,9 @@ error:
  */
 int init_child_trusted(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if (!db_url.s) {
 		return 0;
 	}
diff --git a/modules_k/presence/presence.c b/modules_k/presence/presence.c
index 13b74ae..a07cf21 100644
--- a/modules_k/presence/presence.c
+++ b/modules_k/presence/presence.c
@@ -350,6 +350,9 @@ static int mod_init(void)
  */
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	pid = my_pid();
 	
 	if(library_mode)
diff --git a/modules_k/presence_xml/presence_xml.c b/modules_k/presence_xml/presence_xml.c
index 90d4312..d54e76c 100644
--- a/modules_k/presence_xml/presence_xml.c
+++ b/modules_k/presence_xml/presence_xml.c
@@ -283,6 +283,9 @@ static int child_init(int rank)
 {
 	LM_DBG("[%d]  pid [%d]\n", rank, getpid());
 	
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if (pxml_dbf.init==0)
 	{
 		LM_CRIT("database not bound\n");
diff --git a/modules_k/pua/pua.c b/modules_k/pua/pua.c
index 46dadd3..3efaaac 100644
--- a/modules_k/pua/pua.c
+++ b/modules_k/pua/pua.c
@@ -247,6 +247,9 @@ static int mod_init(void)
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if (pua_dbf.init==0)
 	{
 		LM_CRIT("database not bound\n");
diff --git a/modules_k/rls/rls.c b/modules_k/rls/rls.c
index 1f5f9e9..fd9f5da 100644
--- a/modules_k/rls/rls.c
+++ b/modules_k/rls/rls.c
@@ -453,6 +453,9 @@ static int mod_init(void)
  */
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	LM_DBG("child [%d]  pid [%d]\n", rank, getpid());
 	if (rls_dbf.init==0)
 	{
diff --git a/modules_k/siptrace/siptrace.c b/modules_k/siptrace/siptrace.c
index 421f6b5..cdd3f5c 100644
--- a/modules_k/siptrace/siptrace.c
+++ b/modules_k/siptrace/siptrace.c
@@ -321,6 +321,9 @@ static int mod_init(void)
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	db_con = db_funcs.init(&db_url);
 	if (!db_con) {
 		LM_ERR("unable to connect to database. Please check configuration.\n");
diff --git a/modules_k/speeddial/speeddial.c b/modules_k/speeddial/speeddial.c
index e31a7ac..5db4fcb 100644
--- a/modules_k/speeddial/speeddial.c
+++ b/modules_k/speeddial/speeddial.c
@@ -113,6 +113,9 @@ struct module_exports exports = {
  */
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	db_handle = db_funcs.init(&db_url);
 	if (!db_handle)
 	{
diff --git a/modules_k/sqlops/sqlops.c b/modules_k/sqlops/sqlops.c
index db56345..b38bbe3 100644
--- a/modules_k/sqlops/sqlops.c
+++ b/modules_k/sqlops/sqlops.c
@@ -89,7 +89,7 @@ struct module_exports exports= {
 
 static int child_init(int rank)
 {
-	if (rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
 		return 0;
 	return sql_connect();
 }
diff --git a/modules_k/uri_db/uridb_mod.c b/modules_k/uri_db/uridb_mod.c
index 7206cba..7a9e296 100644
--- a/modules_k/uri_db/uridb_mod.c
+++ b/modules_k/uri_db/uridb_mod.c
@@ -141,6 +141,9 @@ struct module_exports exports = {
  */
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if (db_url.len)
 		return uridb_db_init(&db_url);
 	else
diff --git a/modules_k/userblacklist/userblacklist.c b/modules_k/userblacklist/userblacklist.c
index 0e7365e..5179ee4 100644
--- a/modules_k/userblacklist/userblacklist.c
+++ b/modules_k/userblacklist/userblacklist.c
@@ -567,6 +567,9 @@ static int mod_init(void)
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if (userblacklist_db_open() != 0) return -1;
 	dtrie_root=dtrie_init(10);
 	if (dtrie_root == NULL) {
diff --git a/modules_k/xcap_client/xcap_client.c b/modules_k/xcap_client/xcap_client.c
index dab7ef3..8be0798 100644
--- a/modules_k/xcap_client/xcap_client.c
+++ b/modules_k/xcap_client/xcap_client.c
@@ -169,6 +169,9 @@ static int mod_init(void)
 
 static int child_init(int rank)
 {
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+		return 0; /* do nothing for the main process */
+
 	if((xcap_db = xcap_dbf.init(&xcap_db_url))==NULL)
 	{
 		LM_ERR("cannot connect to db\n");




More information about the sr-dev mailing list