Windows: engage path mtu discovery for rx
[openafs.git] / src / WINNT / afsd / afsd_eventlog.c
index efe295c..065ec5a 100644 (file)
@@ -64,12 +64,16 @@ static BOOL
 AddEventSource()
 {
     HKEY       hKey = NULL, hLogKey; 
-    UCHAR      szBuf[MAX_PATH]; 
+    UCHAR      szBuf[MAX_PATH] = "afsd_service.exe"; 
     DWORD      dwData, dwDisposition; 
-    BOOL       bRet = TRUE;
+    static BOOL        bRet = TRUE;
+    static BOOL bOnce = TRUE;
+
+    if (!bOnce)
+        return bRet;
 
     if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, AFSREG_APPLOG_SUBKEY, 0,
-                       KEY_QUERY_VALUE, &hLogKey ) )
+                       KEY_SET_VALUE, &hLogKey ) )
     {                          
         // nope - create it            
         if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSREG_APPLOG_SUBKEY, 0,
@@ -86,7 +90,7 @@ AddEventSource()
     // Application key in the EventLog registry key.  If not,
     // create it.
     if ( RegOpenKeyEx( hLogKey, AFSREG_CLT_APPLOG_SUBKEY, 0,
-                       KEY_QUERY_VALUE, &hKey ) )
+                       KEY_SET_VALUE, &hKey ) )
     {                          
         // nope - create it            
         if ( RegCreateKeyEx(hLogKey, AFSREG_CLT_APPLOG_SUBKEY, 0,
@@ -99,17 +103,6 @@ AddEventSource()
         }
     }
 
-#if 0
-    // Set the name of the message file
-    // Get "ImagePath" from TransarcAFSDaemon service
-    memset(szBuf, '\0', MAX_PATH);
-    dwData = MAX_PATH;
-    GetServicePath(szBuf, &dwData);
-#else   
-    // Windows takes the specified name and searchs the PATH environment variable
-    // It never appears to even try the fully qualified name.
-    strcpy(szBuf, "afsd_service.exe");
-#endif  
     // Add the name to the EventMessageFile subkey. 
     if ( RegSetValueEx( hKey,                  // subkey handle 
                         AFSREG_APPLOG_MSGFILE_VALUE,   // value name 
@@ -164,17 +157,26 @@ LogEventMessage(WORD wEventType, DWORD dwEventID, DWORD dwMessageID)
 // Use the ReportEvent API to write an entry to the system event log.
 //
 #define MAXARGS 8
-#define STRLEN  64
+#define STRLEN  128
+
 VOID
 LogEvent(WORD wEventType, DWORD dwEventID, ...)
 {
     va_list    listArgs;
     HANDLE     hEventSource;
+    HANDLE      hMutex = NULL;
     LPTSTR     lpArgs[MAXARGS];
     CHAR       lpStrings[MAXARGS][STRLEN];
+    static CHAR lpLastStrings[MAXARGS][STRLEN];
     WORD       wNumArgs = 0;
-    WORD       wNumStrings = 0;
-       DWORD   code;
+    static WORD wLastNumArgs = MAXARGS;
+    static time_t lastMessageTime = 0;
+    static WORD wLastEventType = 0;
+    static DWORD dwLastEventID = 0;
+    time_t      now;
+    DWORD       code;
+    BOOL        bLogMessage = TRUE;
+    WORD        i = 0, j;
 
     // Ensure that our event source is properly initialized.
     if (!AddEventSource())
@@ -211,11 +213,14 @@ LogEvent(WORD wEventType, DWORD dwEventID, ...)
     case MSG_SERVICE_ERROR_STOP_WITH_MSG:
     case MSG_SMB_SEND_PACKET_FAILURE:
     case MSG_UNEXPECTED_SMB_SESSION_CLOSE:
+    case MSG_RX_MSGSIZE_EXCEEDED:
        wNumArgs = 1;
        lpArgs[0] = va_arg(listArgs, LPTSTR);
        break;
     case MSG_TIME_FLUSH_PER_VOLUME:
     case MSG_TIME_FLUSH_TOTAL:
+    case MSG_SMB_MAX_MPX_COUNT:
+    case MSG_SMB_MAX_BUFFER_SIZE:
        wNumArgs = 2;
        lpArgs[0] = va_arg(listArgs, LPTSTR);
        lpArgs[1] = va_arg(listArgs, LPTSTR);
@@ -226,6 +231,11 @@ LogEvent(WORD wEventType, DWORD dwEventID, ...)
     case MSG_SERVER_REPORTS_VSALVAGE:
     case MSG_SERVER_REPORTS_VNOSERVICE:
     case MSG_SERVER_REPORTS_VIO:
+    case MSG_SERVER_REPORTS_VBUSY:
+    case MSG_SERVER_REPORTS_VRESTARTING:
+    case MSG_ALL_SERVERS_BUSY:
+    case MSG_ALL_SERVERS_OFFLINE:
+    case MSG_ALL_SERVERS_DOWN:
        wNumArgs = 2;
        lpArgs[0] = va_arg(listArgs, LPTSTR);
        StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32));
@@ -294,24 +304,81 @@ LogEvent(WORD wEventType, DWORD dwEventID, ...)
        lpArgs[1] = lpStrings[1];
        lpArgs[2] = va_arg(listArgs,LPTSTR);
        break;
+    case MSG_DIRTY_BUFFER_AT_SHUTDOWN:
+       wNumArgs = 6;
+       lpArgs[0] = va_arg(listArgs, LPTSTR);
+        lpArgs[1] = va_arg(listArgs, LPTSTR);
+       StringCbPrintf(lpStrings[2],STRLEN,"%u",va_arg(listArgs,int));
+       StringCbPrintf(lpStrings[3],STRLEN,"%u",va_arg(listArgs,int));
+       StringCbPrintf(lpStrings[4],STRLEN,"%I64u",va_arg(listArgs,afs_int64));
+       StringCbPrintf(lpStrings[5],STRLEN,"%I64u",va_arg(listArgs,afs_int64));
+       lpArgs[2] = lpStrings[2];
+       lpArgs[3] = lpStrings[3];
+       lpArgs[4] = lpStrings[4];
+       lpArgs[5] = lpStrings[5];
+       break;
     }
     va_end(listArgs);
 
     // Make sure we were not given too many args.
     if (wNumArgs >= MAXARGS)
-       return;
+        goto done;
+
+    hMutex = CreateMutex( NULL, TRUE, "AFSD Event Log Mutex");
+    if (hMutex == NULL)
+        goto done;
+
+    if (GetLastError() == ERROR_ALREADY_EXISTS) {
+        code = WaitForSingleObject( hMutex, 500);
+        if (code != WAIT_OBJECT_0)
+            goto done;
+    }
+
+    /*
+     * We rate limit consecutive duplicate messages to one every
+     * five seconds.
+     */
+    now = time(NULL);
+    if (now < lastMessageTime + 5 &&
+        wEventType == wLastEventType &&
+        dwEventID == dwLastEventID &&
+        wNumArgs == wLastNumArgs) {
+        for (i=0; i<wNumArgs; i++) {
+            if ( strncmp(lpArgs[i], lpLastStrings[i], STRLEN))
+                break;
+        }
+        if (i == wNumArgs)
+            bLogMessage = FALSE;
+    }
+
+    if ( bLogMessage) {
+        wLastNumArgs = wNumArgs;
+        wLastEventType = wEventType;
+        dwLastEventID = dwEventID;
+        lastMessageTime = now;
+
+        for ( j = (i == wNumArgs ? 0 : i) ; i < wNumArgs; i++) {
+            StringCbCopyEx( lpLastStrings[i], STRLEN, lpArgs[i], NULL, NULL, STRSAFE_NULL_ON_FAILURE);
+        }
+    }
+
+    ReleaseMutex(hMutex);
 
     // Log the event.
-    code = ReportEvent(hEventSource,           // handle of event source
-                wEventType,            // event type
-                0,                     // event category
-                dwEventID,             // event ID
-                NULL,                  // current user's SID
-                wNumArgs,              // strings in lpszArgs
-                0,                     // no bytes of raw data
-                wNumArgs ? lpArgs : NULL,              // array of error strings
-                NULL);                 // no raw data
+    if ( bLogMessage)
+        code = ReportEvent(hEventSource,               // handle of event source
+                           wEventType,         // event type
+                           0,                  // event category
+                           dwEventID,          // event ID
+                           NULL,                       // current user's SID
+                           wNumArgs,           // strings in lpszArgs
+                           0,                  // no bytes of raw data
+                           wNumArgs ? lpArgs : NULL,// array of error strings
+                           NULL);                      // no raw data
 
+  done:
+    if (hMutex)
+        CloseHandle(hMutex);
 
     DeregisterEventSource(hEventSource);
 }