Selaa lähdekoodia

Merge branch 'andrei/mod_register' of ssh://git.sip-router.org/sip-router into daniel

* 'andrei/mod_register' of ssh://git.sip-router.org/sip-router:
  minor macro additions for kamailio compatibility
  modules: support for mod_register
Daniel-Constantin Mierla 17 vuotta sitten
vanhempi
commit
343d7baa4c
4 muutettua tiedostoa jossa 70 lisäystä ja 3 poistoa
  1. 1 0
      dprint.h
  2. 52 2
      sr_module.c
  3. 15 0
      sr_module.h
  4. 2 1
      statistics.h

+ 1 - 0
dprint.h

@@ -87,6 +87,7 @@ int log_facility_fixup(void *handle, str *name, void **val);
 #define LOC_INFO	__FILE__ ":" CT2STR(__LINE__) ": "
 
 
+#define is_printable(level) (cfg_get(core, core_cfg, debug)>=(level))
 
 #ifdef NO_DEBUG
 	#ifdef __SUNPRO_C

+ 52 - 2
sr_module.c

@@ -216,17 +216,29 @@ static inline int version_control(void *handle, char *path)
 	return 0;
 }
 
-/* returns 0 on success , <0 on error */
+/** load a sr module.
+ * tries to load the module specified by path.
+ * If modname does contain a '/' or a '.' it would be assumed to contain a 
+ * path to the module and it will be used as give.
+ * else <MODS_DIR>/<modname>.so will be tried and if this fails
+ *  <MODS_DIR>/<modname>/<modname>.so
+ * @param modname - path or module name
+ * @return 0 on success , <0 on error
+ */
 int load_module(char* path)
 {
 	void* handle;
 	char* error;
+	mod_register_function mr;
 	union module_exports_u* exp;
 	unsigned* mod_if_ver;
 	struct sr_module* t;
 	struct stat stat_buf;
 	char* modname;
 	int len;
+	int dlflags;
+	int new_dlflags;
+	int retries;
 
 #ifndef RTLD_NOW
 /* for openbsd */
@@ -287,7 +299,9 @@ int load_module(char* path)
 		}
 #endif /* !EXTRA_DEBUG */
 	}
-
+	retries=2;
+	dlflags=RTLD_NOW;
+reload:
 	handle=dlopen(path, RTLD_NOW); /* resolve all symbols now */
 	if (handle==0){
 		LOG(L_ERR, "ERROR: load_module: could not open module <%s>: %s\n",
@@ -314,11 +328,47 @@ int load_module(char* path)
 		goto error1;
 	}
 	/* launch register */
+	mr = (mod_register_function)dlsym(handle, DLSYM_PREFIX "mod_register");
+	if (((error =(char*)dlerror())==0) && mr) {
+		/* no error call it */
+		new_dlflags=dlflags;
+		if (mr(path, &dlflags, 0, 0)!=0) {
+			LOG(L_ERR, "ERROR: load_module: %s: mod_register failed\n", path);
+			goto error1;
+		}
+		if (new_dlflags!=dlflags && new_dlflags!=0) {
+			/* we have to reload the module */
+			dlclose(handle);
+			dlflags=new_dlflags;
+			retries--;
+			if (retries>0) goto reload;
+			LOG(L_ERR, "ERROR: load_module: %s: cannot agree"
+					" on the dlflags\n", path);
+			goto error;
+		}
+	}
 	exp = (union module_exports_u*)dlsym(handle, DLSYM_PREFIX "exports");
 	if ( (error =(char*)dlerror())!=0 ){
 		LOG(L_ERR, "ERROR: load_module: %s\n", error);
 		goto error1;
 	}
+	/* hack to allow for kamailio style dlflags inside exports */
+	if (*mod_if_ver == 1) {
+		new_dlflags = exp->v1.dlflags;
+		if (new_dlflags!=dlflags && new_dlflags!=DEFAULT_DLFLAGS) {
+			/* we have to reload the module */
+			dlclose(handle);
+			WARN("%s: exports dlflags interface is deprecated and it will not"
+					"be supported in newer versions; consider using"
+					" mod_register() instead", path);
+			dlflags=new_dlflags;
+			retries--;
+			if (retries>0) goto reload;
+			LOG(L_ERR, "ERROR: load_module: %s: cannot agree"
+					" on the dlflags\n", path);
+			goto error;
+		}
+	}
 	if (register_module(*mod_if_ver, exp, path, handle)<0) goto error1;
 	return 0;
 

+ 15 - 0
sr_module.h

@@ -89,6 +89,20 @@
 
 #endif
 
+/** type used for the mod_register function export.
+ *  mod_register is a function called when loading a module
+ *  (if present), prior to registering the module exports.
+ *  @param path - path to the module, including file name
+ *  @param dlflags - pointer to the dlflags used when loading the module.
+ *                   If the value is changed to a different and non-zero
+ *                   value, the module will be reloaded with the new flags.
+ *  @param reserved1 - reserved for future use.
+ *  @param reserved2 - reserver for future use
+ *  @return 0 on success, -1 on error, all the other values are reserved
+ *                      for future use (<0 meaning error and >0 success)
+ */
+typedef  int (*mod_register_function)(char*, int*, void*, void*);
+
 typedef  struct module_exports* (*module_register)(void);
 typedef  int (*cmd_function)(struct sip_msg*, char*, char*);
 typedef  int (*cmd_function3)(struct sip_msg*, char*, char*, char*);
@@ -138,6 +152,7 @@ typedef int (*param_func_t)( modparam_t type, void* val);
 #define BRANCH_ROUTE  8  /* Function can be used in branch_route blocks */
 #define ONSEND_ROUTE 16  /* Function can be used in onsend_route blocks */
 #define ERROR_ROUTE  32  /* Function can be used in an error route */ 
+#define LOCAL_ROUTE  64  /* Function can be used in a local route */
 
 /* Macros - used as rank in child_init function */
 #define PROC_MAIN      0  /* Main ser process */

+ 2 - 1
statistics.h

@@ -61,9 +61,10 @@ typedef struct stat_export_ {
 
 #define get_stat(name)  0
 #define get_stat_val(v) 0
+#define get_stat_var_from_num_code(num_code, in_code) 0
 #define update_stat(v, n)
 #define reset_stat(v)
-#define if_update_stat (cond, v, n)
+#define if_update_stat(cond, v, n)
 
 #ifdef STATISTICS
 #warning "sorry sip-router does not support STATISTICS"