Branch setupapi
[reactos.git] / reactos / services / eventlog / logport.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: services/eventlog/logport.c
24 * PURPOSE: Event logger service
25 * PROGRAMMER: Eric Kohl
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #define UNICODE
31
32 #define NTOS_MODE_USER
33 #include <ntos.h>
34 #include <windows.h>
35 #include <string.h>
36
37 #include "eventlog.h"
38
39 #define NDEBUG
40 #include <debug.h>
41
42
43 /* GLOBALS ******************************************************************/
44
45 HANDLE PortThreadHandle = NULL;
46 HANDLE ConnectPortHandle = NULL;
47 HANDLE MessagePortHandle = NULL;
48
49
50 /* FUNCTIONS ****************************************************************/
51
52 static NTSTATUS
53 InitLogPort (VOID)
54 {
55 OBJECT_ATTRIBUTES ObjectAttributes;
56 UNICODE_STRING PortName;
57 LPC_MAX_MESSAGE Request;
58 NTSTATUS Status;
59
60 ConnectPortHandle = NULL;
61 MessagePortHandle = NULL;
62
63 RtlInitUnicodeString(&PortName,
64 L"\\ErrorLogPort");
65 InitializeObjectAttributes(&ObjectAttributes,
66 &PortName,
67 0,
68 NULL,
69 NULL);
70
71 Status = NtCreatePort(&ConnectPortHandle,
72 &ObjectAttributes,
73 0,
74 0x100,
75 0x2000);
76 if (!NT_SUCCESS(Status))
77 {
78 DPRINT1("NtCreatePort() failed (Status %lx)\n", Status);
79 goto ByeBye;
80 }
81
82 Status = NtListenPort(ConnectPortHandle,
83 &Request.Header);
84 if (!NT_SUCCESS(Status))
85 {
86 DPRINT1("NtListenPort() failed (Status %lx)\n", Status);
87 goto ByeBye;
88 }
89
90 Status = NtAcceptConnectPort(&MessagePortHandle,
91 ConnectPortHandle,
92 NULL,
93 TRUE,
94 NULL,
95 NULL);
96 if (!NT_SUCCESS (Status))
97 {
98 DPRINT1("NtAcceptConnectPort() failed (Status %lx)\n", Status);
99 goto ByeBye;
100 }
101
102 Status = NtCompleteConnectPort (MessagePortHandle);
103 if (!NT_SUCCESS (Status))
104 {
105 DPRINT1("NtCompleteConnectPort() failed (Status %lx)\n", Status);
106 goto ByeBye;
107 }
108
109 ByeBye:
110 if (!NT_SUCCESS (Status))
111 {
112 if (ConnectPortHandle != NULL)
113 NtClose (ConnectPortHandle);
114
115 if (MessagePortHandle != NULL)
116 NtClose (MessagePortHandle);
117 }
118
119 return Status;
120 }
121
122
123 static NTSTATUS
124 ProcessPortMessage(VOID)
125 {
126 LPC_MAX_MESSAGE Request;
127 PIO_ERROR_LOG_MESSAGE Message;
128 //#ifndef NDEBUG
129 ULONG i;
130 PWSTR p;
131 //#endif
132 NTSTATUS Status;
133
134
135 DPRINT1("ProcessPortMessage() called\n");
136
137 Status = STATUS_SUCCESS;
138
139 while (TRUE)
140 {
141 Status = NtReplyWaitReceivePort(MessagePortHandle,
142 0,
143 NULL,
144 &Request.Header);
145 if (!NT_SUCCESS(Status))
146 {
147 DPRINT1("NtReplyWaitReceivePort() failed (Status %lx)\n", Status);
148 break;
149 }
150
151 DPRINT ("Received message\n");
152
153 if (Request.Header.MessageType == LPC_PORT_CLOSED)
154 {
155 DPRINT ("Port closed\n");
156
157 return (STATUS_UNSUCCESSFUL);
158 }
159 if (Request.Header.MessageType == LPC_REQUEST)
160 {
161 DPRINT ("Received request\n");
162
163 }
164 else if (Request.Header.MessageType == LPC_DATAGRAM)
165 {
166 DPRINT ("Received datagram\n");
167
168
169 Message = (PIO_ERROR_LOG_MESSAGE)&Request.Data;
170
171 DPRINT ("Message->Type %hx\n", Message->Type);
172 DPRINT ("Message->Size %hu\n", Message->Size);
173
174 //#ifndef NDEBUG
175 DbgPrint ("\n Error mesage:\n");
176 DbgPrint ("Error code: %lx\n", Message->EntryData.ErrorCode);
177 DbgPrint ("Retry count: %u\n", Message->EntryData.RetryCount);
178 DbgPrint ("Sequence number: %lu\n", Message->EntryData.SequenceNumber);
179
180 if (Message->DriverNameLength != 0)
181 {
182 DbgPrint ("Driver name: %.*S\n",
183 Message->DriverNameLength / sizeof(WCHAR),
184 (PWCHAR)((ULONG_PTR)Message + Message->DriverNameOffset));
185 }
186
187 if (Message->EntryData.NumberOfStrings != 0)
188 {
189 p = (PWSTR)((ULONG_PTR)&Message->EntryData + Message->EntryData.StringOffset);
190 for (i = 0; i < Message->EntryData.NumberOfStrings; i++)
191 {
192 DbgPrint ("String %lu: %S\n", i, p);
193 p += wcslen(p) + 1;
194 }
195 DbgPrint("\n");
196 }
197
198 //#endif
199 }
200 }
201
202 return Status;
203 }
204
205
206 static NTSTATUS STDCALL
207 PortThreadRoutine(PVOID Param)
208 {
209 NTSTATUS Status = STATUS_SUCCESS;
210
211 Status = InitLogPort();
212 if (!NT_SUCCESS(Status))
213 return(Status);
214
215 while (NT_SUCCESS(Status))
216 {
217 Status = ProcessPortMessage();
218 }
219
220 if (ConnectPortHandle != NULL)
221 NtClose (ConnectPortHandle);
222
223 if (MessagePortHandle != NULL)
224 NtClose (MessagePortHandle);
225
226 return(Status);
227 }
228
229
230 BOOL
231 StartPortThread(VOID)
232 {
233 DWORD ThreadId;
234
235 PortThreadHandle = CreateThread(NULL,
236 0x1000,
237 (LPTHREAD_START_ROUTINE)PortThreadRoutine,
238 NULL,
239 0,
240 &ThreadId);
241
242 return((PortThreadHandle != NULL));
243 }
244
245 /* EOF */