Browse Source

cfgutils: lock/unlock functions exported to inter-module API

Daniel-Constantin Mierla 13 years ago
parent
commit
f7ef19861e
2 changed files with 110 additions and 10 deletions
  1. 62 0
      modules_k/cfgutils/api.h
  2. 48 10
      modules_k/cfgutils/cfgutils.c

+ 62 - 0
modules_k/cfgutils/api.h

@@ -0,0 +1,62 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2012 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _CFGUTILS_API_H_
+#define _CFGUTILS_API_H_
+
+#include "../../str.h"
+
+typedef int (*cfgutils_lock_f)(str *lkey);
+typedef int (*cfgutils_unlock_f)(str *lkey);
+
+/**
+ * @brief CFGUTILS API structure
+ */
+typedef struct cfgutils_api {
+	cfgutils_lock_f lock;
+	cfgutils_unlock_f unlock;
+} cfgutils_api_t;
+
+typedef int (*bind_cfgutils_f)(cfgutils_api_t* api);
+
+/**
+ * @brief Load the CFGUTILS API
+ */
+static inline int cfgutils_load_api(cfgutils_api_t *api)
+{
+	bind_cfgutils_f bindcfgutils;
+
+	bindcfgutils = (bind_cfgutils_f)find_export("bind_cfgutils", 0, 0);
+	if(bindcfgutils == 0) {
+		LM_ERR("cannot find bind_cfgutils\n");
+		return -1;
+	}
+	if (bindcfgutils(api)<0)
+	{
+		LM_ERR("cannot bind cfgutils api\n");
+		return -1;
+	}
+	return 0;
+}
+
+#endif

+ 48 - 10
modules_k/cfgutils/cfgutils.c

@@ -76,6 +76,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "api.h"
+
 MODULE_VERSION
 
 static int set_prob(struct sip_msg*, char *, char *);
@@ -678,16 +680,10 @@ static int dbg_shm_summary(struct sip_msg* msg, char* foo, char* bar)
 	return 1;
 }
 
-int cfg_lock_helper(struct sip_msg *msg, gparam_p key, int mode)
+int cfg_lock_helper(str *lkey, int mode)
 {
-	str s;
 	unsigned int pos;
-	if(fixup_get_svalue(msg, key, &s)!=0)
-	{
-		LM_ERR("cannot get first parameter\n");
-		return -1;
-	}
-	pos = core_case_hash(&s, 0, _cfg_lock_size);
+	pos = core_case_hash(lkey, 0, _cfg_lock_size);
 	LM_DBG("cfg_lock mode %d on %u\n", mode, pos);
 	if(mode==0)
 		lock_set_get(_cfg_lock_set, pos);
@@ -696,18 +692,29 @@ int cfg_lock_helper(struct sip_msg *msg, gparam_p key, int mode)
 	return 1;
 }
 
+int cfg_lock_wrapper(struct sip_msg *msg, gparam_p key, int mode)
+{
+	str s;
+	if(fixup_get_svalue(msg, key, &s)!=0)
+	{
+		LM_ERR("cannot get first parameter\n");
+		return -1;
+	}
+	return cfg_lock_helper(&s, mode);
+}
+
 static int cfg_lock(struct sip_msg *msg, char *key, char *s2)
 {
 	if(_cfg_lock_set==NULL || key==NULL)
 		return -1;
-	return cfg_lock_helper(msg, (gparam_p)key, 0);
+	return cfg_lock_wrapper(msg, (gparam_p)key, 0);
 }
 
 static int cfg_unlock(struct sip_msg *msg, char *key, char *s2)
 {
 	if(_cfg_lock_set==NULL || key==NULL)
 		return -1;
-	return cfg_lock_helper(msg, (gparam_p)key, 1);
+	return cfg_lock_wrapper(msg, (gparam_p)key, 1);
 }
 
 
@@ -775,3 +782,34 @@ static void mod_destroy(void)
 		lock_set_dealloc(_cfg_lock_set);
 	}
 }
+
+/**
+ *
+ */
+int cfgutils_lock(str *lkey)
+{
+	return cfg_lock_helper(lkey, 0);
+}
+
+/**
+ *
+ */
+int cfgutils_unlock(str *lkey)
+{
+	return cfg_lock_helper(lkey, 1);
+}
+
+/**
+ * @brief bind functions to CFGUTILS API structure
+ */
+int bind_cfgutils(cfgutils_api_t *api)
+{
+	if (!api) {
+		ERR("Invalid parameter value\n");
+		return -1;
+	}
+	api->lock   = cfgutils_lock;
+	api->unlock = cfgutils_unlock;
+
+	return 0;
+}