Windows: fix checked UNICODE build of talocale
[openafs.git] / src / WINNT / afsd / afsd_eventlog.c
1 ////////////////////////////////////////////////////////////////////
2 //
3 //
4 //              E V E N T   L O G G I N G   F U N C T I O N S 
5 //
6 //
7 ////////////////////////////////////////////////////////////////////
8
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <roken.h>
13
14 #include <windows.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <strsafe.h>
18 #include <WINNT/afsreg.h>
19 #include "afsd.h"
20 #include "afsd_eventlog.h"
21 #define AFS_VERSION_STRINGS
22 #include "afs_component_version_number.h"
23
24 static BOOL     GetServicePath(LPTSTR lpPathBuf, PDWORD pdwPathBufSize);
25 static BOOL     AddEventSource(void);
26
27 static BOOL
28 GetServicePath(LPTSTR lpPathBuf, PDWORD pdwPathBufSize)
29 {
30     HKEY        hKey = NULL; 
31     DWORD       dwData = 0;
32     BOOL        bRet = TRUE;
33
34     do {
35         // Open key
36         if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_SUBKEY, 0, KEY_QUERY_VALUE, &hKey ) )
37         {               
38             bRet = FALSE;
39             break;
40         }
41
42         // prepare user's buffer and read into it
43         dwData = *pdwPathBufSize;
44         memset(lpPathBuf, '\0', dwData);
45         if ( RegQueryValueEx( hKey,                     // handle to key
46                               "ImagePath",              // value name
47                               NULL,                     // reserved
48                               NULL,                     // type buffer
49                               (LPBYTE) lpPathBuf,       // data buffer
50                               &dwData))         // size of data buffer
51         {       
52             bRet = FALSE;
53             break;
54         }
55                 
56         *pdwPathBufSize = dwData;
57
58     } while (0);
59                                 
60     if (hKey != NULL)
61         RegCloseKey(hKey); 
62
63     return bRet;
64
65
66 //
67 // Ensure name for message file is in proper location in Registry.
68 //
69 static BOOL
70 AddEventSource()
71 {
72     HKEY        hKey = NULL, hLogKey; 
73     UCHAR       szBuf[MAX_PATH] = "afsd_service.exe"; 
74     DWORD       dwData, dwDisposition; 
75     static BOOL bRet = TRUE;
76     static BOOL bOnce = TRUE;
77
78     if (!bOnce)
79         return bRet;
80
81     if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, AFSREG_APPLOG_SUBKEY, 0,
82                        KEY_SET_VALUE, &hLogKey ) )
83     {                           
84         // nope - create it             
85         if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSREG_APPLOG_SUBKEY, 0,
86                              NULL, REG_OPTION_NON_VOLATILE,
87                              KEY_ALL_ACCESS, NULL, &hLogKey,
88                              &dwDisposition)) 
89         {       
90             bRet = FALSE;
91             goto done;
92         }
93     }
94
95     // Let's see if key already exists as a subkey under the 
96     // Application key in the EventLog registry key.  If not,
97     // create it.
98     if ( RegOpenKeyEx( hLogKey, AFSREG_CLT_APPLOG_SUBKEY, 0,
99                        KEY_SET_VALUE, &hKey ) )
100     {                           
101         // nope - create it             
102         if ( RegCreateKeyEx(hLogKey, AFSREG_CLT_APPLOG_SUBKEY, 0,
103                              NULL, REG_OPTION_NON_VOLATILE,
104                              KEY_ALL_ACCESS, NULL, &hKey,
105                              &dwDisposition)) 
106         {               
107             bRet = FALSE;
108             goto done;
109         }
110     }
111
112     // Add the name to the EventMessageFile subkey. 
113     if ( RegSetValueEx( hKey,                   // subkey handle 
114                         AFSREG_APPLOG_MSGFILE_VALUE,    // value name 
115                         0,                      // must be zero 
116                         REG_SZ,                 // value type 
117                         (LPBYTE) szBuf,         // pointer to value data 
118                         (DWORD)strlen(szBuf) + 1))      // length of value data
119     {           
120         bRet = FALSE;
121         goto done;
122     }
123
124     // Set the supported event types in the TypesSupported subkey. 
125     dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | 
126         EVENTLOG_INFORMATION_TYPE; 
127
128     if ( RegSetValueEx( hKey,                   // subkey handle 
129                         AFSREG_APPLOG_MSGTYPE_VALUE,    // value name 
130                         0,                      // must be zero 
131                         REG_DWORD,              // value type 
132                         (LPBYTE) &dwData,       // pointer to value data 
133                         sizeof(DWORD)))         // length of value data
134     {           
135         bRet = FALSE;
136         goto done;
137     }
138             
139   done:
140     if (hKey != NULL)
141         RegCloseKey(hKey); 
142
143     if (hLogKey != NULL)
144         RegCloseKey(hLogKey); 
145
146     return bRet;
147 }       
148
149 // Log an event with a formatted system message as the (only) substitution
150 // string, from the given message ID.
151 VOID
152 LogEventMessage(WORD wEventType, DWORD dwEventID, DWORD dwMessageID)
153 {
154     LPTSTR msgBuf;
155
156     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
157                    NULL, dwMessageID, 0, (LPTSTR)&msgBuf, 0, NULL);
158     LogEvent(wEventType, dwEventID, msgBuf, NULL);
159     LocalFree(msgBuf);
160 }
161
162 //
163 // Use the ReportEvent API to write an entry to the system event log.
164 //
165 #define MAXARGS 8
166 #define STRLEN  128
167
168 VOID
169 LogEvent(WORD wEventType, DWORD dwEventID, ...)
170 {
171     va_list     listArgs;
172     HANDLE      hEventSource;
173     HANDLE      hMutex = NULL;
174     LPTSTR      lpArgs[MAXARGS];
175     CHAR        lpStrings[MAXARGS][STRLEN];
176     static CHAR lpLastStrings[MAXARGS][STRLEN];
177     WORD        wNumArgs = 0;
178     static WORD wLastNumArgs = MAXARGS;
179     static time_t lastMessageTime = 0;
180     static WORD wLastEventType = 0;
181     static DWORD dwLastEventID = 0;
182     time_t      now;
183     DWORD       code;
184     BOOL        bLogMessage = TRUE;
185     WORD        i = 0, j;
186
187     // Ensure that our event source is properly initialized.
188     if (!AddEventSource())
189         return;
190
191     // Get a handle to the event log.
192     hEventSource = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
193     if (hEventSource == NULL)
194         return;
195
196     // Construct the array of substitution strings.
197     va_start(listArgs, dwEventID);
198
199     switch ( dwEventID ) {
200     case MSG_FLUSH_NO_SHARE_NAME:
201     case MSG_FLUSH_NO_MEMORY:
202     case MSG_FLUSH_IMPERSONATE_ERROR:
203     case MSG_FLUSH_UNEXPECTED_EVENT:
204     case MSG_UNHANDLED_EXCEPTION:
205     case MSG_SMB_ZERO_TRANSACTION_COUNT:
206     case MSG_SERVICE_INCORRECT_VERSIONS:
207     case MSG_SERVICE_RUNNING:
208     case MSG_SERVICE_STOPPING:
209     case MSG_SERVICE_ERROR_STOP:
210     case MSG_CRYPT_OFF:
211     case MSG_CRYPT_ON:
212         break;
213     case MSG_SERVICE_START_PENDING:
214         wNumArgs = 1;
215         lpArgs[0] = AFSVersion;
216         break;
217     case MSG_FLUSH_BAD_SHARE_NAME:
218     case MSG_FLUSH_OPEN_ENUM_ERROR:
219     case MSG_FLUSH_ENUM_ERROR:
220     case MSG_FLUSH_FAILED:
221     case MSG_RX_HARD_DEAD_TIME_EXCEEDED:
222     case MSG_SERVICE_ERROR_STOP_WITH_MSG:
223     case MSG_SMB_SEND_PACKET_FAILURE:
224     case MSG_UNEXPECTED_SMB_SESSION_CLOSE:
225     case MSG_RX_MSGSIZE_EXCEEDED:
226         wNumArgs = 1;
227         lpArgs[0] = va_arg(listArgs, LPTSTR);
228         break;
229     case MSG_TIME_FLUSH_PER_VOLUME:
230     case MSG_TIME_FLUSH_TOTAL:
231     case MSG_SMB_MAX_MPX_COUNT:
232     case MSG_SMB_MAX_BUFFER_SIZE:
233         wNumArgs = 2;
234         lpArgs[0] = va_arg(listArgs, LPTSTR);
235         lpArgs[1] = va_arg(listArgs, LPTSTR);
236         break;
237     case MSG_SERVER_REPORTS_VNOVOL:
238     case MSG_SERVER_REPORTS_VMOVED:
239     case MSG_SERVER_REPORTS_VOFFLINE:
240     case MSG_SERVER_REPORTS_VSALVAGE:
241     case MSG_SERVER_REPORTS_VNOSERVICE:
242     case MSG_SERVER_REPORTS_VIO:
243     case MSG_SERVER_REPORTS_VBUSY:
244     case MSG_SERVER_REPORTS_VRESTARTING:
245         wNumArgs = 3;
246         lpArgs[0] = va_arg(listArgs, LPTSTR);
247         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32));
248         lpArgs[1] = lpStrings[1];
249         lpArgs[2] = va_arg(listArgs, LPTSTR);
250         break;
251     case MSG_ALL_SERVERS_BUSY:
252     case MSG_ALL_SERVERS_OFFLINE:
253     case MSG_ALL_SERVERS_DOWN:
254         wNumArgs = 2;
255         lpArgs[0] = va_arg(listArgs, LPTSTR);
256         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32));
257         lpArgs[1] = lpStrings[1];
258         break;
259     case MSG_BAD_SMB_PARAM:
260         wNumArgs = 5;
261         lpArgs[0] = va_arg(listArgs, LPTSTR);
262         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int));
263         StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,int));
264         StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,int));
265         StringCbPrintf(lpStrings[4],STRLEN,"%d",va_arg(listArgs,WORD));
266         lpArgs[1] = lpStrings[1];
267         lpArgs[2] = lpStrings[2];
268         lpArgs[3] = lpStrings[3];
269         lpArgs[4] = lpStrings[4];
270         break;
271     case MSG_BAD_SMB_PARAM_WITH_OFFSET:
272         wNumArgs = 6;
273         lpArgs[0] = va_arg(listArgs, LPTSTR);
274         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int));
275         StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,int));
276         StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,int));
277         StringCbPrintf(lpStrings[4],STRLEN,"%d",va_arg(listArgs,int));
278         StringCbPrintf(lpStrings[5],STRLEN,"%d",va_arg(listArgs,WORD));
279         lpArgs[1] = lpStrings[1];
280         lpArgs[2] = lpStrings[2];
281         lpArgs[3] = lpStrings[3];
282         lpArgs[4] = lpStrings[4];
283         lpArgs[5] = lpStrings[5];
284         break;
285     case MSG_BAD_SMB_TOO_SHORT:
286     case MSG_BAD_SMB_INVALID:
287     case MSG_BAD_SMB_INCOMPLETE:
288         wNumArgs = 1;
289         StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,WORD));
290         lpArgs[0] = lpStrings[0];
291         break;
292     case MSG_SMB_SESSION_START:
293         wNumArgs = 1;
294         StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,long));
295         lpArgs[0] = lpStrings[0];
296         break;
297     case MSG_BAD_SMB_WRONG_SESSION:
298         wNumArgs = 2;
299         StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,DWORD));
300         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,WORD));
301         lpArgs[0] = lpStrings[0];
302         lpArgs[1] = lpStrings[1];
303         break;
304     case MSG_BAD_VCP:
305         wNumArgs = 4;
306         StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,UCHAR));
307         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,UCHAR));
308         StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,UCHAR));
309         StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,UCHAR));
310         lpArgs[0] = lpStrings[0];
311         lpArgs[1] = lpStrings[1];
312         lpArgs[2] = lpStrings[2];
313         lpArgs[3] = lpStrings[3];
314         break;
315     case MSG_SERVICE_ERROR_STOP_WITH_MSG_AND_LOCATION:
316         wNumArgs = 3;
317         lpArgs[0] = va_arg(listArgs, LPTSTR);
318         StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int));
319         lpArgs[1] = lpStrings[1];
320         lpArgs[2] = va_arg(listArgs,LPTSTR);
321         break;
322     case MSG_DIRTY_BUFFER_AT_SHUTDOWN:
323         wNumArgs = 6;
324         lpArgs[0] = va_arg(listArgs, LPTSTR);
325         lpArgs[1] = va_arg(listArgs, LPTSTR);
326         StringCbPrintf(lpStrings[2],STRLEN,"%u",va_arg(listArgs,int));
327         StringCbPrintf(lpStrings[3],STRLEN,"%u",va_arg(listArgs,int));
328         StringCbPrintf(lpStrings[4],STRLEN,"%I64u",va_arg(listArgs,afs_int64));
329         StringCbPrintf(lpStrings[5],STRLEN,"%I64u",va_arg(listArgs,afs_int64));
330         lpArgs[2] = lpStrings[2];
331         lpArgs[3] = lpStrings[3];
332         lpArgs[4] = lpStrings[4];
333         lpArgs[5] = lpStrings[5];
334         break;
335     }
336     va_end(listArgs);
337
338     // Make sure we were not given too many args.
339     if (wNumArgs >= MAXARGS)
340         goto done;
341
342     hMutex = CreateMutex( NULL, TRUE, "AFSD Event Log Mutex");
343     if (hMutex == NULL)
344         goto done;
345
346     if (GetLastError() == ERROR_ALREADY_EXISTS) {
347         code = WaitForSingleObject( hMutex, 500);
348         if (code != WAIT_OBJECT_0)
349             goto done;
350     }
351
352     /*
353      * We rate limit consecutive duplicate messages to one every
354      * five seconds.
355      */
356     now = time(NULL);
357     if (now < lastMessageTime + 5 &&
358         wEventType == wLastEventType &&
359         dwEventID == dwLastEventID &&
360         wNumArgs == wLastNumArgs) {
361         for (i=0; i<wNumArgs; i++) {
362             if ( strncmp(lpArgs[i], lpLastStrings[i], STRLEN))
363                 break;
364         }
365         if (i == wNumArgs)
366             bLogMessage = FALSE;
367     }
368
369     if ( bLogMessage) {
370         wLastNumArgs = wNumArgs;
371         wLastEventType = wEventType;
372         dwLastEventID = dwEventID;
373         lastMessageTime = now;
374
375         for ( j = (i == wNumArgs ? 0 : i) ; i < wNumArgs; i++) {
376             StringCbCopyEx( lpLastStrings[i], STRLEN, lpArgs[i], NULL, NULL, STRSAFE_NULL_ON_FAILURE);
377         }
378     }
379
380     ReleaseMutex(hMutex);
381
382     // Log the event.
383     if ( bLogMessage)
384         code = ReportEvent(hEventSource,                // handle of event source
385                            wEventType,          // event type
386                            0,                   // event category
387                            dwEventID,           // event ID
388                            NULL,                        // current user's SID
389                            wNumArgs,            // strings in lpszArgs
390                            0,                   // no bytes of raw data
391                            wNumArgs ? lpArgs : NULL,// array of error strings
392                            NULL);                       // no raw data
393
394   done:
395     if (hMutex)
396         CloseHandle(hMutex);
397
398     DeregisterEventSource(hEventSource);
399 }       
400
401