On Fri, Nov 12, 2010 at 05:04:45PM +0100, bs_li...@aakef.fastmail.fm wrote:
> # HG changeset patch
> # User Bernd Schubert <bernd.schub...@fastmail.fm>
> # Date 1289577717 -3600
> # Node ID 8b4e11cab67165eb44bd89b9cbb0319078fc8fbc
> # Parent  112b6e5408892b791dbd46b66ce75fe98dd06c26
> 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,8 +59,10 @@ 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);
> -const char * prio2str(int priority);
> +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);
> +const char *         prio2str(int priority);
>  
>  #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;
> @@ -546,6 +552,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);
> +     }

Shouldn't both bodies be reduced to something like this:

                if (log_fd != -1)
                        close(log_fd); /* ignore errors */
                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
> @@ -580,23 +610,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)

Should this be debug_fd?

Cheers,

Dejan

> +             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
> @@ -727,6 +727,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)
> @@ -766,6 +776,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;
> @@ -875,6 +889,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);
>       
> _______________________________________________________
> Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org
> http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
> Home Page: http://linux-ha.org/
_______________________________________________________
Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/

Reply via email to