Signed-off-by: Steffen (Daode) Nurpmeso <[email protected]>
---
 sysklogd/syslogd.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

Maybe you find this useful.  I was wondering how to get my logs
mailed to me with the busybox syslog, in such a way that i can
create the mail directly after the log has been rotated.  Also log
file compression would be nice.  I'm used to logrotate(8), but all
i need with the Alpine/busybox default is effectively a hook.

So -H PROG will set a program that will be spawn()d, zombies are
avoided via signal(, SIG_IGN).  Using spawn_and_wait() doesn't
seem to be a good idea, just like a double fork for waiting
purposes.  And i never did anything with busybox, this is
a two-hour thing now.  Maybe there are better ways.
Especially: it would possibly be nice to go over "sh -c" instead
of requiring -H to be something real.

Ciao.

diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 0ea557a..5bbac79 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -35,6 +35,14 @@
 //config:        This enables syslogd to rotate the message files
 //config:        on his own. No need to use an external rotatescript.
 //config:
+//config:config FEATURE_ROTATE_NOTIFY
+//config:      bool "Message file rotation notifications"
+//config:      default n
+//config:      depends on FEATURE_ROTATE_LOGFILE
+//config:      help
+//config:        If enabled a program registered via -H will be executed
+//config:        whenever a message file has been rotated.
+//config:
 //config:config FEATURE_REMOTE_LOG
 //config:      bool "Remote Log support"
 //config:      default y
@@ -138,6 +146,9 @@
 //usage:     "\n       -s SIZE         Max size (KB) before rotation 
(default:200KB, 0=off)"
 //usage:     "\n       -b N            N rotated logs to keep (default:1, 
max=99, 0=purge)"
 //usage:       )
+//usage:       IF_FEATURE_ROTATE_NOTIFY(
+//usage:     "\n       -H PROG         Run PROG after (the given) message file 
was rotated"
+//usage:       )
 //usage:     "\n       -l N            Log only messages more urgent than prio 
N (1-8)"
 //usage:     "\n       -S              Smaller output"
 //usage:       IF_FEATURE_SYSLOGD_DUP(
@@ -239,6 +250,9 @@ IF_FEATURE_ROTATE_LOGFILE( \
        /* number of rotated message files */   \
        unsigned logFileRotate;                 \
 ) \
+IF_FEATURE_ROTATE_NOTIFY( \
+       const char *logRotateNotifyHook;                \
+) \
 IF_FEATURE_IPC_SYSLOG( \
        int shmid; /* ipc shared memory id */   \
        int s_semid; /* ipc semaphore id */     \
@@ -293,6 +307,9 @@ static const struct init_globals init_data = {
 #if ENABLE_FEATURE_ROTATE_LOGFILE
        .logFileSize = 200 * 1024,
        .logFileRotate = 1,
+# if ENABLE_FEATURE_ROTATE_NOTIFY
+       .logRotateNotifyHook = NULL,
+# endif
 #endif
 #if ENABLE_FEATURE_IPC_SYSLOG
        .shmid = -1,
@@ -318,6 +335,7 @@ enum {
        OPTBIT_small, // -S
        IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize   ,)  // -s
        IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt  ,)  // -b
+       IF_FEATURE_ROTATE_NOTIFY( OPTBIT_rotatehook ,)  // -H
        IF_FEATURE_REMOTE_LOG(    OPTBIT_remotelog  ,)  // -R
        IF_FEATURE_REMOTE_LOG(    OPTBIT_locallog   ,)  // -L
        IF_FEATURE_IPC_SYSLOG(    OPTBIT_circularlog,)  // -C
@@ -332,6 +350,7 @@ enum {
        OPT_small       = 1 << OPTBIT_small   ,
        OPT_filesize    = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize   )) 
+ 0,
        OPT_rotatecnt   = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt  )) 
+ 0,
+       OPT_rotatehook  = IF_FEATURE_ROTATE_NOTIFY( (1 << OPTBIT_rotatehook )) 
+ 0,
        OPT_remotelog   = IF_FEATURE_REMOTE_LOG(    (1 << OPTBIT_remotelog  )) 
+ 0,
        OPT_locallog    = IF_FEATURE_REMOTE_LOG(    (1 << OPTBIT_locallog   )) 
+ 0,
        OPT_circularlog = IF_FEATURE_IPC_SYSLOG(    (1 << OPTBIT_circularlog)) 
+ 0,
@@ -342,6 +361,7 @@ enum {
 #define OPTION_STR "m:nO:l:S" \
        IF_FEATURE_ROTATE_LOGFILE("s:" ) \
        IF_FEATURE_ROTATE_LOGFILE("b:" ) \
+       IF_FEATURE_ROTATE_NOTIFY( "H:" ) \
        IF_FEATURE_REMOTE_LOG(    "R:" ) \
        IF_FEATURE_REMOTE_LOG(    "L"  ) \
        IF_FEATURE_IPC_SYSLOG(    "C::") \
@@ -356,6 +376,7 @@ enum {
 #define OPTION_PARAM &opt_m, &(G.logFile.path), &opt_l \
        IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
        IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
+       IF_FEATURE_ROTATE_NOTIFY( ,&(G.logRotateNotifyHook)) \
        IF_FEATURE_REMOTE_LOG(    ,&remoteAddrList) \
        IF_FEATURE_IPC_SYSLOG(    ,&opt_C) \
        IF_FEATURE_SYSLOGD_CFG(   ,&opt_f)
@@ -680,6 +701,9 @@ static void log_to_kmsg(int pri UNUSED_PARAM, const char 
*msg UNUSED_PARAM) {}
 /* Print a message to the log file. */
 static void log_locally(time_t now, char *msg, logFile_t *log_file)
 {
+#if ENABLE_FEATURE_ROTATE_NOTIFY
+       bool donotify = 0;
+#endif
 #ifdef SYSLOGD_WRLOCK
        struct flock fl;
 #endif
@@ -722,7 +746,7 @@ static void log_locally(time_t now, char *msg, logFile_t 
*log_file)
                                full_write(fd, msg, len);
                                if (fd != 2)
                                        close(fd);
-                               return;
+                               goto jleave;
                        }
 #if ENABLE_FEATURE_ROTATE_LOGFILE
                        {
@@ -760,6 +784,9 @@ static void log_locally(time_t now, char *msg, logFile_t 
*log_file)
                        }
                        /* newFile == "f.0" now */
                        rename(log_file->path, newFile);
+# if ENABLE_FEATURE_ROTATE_NOTIFY
+                       donotify = 1;
+# endif
                }

                /* We may or may not have just renamed the file away;
@@ -791,6 +818,18 @@ static void log_locally(time_t now, char *msg, logFile_t 
*log_file)
        fl.l_type = F_UNLCK;
        fcntl(log_file->fd, F_SETLKW, &fl);
 #endif
+
+jleave:;
+#if ENABLE_FEATURE_ROTATE_NOTIFY
+       if (donotify && G.logRotateNotifyHook != NULL) {
+               char const *argv[3] = { G.logRotateNotifyHook, log_file->path, 
NULL };
+               pid_t npid;
+
+               if ((npid = spawn((char**)argv)) == -1)
+                       bb_error_msg("'%s': can't run rotation notifier '%s'",
+                               log_file->path, G.logRotateNotifyHook);
+       }
+#endif
 }

 static void parse_fac_prio_20(int pri, char *res20)
@@ -1136,6 +1175,11 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv)
        if (opts & OPT_rotatecnt) // -b
                G.logFileRotate = xatou_range(opt_b, 0, 99);
 #endif
+       /* Not waiting, avoid zombies */
+#if ENABLE_FEATURE_ROTATE_NOTIFY
+       if (G.logRotateNotifyHook != NULL)
+               signal(SIGCHLD, SIG_IGN);
+#endif
 #if ENABLE_FEATURE_IPC_SYSLOG
        if (opt_C) // -Cn
                G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
--
2.7.1

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to