pipe-logging-fix-20050610
[openafs.git] / src / util / serverLog.c
index 8abfde9..2f1e78b 100644 (file)
 /*                                                                        */
 /* ********************************************************************** */
 
+#include <afsconfig.h>
 #include <afs/param.h>
+
+RCSID
+    ("$Header$");
+
 #include <stdio.h>
 #ifdef AFS_NT40_ENV
 #include <io.h>
 #endif
 #include <sys/param.h>
 #include <sys/time.h>
+#include <syslog.h>
 #endif
-#include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
+#include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
 #include <fcntl.h>
 #include <afs/stds.h>
-#if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
+#ifdef HAVE_STRING_H
 #include <string.h>
 #else
+#ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
+#endif
+#include <sys/stat.h>
 #include "afsutil.h"
 #include "fileutil.h"
 #if defined(AFS_PTHREAD_ENV)
@@ -52,125 +61,221 @@ static pthread_mutex_t serverLogMutex;
 #endif
 
 #else /* AFS_PTHREAD_ENV */
-#define LOCK_SERVERLOG() 
-#define UNLOCK_SERVERLOG() 
-#endif  /* AFS_PTHREAD_ENV */
+#define LOCK_SERVERLOG()
+#define UNLOCK_SERVERLOG()
+#endif /* AFS_PTHREAD_ENV */
 
 #ifdef AFS_NT40_ENV
 #define F_OK 0
+#define O_NONBLOCK 0
 #endif
 
+char *(*threadNameProgram) ();
+
 static int serverLogFD = -1;
 
+#ifndef AFS_NT40_ENV
+int serverLogSyslog = 0;
+int serverLogSyslogFacility = LOG_DAEMON;
+char *serverLogSyslogTag = 0;
+#endif
+
 #include <stdarg.h>
 int LogLevel;
+int mrafsStyleLogs = 0;
+int printLocks = 0;
+static char ourName[MAXPATHLEN];
 
-/* VARARGS1 */
-void FSLog (const char *format, ...)
+void
+WriteLogBuffer(char *buf, afs_uint32 len)
 {
-    va_list args;
+    LOCK_SERVERLOG();
+    if (serverLogFD > 0)
+       (void)write(serverLogFD, buf, len);
+    UNLOCK_SERVERLOG();
+}
 
+void
+vFSLog(const char *format, va_list args)
+{
     time_t currenttime;
     char *timeStamp;
     char tbuffer[1024];
     char *info;
     int len;
+    char *name;
 
     currenttime = time(0);
     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
-    timeStamp[24] = ' ';  /* ts[24] is the newline, 25 is the null */
+    timeStamp[24] = ' ';       /* ts[24] is the newline, 25 is the null */
     info = &timeStamp[25];
 
-    va_start(args, format);
-    (void) vsprintf(info, format, args);
-    va_end(args);
+    if (mrafsStyleLogs) {
+       name = (*threadNameProgram) ();
+       (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%s] ",
+                          name);
+       info += strlen(info);
+    }
+
+    (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
+                       args);
 
     len = strlen(tbuffer);
     LOCK_SERVERLOG();
+#ifndef AFS_NT40_ENV
+    if (serverLogSyslog) {
+       syslog(LOG_INFO, "%s", info);
+    } else
+#endif
     if (serverLogFD > 0)
-       write(serverLogFD, tbuffer, len);
+       (void)write(serverLogFD, tbuffer, len);
     UNLOCK_SERVERLOG();
 
-#if !defined(AFS_PTHREAD_ENV)
-    fflush(stdout);
-    fflush(stderr);     /* in case they're sharing the same FD */
+#if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
+    if (!serverLogSyslog) {
+       fflush(stdout);
+       fflush(stderr);         /* in case they're sharing the same FD */
+    }
 #endif
-}
+}                              /*vFSLog */
+
+/* VARARGS1 */
+/*@printflike@*/
+void
+FSLog(const char *format, ...)
+{
+    va_list args;
 
-static void DebugOn(int loglevel)
+    va_start(args, format);
+    vFSLog(format, args);
+    va_end(args);
+}                              /*FSLog */
+
+static int
+DebugOn(int loglevel)
 {
     if (loglevel == 0) {
-        ViceLog(0, ("Reset Debug levels to 0\n"));
+       ViceLog(0, ("Reset Debug levels to 0\n"));
     } else {
-        ViceLog(0, ("Set Debug On level = %d\n",loglevel));
+       ViceLog(0, ("Set Debug On level = %d\n", loglevel));
     }
-} /*DebugOn*/
+    return 0;
+}                              /*DebugOn */
 
 
 
-void SetDebug_Signal(int signo)
+void
+SetDebug_Signal(int signo)
 {
-    extern int IOMGR_SoftSig();
+/*    extern int IOMGR_SoftSig();*/
 
     if (LogLevel > 0) {
-        LogLevel *= 5;
-    }
-    else {
-        LogLevel = 1;
+       LogLevel *= 5;
+    } else {
+       LogLevel = 1;
     }
+    printLocks = 2;
 #if defined(AFS_PTHREAD_ENV)
     DebugOn(LogLevel);
 #else /* AFS_PTHREAD_ENV */
     IOMGR_SoftSig(DebugOn, LogLevel);
 #endif /* AFS_PTHREAD_ENV */
 
-    signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
-                                     /* handler needs to be set again */
-} /*SetDebug_Signal*/
+    (void)signal(signo, SetDebug_Signal);      /* on some platforms, this
+                                                * signal handler needs to
+                                                * be set again */
+}                              /*SetDebug_Signal */
 
-void ResetDebug_Signal(int signo)
+void
+ResetDebug_Signal(int signo)
 {
     LogLevel = 0;
 
+    if (printLocks > 0)
+       --printLocks;
 #if defined(AFS_PTHREAD_ENV)
     DebugOn(LogLevel);
 #else /* AFS_PTHREAD_ENV */
-    IOMGR_SoftSig(DebugOn, LogLevel);
+    IOMGR_SoftSig(DebugOn, (void *)LogLevel);
 #endif /* AFS_PTHREAD_ENV */
 
-    signal(signo, ResetDebug_Signal);   /* on some platforms, this signal */
-                                       /* handler needs to be set again */
-} /*ResetDebug_Signal*/
+    (void)signal(signo, ResetDebug_Signal);    /* on some platforms,
+                                                * this signal handler
+                                                * needs to be set
+                                                * again */
+    if (mrafsStyleLogs)
+       OpenLog((char *)&ourName);
+}                              /*ResetDebug_Signal */
 
 
-void SetupLogSignals(void)
+void
+SetupLogSignals(void)
 {
-    signal(SIGHUP, ResetDebug_Signal);
+    (void)signal(SIGHUP, ResetDebug_Signal);
     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
-    signal(SIGTSTP, SetDebug_Signal);
+    (void)signal(SIGTSTP, SetDebug_Signal);
+#ifndef AFS_NT40_ENV
+    (void)signal(SIGPIPE, SIG_IGN);
+#endif
 }
 
-int OpenLog(const char *fileName) 
+int
+OpenLog(const char *fileName)
 {
     /*
      * This function should allow various libraries that inconsistently
      * use stdout/stderr to all go to the same place
      */
-    int tempfd;
+    int tempfd, isfifo = 0;
     char oldName[MAXPATHLEN];
+    struct timeval Start;
+    struct tm *TimeFields;
+    char FileName[MAXPATHLEN];
+
+#ifndef AFS_NT40_ENV
+    struct stat statbuf;
+
+    if (serverLogSyslog) {
+       openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
+       return (0);
+    }
+
+    /* Support named pipes as logs by not rotating them */
+    if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
+       isfifo = 1;
+    }
+#endif
 
-    strcpy(oldName, fileName);
-    strcat(oldName, ".old");
+    if (mrafsStyleLogs) {
+        time_t t = Start.tv_sec;
+       TM_GetTimeOfDay(&Start, 0);
+       TimeFields = localtime(&t);
+       if (fileName) {
+           if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
+               strcpy((char *)&ourName, (char *)fileName);
+       }
+       afs_snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
+                    ourName, TimeFields->tm_year + 1900,
+                    TimeFields->tm_mon + 1, TimeFields->tm_mday,
+                    TimeFields->tm_hour, TimeFields->tm_min,
+                    TimeFields->tm_sec);
+       if (!isfifo)
+           renamefile(fileName, FileName);     /* don't check error code */
+       tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
+    } else {
+       strcpy(oldName, fileName);
+       strcat(oldName, ".old");
 
-    /* don't check error */
-    renamefile(fileName, oldName);
-    tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
-    if(tempfd < 0)
-    {
+       /* don't check error */
+       if (!isfifo)
+           renamefile(fileName, oldName);
+       tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
+    }
+
+    if (tempfd < 0) {
        printf("Unable to open log file %s\n", fileName);
        return -1;
     }
-
 #if defined(AFS_PTHREAD_ENV)
     /* redirect stdout and stderr so random printf's don't write to data */
     assert(freopen(NULLDEV, "w", stdout) != NULL);
@@ -180,45 +285,60 @@ int OpenLog(const char *fileName)
 
     serverLogFD = tempfd;
 #else
-    close(tempfd); /* just checking.... */
-    freopen(fileName, "w", stdout);
-    freopen(fileName, "w", stderr);
+    close(tempfd);             /* just checking.... */
+    (void)freopen(fileName, "w", stdout);
+    (void)freopen(fileName, "w", stderr);
     serverLogFD = fileno(stdout);
 #endif /* AFS_PTHREAD_ENV */
 
     return 0;
-} /*OpenLog*/
+}                              /*OpenLog */
 
-int ReOpenLog(const char *fileName) 
+int
+ReOpenLog(const char *fileName)
 {
+    int isfifo = 0;
 #if !defined(AFS_PTHREAD_ENV)
     int tempfd;
 #endif
+#if !defined(AFS_NT40_ENV)
+    struct stat statbuf;
+#endif
 
-    if (access(fileName, F_OK)==0)
-       return 0; /* exists, no need to reopen. */
+    if (access(fileName, F_OK) == 0)
+       return 0;               /* exists, no need to reopen. */
+
+#if !defined(AFS_NT40_ENV)
+    if (serverLogSyslog) {
+       return 0;
+    }
+
+    /* Support named pipes as logs by not rotating them */
+    if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
+       isfifo = 1;
+    }
+#endif
 
 #if defined(AFS_PTHREAD_ENV)
     LOCK_SERVERLOG();
     if (serverLogFD > 0)
        close(serverLogFD);
-    serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
+    serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
     UNLOCK_SERVERLOG();
     return serverLogFD < 0 ? -1 : 0;
 #else
 
-    tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
-    if(tempfd < 0)
-    {
+    tempfd = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
+    if (tempfd < 0) {
        printf("Unable to open log file %s\n", fileName);
        return -1;
     }
     close(tempfd);
 
-    freopen(fileName, "a", stdout);
-    freopen(fileName, "a", stderr);
+    (void)freopen(fileName, "a", stdout);
+    (void)freopen(fileName, "a", stderr);
     serverLogFD = fileno(stdout);
-  
+
 
     return 0;
 #endif /* AFS_PTHREAD_ENV */