[PATCH] base/logging: Handle case where log file can't be opened better

Robin Sonefors robin.sonefors at op5.com
Mon Dec 3 18:26:17 CET 2012


When log file couldn't be opened, nagios would try to print a message to
STDOUT, and then segfault. You could say that gets the point across, I'd
say it's unnecessarily brutal.

This patch sends nagios back to work instead, skipping the logging.
Maybe the log file error will sort itself out once a flaky connection
has restabilized, maybe it won't and nagios'll notice and send out a
notification, maybe other means of logging is in use.

Signed-off-by: Robin Sonefors <robin.sonefors at op5.com>
---
 base/logging.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/base/logging.c b/base/logging.c
index 5d6c7d7..f05721f 100644
--- a/base/logging.c
+++ b/base/logging.c
@@ -116,8 +116,11 @@ FILE *open_log_file(void)
 		return log_fp;
 
 	log_fp = fopen(log_file, "a+");
-	if(log_fp == NULL && daemon_mode == FALSE) {
-		printf("Warning: Cannot open log file '%s' for writing\n", log_file);
+	if(log_fp == NULL) {
+		if (daemon_mode == FALSE) {
+			printf("Warning: Cannot open log file '%s' for writing\n", log_file);
+			}
+		return NULL;
 		}
 
 	(void)fcntl(fileno(log_fp), F_SETFD, FD_CLOEXEC);
@@ -152,6 +155,8 @@ int write_to_log(char *buffer, unsigned long data_type, time_t *timestamp) {
 		return OK;
 
 	fp = open_log_file();
+	if (fp == NULL)
+		return ERROR;
 	/* what timestamp should we use? */
 	if(timestamp == NULL)
 		time(&log_time);
@@ -365,6 +370,8 @@ int rotate_log_file(time_t rotation_time) {
 	/* rotate the log file */
 	rename_result = my_rename(log_file, log_archive);
 	log_fp = open_log_file();
+	if (log_fp == NULL)
+		return ERROR;
 
 	if(rename_result) {
 		my_free(log_archive);
-- 
1.7.11.7


------------------------------------------------------------------------------
Keep yourself connected to Go Parallel: 
BUILD Helping you discover the best ways to construct your parallel projects.
http://goparallel.sourceforge.net




More information about the Developers mailing list