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