[EVENTLOG]

- LogfCreate: Use NtCreateFile instead of CreateFile
- Implement ElfCreateBackupLogHandle

svn path=/trunk/; revision=54987
This commit is contained in:
Eric Kohl 2012-01-16 23:23:29 +00:00
parent 3b7a2e8b40
commit c114281942
4 changed files with 156 additions and 72 deletions

View file

@ -267,7 +267,9 @@ PLOGFILE LoadLogFile(HKEY hKey, WCHAR * LogName)
DWORD MaxValueLen, ValueLen, Type, ExpandedLen; DWORD MaxValueLen, ValueLen, Type, ExpandedLen;
WCHAR *Buf = NULL, *Expanded = NULL; WCHAR *Buf = NULL, *Expanded = NULL;
LONG Result; LONG Result;
PLOGFILE pLogf; PLOGFILE pLogf = NULL;
UNICODE_STRING FileName;
NTSTATUS Status;
DPRINT("LoadLogFile: %S\n", LogName); DPRINT("LoadLogFile: %S\n", LogName);
@ -314,13 +316,21 @@ PLOGFILE LoadLogFile(HKEY hKey, WCHAR * LogName)
ExpandEnvironmentStrings(Buf, Expanded, ExpandedLen); ExpandEnvironmentStrings(Buf, Expanded, ExpandedLen);
if (!RtlDosPathNameToNtPathName_U(Expanded, &FileName,
NULL, NULL))
{
DPRINT1("Can't convert path!\n");
HeapFree(MyHeap, 0, Expanded);
HeapFree(MyHeap, 0, Buf);
return NULL;
}
DPRINT("%S -> %S\n", Buf, Expanded); DPRINT("%S -> %S\n", Buf, Expanded);
pLogf = LogfCreate(LogName, Expanded); Status = LogfCreate(&pLogf, LogName, &FileName);
if (!NT_SUCCESS(Status))
if (pLogf == NULL)
{ {
DPRINT1("Failed to create %S!\n", Expanded); DPRINT1("Failed to create %S! (Status %08lx)\n", Expanded, Status);
} }
HeapFree(MyHeap, 0, Buf); HeapFree(MyHeap, 0, Buf);

View file

@ -102,12 +102,17 @@ typedef struct _EVENTSOURCE
WCHAR szName[1]; WCHAR szName[1];
} EVENTSOURCE, *PEVENTSOURCE; } EVENTSOURCE, *PEVENTSOURCE;
/* Log Handle Flags */
#define LOG_HANDLE_BACKUP_FILE 1
typedef struct _LOGHANDLE typedef struct _LOGHANDLE
{ {
LIST_ENTRY LogHandleListEntry; LIST_ENTRY LogHandleListEntry;
PEVENTSOURCE EventSource; PEVENTSOURCE EventSource;
PLOGFILE LogFile; PLOGFILE LogFile;
ULONG CurrentRecord; ULONG CurrentRecord;
ULONG Flags;
WCHAR szName[1]; WCHAR szName[1];
} LOGHANDLE, *PLOGHANDLE; } LOGHANDLE, *PLOGHANDLE;
@ -149,8 +154,10 @@ NTSTATUS
LogfBackupFile(PLOGFILE LogFile, LogfBackupFile(PLOGFILE LogFile,
PUNICODE_STRING BackupFileName); PUNICODE_STRING BackupFileName);
PLOGFILE LogfCreate(WCHAR * LogName, NTSTATUS
WCHAR * FileName); LogfCreate(PLOGFILE *Logfile,
WCHAR * LogName,
PUNICODE_STRING FileName);
VOID LogfClose(PLOGFILE LogFile); VOID LogfClose(PLOGFILE LogFile);

View file

@ -288,104 +288,127 @@ BOOL LogfInitializeExisting(PLOGFILE LogFile)
return TRUE; return TRUE;
} }
PLOGFILE LogfCreate(WCHAR * LogName, WCHAR * FileName)
{
PLOGFILE LogFile;
BOOL bResult, bCreateNew = FALSE;
LogFile = (LOGFILE *) HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, sizeof(LOGFILE)); NTSTATUS
if (!LogFile) LogfCreate(PLOGFILE *LogFile,
WCHAR * LogName,
PUNICODE_STRING FileName)
{
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
PLOGFILE pLogFile;
BOOL bResult, bCreateNew = FALSE;
NTSTATUS Status = STATUS_SUCCESS;
pLogFile = (LOGFILE *) HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, sizeof(LOGFILE));
if (!pLogFile)
{ {
DPRINT1("Can't allocate heap!\n"); DPRINT1("Can't allocate heap!\n");
return NULL; return STATUS_NO_MEMORY;
} }
LogFile->hFile = CreateFile(FileName, InitializeObjectAttributes(&ObjectAttributes,
GENERIC_READ | GENERIC_WRITE, FileName,
FILE_SHARE_READ, OBJ_CASE_INSENSITIVE,
NULL, NULL,
OPEN_ALWAYS, NULL);
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL);
if (LogFile->hFile == INVALID_HANDLE_VALUE) Status = NtCreateFile(&pLogFile->hFile,
GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (!NT_SUCCESS(Status))
{ {
DPRINT1("Can't create file %S.\n", FileName); DPRINT1("Can't create file %wZ (Status: 0x%08lx)\n", FileName, Status);
HeapFree(MyHeap, 0, LogFile); goto fail;
return NULL;
} }
bCreateNew = (GetLastError() == ERROR_ALREADY_EXISTS) ? FALSE : TRUE; bCreateNew = (IoStatusBlock.Information == FILE_CREATED) ? TRUE: FALSE;
LogFile->LogName = pLogFile->LogName =
(WCHAR *) HeapAlloc(MyHeap, (WCHAR *) HeapAlloc(MyHeap,
HEAP_ZERO_MEMORY, HEAP_ZERO_MEMORY,
(lstrlenW(LogName) + 1) * sizeof(WCHAR)); (lstrlenW(LogName) + 1) * sizeof(WCHAR));
if (pLogFile->LogName == NULL)
if (LogFile->LogName)
lstrcpyW(LogFile->LogName, LogName);
else
{
DPRINT1("Can't allocate heap\n");
HeapFree(MyHeap, 0, LogFile);
return NULL;
}
LogFile->FileName =
(WCHAR *) HeapAlloc(MyHeap,
HEAP_ZERO_MEMORY,
(lstrlenW(FileName) + 1) * sizeof(WCHAR));
if (LogFile->FileName)
lstrcpyW(LogFile->FileName, FileName);
else
{ {
DPRINT1("Can't allocate heap\n"); DPRINT1("Can't allocate heap\n");
Status = STATUS_NO_MEMORY;
goto fail; goto fail;
} }
LogFile->OffsetInfo = lstrcpyW(pLogFile->LogName, LogName);
pLogFile->FileName =
(WCHAR *) HeapAlloc(MyHeap,
HEAP_ZERO_MEMORY,
(lstrlenW(FileName->Buffer) + 1) * sizeof(WCHAR));
if (pLogFile->FileName == NULL)
{
DPRINT1("Can't allocate heap\n");
Status = STATUS_NO_MEMORY;
goto fail;
}
lstrcpyW(pLogFile->FileName, FileName->Buffer);
pLogFile->OffsetInfo =
(PEVENT_OFFSET_INFO) HeapAlloc(MyHeap, (PEVENT_OFFSET_INFO) HeapAlloc(MyHeap,
HEAP_ZERO_MEMORY, HEAP_ZERO_MEMORY,
sizeof(EVENT_OFFSET_INFO) * 64); sizeof(EVENT_OFFSET_INFO) * 64);
if (pLogFile->OffsetInfo == NULL)
if (!LogFile->OffsetInfo)
{ {
DPRINT1("Can't allocate heap\n"); DPRINT1("Can't allocate heap\n");
Status = STATUS_NO_MEMORY;
goto fail; goto fail;
} }
LogFile->OffsetInfoSize = 64; pLogFile->OffsetInfoSize = 64;
if (bCreateNew) if (bCreateNew)
bResult = LogfInitializeNew(LogFile); bResult = LogfInitializeNew(pLogFile);
else else
bResult = LogfInitializeExisting(LogFile); bResult = LogfInitializeExisting(pLogFile);
if (!bResult) if (!bResult)
goto fail;
RtlInitializeResource(&LogFile->Lock);
LogfListAddItem(LogFile);
return LogFile;
fail:
if (LogFile)
{ {
if (LogFile->OffsetInfo) Status = STATUS_UNSUCCESSFUL;
HeapFree(MyHeap, 0, LogFile->OffsetInfo); goto fail;
if (LogFile->FileName)
HeapFree(MyHeap, 0, LogFile->FileName);
if (LogFile->LogName)
HeapFree(MyHeap, 0, LogFile->LogName);
HeapFree(MyHeap, 0, LogFile);
} }
return NULL; RtlInitializeResource(&pLogFile->Lock);
LogfListAddItem(pLogFile);
fail:
if (!NT_SUCCESS(Status))
{
if ((pLogFile->hFile != NULL) && (pLogFile->hFile != INVALID_HANDLE_VALUE))
CloseHandle(pLogFile->hFile);
if (pLogFile->OffsetInfo)
HeapFree(MyHeap, 0, pLogFile->OffsetInfo);
if (pLogFile->FileName)
HeapFree(MyHeap, 0, pLogFile->FileName);
if (pLogFile->LogName)
HeapFree(MyHeap, 0, pLogFile->LogName);
HeapFree(MyHeap, 0, pLogFile);
}
else
{
*LogFile = pLogFile;
}
return Status;
} }
VOID LogfClose(PLOGFILE LogFile) VOID LogfClose(PLOGFILE LogFile)

View file

@ -142,8 +142,48 @@ static NTSTATUS
ElfCreateBackupLogHandle(PLOGHANDLE *LogHandle, ElfCreateBackupLogHandle(PLOGHANDLE *LogHandle,
PUNICODE_STRING FileName) PUNICODE_STRING FileName)
{ {
PLOGHANDLE lpLogHandle;
NTSTATUS Status = STATUS_SUCCESS;
DPRINT("ElfCreateBackupLogHandle(FileName: %wZ)\n", FileName); DPRINT("ElfCreateBackupLogHandle(FileName: %wZ)\n", FileName);
return STATUS_NOT_IMPLEMENTED;
lpLogHandle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LOGHANDLE));
if (lpLogHandle == NULL)
{
DPRINT1("Failed to allocate Heap!\n");
return STATUS_NO_MEMORY;
}
/* Create the log file */
Status = LogfCreate(&lpLogHandle->LogFile,
NULL,
FileName);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to create the log file! (Status 0x%08lx)\n", Status);
goto Done;
}
/* Set the backup flag */
lpLogHandle->Flags |= LOG_HANDLE_BACKUP_FILE;
/* Get the current record */
lpLogHandle->CurrentRecord = LogfGetOldestRecord(lpLogHandle->LogFile);
Done:
if (NT_SUCCESS(Status))
{
/* Append log handle */
InsertTailList(&LogHandleListHead, &lpLogHandle->LogHandleListEntry);
*LogHandle = lpLogHandle;
}
else
{
HeapFree(GetProcessHeap(), 0, lpLogHandle);
}
return Status;
} }
@ -170,6 +210,10 @@ ElfDeleteEventLogHandle(IELF_HANDLE EventLogHandle)
if (!ElfGetLogHandleEntryByHandle(lpLogHandle)) if (!ElfGetLogHandleEntryByHandle(lpLogHandle))
return STATUS_INVALID_HANDLE; return STATUS_INVALID_HANDLE;
/* Close the log file if it is a backup file */
if (lpLogHandle->Flags & LOG_HANDLE_BACKUP_FILE)
LogfClose(lpLogHandle->LogFile);
RemoveEntryList(&lpLogHandle->LogHandleListEntry); RemoveEntryList(&lpLogHandle->LogHandleListEntry);
HeapFree(GetProcessHeap(),0,lpLogHandle); HeapFree(GetProcessHeap(),0,lpLogHandle);