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");
66 UpdateServiceStatus(SERVICE_STOPPED
);
69 case SERVICE_CONTROL_PAUSE
:
70 DPRINT(" SERVICE_CONTROL_PAUSE received\n");
71 UpdateServiceStatus(SERVICE_PAUSED
);
74 case SERVICE_CONTROL_CONTINUE
:
75 DPRINT(" SERVICE_CONTROL_CONTINUE received\n");
76 UpdateServiceStatus(SERVICE_RUNNING
);
79 case SERVICE_CONTROL_INTERROGATE
:
80 DPRINT(" SERVICE_CONTROL_INTERROGATE received\n");
81 SetServiceStatus(ServiceStatusHandle
,
85 case SERVICE_CONTROL_SHUTDOWN
:
86 DPRINT(" SERVICE_CONTROL_SHUTDOWN received\n");
87 UpdateServiceStatus(SERVICE_STOPPED
);
91 DPRINT1(" Control %lu received\n");
92 return ERROR_CALL_NOT_IMPLEMENTED
;
102 hThread
= CreateThread(NULL
,
104 (LPTHREAD_START_ROUTINE
)
112 DPRINT("Can't create PortThread\n");
113 return GetLastError();
116 CloseHandle(hThread
);
118 hThread
= CreateThread(NULL
,
120 (LPTHREAD_START_ROUTINE
)
128 DPRINT("Can't create RpcThread\n");
129 return GetLastError();
132 CloseHandle(hThread
);
134 return ERROR_SUCCESS
;
139 ReportProductInfoEvent(VOID
)
141 OSVERSIONINFOW versionInfo
;
147 LONG lResult
= ERROR_SUCCESS
;
149 ZeroMemory(&versionInfo
, sizeof(OSVERSIONINFO
));
150 versionInfo
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
152 /* Get version information */
153 if (!GetVersionExW(&versionInfo
))
156 ZeroMemory(szBuffer
, 512 * sizeof(WCHAR
));
158 /* Write version into the buffer */
159 dwLength
= swprintf(szBuffer
,
161 versionInfo
.dwMajorVersion
,
162 versionInfo
.dwMinorVersion
) + 1;
164 /* Write build number into the buffer */
165 dwLength
+= swprintf(&szBuffer
[dwLength
],
167 versionInfo
.dwBuildNumber
) + 1;
169 /* Write service pack info into the buffer */
170 wcscpy(&szBuffer
[dwLength
], versionInfo
.szCSDVersion
);
171 dwLength
+= wcslen(versionInfo
.szCSDVersion
) + 1;
173 /* Read 'CurrentType' from the registry and write it into the buffer */
174 lResult
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
175 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
179 if (lResult
== ERROR_SUCCESS
)
181 dwValueLength
= 512 - dwLength
;
182 lResult
= RegQueryValueEx(hKey
,
186 (LPBYTE
)&szBuffer
[dwLength
],
192 /* Log the product information */
193 LogfReportEvent(EVENTLOG_INFORMATION_TYPE
,
195 EVENT_EventLogProductInfo
,
204 ServiceMain(DWORD argc
,
209 UNREFERENCED_PARAMETER(argc
);
210 UNREFERENCED_PARAMETER(argv
);
212 DPRINT("ServiceMain() called\n");
214 ServiceStatusHandle
= RegisterServiceCtrlHandlerExW(ServiceName
,
215 ServiceControlHandler
,
217 if (!ServiceStatusHandle
)
219 dwError
= GetLastError();
220 DPRINT1("RegisterServiceCtrlHandlerW() failed! (Error %lu)\n", dwError
);
224 UpdateServiceStatus(SERVICE_START_PENDING
);
226 dwError
= ServiceInit();
227 if (dwError
!= ERROR_SUCCESS
)
229 DPRINT("Service stopped (dwError: %lu\n", dwError
);
230 UpdateServiceStatus(SERVICE_START_PENDING
);
234 DPRINT("Service started\n");
235 UpdateServiceStatus(SERVICE_RUNNING
);
237 ReportProductInfoEvent();
239 LogfReportEvent(EVENTLOG_INFORMATION_TYPE
,
241 EVENT_EventlogStarted
,
248 DPRINT("ServiceMain() done\n");
252 PLOGFILE
LoadLogFile(HKEY hKey
, WCHAR
* LogName
)
254 DWORD MaxValueLen
, ValueLen
, Type
, ExpandedLen
;
255 WCHAR
*Buf
= NULL
, *Expanded
= NULL
;
259 DPRINT("LoadLogFile: %S\n", LogName
);
261 RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
262 NULL
, NULL
, &MaxValueLen
, NULL
, NULL
);
264 Buf
= HeapAlloc(MyHeap
, 0, MaxValueLen
);
267 DPRINT1("Can't allocate heap!\n");
271 ValueLen
= MaxValueLen
;
273 Result
= RegQueryValueEx(hKey
,
279 if (Result
!= ERROR_SUCCESS
)
281 DPRINT1("RegQueryValueEx failed: %d\n", GetLastError());
282 HeapFree(MyHeap
, 0, Buf
);
286 if (Type
!= REG_EXPAND_SZ
&& Type
!= REG_SZ
)
288 DPRINT1("%S\\File - value of wrong type %x.\n", LogName
, Type
);
289 HeapFree(MyHeap
, 0, Buf
);
293 ExpandedLen
= ExpandEnvironmentStrings(Buf
, NULL
, 0);
294 Expanded
= HeapAlloc(MyHeap
, 0, ExpandedLen
* sizeof(WCHAR
));
297 DPRINT1("Can't allocate heap!\n");
298 HeapFree(MyHeap
, 0, Buf
);
302 ExpandEnvironmentStrings(Buf
, Expanded
, ExpandedLen
);
304 DPRINT("%S -> %S\n", Buf
, Expanded
);
306 pLogf
= LogfCreate(LogName
, Expanded
);
310 DPRINT1("Failed to create %S!\n", Expanded
);
313 HeapFree(MyHeap
, 0, Buf
);
314 HeapFree(MyHeap
, 0, Expanded
);
318 BOOL
LoadLogFiles(HKEY eventlogKey
)
321 DWORD MaxLognameLen
, LognameLen
;
326 RegQueryInfoKey(eventlogKey
,
327 NULL
, NULL
, NULL
, NULL
,
329 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
333 Buf
= HeapAlloc(MyHeap
, 0, MaxLognameLen
* sizeof(WCHAR
));
337 DPRINT1("Error: can't allocate heap!\n");
342 LognameLen
= MaxLognameLen
;
344 while (RegEnumKeyEx(eventlogKey
,
348 NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
354 result
= RegOpenKeyEx(eventlogKey
, Buf
, 0, KEY_ALL_ACCESS
, &SubKey
);
355 if (result
!= ERROR_SUCCESS
)
357 DPRINT1("Failed to open %S key.\n", Buf
);
358 HeapFree(MyHeap
, 0, Buf
);
362 pLogFile
= LoadLogFile(SubKey
, Buf
);
363 if (pLogFile
!= NULL
)
365 DPRINT("Loaded %S\n", Buf
);
366 LoadEventSources(SubKey
, pLogFile
);
370 DPRINT1("Failed to load %S\n", Buf
);
374 LognameLen
= MaxLognameLen
;
378 HeapFree(MyHeap
, 0, Buf
);
384 WCHAR LogPath
[MAX_PATH
];
389 LogfListInitialize();
390 InitEventSourceList();
392 MyHeap
= HeapCreate(0, 1024 * 256, 0);
396 DPRINT1("FATAL ERROR, can't create heap.\n");
401 GetWindowsDirectory(LogPath
, MAX_PATH
);
403 if (GetDriveType(LogPath
) == DRIVE_CDROM
)
405 DPRINT("LiveCD detected\n");
410 result
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
411 L
"SYSTEM\\CurrentControlSet\\Services\\EventLog",
416 if (result
!= ERROR_SUCCESS
)
418 DPRINT1("Fatal error: can't open eventlog registry key.\n");
423 LoadLogFiles(elogKey
);
426 StartServiceCtrlDispatcher(ServiceTable
);
437 VOID
EventTimeToSystemTime(DWORD EventTime
, SYSTEMTIME
* pSystemTime
)
439 SYSTEMTIME st1970
= { 1970, 1, 0, 1, 0, 0, 0, 0 };
447 uUCT
.ft
.dwHighDateTime
= 0;
448 uUCT
.ft
.dwLowDateTime
= EventTime
;
449 SystemTimeToFileTime(&st1970
, &u1970
.ft
);
450 uUCT
.ll
= uUCT
.ll
* 10000000 + u1970
.ll
;
451 FileTimeToLocalFileTime(&uUCT
.ft
, &ftLocal
);
452 FileTimeToSystemTime(&ftLocal
, pSystemTime
);
455 VOID
SystemTimeToEventTime(SYSTEMTIME
* pSystemTime
, DWORD
* pEventTime
)
457 SYSTEMTIME st1970
= { 1970, 1, 0, 1, 0, 0, 0, 0 };
464 SystemTimeToFileTime(pSystemTime
, &Time
.ft
);
465 SystemTimeToFileTime(&st1970
, &u1970
.ft
);
466 *pEventTime
= (DWORD
)((Time
.ll
- u1970
.ll
) / 10000000ull);
469 VOID
PRINT_HEADER(PEVENTLOGHEADER header
)
471 DPRINT("HeaderSize = %d\n", header
->HeaderSize
);
472 DPRINT("Signature = 0x%x\n", header
->Signature
);
473 DPRINT("MajorVersion = %d\n", header
->MajorVersion
);
474 DPRINT("MinorVersion = %d\n", header
->MinorVersion
);
475 DPRINT("StartOffset = %d\n", header
->StartOffset
);
476 DPRINT("EndOffset = 0x%x\n", header
->EndOffset
);
477 DPRINT("CurrentRecordNumber = %d\n", header
->CurrentRecordNumber
);
478 DPRINT("OldestRecordNumber = %d\n", header
->OldestRecordNumber
);
479 DPRINT("MaxSize = 0x%x\n", header
->MaxSize
);
480 DPRINT("Retention = 0x%x\n", header
->Retention
);
481 DPRINT("EndHeaderSize = %d\n", header
->EndHeaderSize
);
483 if (header
->Flags
& ELF_LOGFILE_HEADER_DIRTY
) DPRINT("ELF_LOGFILE_HEADER_DIRTY");
484 if (header
->Flags
& ELF_LOGFILE_HEADER_WRAP
) DPRINT("| ELF_LOGFILE_HEADER_WRAP ");
485 if (header
->Flags
& ELF_LOGGFILE_LOGFULL_WRITTEN
) DPRINT("| ELF_LOGGFILE_LOGFULL_WRITTEN ");
486 if (header
->Flags
& ELF_LOGFILE_ARCHIVE_SET
) DPRINT("| ELF_LOGFILE_ARCHIVE_SET ");
490 VOID
PRINT_RECORD(PEVENTLOGRECORD pRec
)
496 DPRINT("Length = %d\n", pRec
->Length
);
497 DPRINT("Reserved = 0x%x\n", pRec
->Reserved
);
498 DPRINT("RecordNumber = %d\n", pRec
->RecordNumber
);
500 EventTimeToSystemTime(pRec
->TimeGenerated
, &time
);
501 DPRINT("TimeGenerated = %d.%d.%d %d:%d:%d\n",
502 time
.wDay
, time
.wMonth
, time
.wYear
,
503 time
.wHour
, time
.wMinute
, time
.wSecond
);
505 EventTimeToSystemTime(pRec
->TimeWritten
, &time
);
506 DPRINT("TimeWritten = %d.%d.%d %d:%d:%d\n",
507 time
.wDay
, time
.wMonth
, time
.wYear
,
508 time
.wHour
, time
.wMinute
, time
.wSecond
);
510 DPRINT("EventID = %d\n", pRec
->EventID
);
512 switch (pRec
->EventType
)
514 case EVENTLOG_ERROR_TYPE
:
515 DPRINT("EventType = EVENTLOG_ERROR_TYPE\n");
517 case EVENTLOG_WARNING_TYPE
:
518 DPRINT("EventType = EVENTLOG_WARNING_TYPE\n");
520 case EVENTLOG_INFORMATION_TYPE
:
521 DPRINT("EventType = EVENTLOG_INFORMATION_TYPE\n");
523 case EVENTLOG_AUDIT_SUCCESS
:
524 DPRINT("EventType = EVENTLOG_AUDIT_SUCCESS\n");
526 case EVENTLOG_AUDIT_FAILURE
:
527 DPRINT("EventType = EVENTLOG_AUDIT_FAILURE\n");
530 DPRINT("EventType = %d\n", pRec
->EventType
);
533 DPRINT("NumStrings = %d\n", pRec
->NumStrings
);
534 DPRINT("EventCategory = %d\n", pRec
->EventCategory
);
535 DPRINT("ReservedFlags = 0x%x\n", pRec
->ReservedFlags
);
536 DPRINT("ClosingRecordNumber = %d\n", pRec
->ClosingRecordNumber
);
537 DPRINT("StringOffset = %d\n", pRec
->StringOffset
);
538 DPRINT("UserSidLength = %d\n", pRec
->UserSidLength
);
539 DPRINT("UserSidOffset = %d\n", pRec
->UserSidOffset
);
540 DPRINT("DataLength = %d\n", pRec
->DataLength
);
541 DPRINT("DataOffset = %d\n", pRec
->DataOffset
);
543 DPRINT("SourceName: %S\n", (WCHAR
*) (((PBYTE
) pRec
) + sizeof(EVENTLOGRECORD
)));
545 i
= (lstrlenW((WCHAR
*) (((PBYTE
) pRec
) + sizeof(EVENTLOGRECORD
))) + 1) *
548 DPRINT("ComputerName: %S\n", (WCHAR
*) (((PBYTE
) pRec
) + sizeof(EVENTLOGRECORD
) + i
));
550 if (pRec
->StringOffset
< pRec
->Length
&& pRec
->NumStrings
)
552 DPRINT("Strings:\n");
553 str
= (WCHAR
*) (((PBYTE
) pRec
) + pRec
->StringOffset
);
554 for (i
= 0; i
< pRec
->NumStrings
; i
++)
556 DPRINT("[%d] %S\n", i
, str
);
557 str
= str
+ lstrlenW(str
) + 1;
561 DPRINT("Length2 = %d\n", *(PDWORD
) (((PBYTE
) pRec
) + pRec
->Length
- 4));