Browse code

- updated INSTALL - fixed MKDEP in Makefile - added shm_mem.{c,h} = shared memory + shared malloc (sh_malloc)

Andrei Pelinescu-Onciul authored on 26/11/2001 23:31:03
Showing 7 changed files
... ...
@@ -5,6 +5,8 @@ Installation Notes
5 5
 Supported architectures: Linux, FreeBSD, Solaris, Win* (CYGWIN)
6 6
 (for other architectures the Makefile must be edited)
7 7
 
8
+There are various configuration options defined in the Makefile.
9
+
8 10
 Requirements:
9 11
 
10 12
 
... ...
@@ -22,8 +24,13 @@ Arhitecture Notes:
22 22
 	(>=0.12). 
23 23
 	
24 24
 
25
+Clean:
26
+make clean   (clean the modules too)
27
+make proper  (clean also the dependencies)
28
+
25 29
 Compile:
26 30
 
31
+make proper
27 32
 make
28 33
 (or gmake on non-Linux systems)
29 34
 make modules 
... ...
@@ -28,7 +28,14 @@ NAME=ser
28 28
 # DEBUG compiles in some extra debugging code
29 29
 # OLD_PARSER uses the old and stable parser (from ser 8.3.2)
30 30
 # DNS_IP_HACK faster ip address resolver for ip strings (e.g "127.0.0.1")
31
-DEFS=-DNOCR -DMACROEATER -DDNS_IP_HACK -DPKG_MALLOC #-DNO_DEBUG#-DSTATS -DNO_DEBUG 
31
+# SHM_MEM    compiles in shared mem. support, needed by some modules and
32
+#            by USE_SHM_MEM
33
+# PKG_MALLOC uses a faster malloc (exclusive w/ USE_SHM_MEM)
34
+# USE_SHM_MEM all pkg_malloc => sh_malloc (most mallocs use a common sh. mem.
35
+#           segment); don't define PKG_MALLOC!
36
+DEFS=-DNOCR -DMACROEATER -DDNS_IP_HACK  -DSHM_MEM -DUSE_SHM_MEM -DNO_DEBUG 
37
+#-DPKG_MALLOC
38
+#-DNO_DEBUG#-DSTATS -DNO_DEBUG 
32 39
 #-DNO_LOG
33 40
 
34 41
 PROFILE=  # -pg #set this if you want profiling
... ...
@@ -84,7 +91,7 @@ ifneq (,$(findstring CYGWIN, $(ARCH)))
84 84
 endif
85 85
 
86 86
 
87
-MKDEP=gcc -M 
87
+MKDEP=gcc -M $(DEFS)
88 88
 
89 89
 ALLDEP=Makefile
90 90
 
... ...
@@ -44,5 +44,8 @@
44 44
 /*used only if PKG_MALLOC is defined*/
45 45
 #define PKG_MEM_POOL_SIZE 1024*1024
46 46
 
47
+/*used is SH_MEM is defined*/
48
+#define SHM_MEM_SIZE 1024*1024
49
+
47 50
 
48 51
 #endif
... ...
@@ -24,6 +24,9 @@
24 24
 #include "udp_server.h"
25 25
 #include "globals.h"
26 26
 #include "mem.h"
27
+#ifdef SHM_MEM
28
+#include "shm_mem.h"
29
+#endif
27 30
 
28 31
 
29 32
 #include <signal.h>
... ...
@@ -294,9 +297,16 @@ static void sig_usr(int signo)
294 294
 #ifdef PKG_MALLOC
295 295
 		pkg_status();
296 296
 #endif
297
+#ifdef SHM_MEM
298
+		sh_status();
299
+#endif
297 300
 		DPrint("INT received, program terminates\n");
298 301
 		DPrint("Thank you for flying ser\n");
302
+		/* WARNING: very dangerous, might be unsafe*/
303
+#ifdef SHM_MEM
304
+		shm_mem_destroy();
299 305
 		exit(0);
306
+#endif
300 307
 	} else if (signo==SIGUSR1) { /* statistic */
301 308
 #ifdef STATS
302 309
 		dump_all_statistic();
... ...
@@ -304,6 +314,9 @@ static void sig_usr(int signo)
304 304
 #ifdef PKG_MALLOC
305 305
 		pkg_status();
306 306
 #endif
307
+#ifdef SHM_MEM
308
+		sh_status();
309
+#endif
307 310
 	}
308 311
 }
309 312
 	
... ...
@@ -319,16 +332,15 @@ int main(int argc, char** argv)
319 319
 	char *options;
320 320
 
321 321
 	/* added by jku: add exit handler */
322
-        if (signal(SIGINT, sig_usr) == SIG_ERR ) {
323
- 		DPrint("ERROR: no SIGINT signal handler can be installed\n");
324
-                goto error;
325
-        }
326
-#ifdef STATS
322
+	if (signal(SIGINT, sig_usr) == SIG_ERR ) {
323
+		DPrint("ERROR: no SIGINT signal handler can be installed\n");
324
+		goto error;
325
+	}
326
+
327 327
 	if (signal(SIGUSR1, sig_usr)  == SIG_ERR ) {
328
-                DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
329
-                goto error;
330
-        }
331
-#endif
328
+		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
329
+		goto error;
330
+	}
332 331
 
333 332
 	/* process command line (get port no, cfg. file path etc) */
334 333
 	opterr=0;
... ...
@@ -529,7 +541,13 @@ int main(int argc, char** argv)
529 529
 		goto error;
530 530
 	}
531 531
 #endif
532
-
532
+	
533
+#ifdef SHM_MEM
534
+	if (shm_mem_init()==-1) {
535
+		LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
536
+		goto error;
537
+	}
538
+#endif
533 539
 	
534 540
 	return main_loop();
535 541
 
... ...
@@ -19,7 +19,16 @@ extern struct qm_block* mem_block;
19 19
 #define pkg_free(p)   qm_free(mem_block, p)
20 20
 #define pkg_status()  qm_status(mem_block)
21 21
 
22
+#elif defined(SHM_MEM) && defined(USE_SHM_MEM)
23
+
24
+#include "shm_mem.h"
25
+
26
+#define pkg_malloc(s) sh_malloc(s)
27
+#define pkg_free(p)   sh_free(p)
28
+#define pkg_status()  sh_status()
29
+
22 30
 #else
31
+
23 32
 #include <stdlib.h>
24 33
 
25 34
 #define pkg_malloc(s) \
26 35
new file mode 100644
... ...
@@ -0,0 +1,114 @@
0
+/* $Id$
1
+ *
2
+ * Shared memory functions
3
+ */
4
+
5
+#ifdef SHM_MEM
6
+
7
+#include "shm_mem.h"
8
+#include "config.h"
9
+
10
+
11
+/* define semun */
12
+#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
13
+	/* union semun is defined by including <sys/sem.h> */
14
+#else
15
+	/* according to X/OPEN we have to define it ourselves */
16
+	union semun {
17
+		int val;                    /* value for SETVAL */
18
+		struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
19
+		unsigned short int *array;  /* array for GETALL, SETALL */
20
+		struct seminfo *__buf;      /* buffer for IPC_INFO */
21
+	};
22
+#endif
23
+
24
+
25
+
26
+
27
+static int shm_shmid=-1; /*shared memory id*/
28
+int shm_semid=-1; /*semaphore id*/
29
+static void* shm_mempool=(void*)-1;
30
+struct qm_block* shm_block;
31
+
32
+
33
+
34
+/* ret -1 on erro*/
35
+int shm_mem_init()
36
+{
37
+
38
+	struct shmid_ds shm_info;
39
+	union semun su;
40
+	int ret;
41
+
42
+	if ((shm_shmid!=-1)||(shm_semid!=-1)||(shm_mempool!=(void*)-1)){
43
+		LOG(L_CRIT, "BUG: shm_mem_init: shm already initialized\n");
44
+		return -1;
45
+	}
46
+	
47
+	shm_shmid=shmget(IPC_PRIVATE, SHM_MEM_SIZE, 0700);
48
+	if (shm_shmid==-1){
49
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate shared memory"
50
+				" segment: %s\n", strerror(errno));
51
+		return -1;
52
+	}
53
+	shm_mempool=shmat(shm_shmid, 0, 0);
54
+	if (shm_mempool==(void*)-1){
55
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not attach shared memory"
56
+				" segment: %s\n", strerror(errno));
57
+		/* destroy segment*/
58
+		shm_mem_destroy();
59
+		return -1;
60
+	}
61
+	/* alloc a semaphore (for malloc)*/
62
+	shm_semid=semget(IPC_PRIVATE, 1, 0700);
63
+	if (shm_semid==-1){
64
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate semaphore: %s\n",
65
+				strerror(errno));
66
+		shm_mem_destroy();
67
+		return -1;
68
+	}
69
+	/* set its value to 1 (mutex)*/
70
+	su.val=1;
71
+	ret=semctl(shm_semid, 0, SETVAL, su);
72
+	if (ret==-1){
73
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not set initial semaphore"
74
+				" value: %s\n", strerror(errno));
75
+		shm_mem_destroy();
76
+		return -1;
77
+	}
78
+	/* init it for malloc*/
79
+	shm_block=qm_malloc_init(shm_mempool, SHM_MEM_SIZE);
80
+	if (shm_block==0){
81
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize shared"
82
+				" malloc\n");
83
+		shm_mem_destroy();
84
+		return -1;
85
+	}
86
+	DBG("shm_mem_init: success\n");
87
+	
88
+	return 0;
89
+}
90
+
91
+
92
+
93
+void shm_mem_destroy()
94
+{
95
+	struct shmid_ds shm_info;
96
+	
97
+	DBG("shm_mem_destroy\n");
98
+	if (shm_mempool && (shm_mempool!=(void*)-1)) {
99
+		shmdt(shm_mempool);
100
+		shm_mempool=(void*)-1;
101
+	}
102
+	if (shm_shmid!=-1) {
103
+		shmctl(shm_shmid, IPC_RMID, &shm_info);
104
+		shm_shmid=-1;
105
+	}
106
+	if (shm_semid!=-1) {
107
+		semctl(shm_semid, 0, IPC_RMID, (union semun)0);
108
+		shm_semid=-1;
109
+	}
110
+}
111
+
112
+
113
+#endif
0 114
new file mode 100644
... ...
@@ -0,0 +1,122 @@
0
+/* $Id$*
1
+ *
2
+ * shared mem stuff
3
+ */
4
+
5
+#ifdef SHM_MEM
6
+
7
+#ifndef shm_mem_h
8
+#define shm_mem_h
9
+
10
+#include <string.h>
11
+#include <errno.h>
12
+#include <sys/types.h>
13
+#include <sys/ipc.h>
14
+#include <sys/shm.h>
15
+#include <sys/sem.h>
16
+#include <string.h>
17
+#include <errno.h>
18
+
19
+
20
+
21
+#include "q_malloc.h"
22
+#include "dprint.h"
23
+
24
+extern struct qm_block* shm_block;
25
+extern int shm_semid;
26
+
27
+int shm_mem_init();
28
+void shm_mem_destroy();
29
+
30
+
31
+
32
+inline static void sh_lock()
33
+{
34
+	struct sembuf sop;
35
+	
36
+	sop.sem_num=0;
37
+	sop.sem_op=-1; /*down*/
38
+	sop.sem_flg=0 /*SEM_UNDO*/;
39
+again:
40
+//	semop(shm_semid, &sop, 1);
41
+#if 0
42
+	switch(ret){
43
+		case 0: /*ok*/
44
+			break;
45
+		case EINTR: /*interrupted by signal, try again*/
46
+			DBG("sh_lock: interrupted by signal, trying again...\n");
47
+			goto again;
48
+		default:
49
+			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
50
+					strerror(errno));
51
+	}
52
+#endif
53
+}
54
+
55
+
56
+
57
+inline static void sh_unlock()
58
+{
59
+	struct sembuf sop;
60
+	
61
+	sop.sem_num=0;
62
+	sop.sem_op=1; /*up*/
63
+	sop.sem_flg=0 /*SEM_UNDO*/;
64
+again:
65
+//	semop(shm_semid, &sop, 1);
66
+#if 0
67
+	/*should ret immediately*/
68
+	switch(ret){
69
+		case 0: /*ok*/
70
+			break;
71
+		case EINTR: /*interrupted by signal, try again*/
72
+			DBG("sh_lock: interrupted by signal, trying again...\n");
73
+			goto again;
74
+		default:
75
+			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
76
+					strerror(errno));
77
+	}
78
+#endif
79
+}
80
+
81
+
82
+inline static void* sh_malloc(unsigned int size)
83
+{
84
+	void *p;
85
+	
86
+	/*if (sh_lock()==0){*/
87
+		sh_lock();
88
+		p=qm_malloc(shm_block, size);
89
+		sh_unlock();
90
+	/*
91
+	}else{
92
+		p=0;
93
+	}*/
94
+	return p;
95
+}
96
+
97
+
98
+
99
+#define sh_free(p) \
100
+do { \
101
+		sh_lock(); \
102
+		qm_free(shm_block, p); \
103
+		sh_unlock(); \
104
+}while(0)
105
+
106
+
107
+
108
+#define sh_status() \
109
+do { \
110
+		sh_lock(); \
111
+		qm_status(shm_block); \
112
+		sh_unlock(); \
113
+}while(0)
114
+
115
+
116
+	
117
+
118
+#endif
119
+
120
+#endif
121
+