]> git.decadent.org.uk Git - nfs-utils.git/commitdiff
Sanitise stderr logging.
authorneilbrown <neilbrown>
Mon, 14 Mar 2005 05:49:57 +0000 (05:49 +0000)
committerneilbrown <neilbrown>
Mon, 14 Mar 2005 05:49:57 +0000 (05:49 +0000)
support/include/xlog.h
support/nfs/xlog.c
utils/mountd/mountd.c

index 2a839c7caa69284e2d626a719275aa4db670474a..cf9bc916941ace96fc7e8ca551524215e6bd227e 100644 (file)
@@ -7,12 +7,15 @@
 #ifndef XLOG_H
 #define XLOG_H
 
+/* These are logged always. L_FATAL also does exit(1) */
 #define L_FATAL                0x0100
 #define L_ERROR                0x0200
 #define L_WARNING      0x0400
 #define L_NOTICE       0x0800
 #define L_ALL          0xFF00
 
+/* These are logged if enabled with xlog_[s]config */
+/* NB: code does not expect ORing together D_ and L_ */
 #define D_GENERAL      0x0001          /* general debug info */
 #define D_CALL         0x0002
 #define D_AUTH         0x0004
@@ -31,7 +34,8 @@ struct xlog_debugfac {
 };
 
 void                   xlog_open(char *progname);
-void                   xlog_background(void);
+void                   xlog_stderr(int on);
+void                   xlog_syslog(int on);
 void                   xlog_config(int fac, int on);
 void                   xlog_sconfig(char *, int on);
 int                    xlog_enabled(int fac);
index b06d12eb23edc62f2dc34c2599f7c4007f7a9bcb..e1e4c3f0bf412817ea6e9ca633266b949c72f7ca 100644 (file)
 
 #undef VERBOSE_PRINTF
 
-static int  foreground = 1;            /* not a daemon initially       */
+static int  log_stderr = 1;
+static int  log_syslog = 1;
 static int  logging = 0;               /* enable/disable DEBUG logs    */
 static int  logmask = 0;               /* What will be logged          */
 static char log_name[256];             /* name of this program         */
 static int  log_pid = -1;              /* PID of this program          */
-static FILE *log_fp = (FILE *)NULL;    /* fp for the log file          */
 
 static void    xlog_toggle(int sig);
 static struct xlog_debugfac    debugnames[] = {
@@ -50,11 +50,6 @@ void
 xlog_open(char *progname)
 {
        openlog(progname, LOG_PID, LOG_DAEMON);
-       if (foreground) {
-               log_fp = stderr;
-               if (log_fp != NULL)
-                       setbuf(log_fp, NULL);
-       }
 
        strncpy(log_name, progname, sizeof (log_name) - 1);
        log_name [sizeof (log_name) - 1] = '\0';
@@ -65,9 +60,15 @@ xlog_open(char *progname)
 }
 
 void
-xlog_background(void)
+xlog_stderr(int on)
+{
+       log_stderr = on;
+}
+
+void
+xlog_syslog(int on)
 {
-       foreground = 0;
+       log_syslog = on;
 }
 
 static void
@@ -126,17 +127,13 @@ xlog_enabled(int fac)
 }
 
 
-/* Write something to the system logfile. */
+/* Write something to the system logfile and/or stderr */
 void
 xlog(int kind, const char *fmt, ...)
 {
        char            buff[1024];
        va_list         args;
-       int             logged = 1, n;
-#ifdef VERBOSE_PRINTF
-       time_t          now;
-       struct tm       *tm;
-#endif
+       int             n;
 
        if (!(kind & (L_ALL)) && !(logging && (kind & logmask)))
                return;
@@ -148,40 +145,44 @@ xlog(int kind, const char *fmt, ...)
        if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
                buff[--n] = '\0';
 
-       switch (kind) {
-       case L_FATAL:
-               syslog(LOG_ERR, "%s", buff);
-               break;
-       case L_ERROR:
-               syslog(LOG_ERR, "%s", buff);
-               break;
-       case L_WARNING:
-               syslog(LOG_WARNING, "%s", buff);
-               break;
-       case L_NOTICE:
-               syslog(LOG_NOTICE, "%s", buff);
-               break;
-       default:
-               logged = 0;
-               break;
+       if (log_syslog) {
+               switch (kind) {
+               case L_FATAL:
+                       syslog(LOG_ERR, "%s", buff);
+                       break;
+               case L_ERROR:
+                       syslog(LOG_ERR, "%s", buff);
+                       break;
+               case L_WARNING:
+                       syslog(LOG_WARNING, "%s", buff);
+                       break;
+               case L_NOTICE:
+                       syslog(LOG_NOTICE, "%s", buff);
+                       break;
+               default:
+                       if (!log_stderr)
+                               syslog(LOG_DEBUG, "%s", buff);
+                       break;
+               }
        }
-       if (!logged || foreground) {
-               if (!logged && log_fp == NULL) {
-                       syslog(LOG_DEBUG, "%s", buff);
-               } else if (log_fp != NULL) {
+
+       if (log_stderr) {
 #ifdef VERBOSE_PRINTF
-                       time(&now);
-                       tm = localtime(&now);
-                       fprintf(log_fp, "%s[%d] %02d/%02d/%02d %02d:%02d %s\n",
-                                       log_name, log_pid,
-                                       tm->tm_mon + 1, tm->tm_mday,
-                                       tm->tm_year, tm->tm_hour, tm->tm_min,
-                                       buff);
+               time_t          now;
+               struct tm       *tm;
+
+               time(&now);
+               tm = localtime(&now);
+               fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
+                               log_name, log_pid,
+                               tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
+                               tm->tm_hour, tm->tm_min, tm->tm_sec,
+                               buff);
 #else
-                       fprintf(log_fp, "%s: %s\n", log_name, buff);
+               fprintf(stderr, "%s: %s\n", log_name, buff);
 #endif
-               }
        }
+
        if (kind == L_FATAL)
                exit(1);
 }
index 7cc1b1bf0e506c5593be3125744bef98d2daaf9a..14dac4185c32e9483f108a7faccdc9811ff0f3c1 100644 (file)
@@ -531,7 +531,8 @@ main(int argc, char **argv)
                }
        }
        /* Initialize logging. */
-/*     xlog_open("mountd"); */
+       if (!foreground) xlog_stderr(0);
+       xlog_open("mountd");
 
        sa.sa_handler = SIG_IGN;
        sa.sa_flags = 0;
@@ -589,7 +590,6 @@ main(int argc, char **argv)
                        if (fd > 2) (void) close(fd);
                }
                setsid();
-               xlog_background();
        }
 
        my_svc_run();