- Implement EnumServicesStatusW.
[reactos.git] / reactos / services / eventlog / logport.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: services/eventlog/logport.c
5 * PURPOSE: Event logging service
6 * PROGRAMMER: Eric Kohl
7 * Saveliy Tretiakov (saveliyt@mail.ru)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include "eventlog.h"
13
14 /* GLOBALS ******************************************************************/
15
16 HANDLE ConnectPortHandle = NULL;
17 HANDLE MessagePortHandle = NULL;
18 extern PLOGFILE SystemLog;
19 extern HANDLE MyHeap;
20
21
22 /* FUNCTIONS ****************************************************************/
23
24 NTSTATUS STDCALL PortThreadRoutine(PVOID Param)
25 {
26 NTSTATUS Status = STATUS_SUCCESS;
27
28 Status = InitLogPort();
29 if(!NT_SUCCESS(Status))
30 return Status;
31
32 while(NT_SUCCESS(Status))
33 {
34 Status = ProcessPortMessage();
35 }
36
37 if(ConnectPortHandle != NULL)
38 NtClose(ConnectPortHandle);
39
40 if(MessagePortHandle != NULL)
41 NtClose(MessagePortHandle);
42
43 return Status;
44 }
45
46 NTSTATUS InitLogPort(VOID)
47 {
48 OBJECT_ATTRIBUTES ObjectAttributes;
49 UNICODE_STRING PortName;
50 PORT_MESSAGE Request;
51 NTSTATUS Status;
52
53 ConnectPortHandle = NULL;
54 MessagePortHandle = NULL;
55
56 RtlInitUnicodeString(&PortName, L"\\ErrorLogPort");
57 InitializeObjectAttributes(
58 &ObjectAttributes,
59 &PortName,
60 0,
61 NULL,
62 NULL);
63
64 Status = NtCreatePort(
65 &ConnectPortHandle,
66 &ObjectAttributes,
67 0,
68 0x100,
69 0x2000);
70
71 if(!NT_SUCCESS(Status))
72 {
73 DPRINT1("NtCreatePort() failed (Status %lx)\n", Status);
74 goto ByeBye;
75 }
76
77 Status = NtListenPort(ConnectPortHandle, &Request);
78
79 if(!NT_SUCCESS(Status))
80 {
81 DPRINT1("NtListenPort() failed (Status %lx)\n", Status);
82 goto ByeBye;
83 }
84
85 Status = NtAcceptConnectPort(
86 &MessagePortHandle,
87 ConnectPortHandle,
88 NULL,
89 TRUE,
90 NULL,
91 NULL);
92
93 if (!NT_SUCCESS(Status))
94 {
95 DPRINT1("NtAcceptConnectPort() failed (Status %lx)\n", Status);
96 goto ByeBye;
97 }
98
99 Status = NtCompleteConnectPort(MessagePortHandle);
100 if (!NT_SUCCESS(Status))
101 {
102 DPRINT1("NtCompleteConnectPort() failed (Status %lx)\n", Status);
103 goto ByeBye;
104 }
105
106 ByeBye:
107 if (!NT_SUCCESS(Status))
108 {
109 if(ConnectPortHandle != NULL)
110 NtClose(ConnectPortHandle);
111
112 if(MessagePortHandle != NULL)
113 NtClose(MessagePortHandle);
114 }
115 return Status;
116 }
117
118
119 NTSTATUS ProcessPortMessage(VOID)
120 {
121 IO_ERROR_LPC Request;
122 PIO_ERROR_LOG_MESSAGE Message;
123 PEVENTLOGRECORD pRec;
124 ULONG ulRecNum, ulRecSize ;
125 NTSTATUS Status;
126 PLOGFILE SystemLog = NULL;
127
128 DPRINT1("ProcessPortMessage() called\n");
129
130 Status = STATUS_SUCCESS;
131
132 while(TRUE)
133 {
134 Status = NtReplyWaitReceivePort(
135 MessagePortHandle,
136 0,
137 NULL,
138 &Request.Header);
139
140 if(!NT_SUCCESS(Status))
141 {
142 DPRINT1("NtReplyWaitReceivePort() failed (Status %lx)\n", Status);
143 break;
144 }
145
146 DPRINT("Received message\n");
147
148 if(Request.Header.u2.s2.Type == LPC_PORT_CLOSED)
149 {
150 DPRINT("Port closed\n");
151 return STATUS_SUCCESS;
152 }
153
154 if(Request.Header.u2.s2.Type == LPC_REQUEST)
155 {
156 DPRINT("Received request\n");
157 }
158 else if (Request.Header.u2.s2.Type == LPC_DATAGRAM)
159 {
160 DPRINT("Received datagram\n");
161 Message = (PIO_ERROR_LOG_MESSAGE)&Request.Message;
162 ulRecNum = SystemLog ? SystemLog->Header.NextRecord : 0;
163
164 ulRecSize = LogfBuildNewRecord(NULL,
165 ulRecNum,
166 Message->Type,
167 Message->EntryData.EventCategory,
168 Message->EntryData.ErrorCode,
169 (WCHAR*)(((PBYTE)Message)+Message->DriverNameOffset),
170 L"MyComputer", /* FIXME */
171 0,
172 NULL,
173 Message->EntryData.NumberOfStrings,
174 (WCHAR*)(((PBYTE)Message)+Message->EntryData.StringOffset),
175 Message->EntryData.DumpDataSize,
176 (LPVOID)(((PBYTE)Message)
177 +sizeof(IO_ERROR_LOG_PACKET)-sizeof(ULONG)));
178
179 DPRINT("ulRecSize = %d\n", ulRecSize);
180
181 pRec = HeapAlloc(MyHeap, 0, ulRecSize);
182
183 if(pRec == NULL)
184 {
185 DPRINT("Can't allocate heap!\n");
186 return STATUS_NO_MEMORY;
187 }
188
189 LogfBuildNewRecord((PBYTE)pRec,
190 ulRecNum,
191 Message->Type,
192 Message->EntryData.EventCategory,
193 Message->EntryData.ErrorCode,
194 (WCHAR*)(((PBYTE)Message)+Message->DriverNameOffset),
195 L"MyComputer", /* FIXME */
196 0,
197 NULL,
198 Message->EntryData.NumberOfStrings,
199 (WCHAR*)(((PBYTE)Message)+Message->EntryData.StringOffset),
200 Message->EntryData.DumpDataSize,
201 (LPVOID)(((PBYTE)Message)
202 +sizeof(IO_ERROR_LOG_PACKET)-sizeof(ULONG)));
203
204 DPRINT("\n --- EVENTLOG RECORD ---\n");
205 PRINT_RECORD(pRec);
206 DPRINT("\n");
207
208 if(SystemLog)
209 {
210 if(!LogfWriteData(SystemLog, ulRecSize, (PBYTE)pRec))
211 DPRINT("LogfWriteData failed!\n");
212 else DPRINT("Data written to Log!\n");
213 }
214
215 HeapFree(MyHeap, 0, pRec);
216 }
217 }
218 return Status;
219 }
220