Sfoglia il codice sorgente

core: new command line parameter --modparam

- allow setting a module parameter via command line
- format: --modparam=modname:paramname:type:valye
- type can be: 'i' for integer value; 's' for string value
- example:

kamailio --loadmodule=xprint.so --modparam=xprint:buf_size:i:2048
Daniel-Constantin Mierla 5 anni fa
parent
commit
1c402ddc4c
3 ha cambiato i file con 74 aggiunte e 0 eliminazioni
  1. 60 0
      src/core/modparam.c
  2. 2 0
      src/core/modparam.h
  3. 12 0
      src/main.c

+ 60 - 0
src/core/modparam.c

@@ -154,3 +154,63 @@ int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
 	}
 	return 0;
 }
+
+int set_mod_param_serialized(char* mval)
+{
+#define MPARAM_MBUF_SIZE 256
+	char mbuf[MPARAM_MBUF_SIZE];
+	char *mname = NULL;
+	char *mparam = NULL;
+	char *sval = NULL;
+	int ival = 0;
+	int ptype = PARAM_STRING;
+	char *p = NULL;
+
+	if(strlen(mval) >= MPARAM_MBUF_SIZE) {
+		LM_ERR("argument is too long: %s\n", mval);
+		return -1;
+	}
+	strcpy(mbuf, mval);
+	mname = mbuf;
+	p = strchr(mbuf, ':');
+	if(p==NULL) {
+		LM_ERR("invalid format for argument: %s\n", mval);
+		return -1;
+	}
+	*p = '\0';
+	p++;
+	mparam = p;
+	p = strchr(p, ':');
+	if(p==NULL) {
+		LM_ERR("invalid format for argument: %s\n", mval);
+		return -1;
+	}
+	*p = '\0';
+	p++;
+	if(*p=='i' || *p=='I') {
+		ptype = PARAM_INT;
+	} else if(*p=='s' || *p=='S') {
+		ptype = PARAM_STRING;
+	} else {
+		LM_ERR("invalid format for argument: %s\n", mval);
+		return -1;
+	}
+	p++;
+	if(*p!=':') {
+		LM_ERR("invalid format for argument: %s\n", mval);
+		return -1;
+	}
+	p++;
+	sval = p;
+
+	if(ptype == PARAM_STRING) {
+		return set_mod_param_regex(mname, mparam, PARAM_STRING, sval);
+	} else {
+		if(strlen(sval) <= 0) {
+			LM_ERR("invalid format for argument: %s\n", mval);
+			return -1;
+		}
+		strz2sint(sval, &ival);
+		return set_mod_param_regex(mname, mparam, PARAM_INT, (void*)(long)ival);
+	}
+}

+ 2 - 0
src/core/modparam.h

@@ -34,4 +34,6 @@ int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val);
 
 int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val);
 
+int set_mod_param_serialized(char* mval);
+
 #endif

+ 12 - 0
src/main.c

@@ -78,6 +78,7 @@
 #include "core/mem/shm_mem.h"
 #include "core/shm_init.h"
 #include "core/sr_module.h"
+#include "core/modparam.h"
 #include "core/timer.h"
 #include "core/parser/msg_parser.h"
 #include "core/ip_addr.h"
@@ -196,6 +197,9 @@ Options:\n\
     --loadmodule=name load the module specified by name\n\
     -L path      Modules search path (default: " MODS_DIR ")\n\
     -m nr        Size of shared memory allocated in Megabytes\n\
+    --modparam=modname:paramname:type:value set the module parameter\n\
+                  type has to be 's' for string value and 'i' for int value, \n\
+                  example: --modparam=corex:alias_subdomains:s:" NAME ".org\n\
     -M nr        Size of private memory allocated, in Megabytes\n\
     -n processes Number of child processes to fork per interface\n\
                   (default: 8)\n"
@@ -1924,6 +1928,7 @@ int main(int argc, char** argv)
 		{"substdefs",   required_argument, 0, KARGOPTVAL + 3},
 		{"server-id",   required_argument, 0, KARGOPTVAL + 4},
 		{"loadmodule",  required_argument, 0, KARGOPTVAL + 5},
+		{"modparam",    required_argument, 0, KARGOPTVAL + 6},
 		{0, 0, 0, 0 }
 	};
 
@@ -2140,6 +2145,7 @@ int main(int argc, char** argv)
 			case 's':
 			case 'Y':
 			case KARGOPTVAL+5:
+			case KARGOPTVAL+6:
 					break;
 
 			/* long options */
@@ -2239,6 +2245,12 @@ int main(int argc, char** argv)
 						goto error;
 					}
 					break;
+			case KARGOPTVAL+6:
+					if(set_mod_param_serialized(optarg) < 0) {
+						LM_ERR("failed to set modparam: %s\n", optarg);
+						goto error;
+					}
+					break;
 			default:
 					break;
 		}