util: LogCommandLine has to have a command line
[openafs.git] / src / util / serverLog.c
index cbb4f0a..ebe9fbc 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
-RCSID
-    ("$Header$");
-
-#include <stdio.h>
-#ifdef AFS_NT40_ENV
-#include <io.h>
-#include <time.h>
-#else
-#ifdef AFS_AIX_ENV
-#include <time.h>
-#endif
-#include <sys/param.h>
-#include <sys/time.h>
-#include <syslog.h>
-#endif
 #include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
-#include <fcntl.h>
-#include <afs/stds.h>
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-#include <sys/stat.h>
+
+#include <roken.h>             /* Must come after procmgmt.h */
+#include <afs/opr.h>
+
 #include "afsutil.h"
 #include "fileutil.h"
+#include <lwp.h>
+
 #if defined(AFS_PTHREAD_ENV)
-#include <assert.h>
+/* can't include rx when we are libutil; it's too early */
+#include <rx/rx.h>
 #include <pthread.h>
 static pthread_mutex_t serverLogMutex;
-#define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
-#define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
+#define LOCK_SERVERLOG() MUTEX_ENTER(&serverLogMutex)
+#define UNLOCK_SERVERLOG() MUTEX_EXIT(&serverLogMutex)
 
 #ifdef AFS_NT40_ENV
 #define NULLDEV "NUL"
@@ -70,7 +53,12 @@ static pthread_mutex_t serverLogMutex;
 #define O_NONBLOCK 0
 #endif
 
-static char *(*threadNameProgram) () = NULL;
+static int
+dummyThreadNum(void)
+{
+    return -1;
+}
+static int (*threadNumProgram) (void) = dummyThreadNum;
 
 static int serverLogFD = -1;
 
@@ -88,9 +76,9 @@ int printLocks = 0;
 static char ourName[MAXPATHLEN];
 
 void
-SetLogThreadNameProgram(char *(*func) () )
+SetLogThreadNumProgram(int (*func) (void) )
 {
-    threadNameProgram = func;
+    threadNumProgram = func;
 }
 
 void
@@ -102,32 +90,37 @@ WriteLogBuffer(char *buf, afs_uint32 len)
     UNLOCK_SERVERLOG();
 }
 
+int
+LogThreadNum(void)
+{
+  return (*threadNumProgram) ();
+}
+
 void
 vFSLog(const char *format, va_list args)
 {
     time_t currenttime;
-    char *timeStamp;
     char tbuffer[1024];
     char *info;
-    int len;
-    char *name;
+    size_t len;
+    struct tm tm;
+    int num;
 
-    currenttime = time(0);
-    timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
-    timeStamp[24] = ' ';       /* ts[24] is the newline, 25 is the null */
-    info = &timeStamp[25];
+    currenttime = time(NULL);
+    len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
+                  localtime_r(&currenttime, &tm));
+    info = &tbuffer[len];
 
     if (mrafsStyleLogs || threadIdLogs) {
-       name = (*threadNameProgram) ();
-        if (name) {
-       (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%s] ",
-                          name);
-       info += strlen(info);
-    }
+       num = (*threadNumProgram) ();
+        if (num > -1) {
+           snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
+                    num);
+           info += strlen(info);
+       }
     }
 
-    (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
-                       args);
+    vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
 
     len = strlen(tbuffer);
     LOCK_SERVERLOG();
@@ -160,9 +153,39 @@ FSLog(const char *format, ...)
     va_end(args);
 }                              /*FSLog */
 
-static int
-DebugOn(int loglevel)
+void
+LogCommandLine(int argc, char **argv, const char *progname,
+              const char *version, const char *logstring,
+              void (*log) (const char *format, ...))
+{
+    int i, l;
+    char *commandLine, *cx;
+
+    opr_Assert(argc != 0);
+
+    for (l = i = 0; i < argc; i++)
+       l += strlen(argv[i]) + 1;
+    if ((commandLine = malloc(l))) {
+       for (cx = commandLine, i = 0; i < argc; i++) {
+           strcpy(cx, argv[i]);
+           cx += strlen(cx);
+           *(cx++) = ' ';
+       }
+       commandLine[l-1] = '\0';
+       (*log)("%s %s %s%s(%s)\n", logstring, progname,
+                   version, strlen(version)>0?" ":"", commandLine);
+       free(commandLine);
+    } else {
+       /* What, we're out of memory already!? */
+       (*log)("%s %s%s%s\n", logstring,
+             progname, strlen(version)>0?" ":"", version);
+    }
+}
+
+static void*
+DebugOn(void *param)
 {
+    int loglevel = (intptr_t)param;
     if (loglevel == 0) {
        ViceLog(0, ("Reset Debug levels to 0\n"));
     } else {
@@ -176,13 +199,11 @@ DebugOn(int loglevel)
 void
 SetDebug_Signal(int signo)
 {
-/*    extern int IOMGR_SoftSig();*/
-
     if (LogLevel > 0) {
        LogLevel *= 5;
 
 #if defined(AFS_PTHREAD_ENV)
-        if (LogLevel > 999 && threadNameProgram != NULL && 
+        if (LogLevel > 1 && threadNumProgram != NULL &&
             threadIdLogs == 0) {
             threadIdLogs = 1;
         }
@@ -197,9 +218,9 @@ SetDebug_Signal(int signo)
     }
     printLocks = 2;
 #if defined(AFS_PTHREAD_ENV)
-    DebugOn(LogLevel);
+    DebugOn((void *)(intptr_t)LogLevel);
 #else /* AFS_PTHREAD_ENV */
-    IOMGR_SoftSig(DebugOn, LogLevel);
+    IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
 #endif /* AFS_PTHREAD_ENV */
 
     (void)signal(signo, SetDebug_Signal);      /* on some platforms, this
@@ -215,9 +236,9 @@ ResetDebug_Signal(int signo)
     if (printLocks > 0)
        --printLocks;
 #if defined(AFS_PTHREAD_ENV)
-    DebugOn(LogLevel);
+    DebugOn((void *)(intptr_t)LogLevel);
 #else /* AFS_PTHREAD_ENV */
-    IOMGR_SoftSig(DebugOn, (void *)LogLevel);
+    IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
 #endif /* AFS_PTHREAD_ENV */
 
     (void)signal(signo, ResetDebug_Signal);    /* on some platforms,
@@ -273,18 +294,25 @@ OpenLog(const char *fileName)
 
     if (mrafsStyleLogs) {
         time_t t;
-       TM_GetTimeOfDay(&Start, 0);
-        t = Start.tv_sec;      
+       struct stat buf;
+       gettimeofday(&Start, NULL);
+        t = Start.tv_sec;
        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);
+    makefilename:
+       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(lstat(FileName, &buf) == 0) {
+           /* avoid clobbering a log */
+           TimeFields->tm_sec++;
+           goto makefilename;
+       }
        if (!isfifo)
            renamefile(fileName, FileName);     /* don't check error code */
        tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
@@ -302,20 +330,20 @@ OpenLog(const char *fileName)
        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);
-    assert(freopen(NULLDEV, "w", stderr) != NULL);
+    (void)freopen(fileName, "a", stdout);
+    (void)freopen(fileName, "a", stderr);
+#ifdef HAVE_SETVBUF
+    setvbuf(stderr, NULL, _IONBF, 0);
+#else
+    setbuf(stderr, NULL);
+#endif
 
-    assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
+#if defined(AFS_PTHREAD_ENV)
+    MUTEX_INIT(&serverLogMutex, "serverlog", MUTEX_DEFAULT, 0);
+#endif /* AFS_PTHREAD_ENV */
 
     serverLogFD = tempfd;
-#else
-    close(tempfd);             /* just checking.... */
-    (void)freopen(fileName, "w", stdout);
-    (void)freopen(fileName, "w", stderr);
-    serverLogFD = fileno(stdout);
-#endif /* AFS_PTHREAD_ENV */
 
     return 0;
 }                              /*OpenLog */
@@ -324,9 +352,6 @@ 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
@@ -345,27 +370,24 @@ ReOpenLog(const char *fileName)
     }
 #endif
 
-#if defined(AFS_PTHREAD_ENV)
     LOCK_SERVERLOG();
     if (serverLogFD > 0)
        close(serverLogFD);
     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
-    UNLOCK_SERVERLOG();
-    return serverLogFD < 0 ? -1 : 0;
+    if (serverLogFD > 0) {
+       (void)freopen(fileName, "a", stdout);
+       (void)freopen(fileName, "a", stderr);
+#ifdef HAVE_SETVBUF
+#ifdef SETVBUF_REVERSED
+       setvbuf(stderr, _IONBF, NULL, 0);
 #else
+       setvbuf(stderr, NULL, _IONBF, 0);
+#endif
+#else
+       setbuf(stderr, NULL);
+#endif
 
-    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);
-
-    (void)freopen(fileName, "a", stdout);
-    (void)freopen(fileName, "a", stderr);
-    serverLogFD = fileno(stdout);
-
-
-    return 0;
-#endif /* AFS_PTHREAD_ENV */
+    UNLOCK_SERVERLOG();
+    return serverLogFD < 0 ? -1 : 0;
 }