ReactOS 0.4.15-dev-7788-g1ad9096
eventsource.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS EventLog Service
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/services/eventlog/eventsource.c
5 * PURPOSE: Event log sources support
6 * COPYRIGHT: Copyright 2011 Eric Kohl
7 */
8
9/* INCLUDES *****************************************************************/
10
11#include "eventlog.h"
12
13#define NDEBUG
14#include <debug.h>
15
18
19/* FUNCTIONS ****************************************************************/
20
21VOID
23{
26}
27
28
29static VOID
31{
32 PLIST_ENTRY CurrentEntry;
33 PEVENTSOURCE EventSource;
34
35 DPRINT("DumpEventSourceList()\n");
37
38 CurrentEntry = EventSourceListHead.Flink;
39 while (CurrentEntry != &EventSourceListHead)
40 {
41 EventSource = CONTAINING_RECORD(CurrentEntry,
43 EventSourceListEntry);
44
45 DPRINT("EventSource->szName: %S\n", EventSource->szName);
46
47 CurrentEntry = CurrentEntry->Flink;
48 }
49
51
52 DPRINT("Done\n");
53}
54
55
56static BOOL
58 PWSTR lpSourceName)
59{
61
63 FIELD_OFFSET(EVENTSOURCE, szName[wcslen(lpSourceName) + 1]));
64 if (lpEventSource != NULL)
65 {
66 wcscpy(lpEventSource->szName, lpSourceName);
67 lpEventSource->LogFile = pLogFile;
68
69 DPRINT("Insert event source: %S\n", lpEventSource->szName);
70
73 &lpEventSource->EventSourceListEntry);
75 }
76
77 return (lpEventSource != NULL);
78}
79
80
81BOOL
83 PLOGFILE pLogFile)
84{
86 DWORD dwNumSubKeys, dwMaxSubKeyLength;
87 DWORD dwEventSourceNameLength, MaxValueLen;
88 DWORD dwIndex;
89 PWSTR Buf = NULL, SourceList = NULL, Source = NULL;
90 size_t cchRemaining = 0;
92
93 DPRINT("LoadEventSources\n");
94
95 Result = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &dwNumSubKeys, &dwMaxSubKeyLength,
96 NULL, NULL, NULL, NULL, NULL, NULL);
97 if (Result != ERROR_SUCCESS)
98 {
99 DPRINT1("RegQueryInfoKeyW failed: %lu\n", Result);
100 return FALSE;
101 }
102
103 dwMaxSubKeyLength++;
104
105 Buf = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLength * sizeof(WCHAR));
106 if (!Buf)
107 {
108 DPRINT1("Error: cannot allocate heap!\n");
109 return FALSE;
110 }
111
112 /*
113 * Allocate a buffer for storing the names of the sources as a REG_MULTI_SZ
114 * in the registry. Also add the event log as its own source.
115 * Add a final NULL-terminator.
116 */
117 MaxValueLen = dwNumSubKeys * dwMaxSubKeyLength + wcslen(pLogFile->LogName) + 2;
118 SourceList = HeapAlloc(GetProcessHeap(), 0, MaxValueLen * sizeof(WCHAR));
119 if (!SourceList)
120 {
121 DPRINT1("Error: cannot allocate heap!\n");
122 /* It is not dramatic if we cannot create it */
123 }
124 else
125 {
126 cchRemaining = MaxValueLen;
128 }
129
130 /*
131 * Enumerate all the subkeys of the event log key, that constitute
132 * all the possible event sources for this event log. At this point,
133 * skip the possible existing source having the same name as the
134 * event log, it will be added later on.
135 */
136 dwEventSourceNameLength = dwMaxSubKeyLength;
137 dwIndex = 0;
138 while (RegEnumKeyExW(hKey,
139 dwIndex,
140 Buf,
141 &dwEventSourceNameLength,
143 {
144 if (_wcsicmp(pLogFile->LogName, Buf) != 0)
145 {
146 DPRINT("Event Source: %S\n", Buf);
147 Success = AddNewEventSource(pLogFile, Buf);
148 if (Success && (Source != NULL))
149 {
150 /* Append the event source name and an extra NULL-terminator */
151 StringCchCopyExW(Source, cchRemaining, Buf, &Source, &cchRemaining, 0);
152 if (cchRemaining > 0)
153 {
154 *++Source = L'\0';
155 cchRemaining--;
156 }
157 }
158 }
159
160 dwEventSourceNameLength = dwMaxSubKeyLength;
161 dwIndex++;
162 }
163
164 /* Finally, allow the event log itself to be its own source */
165 DPRINT("Event Source: %S\n", pLogFile->LogName);
166 Success = AddNewEventSource(pLogFile, pLogFile->LogName);
167 if (Success && (Source != NULL))
168 {
169 /* Append the event source name and an extra NULL-terminator */
170 StringCchCopyExW(Source, cchRemaining, pLogFile->LogName, &Source, &cchRemaining, 0);
171 if (cchRemaining > 0)
172 {
173 *++Source = L'\0';
174 cchRemaining--;
175 }
176 }
177
178 /* Save the list of sources in the registry */
180 L"Sources",
181 0,
184 (MaxValueLen - cchRemaining + 1) * sizeof(WCHAR));
185 if (Result != ERROR_SUCCESS)
186 {
187 DPRINT1("RegSetValueExW failed: %lu\n", Result);
188 }
189
190 if (SourceList)
192
193 HeapFree(GetProcessHeap(), 0, Buf);
194
196
197 return TRUE;
198}
199
200
203{
204 PLIST_ENTRY CurrentEntry;
206
207 DPRINT("GetEventSourceByName(%S)\n", Name);
209
210 CurrentEntry = EventSourceListHead.Flink;
211 while (CurrentEntry != &EventSourceListHead)
212 {
213 Item = CONTAINING_RECORD(CurrentEntry,
215 EventSourceListEntry);
216
217 DPRINT("Item->szName: %S\n", Item->szName);
218// if ((*(Item->szName) != 0) && !_wcsicmp(Item->szName, Name))
219 if (_wcsicmp(Item->szName, Name) == 0)
220 {
221 DPRINT("Found it\n");
222 Result = Item;
223 break;
224 }
225
226 CurrentEntry = CurrentEntry->Flink;
227 }
228
230
231 DPRINT("Done (Result: %p)\n", Result);
232
233 return Result;
234}
#define DPRINT1
Definition: precomp.h:8
static LPWSTR lpEventSource
Definition: log.c:14
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegEnumKeyExW(_In_ HKEY hKey, _In_ DWORD dwIndex, _Out_ LPWSTR lpName, _Inout_ LPDWORD lpcbName, _Reserved_ LPDWORD lpReserved, _Out_opt_ LPWSTR lpClass, _Inout_opt_ LPDWORD lpcbClass, _Out_opt_ PFILETIME lpftLastWriteTime)
Definition: reg.c:2533
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4911
LONG WINAPI RegQueryInfoKeyW(HKEY hKey, LPWSTR lpClass, LPDWORD lpcClass, LPDWORD lpReserved, LPDWORD lpcSubKeys, LPDWORD lpcMaxSubKeyLen, LPDWORD lpcMaxClassLen, LPDWORD lpcValues, LPDWORD lpcMaxValueNameLen, LPDWORD lpcMaxValueLen, LPDWORD lpcbSecurityDescriptor, PFILETIME lpftLastWriteTime)
Definition: reg.c:3691
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define InsertTailList(ListHead, Entry)
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
@ Success
Definition: eventcreate.c:712
BOOL LoadEventSources(HKEY hKey, PLOGFILE pLogFile)
Definition: eventsource.c:82
static LIST_ENTRY EventSourceListHead
Definition: eventsource.c:16
VOID InitEventSourceList(VOID)
Definition: eventsource.c:22
static VOID DumpEventSourceList(VOID)
Definition: eventsource.c:30
static CRITICAL_SECTION EventSourceListCs
Definition: eventsource.c:17
static BOOL AddNewEventSource(PLOGFILE pLogFile, PWSTR lpSourceName)
Definition: eventsource.c:57
PEVENTSOURCE GetEventSourceByName(LPCWSTR Name)
Definition: eventsource.c:202
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3169
#define REG_MULTI_SZ
Definition: nt_native.h:1501
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
static const WCHAR szName[]
Definition: powrprof.c:45
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define DPRINT
Definition: sndvol32.h:71
STRSAFEAPI StringCchCopyExW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc, STRSAFE_LPWSTR *ppszDestEnd, size_t *pcchRemaining, STRSAFE_DWORD dwFlags)
Definition: strsafe.h:184
WCHAR szName[1]
Definition: eventlog.h:48
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
WCHAR * LogName
Definition: eventlog.h:38
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
uint16_t * PWSTR
Definition: typedefs.h:56
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
unsigned char * LPBYTE
Definition: typedefs.h:53
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
_In_ WDFCOLLECTION _In_ WDFOBJECT Item
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
_In_ IN_ADDR _In_ IN_ADDR _Out_ MULTICAST_MODE_TYPE _Inout_ ULONG _Out_writes_ SourceCount IN_ADDR * SourceList
Definition: ws2tcpip.h:622
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185