[sr-dev] git:andrei/shm_early_init: core: moved shm init into separate files

Andrei Pelinescu-Onciul andrei at iptel.org
Sun Jan 10 13:28:05 CET 2010


Module: sip-router
Branch: andrei/shm_early_init
Commit: 2c4ae148162c7a36f25d9a7b10fe64c7667ced31
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=2c4ae148162c7a36f25d9a7b10fe64c7667ced31

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Sun Jan 10 12:50:15 2010 +0100

core: moved shm init into separate files

- shm init moved from main.c into shm_init.c and shm_init.h.
- added a function to check if shm was already intialized
  (shm_intialized()).
- make sure the user is set before parsing the config, if present
  on the command line (the user is needed to initialize the shared
  memory when sysv semaphores are used as the locking method).

---

 main.c     |   14 ++++++++--
 shm_init.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 shm_init.h |   35 ++++++++++++++++++++++++++
 3 files changed, 125 insertions(+), 3 deletions(-)

diff --git a/main.c b/main.c
index fd4b642..31a4b65 100644
--- a/main.c
+++ b/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"
@@ -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){
diff --git a/shm_init.c b/shm_init.c
new file mode 100644
index 0000000..d981ae5
--- /dev/null
+++ b/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: */
diff --git a/shm_init.h b/shm_init.h
new file mode 100644
index 0000000..b25ab43
--- /dev/null
+++ b/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: */




More information about the sr-dev mailing list