2 * PROJECT: ReactOS kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: services/eventlog/eventlog.c
5 * PURPOSE: Event logging service
6 * COPYRIGHT: Copyright 2002 Eric Kohl
7 * Copyright 2005 Saveliy Tretiakov
10 /* INCLUDES *****************************************************************/
14 /* GLOBALS ******************************************************************/
16 static VOID CALLBACK
ServiceMain(DWORD
, LPWSTR
*);
17 static WCHAR ServiceName
[] = L
"EventLog";
18 static SERVICE_TABLE_ENTRYW ServiceTable
[2] =
20 { ServiceName
, ServiceMain
},
24 SERVICE_STATUS ServiceStatus
;
25 SERVICE_STATUS_HANDLE ServiceStatusHandle
;
27 BOOL onLiveCD
= FALSE
; // On livecd events will go to debug output only
30 /* FUNCTIONS ****************************************************************/
33 UpdateServiceStatus(DWORD dwState
)
35 ServiceStatus
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
36 ServiceStatus
.dwCurrentState
= dwState
;
37 ServiceStatus
.dwControlsAccepted
= 0;
38 ServiceStatus
.dwWin32ExitCode
= 0;
39 ServiceStatus
.dwServiceSpecificExitCode
= 0;
40 ServiceStatus
.dwCheckPoint
= 0;
42 if (dwState
== SERVICE_START_PENDING
||
43 dwState
== SERVICE_STOP_PENDING
||
44 dwState
== SERVICE_PAUSE_PENDING
||
45 dwState
== SERVICE_CONTINUE_PENDING
)
46 ServiceStatus
.dwWaitHint
= 10000;
48 ServiceStatus
.dwWaitHint
= 0;
50 SetServiceStatus(ServiceStatusHandle
,
55 ServiceControlHandler(DWORD dwControl
,
60 DPRINT("ServiceControlHandler() called\n");
64 case SERVICE_CONTROL_STOP
:
65 DPRINT(" SERVICE_CONTROL_STOP received\n");
67 LogfReportEvent(EVENTLOG_INFORMATION_TYPE
,
69 EVENT_EventlogStopped
, 0, NULL
, 0, NULL
);
72 /* Stop listening to incoming RPC messages */
73 RpcMgmtStopServerListening(NULL
);
74 UpdateServiceStatus(SERVICE_STOPPED
);
77 case SERVICE_CONTROL_PAUSE
:
78 DPRINT(" SERVICE_CONTROL_PAUSE received\n");
79 UpdateServiceStatus(SERVICE_PAUSED
);
82 case SERVICE_CONTROL_CONTINUE
:
83 DPRINT(" SERVICE_CONTROL_CONTINUE received\n");
84 UpdateServiceStatus(SERVICE_RUNNING
);
87 case SERVICE_CONTROL_INTERROGATE
:
88 DPRINT(" SERVICE_CONTROL_INTERROGATE received\n");
89 SetServiceStatus(ServiceStatusHandle
,
93 case SERVICE_CONTROL_SHUTDOWN
:
94 DPRINT(" SERVICE_CONTROL_SHUTDOWN received\n");
96 LogfReportEvent(EVENTLOG_INFORMATION_TYPE
,
98 EVENT_EventlogStopped
, 0, NULL
, 0, NULL
);
100 UpdateServiceStatus(SERVICE_STOPPED
);
101 return ERROR_SUCCESS
;
104 DPRINT1(" Control %lu received\n");
105 return ERROR_CALL_NOT_IMPLEMENTED
;
115 hThread
= CreateThread(NULL
,
117 (LPTHREAD_START_ROUTINE
)
125 DPRINT("Can't create PortThread\n");
126 return GetLastError();
129 CloseHandle(hThread
);
131 hThread
= CreateThread(NULL
,
133 (LPTHREAD_START_ROUTINE
)
141 DPRINT("Can't create RpcThread\n");
142 return GetLastError();
145 CloseHandle(hThread
);
147 return ERROR_SUCCESS
;
152 ReportProductInfoEvent(VOID
)
154 OSVERSIONINFOW versionInfo
;
160 LONG lResult
= ERROR_SUCCESS
;
162 ZeroMemory(&versionInfo
, sizeof(OSVERSIONINFO
));
163 versionInfo
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
165 /* Get version information */
166 if (!GetVersionExW(&versionInfo
))
169 ZeroMemory(szBuffer
, 512 * sizeof(WCHAR
));
171 /* Write version into the buffer */
172 dwLength
= swprintf(szBuffer
,
174 versionInfo
.dwMajorVersion
,
175 versionInfo
.dwMinorVersion
) + 1;
177 /* Write build number into the buffer */
178 dwLength
+= swprintf(&szBuffer
[dwLength
],
180 versionInfo
.dwBuildNumber
) + 1;
182 /* Write service pack info into the buffer */
183 wcscpy(&szBuffer
[dwLength
], versionInfo
.szCSDVersion
);
184 dwLength
+= wcslen(versionInfo
.szCSDVersion
) + 1;
186 /* Read 'CurrentType' from the registry and write it into the buffer */
187 lResult
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
188 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
192 if (lResult
== ERROR_SUCCESS
)
194 dwValueLength
= 512 - dwLength
;
195 lResult
= RegQueryValueEx(hKey
,
199 (LPBYTE
)&szBuffer
[dwLength
],
205 /* Log the product information */
206 LogfReportEvent(EVENTLOG_INFORMATION_TYPE
,
208 EVENT_EventLogProductInfo
,
217 ServiceMain(DWORD argc
,
222 UNREFERENCED_PARAMETER(argc
);
223 UNREFERENCED_PARAMETER(argv
);
225 DPRINT("ServiceMain() called\n");
227 ServiceStatusHandle
= RegisterServiceCtrlHandlerExW(ServiceName
,
228 ServiceControlHandler
,
230 if (!ServiceStatusHandle
)
232 dwError
= GetLastError();
233 DPRINT1("RegisterServiceCtrlHandlerW() failed! (Error %lu)\n", dwError
);
237 UpdateServiceStatus(SERVICE_START_PENDING
);
239 dwError
= ServiceInit();
240 if (dwError
!= ERROR_SUCCESS
)
242 DPRINT("Service stopped (dwError: %lu\n", dwError
);
243 UpdateServiceStatus(SERVICE_START_PENDING
);
247 DPRINT("Service started\n");
248 UpdateServiceStatus(SERVICE_RUNNING
);
250 ReportProductInfoEvent();
252 LogfReportEvent(EVENTLOG_INFORMATION_TYPE
,
254 EVENT_EventlogStarted
,
261 DPRINT("ServiceMain() done\n");
265 PLOGFILE
LoadLogFile(HKEY hKey
, WCHAR
* LogName
)
267 DWORD MaxValueLen
, ValueLen
, Type
, ExpandedLen
;
268 WCHAR
*Buf
= NULL
, *Expanded
= NULL
;
272 DPRINT("LoadLogFile: %S\n", LogName
);
274 RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
275 NULL
, NULL
, &MaxValueLen
, NULL
, NULL
);
277 Buf
= HeapAlloc(MyHeap
, 0, MaxValueLen
);
280 DPRINT1("Can't allocate heap!\n");
284 ValueLen
= MaxValueLen
;
286 Result
= RegQueryValueEx(hKey
,
292 if (Result
!= ERROR_SUCCESS
)
294 DPRINT1("RegQueryValueEx failed: %d\n", GetLastError());
295 HeapFree(MyHeap
, 0, Buf
);
299 if (Type
!= REG_EXPAND_SZ
&& Type
!= REG_SZ
)
301 DPRINT1("%S\\File - value of wrong type %x.\n", LogName
, Type
);
302 HeapFree(MyHeap
, 0, Buf
);
306 ExpandedLen
= ExpandEnvironmentStrings(Buf
, NULL
, 0);
307 Expanded
= HeapAlloc(MyHeap
, 0, ExpandedLen
* sizeof(WCHAR
));
310 DPRINT1("Can't allocate heap!\n");
311 HeapFree(MyHeap
, 0, Buf
);
315 ExpandEnvironmentStrings(Buf
, Expanded
, ExpandedLen
);
317 DPRINT("%S -> %S\n", Buf
, Expanded
);
319 pLogf
= LogfCreate(LogName
, Expanded
);
323 DPRINT1("Failed to create %S!\n", Expanded
);
326 HeapFree(MyHeap
, 0, Buf
);
327 HeapFree(MyHeap
, 0, Expanded
);
331 BOOL
LoadLogFiles(HKEY eventlogKey
)
334 DWORD MaxLognameLen
, LognameLen
;
339 RegQueryInfoKey(eventlogKey
,
340 NULL
, NULL
, NULL
, NULL
,
342 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
346 Buf
= HeapAlloc(MyHeap
, 0, MaxLognameLen
* sizeof(WCHAR
));
350 DPRINT1("Error: can't allocate heap!\n");
355 LognameLen
= MaxLognameLen
;
357 while (RegEnumKeyEx(eventlogKey
,
361 NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
367 result
= RegOpenKeyEx(eventlogKey
, Buf
, 0, KEY_ALL_ACCESS
, &SubKey
);
368 if (result
!= ERROR_SUCCESS
)
370 DPRINT1("Failed to open %S key.\n", Buf
);
371 HeapFree(MyHeap
, 0, Buf
);
375 pLogFile
= LoadLogFile(SubKey
, Buf
);
376 if (pLogFile
!= NULL
)
378 DPRINT("Loaded %S\n", Buf
);
379 LoadEventSources(SubKey
, pLogFile
);
383 DPRINT1("Failed to load %S\n", Buf
);
387 LognameLen
= MaxLognameLen
;
391 HeapFree(MyHeap
, 0, Buf
);
397 WCHAR LogPath
[MAX_PATH
];
402 LogfListInitialize();
403 InitEventSourceList();
405 MyHeap
= HeapCreate(0, 1024 * 256, 0);
409 DPRINT1("FATAL ERROR, can't create heap.\n");
414 GetWindowsDirectory(LogPath
, MAX_PATH
);
416 if (GetDriveType(LogPath
) == DRIVE_CDROM
)
418 DPRINT("LiveCD detected\n");
423 result
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
424 L
"SYSTEM\\CurrentControlSet\\Services\\EventLog",
429 if (result
!= ERROR_SUCCESS
)
431 DPRINT1("Fatal error: can't open eventlog registry key.\n");
436 LoadLogFiles(elogKey
);
439 StartServiceCtrlDispatcher(ServiceTable
);
450 VOID
EventTimeToSystemTime(DWORD EventTime
, SYSTEMTIME
* pSystemTime
)
452 SYSTEMTIME st1970
= { 1970, 1, 0, 1, 0, 0, 0, 0 };
460 uUCT
.ft
.dwHighDateTime
= 0;
461 uUCT
.ft
.dwLowDateTime
= EventTime
;
462 SystemTimeToFileTime(&st1970
, &u1970
.ft
);
463 uUCT
.ll
= uUCT
.ll
* 10000000 + u1970
.ll
;
464 FileTimeToLocalFileTime(&uUCT
.ft
, &ftLocal
);
465 FileTimeToSystemTime(&ftLocal
, pSystemTime
);
468 VOID
PRINT_HEADER(PEVENTLOGHEADER header
)
470 DPRINT("HeaderSize = %d\n", header
->HeaderSize
);
471 DPRINT("Signature = 0x%x\n", header
->Signature
);
472 DPRINT("MajorVersion = %d\n", header
->MajorVersion
);
473 DPRINT("MinorVersion = %d\n", header
->MinorVersion
);
474 DPRINT("StartOffset = %d\n", header
->StartOffset
);
475 DPRINT("EndOffset = 0x%x\n", header
->EndOffset
);
476 DPRINT("CurrentRecordNumber = %d\n", header
->CurrentRecordNumber
);
477 DPRINT("OldestRecordNumber = %d\n", header
->OldestRecordNumber
);
478 DPRINT("MaxSize = 0x%x\n", header
->MaxSize
);
479 DPRINT("Retention = 0x%x\n", header
->Retention
);
480 DPRINT("EndHeaderSize = %d\n", header
->EndHeaderSize
);
482 if (header
->Flags
& ELF_LOGFILE_HEADER_DIRTY
) DPRINT("ELF_LOGFILE_HEADER_DIRTY");
483 if (header
->Flags
& ELF_LOGFILE_HEADER_WRAP
) DPRINT("| ELF_LOGFILE_HEADER_WRAP ");
484 if (header
->Flags
& ELF_LOGGFILE_LOGFULL_WRITTEN
) DPRINT("| ELF_LOGGFILE_LOGFULL_WRITTEN ");
485 if (header
->Flags
& ELF_LOGFILE_ARCHIVE_SET
) DPRINT("| ELF_LOGFILE_ARCHIVE_SET ");
489 VOID
PRINT_RECORD(PEVENTLOGRECORD pRec
)
495 DPRINT("Length = %d\n", pRec
->Length
);
496 DPRINT("Reserved = 0x%x\n", pRec
->Reserved
);
497 DPRINT("RecordNumber = %d\n", pRec
->RecordNumber
);
499 EventTimeToSystemTime(pRec
->TimeGenerated
, &time
);
500 DPRINT("TimeGenerated = %d.%d.%d %d:%d:%d\n",
501 time
.wDay
, time
.wMonth
, time
.wYear
,
502 time
.wHour
, time
.wMinute
, time
.wSecond
);
504 EventTimeToSystemTime(pRec
->TimeWritten
, &time
);
505 DPRINT("TimeWritten = %d.%d.%d %d:%d:%d\n",
506 time
.wDay
, time
.wMonth
, time
.wYear
,
507 time
.wHour
, time
.wMinute
, time
.wSecond
);
509 DPRINT("EventID = %d\n", pRec
->EventID
);
511 switch (pRec
->EventType
)
513 case EVENTLOG_ERROR_TYPE
:
514 DPRINT("EventType = EVENTLOG_ERROR_TYPE\n");
516 case EVENTLOG_WARNING_TYPE
:
517 DPRINT("EventType = EVENTLOG_WARNING_TYPE\n");
519 case EVENTLOG_INFORMATION_TYPE
:
520 DPRINT("EventType = EVENTLOG_INFORMATION_TYPE\n");
522 case EVENTLOG_AUDIT_SUCCESS
:
523 DPRINT("EventType = EVENTLOG_AUDIT_SUCCESS\n");
525 case EVENTLOG_AUDIT_FAILURE
:
526 DPRINT("EventType = EVENTLOG_AUDIT_FAILURE\n");
529 DPRINT("EventType = %d\n", pRec
->EventType
);
532 DPRINT("NumStrings = %d\n", pRec
->NumStrings
);
533 DPRINT("EventCategory = %d\n", pRec
->EventCategory
);
534 DPRINT("ReservedFlags = 0x%x\n", pRec
->ReservedFlags
);
535 DPRINT("ClosingRecordNumber = %d\n", pRec
->ClosingRecordNumber
);
536 DPRINT("StringOffset = %d\n", pRec
->StringOffset
);
537 DPRINT("UserSidLength = %d\n", pRec
->UserSidLength
);
538 DPRINT("UserSidOffset = %d\n", pRec
->UserSidOffset
);
539 DPRINT("DataLength = %d\n", pRec
->DataLength
);
540 DPRINT("DataOffset = %d\n", pRec
->DataOffset
);
542 DPRINT("SourceName: %S\n", (WCHAR
*) (((PBYTE
) pRec
) + sizeof(EVENTLOGRECORD
)));
544 i
= (lstrlenW((WCHAR
*) (((PBYTE
) pRec
) + sizeof(EVENTLOGRECORD
))) + 1) *
547 DPRINT("ComputerName: %S\n", (WCHAR
*) (((PBYTE
) pRec
) + sizeof(EVENTLOGRECORD
) + i
));
549 if (pRec
->StringOffset
< pRec
->Length
&& pRec
->NumStrings
)
551 DPRINT("Strings:\n");
552 str
= (WCHAR
*) (((PBYTE
) pRec
) + pRec
->StringOffset
);
553 for (i
= 0; i
< pRec
->NumStrings
; i
++)
555 DPRINT("[%d] %S\n", i
, str
);
556 str
= str
+ lstrlenW(str
) + 1;
560 DPRINT("Length2 = %d\n", *(PDWORD
) (((PBYTE
) pRec
) + pRec
->Length
- 4));