Browse code

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).

Andrei Pelinescu-Onciul authored on 10/01/2010 11:50:15
Showing 3 changed files
... ...
@@ -121,7 +121,8 @@
121 121
 #include "mem/mem.h"
122 122
 #ifdef SHM_MEM
123 123
 #include "mem/shm_mem.h"
124
-#endif
124
+#include "shm_init.h"
125
+#endif /* SHM_MEM */
125 126
 #include "sr_module.h"
126 127
 #include "timer.h"
127 128
 #include "parser/msg_parser.h"
... ...
@@ -1720,6 +1721,10 @@ int main(int argc, char** argv)
1720 1720
 						goto error;
1721 1721
 					};
1722 1722
 					break;
1723
+			case 'u':
1724
+					/* user needed for possible shm. pre-init */
1725
+					user=optarg;
1726
+					break;
1723 1727
 			case 'b':
1724 1728
 			case 'l':
1725 1729
 			case 'n':
... ...
@@ -1732,7 +1737,6 @@ int main(int argc, char** argv)
1732 1732
 			case 'W':
1733 1733
 			case 'w':
1734 1734
 			case 't':
1735
-			case 'u':
1736 1735
 			case 'g':
1737 1736
 			case 'P':
1738 1737
 			case 'G':
... ...
@@ -2069,9 +2073,13 @@ try_again:
2069 2069
 	 *     -it must be also before init_timer and init_tcp
2070 2070
 	 *     -it must be after we know uid (so that in the SYSV sems case,
2071 2071
 	 *        the sems will have the correct euid)
2072
+	 *  Note: shm can now be initialized when parsing the config script, that's
2073
+	 *  why checking for a prior initialization is needed.
2072 2074
 	 * --andrei */
2073
-	if (init_shm_mallocs(shm_force_alloc)==-1)
2075
+#ifdef SHM_MEM
2076
+	if (!shm_initialized() && init_shm()<0)
2074 2077
 		goto error;
2078
+#endif /* SHM_MEM */
2075 2079
 	if (init_atomic_ops()==-1)
2076 2080
 		goto error;
2077 2081
 	if (init_basex() != 0){
2078 2082
new file mode 100644
... ...
@@ -0,0 +1,79 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2010 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * shm_init.c
19
+ */
20
+/*
21
+ * History:
22
+ * --------
23
+ *  2010-01-10  initial version (andrei)
24
+*/
25
+
26
+#include "shm_init.h"
27
+#include "mem/mem.h"
28
+#include "globals.h"
29
+
30
+static int shm_init = 0;
31
+
32
+
33
+/** check if shm is initialized.
34
+ * @return 1 if initialized, 0 if not
35
+ */
36
+int shm_initialized()
37
+{
38
+	return shm_init;
39
+}
40
+
41
+
42
+
43
+#ifdef SHM_MEM
44
+/** init shm mem.
45
+ * @return 0 on success, < 0 on error
46
+ * it _must_ be called:
47
+ *  - after the shm_mem_size is known
48
+ *  - after shm_force_alloc is known (mlock_pages should happen at a later
49
+ *     point so it's not yet needed here)
50
+ *  - after the user is known (so that in the SYSV sems case the sems will
51
+ *     have the correct euid)
52
+ *  - before init_timer and init_tcp
53
+ * --andrei
54
+ *
55
+ * Global vars used: shm_mem_size, shm_force_alloc, user & uid.
56
+ * Side effects: it might set uid, gid and shm_mem_size.
57
+ */
58
+int init_shm()
59
+{
60
+	/* set uid if user is set */
61
+	if (user && uid == 0){
62
+		if (user2uid(&uid, &gid, user)<0){
63
+			fprintf(stderr, "bad user name/uid number: -u %s\n", user);
64
+			goto error;
65
+		}
66
+	}
67
+	if (shm_mem_size == 0)
68
+		shm_mem_size=SHM_MEM_SIZE * 1024 * 1024;
69
+	if (init_shm_mallocs(shm_force_alloc)==-1)
70
+		goto error;
71
+	shm_init=1;
72
+	return 0;
73
+error:
74
+	return -1;
75
+}
76
+#endif /* SHM_MEM */
77
+
78
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
0 79
new file mode 100644
... ...
@@ -0,0 +1,35 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2010 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * shm_init.h
19
+ */
20
+/*
21
+ * History:
22
+ * --------
23
+ *  2010-01-10  initial version (andrei)
24
+*/
25
+
26
+#ifndef __shm_init_h
27
+#define __shm_init_h
28
+
29
+int shm_initialized();
30
+int init_shm();
31
+
32
+#endif /*__shm_init_h*/
33
+
34
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */