[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;
WCHAR *Buf = NULL, *Expanded = NULL;
LONG Result;
PLOGFILE pLogf;
PLOGFILE pLogf = NULL;
UNICODE_STRING FileName;
NTSTATUS Status;
DPRINT("LoadLogFile: %S\n", LogName);
@ -314,13 +316,21 @@ PLOGFILE LoadLogFile(HKEY hKey, WCHAR * LogName)
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);
pLogf = LogfCreate(LogName, Expanded);
if (pLogf == NULL)
Status = LogfCreate(&pLogf, LogName, &FileName);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to create %S!\n", Expanded);
DPRINT1("Failed to create %S! (Status %08lx)\n", Expanded, Status);
}
HeapFree(MyHeap, 0, Buf);

View file

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

View file

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

View file

@ -142,8 +142,48 @@ static NTSTATUS
ElfCreateBackupLogHandle(PLOGHANDLE *LogHandle,
PUNICODE_STRING FileName)
{
PLOGHANDLE lpLogHandle;
NTSTATUS Status = STATUS_SUCCESS;
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))
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);
HeapFree(GetProcessHeap(),0,lpLogHandle);