Ver código fonte

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

Andrei Pelinescu-Onciul 19 anos atrás
pai
commit
2d90691cca
2 arquivos alterados com 20 adições e 4 exclusões
  1. 3 0
      dprint.c
  2. 17 4
      dprint.h

+ 3 - 0
dprint.c

@@ -37,6 +37,9 @@
 #include <stdio.h>
 #include <stdio.h>
 #include <strings.h>
 #include <strings.h>
 
 
+volatile int dprint_crit=0; /* signal protection: !=0 when dprint/LOG/DBG are
+								printing */
+
 static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
 static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
 					"LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
 					"LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
 					"LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
 					"LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",

+ 17 - 4
dprint.h

@@ -47,7 +47,12 @@
 extern int debug;
 extern int debug;
 extern int log_stderr;
 extern int log_stderr;
 extern int log_facility;
 extern int log_facility;
+extern volatile int dprint_crit; /* protection against "simultaneous"
+									printing from signal handlers */
 
 
+#define DPRINT_NON_CRIT		(dprint_crit==0)
+#define DPRINT_CRIT_ENTER	(dprint_crit++)
+#define DPRINT_CRIT_EXIT	(dprint_crit--)
 
 
 #define DPRINT_LEV	1
 #define DPRINT_LEV	1
 /* priority at which we log */
 /* priority at which we log */
@@ -87,23 +92,27 @@ int str2facility(char *s);
 	#ifdef __SUNPRO_C
 	#ifdef __SUNPRO_C
 		#define DPrint( ...) \
 		#define DPrint( ...) \
 			do{ \
 			do{ \
-				if (debug>=DPRINT_LEV){ \
+				if ((debug>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
+					DPRINT_CRIT_ENTER; \
 					if (log_stderr){ \
 					if (log_stderr){ \
 						dprint (__VA_ARGS__); \
 						dprint (__VA_ARGS__); \
 					}else{ \
 					}else{ \
 						syslog(DPRINT_LEV|log_facility,  __VA_ARGS__); \
 						syslog(DPRINT_LEV|log_facility,  __VA_ARGS__); \
 					}\
 					}\
+					DPRINT_CRIT_EXIT; \
 				} \
 				} \
 			}while(0)
 			}while(0)
 	#else
 	#else
 			#define DPrint(fmt,args...) \
 			#define DPrint(fmt,args...) \
 			do{ \
 			do{ \
-				if (debug>=DPRINT_LEV){ \
+				if ((debug>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
+					DPRINT_CRIT_ENTER; \
 					if (log_stderr){ \
 					if (log_stderr){ \
 						dprint (fmt, ## args); \
 						dprint (fmt, ## args); \
 					}else{ \
 					}else{ \
 						syslog(DPRINT_LEV|log_facility, fmt, ## args); \
 						syslog(DPRINT_LEV|log_facility, fmt, ## args); \
 					}\
 					}\
+					DPRINT_CRIT_EXIT; \
 				} \
 				} \
 			}while(0)
 			}while(0)
 	#endif
 	#endif
@@ -124,7 +133,8 @@ int str2facility(char *s);
 	#ifdef __SUNPRO_C
 	#ifdef __SUNPRO_C
 		#define LOG(lev, ...) \
 		#define LOG(lev, ...) \
 			do { \
 			do { \
-				if (debug>=(lev)){ \
+				if ((debug>=(lev)) && DPRINT_NON_CRIT){ \
+					DPRINT_CRIT_ENTER; \
 					if (log_stderr) dprint (__VA_ARGS__); \
 					if (log_stderr) dprint (__VA_ARGS__); \
 					else { \
 					else { \
 						switch(lev){ \
 						switch(lev){ \
@@ -151,12 +161,14 @@ int str2facility(char *s);
 								break; \
 								break; \
 						} \
 						} \
 					} \
 					} \
+					DPRINT_CRIT_EXIT; \
 				} \
 				} \
 			}while(0)
 			}while(0)
 	#else
 	#else
 		#define LOG(lev, fmt, args...) \
 		#define LOG(lev, fmt, args...) \
 			do { \
 			do { \
-				if (debug>=(lev)){ \
+				if ((debug>=(lev)) && DPRINT_NON_CRIT){ \
+					DPRINT_CRIT_ENTER; \
 					if (log_stderr) dprint (fmt, ## args); \
 					if (log_stderr) dprint (fmt, ## args); \
 					else { \
 					else { \
 						switch(lev){ \
 						switch(lev){ \
@@ -183,6 +195,7 @@ int str2facility(char *s);
 								break; \
 								break; \
 						} \
 						} \
 					} \
 					} \
+					DPRINT_CRIT_EXIT; \
 				} \
 				} \
 			}while(0)
 			}while(0)
 	#endif /*SUN_PRO_C*/
 	#endif /*SUN_PRO_C*/