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 92
 	#ifdef __SUNPRO_C
88 93
 		#define DPrint( ...) \
89 94
 			do{ \
90
-				if (debug>=DPRINT_LEV){ \
95
+				if ((debug>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
96
+					DPRINT_CRIT_ENTER; \
91 97
 					if (log_stderr){ \
92 98
 						dprint (__VA_ARGS__); \
93 99
 					}else{ \
94 100
 						syslog(DPRINT_LEV|log_facility,  __VA_ARGS__); \
95 101
 					}\
102
+					DPRINT_CRIT_EXIT; \
96 103
 				} \
97 104
 			}while(0)
98 105
 	#else
99 106
 			#define DPrint(fmt,args...) \
100 107
 			do{ \
101
-				if (debug>=DPRINT_LEV){ \
108
+				if ((debug>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
109
+					DPRINT_CRIT_ENTER; \
102 110
 					if (log_stderr){ \
103 111
 						dprint (fmt, ## args); \
104 112
 					}else{ \
105 113
 						syslog(DPRINT_LEV|log_facility, fmt, ## args); \
106 114
 					}\
115
+					DPRINT_CRIT_EXIT; \
107 116
 				} \
108 117
 			}while(0)
109 118
 	#endif
... ...
@@ -124,7 +133,8 @@ int str2facility(char *s);
124 133
 	#ifdef __SUNPRO_C
125 134
 		#define LOG(lev, ...) \
126 135
 			do { \
127
-				if (debug>=(lev)){ \
136
+				if ((debug>=(lev)) && DPRINT_NON_CRIT){ \
137
+					DPRINT_CRIT_ENTER; \
128 138
 					if (log_stderr) dprint (__VA_ARGS__); \
129 139
 					else { \
130 140
 						switch(lev){ \
... ...
@@ -151,12 +161,14 @@ int str2facility(char *s);
151 161
 								break; \
152 162
 						} \
153 163
 					} \
164
+					DPRINT_CRIT_EXIT; \
154 165
 				} \
155 166
 			}while(0)
156 167
 	#else
157 168
 		#define LOG(lev, fmt, args...) \
158 169
 			do { \
159
-				if (debug>=(lev)){ \
170
+				if ((debug>=(lev)) && DPRINT_NON_CRIT){ \
171
+					DPRINT_CRIT_ENTER; \
160 172
 					if (log_stderr) dprint (fmt, ## args); \
161 173
 					else { \
162 174
 						switch(lev){ \
... ...
@@ -183,6 +195,7 @@ int str2facility(char *s);
183 195
 								break; \
184 196
 						} \
185 197
 					} \
198
+					DPRINT_CRIT_EXIT; \
186 199
 				} \
187 200
 			}while(0)
188 201
 	#endif /*SUN_PRO_C*/