2
0
Эх сурвалжийг харах

Merge remote branch 'origin/andrei/shm_early_init' into sr_3.0

* origin/andrei/shm_early_init:
  NEWS: notes about the shm related changes
  core: shm mem size can now be set in the config script
  core: early shm init while parsing the cfg
  core: moved shm init into separate files
Andrei Pelinescu-Onciul 15 жил өмнө
parent
commit
b0853ed66e
6 өөрчлөгдсөн 260 нэмэгдсэн , 17 устгасан
  1. 5 0
      NEWS
  2. 4 0
      cfg.lex
  3. 125 13
      cfg.y
  4. 12 4
      main.c
  5. 79 0
      shm_init.c
  6. 35 0
      shm_init.h

+ 5 - 0
NEWS

@@ -64,6 +64,8 @@ config script changes:
   - while()
   - include file support: include_file "somefile"
   - event route support: event_route[module_name:eventid]
+  - user and shm_force_alloc must now appear prior to any modparam() or route
+     block.
 
 build system:
   - multiple modules directories are now supported (defined in Makefile.dirs)
@@ -79,6 +81,9 @@ new config variables:
            compiled with DBG_QM_MALLOC or DBG_F_MALLOC).
        Default: 1.
        Can be changed at runtime.
+  - shm = number  or shm_mem = number  - size of shared memory in MB. It's
+       overwritten if a value is specified on the command line (-m val).
+       Default: 32 Mb. Must appear prior to any modparam() or route block.
 
 new script commands:
   add_local_rport() - adds the rport parameter to the added via header

+ 4 - 0
cfg.lex

@@ -80,6 +80,7 @@
  *  2009-03-10  added SET_USERPHONE action (Miklos)
  *  2009-04-24  add strlen, strempty and defined operators (andrei)
  *  2009-03-07  RETCODE, it's now  a core pvar (andrei)
+ *  2010-01-10  added SHM_MEM_SZ (andrei)
 */
 
 
@@ -430,6 +431,7 @@ ADVERTISED_ADDRESS	"advertised_address"
 ADVERTISED_PORT		"advertised_port"
 DISABLE_CORE		"disable_core_dump"
 OPEN_FD_LIMIT		"open_files_limit"
+SHM_MEM_SZ		"shm"|"shm_mem"|"shm_mem_size"
 SHM_FORCE_ALLOC		"shm_force_alloc"
 MLOCK_PAGES			"mlock_pages"
 REAL_TIME			"real_time"
@@ -818,6 +820,8 @@ EAT_ABLE	[\ \t\b\r]
 									return DISABLE_CORE; }
 <INITIAL>{OPEN_FD_LIMIT}		{	count(); yylval.strval=yytext;
 									return OPEN_FD_LIMIT; }
+<INITIAL>{SHM_MEM_SZ}		{	count(); yylval.strval=yytext;
+									return SHM_MEM_SZ; }
 <INITIAL>{SHM_FORCE_ALLOC}		{	count(); yylval.strval=yytext;
 									return SHM_FORCE_ALLOC; }
 <INITIAL>{MLOCK_PAGES}		{	count(); yylval.strval=yytext;

+ 125 - 13
cfg.y

@@ -96,6 +96,8 @@
  * 2009-01-26  case/switch() support (andrei)
  * 2009-03-10  added SET_USERPHONE action (Miklos)
  * 2009-05-04  switched if to rval_expr (andrei)
+ * 2010-01-10  init shm on first mod_param or route block;
+ *             added SHM_MEM_SZ (andrei)
 */
 
 %{
@@ -111,6 +113,9 @@
 #include <errno.h>
 #include "route_struct.h"
 #include "globals.h"
+#ifdef SHM_MEM
+#include "shm_init.h"
+#endif /* SHM_MEM */
 #include "route.h"
 #include "switch.h"
 #include "dprint.h"
@@ -490,6 +495,7 @@ extern char *finame;
 %token ADVERTISED_PORT
 %token DISABLE_CORE
 %token OPEN_FD_LIMIT
+%token SHM_MEM_SZ
 %token SHM_FORCE_ALLOC
 %token MLOCK_PAGES
 %token REAL_TIME
@@ -867,8 +873,20 @@ assign_stm:
 	| MEMSUM EQUAL error { yyerror("int value expected"); }
 	| SIP_WARNING EQUAL NUMBER { sip_warning=$3; }
 	| SIP_WARNING EQUAL error { yyerror("boolean value expected"); }
-	| USER EQUAL STRING     { user=$3; }
-	| USER EQUAL ID         { user=$3; }
+	| USER EQUAL STRING     {
+		if (shm_initialized())
+			yyerror("user must be before any modparam or the"
+					" route blocks");
+		else if (user==0)
+			user=$3; 
+	}
+	| USER EQUAL ID         {
+		if (shm_initialized())
+			yyerror("user must be before any modparam or the"
+					" route blocks");
+		else if (user==0)
+			user=$3;
+	}
 	| USER EQUAL error      { yyerror("string value expected"); }
 	| GROUP EQUAL STRING     { group=$3; }
 	| GROUP EQUAL ID         { group=$3; }
@@ -1447,7 +1465,21 @@ assign_stm:
 	| DISABLE_CORE EQUAL error { yyerror("boolean value expected"); }
 	| OPEN_FD_LIMIT EQUAL NUMBER { open_files_limit=$3; }
 	| OPEN_FD_LIMIT EQUAL error { yyerror("number expected"); }
-	| SHM_FORCE_ALLOC EQUAL NUMBER { shm_force_alloc=$3; }
+	| SHM_MEM_SZ EQUAL NUMBER {
+		if (shm_initialized())
+			yyerror("shm/shm_mem_size must be before any modparam or the"
+					" route blocks");
+		else if (shm_mem_size == 0)
+			shm_mem_size=$3 * 1024 * 1024;
+	}
+	| SHM_MEM_SZ EQUAL error { yyerror("number expected"); }
+	| SHM_FORCE_ALLOC EQUAL NUMBER {
+		if (shm_initialized())
+			yyerror("shm_force_alloc must be before any modparam or the"
+					" route blocks");
+		else
+			shm_force_alloc=$3;
+	}
 	| SHM_FORCE_ALLOC EQUAL error { yyerror("boolean value expected"); }
 	| MLOCK_PAGES EQUAL NUMBER { mlock_pages=$3; }
 	| MLOCK_PAGES EQUAL error { yyerror("boolean value expected"); }
@@ -1560,11 +1592,23 @@ module_stm:
 	}
 	| LOADPATH EQUAL error	{ yyerror("string expected"); }
 	| MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		if (set_mod_param_regex($3, $5, PARAM_STRING, $7) != 0) {
 			 yyerror("Can't set module parameter");
 		}
 	}
-        | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
+	| MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		if (set_mod_param_regex($3, $5, PARAM_INT, (void*)$7) != 0) {
 			 yyerror("Can't set module parameter");
 		}
@@ -1645,8 +1689,22 @@ route_name:		NUMBER	{
 ;
 
 route_stm:
-	ROUTE LBRACE actions RBRACE { push($3, &main_rt.rlist[DEFAULT_RT]); }
+	ROUTE LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
+		push($3, &main_rt.rlist[DEFAULT_RT]);
+	}
 	| ROUTE LBRACK route_name RBRACK LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		i_tmp=route_get(&main_rt, $3);
 		if (i_tmp==-1){
 			yyerror("internal error");
@@ -1662,9 +1720,21 @@ route_stm:
 	;
 failure_route_stm:
 	ROUTE_FAILURE LBRACE actions RBRACE {
-									push($3, &failure_rt.rlist[DEFAULT_RT]);
-										}
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
+		push($3, &failure_rt.rlist[DEFAULT_RT]);
+	}
 	| ROUTE_FAILURE LBRACK route_name RBRACK LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		i_tmp=route_get(&failure_rt, $3);
 		if (i_tmp==-1){
 			yyerror("internal error");
@@ -1680,9 +1750,21 @@ failure_route_stm:
 	;
 onreply_route_stm:
 	ROUTE_ONREPLY LBRACE actions RBRACE {
-									push($3, &onreply_rt.rlist[DEFAULT_RT]);
-										}
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
+		push($3, &onreply_rt.rlist[DEFAULT_RT]);
+	}
 	| ROUTE_ONREPLY LBRACK route_name RBRACK LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		i_tmp=route_get(&onreply_rt, $3);
 		if (i_tmp==-1){
 			yyerror("internal error");
@@ -1698,9 +1780,21 @@ onreply_route_stm:
 	;
 branch_route_stm:
 	ROUTE_BRANCH LBRACE actions RBRACE {
-									push($3, &branch_rt.rlist[DEFAULT_RT]);
-										}
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
+		push($3, &branch_rt.rlist[DEFAULT_RT]);
+	}
 	| ROUTE_BRANCH LBRACK route_name RBRACK LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		i_tmp=route_get(&branch_rt, $3);
 		if (i_tmp==-1){
 			yyerror("internal error");
@@ -1715,9 +1809,21 @@ branch_route_stm:
 	| ROUTE_BRANCH error { yyerror("invalid branch_route statement"); }
 	;
 send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
-									push($3, &onsend_rt.rlist[DEFAULT_RT]);
-												}
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
+		push($3, &onsend_rt.rlist[DEFAULT_RT]);
+	}
 	| ROUTE_SEND LBRACK route_name RBRACK LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		i_tmp=route_get(&onsend_rt, $3);
 		if (i_tmp==-1){
 			yyerror("internal error");
@@ -1732,6 +1838,12 @@ send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
 	| ROUTE_SEND error { yyerror("invalid onsend_route statement"); }
 	;
 event_route_stm: ROUTE_EVENT LBRACK EVENT_RT_NAME RBRACK LBRACE actions RBRACE {
+	#ifdef SHM_MEM
+		if (!shm_initialized() && init_shm()<0) {
+			yyerror("Can't initialize shared memory");
+			YYABORT;
+		}
+	#endif /* SHM_MEM */
 		i_tmp=route_get(&event_rt, $3);
 		if (i_tmp==-1){
 			yyerror("internal error");

+ 12 - 4
main.c

@@ -121,7 +121,8 @@
 #include "mem/mem.h"
 #ifdef SHM_MEM
 #include "mem/shm_mem.h"
-#endif
+#include "shm_init.h"
+#endif /* SHM_MEM */
 #include "sr_module.h"
 #include "timer.h"
 #include "parser/msg_parser.h"
@@ -468,7 +469,7 @@ int cfg_warnings=0;
 
 
 /* shared memory (in MB) */
-unsigned long shm_mem_size=SHM_MEM_SIZE * 1024 * 1024;
+unsigned long shm_mem_size=0;
 
 /* export command-line to anywhere else */
 int my_argc;
@@ -1720,6 +1721,10 @@ int main(int argc, char** argv)
 						goto error;
 					};
 					break;
+			case 'u':
+					/* user needed for possible shm. pre-init */
+					user=optarg;
+					break;
 			case 'b':
 			case 'l':
 			case 'n':
@@ -1732,7 +1737,6 @@ int main(int argc, char** argv)
 			case 'W':
 			case 'w':
 			case 't':
-			case 'u':
 			case 'g':
 			case 'P':
 			case 'G':
@@ -2069,9 +2073,13 @@ try_again:
 	 *     -it must be also before init_timer and init_tcp
 	 *     -it must be after we know uid (so that in the SYSV sems case,
 	 *        the sems will have the correct euid)
+	 *  Note: shm can now be initialized when parsing the config script, that's
+	 *  why checking for a prior initialization is needed.
 	 * --andrei */
-	if (init_shm_mallocs(shm_force_alloc)==-1)
+#ifdef SHM_MEM
+	if (!shm_initialized() && init_shm()<0)
 		goto error;
+#endif /* SHM_MEM */
 	if (init_atomic_ops()==-1)
 		goto error;
 	if (init_basex() != 0){

+ 79 - 0
shm_init.c

@@ -0,0 +1,79 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 iptelorg GmbH
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/*
+ * shm_init.c
+ */
+/*
+ * History:
+ * --------
+ *  2010-01-10  initial version (andrei)
+*/
+
+#include "shm_init.h"
+#include "mem/mem.h"
+#include "globals.h"
+
+static int shm_init = 0;
+
+
+/** check if shm is initialized.
+ * @return 1 if initialized, 0 if not
+ */
+int shm_initialized()
+{
+	return shm_init;
+}
+
+
+
+#ifdef SHM_MEM
+/** init shm mem.
+ * @return 0 on success, < 0 on error
+ * it _must_ be called:
+ *  - after the shm_mem_size is known
+ *  - after shm_force_alloc is known (mlock_pages should happen at a later
+ *     point so it's not yet needed here)
+ *  - after the user is known (so that in the SYSV sems case the sems will
+ *     have the correct euid)
+ *  - before init_timer and init_tcp
+ * --andrei
+ *
+ * Global vars used: shm_mem_size, shm_force_alloc, user & uid.
+ * Side effects: it might set uid, gid and shm_mem_size.
+ */
+int init_shm()
+{
+	/* set uid if user is set */
+	if (user && uid == 0){
+		if (user2uid(&uid, &gid, user)<0){
+			fprintf(stderr, "bad user name/uid number: -u %s\n", user);
+			goto error;
+		}
+	}
+	if (shm_mem_size == 0)
+		shm_mem_size=SHM_MEM_SIZE * 1024 * 1024;
+	if (init_shm_mallocs(shm_force_alloc)==-1)
+		goto error;
+	shm_init=1;
+	return 0;
+error:
+	return -1;
+}
+#endif /* SHM_MEM */
+
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */

+ 35 - 0
shm_init.h

@@ -0,0 +1,35 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 iptelorg GmbH
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/*
+ * shm_init.h
+ */
+/*
+ * History:
+ * --------
+ *  2010-01-10  initial version (andrei)
+*/
+
+#ifndef __shm_init_h
+#define __shm_init_h
+
+int shm_initialized();
+int init_shm();
+
+#endif /*__shm_init_h*/
+
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */