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 24
 	(>=0.12). 
23 25
 	
24 26
 
27
+Clean:
28
+make clean   (clean the modules too)
29
+make proper  (clean also the dependencies)
30
+
25 31
 Compile:
26 32
 
33
+make proper
27 34
 make
28 35
 (or gmake on non-Linux systems)
29 36
 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 91
 endif
85 92
 
86 93
 
87
-MKDEP=gcc -M 
94
+MKDEP=gcc -M $(DEFS)
88 95
 
89 96
 ALLDEP=Makefile
90 97
 
... ...
@@ -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>
... ...
@@ -293,16 +296,26 @@ static void sig_usr(int signo)
293 296
 #endif
294 297
 #ifdef PKG_MALLOC
295 298
 		pkg_status();
299
+#endif
300
+#ifdef SHM_MEM
301
+		sh_status();
296 302
 #endif
297 303
 		DPrint("INT received, program terminates\n");
298 304
 		DPrint("Thank you for flying ser\n");
305
+		/* WARNING: very dangerous, might be unsafe*/
306
+#ifdef SHM_MEM
307
+		shm_mem_destroy();
299 308
 		exit(0);
309
+#endif
300 310
 	} else if (signo==SIGUSR1) { /* statistic */
301 311
 #ifdef STATS
302 312
 		dump_all_statistic();
303 313
 #endif
304 314
 #ifdef PKG_MALLOC
305 315
 		pkg_status();
316
+#endif
317
+#ifdef SHM_MEM
318
+		sh_status();
306 319
 #endif
307 320
 	}
308 321
 }
... ...
@@ -319,16 +332,15 @@ int main(int argc, char** argv)
319 332
 	char *options;
320 333
 
321 334
 	/* 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
335
+	if (signal(SIGINT, sig_usr) == SIG_ERR ) {
336
+		DPrint("ERROR: no SIGINT signal handler can be installed\n");
337
+		goto error;
338
+	}
339
+
327 340
 	if (signal(SIGUSR1, sig_usr)  == SIG_ERR ) {
328
-                DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
329
-                goto error;
330
-        }
331
-#endif
341
+		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
342
+		goto error;
343
+	}
332 344
 
333 345
 	/* process command line (get port no, cfg. file path etc) */
334 346
 	opterr=0;
... ...
@@ -529,7 +541,13 @@ int main(int argc, char** argv)
529 541
 		goto error;
530 542
 	}
531 543
 #endif
532
-
544
+	
545
+#ifdef SHM_MEM
546
+	if (shm_mem_init()==-1) {
547
+		LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
548
+		goto error;
549
+	}
550
+#endif
533 551
 	
534 552
 	return main_loop();
535 553
 
... ...
@@ -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 @@
1
+/* $Id$
2
+ *
3
+ * Shared memory functions
4
+ */
5
+
6
+#ifdef SHM_MEM
7
+
8
+#include "shm_mem.h"
9
+#include "config.h"
10
+
11
+
12
+/* define semun */
13
+#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
14
+	/* union semun is defined by including <sys/sem.h> */
15
+#else
16
+	/* according to X/OPEN we have to define it ourselves */
17
+	union semun {
18
+		int val;                    /* value for SETVAL */
19
+		struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
20
+		unsigned short int *array;  /* array for GETALL, SETALL */
21
+		struct seminfo *__buf;      /* buffer for IPC_INFO */
22
+	};
23
+#endif
24
+
25
+
26
+
27
+
28
+static int shm_shmid=-1; /*shared memory id*/
29
+int shm_semid=-1; /*semaphore id*/
30
+static void* shm_mempool=(void*)-1;
31
+struct qm_block* shm_block;
32
+
33
+
34
+
35
+/* ret -1 on erro*/
36
+int shm_mem_init()
37
+{
38
+
39
+	struct shmid_ds shm_info;
40
+	union semun su;
41
+	int ret;
42
+
43
+	if ((shm_shmid!=-1)||(shm_semid!=-1)||(shm_mempool!=(void*)-1)){
44
+		LOG(L_CRIT, "BUG: shm_mem_init: shm already initialized\n");
45
+		return -1;
46
+	}
47
+	
48
+	shm_shmid=shmget(IPC_PRIVATE, SHM_MEM_SIZE, 0700);
49
+	if (shm_shmid==-1){
50
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate shared memory"
51
+				" segment: %s\n", strerror(errno));
52
+		return -1;
53
+	}
54
+	shm_mempool=shmat(shm_shmid, 0, 0);
55
+	if (shm_mempool==(void*)-1){
56
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not attach shared memory"
57
+				" segment: %s\n", strerror(errno));
58
+		/* destroy segment*/
59
+		shm_mem_destroy();
60
+		return -1;
61
+	}
62
+	/* alloc a semaphore (for malloc)*/
63
+	shm_semid=semget(IPC_PRIVATE, 1, 0700);
64
+	if (shm_semid==-1){
65
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate semaphore: %s\n",
66
+				strerror(errno));
67
+		shm_mem_destroy();
68
+		return -1;
69
+	}
70
+	/* set its value to 1 (mutex)*/
71
+	su.val=1;
72
+	ret=semctl(shm_semid, 0, SETVAL, su);
73
+	if (ret==-1){
74
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not set initial semaphore"
75
+				" value: %s\n", strerror(errno));
76
+		shm_mem_destroy();
77
+		return -1;
78
+	}
79
+	/* init it for malloc*/
80
+	shm_block=qm_malloc_init(shm_mempool, SHM_MEM_SIZE);
81
+	if (shm_block==0){
82
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize shared"
83
+				" malloc\n");
84
+		shm_mem_destroy();
85
+		return -1;
86
+	}
87
+	DBG("shm_mem_init: success\n");
88
+	
89
+	return 0;
90
+}
91
+
92
+
93
+
94
+void shm_mem_destroy()
95
+{
96
+	struct shmid_ds shm_info;
97
+	
98
+	DBG("shm_mem_destroy\n");
99
+	if (shm_mempool && (shm_mempool!=(void*)-1)) {
100
+		shmdt(shm_mempool);
101
+		shm_mempool=(void*)-1;
102
+	}
103
+	if (shm_shmid!=-1) {
104
+		shmctl(shm_shmid, IPC_RMID, &shm_info);
105
+		shm_shmid=-1;
106
+	}
107
+	if (shm_semid!=-1) {
108
+		semctl(shm_semid, 0, IPC_RMID, (union semun)0);
109
+		shm_semid=-1;
110
+	}
111
+}
112
+
113
+
114
+#endif
0 115
new file mode 100644
... ...
@@ -0,0 +1,122 @@
1
+/* $Id$*
2
+ *
3
+ * shared mem stuff
4
+ */
5
+
6
+#ifdef SHM_MEM
7
+
8
+#ifndef shm_mem_h
9
+#define shm_mem_h
10
+
11
+#include <string.h>
12
+#include <errno.h>
13
+#include <sys/types.h>
14
+#include <sys/ipc.h>
15
+#include <sys/shm.h>
16
+#include <sys/sem.h>
17
+#include <string.h>
18
+#include <errno.h>
19
+
20
+
21
+
22
+#include "q_malloc.h"
23
+#include "dprint.h"
24
+
25
+extern struct qm_block* shm_block;
26
+extern int shm_semid;
27
+
28
+int shm_mem_init();
29
+void shm_mem_destroy();
30
+
31
+
32
+
33
+inline static void sh_lock()
34
+{
35
+	struct sembuf sop;
36
+	
37
+	sop.sem_num=0;
38
+	sop.sem_op=-1; /*down*/
39
+	sop.sem_flg=0 /*SEM_UNDO*/;
40
+again:
41
+//	semop(shm_semid, &sop, 1);
42
+#if 0
43
+	switch(ret){
44
+		case 0: /*ok*/
45
+			break;
46
+		case EINTR: /*interrupted by signal, try again*/
47
+			DBG("sh_lock: interrupted by signal, trying again...\n");
48
+			goto again;
49
+		default:
50
+			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
51
+					strerror(errno));
52
+	}
53
+#endif
54
+}
55
+
56
+
57
+
58
+inline static void sh_unlock()
59
+{
60
+	struct sembuf sop;
61
+	
62
+	sop.sem_num=0;
63
+	sop.sem_op=1; /*up*/
64
+	sop.sem_flg=0 /*SEM_UNDO*/;
65
+again:
66
+//	semop(shm_semid, &sop, 1);
67
+#if 0
68
+	/*should ret immediately*/
69
+	switch(ret){
70
+		case 0: /*ok*/
71
+			break;
72
+		case EINTR: /*interrupted by signal, try again*/
73
+			DBG("sh_lock: interrupted by signal, trying again...\n");
74
+			goto again;
75
+		default:
76
+			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
77
+					strerror(errno));
78
+	}
79
+#endif
80
+}
81
+
82
+
83
+inline static void* sh_malloc(unsigned int size)
84
+{
85
+	void *p;
86
+	
87
+	/*if (sh_lock()==0){*/
88
+		sh_lock();
89
+		p=qm_malloc(shm_block, size);
90
+		sh_unlock();
91
+	/*
92
+	}else{
93
+		p=0;
94
+	}*/
95
+	return p;
96
+}
97
+
98
+
99
+
100
+#define sh_free(p) \
101
+do { \
102
+		sh_lock(); \
103
+		qm_free(shm_block, p); \
104
+		sh_unlock(); \
105
+}while(0)
106
+
107
+
108
+
109
+#define sh_status() \
110
+do { \
111
+		sh_lock(); \
112
+		qm_status(shm_block); \
113
+		sh_unlock(); \
114
+}while(0)
115
+
116
+
117
+	
118
+
119
+#endif
120
+
121
+#endif
122
+