ReactOS  0.4.11-dev-946-g431643b
logport.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/logport.c
5  * PURPOSE: LPC Port Interface support
6  * COPYRIGHT: Copyright 2002 Eric Kohl
7  * Copyright 2005 Saveliy Tretiakov
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include "eventlog.h"
13 #include <ndk/lpcfuncs.h>
14 #include <iolog/iolog.h>
15 
16 #define NDEBUG
17 #include <debug.h>
18 
19 /* GLOBALS ******************************************************************/
20 
23 extern BOOL onLiveCD;
24 
25 /* FUNCTIONS ****************************************************************/
26 
28 {
30 
31  Status = InitLogPort();
32  if (!NT_SUCCESS(Status))
33  return Status;
34 
35  while (NT_SUCCESS(Status))
36  Status = ProcessPortMessage();
37 
38  if (ConnectPortHandle != NULL)
39  NtClose(ConnectPortHandle);
40 
41  if (MessagePortHandle != NULL)
42  NtClose(MessagePortHandle);
43 
44  return Status;
45 }
46 
48 {
53 
54  ConnectPortHandle = NULL;
55  MessagePortHandle = NULL;
56 
57  InitializeObjectAttributes(&ObjectAttributes, &PortName, 0, NULL, NULL);
58 
59  Status = NtCreatePort(&ConnectPortHandle,
60  &ObjectAttributes,
61  0,
62  PORT_MAXIMUM_MESSAGE_LENGTH, // IO_ERROR_LOG_MESSAGE_LENGTH,
63  2 * PAGE_SIZE);
64  if (!NT_SUCCESS(Status))
65  {
66  DPRINT1("NtCreatePort() failed (Status %lx)\n", Status);
67  goto ByeBye;
68  }
69 
70  Status = NtListenPort(ConnectPortHandle, &Request);
71  if (!NT_SUCCESS(Status))
72  {
73  DPRINT1("NtListenPort() failed (Status %lx)\n", Status);
74  goto ByeBye;
75  }
76 
77  Status = NtAcceptConnectPort(&MessagePortHandle, ConnectPortHandle,
78  &Request, TRUE, NULL, NULL);
79  if (!NT_SUCCESS(Status))
80  {
81  DPRINT1("NtAcceptConnectPort() failed (Status %lx)\n", Status);
82  goto ByeBye;
83  }
84 
85  Status = NtCompleteConnectPort(MessagePortHandle);
86  if (!NT_SUCCESS(Status))
87  {
88  DPRINT1("NtCompleteConnectPort() failed (Status %lx)\n", Status);
89  goto ByeBye;
90  }
91 
92 ByeBye:
93  if (!NT_SUCCESS(Status))
94  {
95  if (ConnectPortHandle != NULL)
96  NtClose(ConnectPortHandle);
97 
98  if (MessagePortHandle != NULL)
99  NtClose(MessagePortHandle);
100  }
101  return Status;
102 }
103 
105 {
107  PLOGFILE SystemLog = NULL;
108  UCHAR Buffer[PORT_MAXIMUM_MESSAGE_LENGTH]; // IO_ERROR_LOG_MESSAGE_LENGTH
111  PEVENTLOGRECORD LogBuffer;
112  SIZE_T RecSize;
113  ULONG Time;
115  UNICODE_STRING SourceName, ComputerName;
116  DWORD dwComputerNameLength;
117  WCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
118 
119  DPRINT("ProcessPortMessage() called\n");
120 
121  SystemLog = LogfListItemByName(L"System");
122 
123  while (TRUE)
124  {
125  Status = NtReplyWaitReceivePort(MessagePortHandle,
126  NULL,
127  NULL,
128  &Message->Header);
129 
130  if (!NT_SUCCESS(Status))
131  {
132  DPRINT1("NtReplyWaitReceivePort() failed (Status %lx)\n", Status);
133  break;
134  }
135 
136  DPRINT("Received message\n");
137 
138  if (Message->Header.u2.s2.Type == LPC_PORT_CLOSED)
139  {
140  DPRINT("Port closed\n");
141  return STATUS_SUCCESS;
142  }
143 
144  if (Message->Header.u2.s2.Type == LPC_REQUEST)
145  {
146  DPRINT("Received request\n");
147  }
148  else if (Message->Header.u2.s2.Type == LPC_DATAGRAM)
149  {
150  DPRINT("Received datagram (0x%x, 0x%x)\n",
151  Message->Unknown[0], Message->Unknown[1]);
152  ErrorMessage = &Message->IoErrorMessage;
153 
154  // ASSERT(ErrorMessage->Type == IO_TYPE_ERROR_MESSAGE);
155 
156  RtlInitEmptyUnicodeString(&SourceName, NULL, 0);
157  if (ErrorMessage->DriverNameLength > sizeof(UNICODE_NULL)) // DriverNameLength counts NULL-terminator
158  {
159  SourceName.Buffer = (PWSTR)((ULONG_PTR)ErrorMessage + ErrorMessage->DriverNameOffset);
160  SourceName.MaximumLength = ErrorMessage->DriverNameLength;
161  SourceName.Length = SourceName.MaximumLength - sizeof(UNICODE_NULL);
162  }
163 
164  dwComputerNameLength = ARRAYSIZE(szComputerName);
165  if (!GetComputerNameW(szComputerName, &dwComputerNameLength))
166  szComputerName[0] = L'\0';
167 
168  RtlInitUnicodeString(&ComputerName, szComputerName);
169 
170  RtlTimeToSecondsSince1970(&ErrorMessage->TimeStamp, &Time);
171 
172  /* Set the event type based on the error code severity */
173  EventType = (USHORT)(ErrorMessage->EntryData.ErrorCode >> 30);
174  if (EventType == STATUS_SEVERITY_SUCCESS)
175  {
176  EventType = EVENTLOG_SUCCESS;
177  }
178  else if (EventType == STATUS_SEVERITY_INFORMATIONAL) // NT_INFORMATION
179  {
180  EventType = EVENTLOG_INFORMATION_TYPE;
181  }
182  else if (EventType == STATUS_SEVERITY_WARNING) // NT_WARNING
183  {
184  EventType = EVENTLOG_WARNING_TYPE;
185  }
186  else if (EventType == STATUS_SEVERITY_ERROR) // NT_ERROR
187  {
188  EventType = EVENTLOG_ERROR_TYPE;
189  }
190  else
191  {
192  /* Unknown severity, set to error */
193  EventType = EVENTLOG_ERROR_TYPE;
194  }
195 
196  /*
197  * The data being saved consists of the IO_ERROR_LOG_PACKET structure
198  * header, plus the additional raw data from the driver.
199  */
200  LogBuffer = LogfAllocAndBuildNewRecord(
201  &RecSize,
202  Time,
203  EventType,
204  ErrorMessage->EntryData.EventCategory,
205  ErrorMessage->EntryData.ErrorCode,
206  &SourceName,
207  &ComputerName,
208  0,
209  NULL,
210  ErrorMessage->EntryData.NumberOfStrings,
211  (PWSTR)((ULONG_PTR)ErrorMessage +
212  ErrorMessage->EntryData.StringOffset),
214  ErrorMessage->EntryData.DumpDataSize,
215  (PVOID)&ErrorMessage->EntryData);
216  if (LogBuffer == NULL)
217  {
218  DPRINT1("LogfAllocAndBuildNewRecord failed!\n");
219  // return STATUS_NO_MEMORY;
220  continue;
221  }
222 
223  if (!onLiveCD && SystemLog)
224  {
225  Status = LogfWriteRecord(SystemLog, LogBuffer, RecSize);
226  if (!NT_SUCCESS(Status))
227  {
228  DPRINT1("ERROR writing to event log `%S' (Status 0x%08lx)\n",
229  SystemLog->LogName, Status);
230  }
231  }
232  else
233  {
234  DPRINT1("\n--- EVENTLOG RECORD ---\n");
235  PRINT_RECORD(LogBuffer);
236  DPRINT1("\n");
237  }
238 
239  LogfFreeRecord(LogBuffer);
240  }
241  }
242 
243  return Status;
244 }
#define STATUS_SEVERITY_INFORMATIONAL
Definition: errmsg.h:60
BOOL onLiveCD
Definition: eventlog.c:33
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define TRUE
Definition: types.h:120
static HANDLE ConnectPortHandle
Definition: logport.c:21
VOID PRINT_RECORD(PEVENTLOGRECORD pRec)
Definition: eventlog.c:544
USHORT MaximumLength
Definition: env_spec_w32.h:370
BOOLEAN NTAPI RtlTimeToSecondsSince1970(PLARGE_INTEGER Time, PULONG ElapsedSeconds)
VOID ErrorMessage(DWORD, LPTSTR,...)
Definition: error.c:26
__wchar_t WCHAR
Definition: xmlstorage.h:180
uint16_t * PWSTR
Definition: typedefs.h:54
LONG NTSTATUS
Definition: precomp.h:26
static __inline void LogfFreeRecord(PEVENTLOGRECORD Record)
Definition: eventlog.h:143
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define STATUS_SEVERITY_WARNING
Definition: errmsg.h:58
BOOL WINAPI GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
Definition: compname.c:339
uint32_t ULONG_PTR
Definition: typedefs.h:63
struct _ELF_API_MSG * PELF_API_MSG
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5155
ULONG Unknown[2]
Definition: iolog.h:20
NTSTATUS ErrorCode
Definition: iotypes.h:1963
#define EVENTLOG_ERROR_TYPE
Definition: winnt_old.h:2630
#define UNICODE_NULL
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
#define ELF_PORT_NAME
Definition: iolog.h:15
#define EVENTLOG_SUCCESS
Definition: winnt_old.h:2629
NTSTATUS NTAPI NtAcceptConnectPort(OUT PHANDLE PortHandle, IN PVOID PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage, IN BOOLEAN AcceptConnection, IN OUT PPORT_VIEW ServerView OPTIONAL, OUT PREMOTE_PORT_VIEW ClientView OPTIONAL)
Definition: complete.c:40
unsigned int BOOL
Definition: ntddk_ex.h:94
NTSTATUS NTAPI NtCreatePort(OUT PHANDLE PortHandle, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG MaxConnectInfoLength, IN ULONG MaxDataLength, IN ULONG MaxPoolUsage)
Definition: create.c:212
#define EVENTLOG_INFORMATION_TYPE
Definition: winnt_old.h:2632
USHORT NumberOfStrings
Definition: iotypes.h:1960
#define EVENTLOG_WARNING_TYPE
Definition: winnt_old.h:2631
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
unsigned long DWORD
Definition: ntddk_ex.h:95
#define STATUS_SEVERITY_SUCCESS
Definition: errmsg.h:59
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3393
CHAR Message[80]
Definition: alive.c:5
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSTATUS NTAPI NtReplyWaitReceivePort(IN HANDLE PortHandle, OUT PVOID *PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage OPTIONAL, OUT PPORT_MESSAGE ReceiveMessage)
Definition: reply.c:743
static const WCHAR L[]
Definition: oid.c:1087
WCHAR * LogName
Definition: eventlog.h:38
PEVENTLOGRECORD LogfAllocAndBuildNewRecord(PSIZE_T pRecSize, ULONG Time, USHORT wType, USHORT wCategory, ULONG dwEventId, PUNICODE_STRING SourceName, PUNICODE_STRING ComputerName, ULONG dwSidLength, PSID pUserSid, USHORT wNumStrings, PWSTR pStrings, ULONG dwDataSize, PVOID pRawData)
Definition: file.c:896
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define STATUS_SEVERITY_ERROR
Definition: errmsg.h:61
LARGE_INTEGER TimeStamp
Definition: iotypes.h:1976
#define WINAPI
Definition: msvc.h:20
Status
Definition: gdiplustypes.h:24
#define MAX_COMPUTERNAME_LENGTH
Definition: winbase.h:240
ULONG_PTR SIZE_T
Definition: typedefs.h:78
static HANDLE MessagePortHandle
Definition: logport.c:22
PORT_MESSAGE Header
Definition: iolog.h:19
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS InitLogPort(VOID)
Definition: logport.c:47
#define PORT_MAXIMUM_MESSAGE_LENGTH
Definition: iotypes.h:1985
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
IO_ERROR_LOG_MESSAGE IoErrorMessage
Definition: iolog.h:21
NTSTATUS LogfWriteRecord(PLOGFILE LogFile, PEVENTLOGRECORD Record, SIZE_T BufSize)
Definition: file.c:858
WCHAR SourceName[256]
Definition: arping.c:28
IO_ERROR_LOG_PACKET EntryData
Definition: iotypes.h:1978
#define DPRINT1
Definition: precomp.h:8
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:421
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS WINAPI PortThreadRoutine(PVOID Param)
Definition: logport.c:27
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSTATUS ProcessPortMessage(VOID)
Definition: logport.c:104
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_ EVENT_TYPE EventType
Definition: exfuncs.h:165
PLOGFILE LogfListItemByName(LPCWSTR Name)
Definition: file.c:33
return STATUS_SUCCESS
Definition: btrfs.c:2710
NTSTATUS NTAPI NtListenPort(IN HANDLE PortHandle, OUT PPORT_MESSAGE ConnectMessage)
Definition: listen.c:22
static PLARGE_INTEGER Time
Definition: time.c:105
IN PUNICODE_STRING PortName
Definition: conport.c:35
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14