Browse code

- LOG & DBG simultaneous execution protection (they can be used almost safely from the signal handlers)

Andrei Pelinescu-Onciul authored on 24/10/2006 16:53:55
Showing 2 changed files
... ...
@@ -37,6 +37,9 @@
37 37
 #include <stdio.h>
38 38
 #include <strings.h>
39 39
 
40
+volatile int dprint_crit=0; /* signal protection: !=0 when dprint/LOG/DBG are
41
+								printing */
42
+
40 43
 static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
41 44
 					"LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
42 45
 					"LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
... ...
@@ -47,7 +47,12 @@
47 47
 extern int debug;
48 48
 extern int log_stderr;
49 49
 extern int log_facility;
50
+extern volatile int dprint_crit; /* protection against "simultaneous"
51
+									printing from signal handlers */
50 52
 
53
+#define DPRINT_NON_CRIT		(dprint_crit==0)
54
+#define DPRINT_CRIT_ENTER	(dprint_crit++)
55
+#define DPRINT_CRIT_EXIT	(dprint_crit--)
51 56
 
52 57
 #define DPRINT_LEV	1
53 58
 /* priority at which we log */
... ...
@@ -87,23 +92,27 @@ int str2facility(char *s);
87 87
 	#ifdef __SUNPRO_C
88 88
 		#define DPrint( ...) \
89 89
 			do{ \
90
-				if (debug>=DPRINT_LEV){ \
90
+				if ((debug>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
91
+					DPRINT_CRIT_ENTER; \
91 92
 					if (log_stderr){ \
92 93
 						dprint (__VA_ARGS__); \
93 94
 					}else{ \
94 95
 						syslog(DPRINT_LEV|log_facility,  __VA_ARGS__); \
95 96
 					}\
97
+					DPRINT_CRIT_EXIT; \
96 98
 				} \
97 99
 			}while(0)
98 100
 	#else
99 101
 			#define DPrint(fmt,args...) \
100 102
 			do{ \
101
-				if (debug>=DPRINT_LEV){ \
103
+				if ((debug>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
104
+					DPRINT_CRIT_ENTER; \
102 105
 					if (log_stderr){ \
103 106
 						dprint (fmt, ## args); \
104 107
 					}else{ \
105 108
 						syslog(DPRINT_LEV|log_facility, fmt, ## args); \
106 109
 					}\
110
+					DPRINT_CRIT_EXIT; \
107 111
 				} \
108 112
 			}while(0)
109 113
 	#endif
... ...
@@ -124,7 +133,8 @@ int str2facility(char *s);
124 124
 	#ifdef __SUNPRO_C
125 125
 		#define LOG(lev, ...) \
126 126
 			do { \
127
-				if (debug>=(lev)){ \
127
+				if ((debug>=(lev)) && DPRINT_NON_CRIT){ \
128
+					DPRINT_CRIT_ENTER; \
128 129
 					if (log_stderr) dprint (__VA_ARGS__); \
129 130
 					else { \
130 131
 						switch(lev){ \
... ...
@@ -151,12 +161,14 @@ int str2facility(char *s);
151 151
 								break; \
152 152
 						} \
153 153
 					} \
154
+					DPRINT_CRIT_EXIT; \
154 155
 				} \
155 156
 			}while(0)
156 157
 	#else
157 158
 		#define LOG(lev, fmt, args...) \
158 159
 			do { \
159
-				if (debug>=(lev)){ \
160
+				if ((debug>=(lev)) && DPRINT_NON_CRIT){ \
161
+					DPRINT_CRIT_ENTER; \
160 162
 					if (log_stderr) dprint (fmt, ## args); \
161 163
 					else { \
162 164
 						switch(lev){ \
... ...
@@ -183,6 +195,7 @@ int str2facility(char *s);
183 183
 								break; \
184 184
 						} \
185 185
 					} \
186
+					DPRINT_CRIT_EXIT; \
186 187
 				} \
187 188
 			}while(0)
188 189
 	#endif /*SUN_PRO_C*/