ha_logd: Add a SIGHUP signal handler to close/open log files

Without the signal handler cl_log uses inefficient IO, as it
has to open/seek/flush/close the log files in order to allow
cron log file rotation.

Signed-off-by: Bernd Schubert <bschub...@ddn.com>


diff --git a/include/clplumbing/cl_log.h b/include/clplumbing/cl_log.h
--- a/include/clplumbing/cl_log.h
+++ b/include/clplumbing/cl_log.h
@@ -59,7 +59,9 @@ void          cl_glib_msg_handler(const gchar *l
 ,              gpointer user_data);
 
 void           cl_flush_logs(void);
-void cl_log_args(int argc, char **argv);
-int cl_log_is_logd_fd(int fd);
+void           cl_log_args(int argc, char **argv);
+int            cl_log_is_logd_fd(int fd);
+void           cl_closeopen_log_files(void);
+void           cl_log_enable_signal_handler(void);
 
 #endif
diff --git a/lib/clplumbing/cl_log.c b/lib/clplumbing/cl_log.c
--- a/lib/clplumbing/cl_log.c
+++ b/lib/clplumbing/cl_log.c
@@ -122,6 +122,12 @@ cl_log_is_logd_fd(int fd)
 }
 
 void
+cl_log_enable_signal_handler(void)
+{
+       have_signal_handler = TRUE;
+}
+
+void
 cl_log_enable_stderr(int truefalse)
 {
        stderr_enabled = truefalse;
@@ -557,6 +563,30 @@ open_log_file(const char * fname)
        return fd;
 }
 
+/* open/re-open log file files
+ * Also used by the signal handler to allow to logrotate log files
+ */
+void cl_closeopen_log_files(void)
+{
+       if (debugfile_name) {
+               if (debug_fd != -1) {
+                       close(debug_fd); /* ignore errors */
+                       debug_fd = -1;
+               }
+               if (debugfile_name)
+                       debug_fd = open_log_file(debugfile_name);
+       }
+       
+       if (logfile_name) {
+               if (log_fd != -1) {
+                       close(log_fd); /* ignore errors */
+                       log_fd = -1;
+               }
+               if (logfile_name)
+                       log_fd = open_log_file(logfile_name);
+       }
+}
+
 /*
  * This function can cost us realtime unless use_logging_daemon
  * is enabled.  Then we log everything through a child process using
@@ -591,23 +621,15 @@ cl_direct_log(int priority, const char* 
                       entity_pid, pristr,  buf, 0);
        }
 
-       if (debugfile_name != NULL) {
-               if (debug_fd != -1) {
-                       debug_fd = open_log_file(debugfile_name);
-               }
-               if (debug_fd != -1)
-                       append_log(debug_fd ,entity, entity_pid, ts, pristr, 
-                                  buf);
+       if (debug_fd == -1 || log_fd == -1) {
+               cl_closeopen_log_files();
        }
+       
+       if (log_fd != -1)
+               append_log(debug_fd ,entity, entity_pid, ts, pristr, buf);
 
-       if (priority != LOG_DEBUG && logfile_name != NULL) {
-               if (log_fd != -1) {
-                       log_fd = open_log_file(logfile_name);
-               }
-               if (log_fd != -1)
-                       append_log(log_fd ,entity, entity_pid, ts, pristr, 
-                                  buf);
-       }
+       if (priority != LOG_DEBUG && log_fd != -1)
+               append_log(log_fd ,entity, entity_pid, ts, pristr, buf);
 
        if (needprivs) {
                return_to_dropped_privs();
diff --git a/logd/ha_logd.c b/logd/ha_logd.c
--- a/logd/ha_logd.c
+++ b/logd/ha_logd.c
@@ -728,6 +728,16 @@ logd_term_action(int sig, gpointer userd
         return TRUE;
 }
 
+/*
+ * Handle SIGHUP to re-open log files
+ */
+static gboolean
+logd_hup_action(int sig, gpointer userdata)
+{
+       cl_closeopen_log_files();
+       
+       return TRUE;
+}
 
 static void
 read_msg_process(IPC_Channel* chan)
@@ -767,6 +777,10 @@ read_msg_process(IPC_Channel* chan)
 
        G_main_add_IPC_Channel(G_PRIORITY_DEFAULT, chan, FALSE,NULL,NULL,NULL);
        
+       G_main_add_SignalHandler(G_PRIORITY_DEFAULT, SIGHUP, 
+                                logd_hup_action, mainloop, NULL);
+       cl_log_enable_signal_handler();
+       
        g_main_run(mainloop);
        
        return;
@@ -876,6 +890,11 @@ write_msg_process(IPC_Channel* readchan)
 
        G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGTERM, 
                                 logd_term_write_action, mainloop, NULL);
+                                
+       G_main_add_SignalHandler(G_PRIORITY_DEFAULT, SIGHUP, 
+                                logd_hup_action, mainloop, NULL);
+       cl_log_enable_signal_handler();
+       
        
        g_main_run(mainloop);
        

_______________________________________________
Pacemaker mailing list: Pacemaker@oss.clusterlabs.org
http://oss.clusterlabs.org/mailman/listinfo/pacemaker

Project Home: http://www.clusterlabs.org
Getting started: http://www.clusterlabs.org/doc/Cluster_from_Scratch.pdf
Bugs: http://developerbugs.linux-foundation.org/enter_bug.cgi?product=Pacemaker

Reply via email to