From 68d79b0c3b4aa41ba213d923f8524e00ca0d6f79 Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Thu, 16 Jun 2016 12:17:38 -0700 Subject: [PATCH] dll: convert all initialization to the initonce pattern --- src/dll/eventlog.c | 18 ++++++----------- src/dll/fs.c | 21 +++++++++++--------- src/dll/fuse/fuse.c | 21 ++++++-------------- src/dll/library.c | 20 ------------------- src/dll/library.h | 7 ------- src/dll/np.c | 3 +-- src/dll/ntstatus.c | 17 +++++----------- src/dll/posix.c | 42 ++++++++++++++++++---------------------- src/dll/service.c | 4 ---- src/dll/util.c | 4 ++-- src/launcher/launchctl.c | 7 ------- src/launcher/launcher.c | 7 ------- src/shared/minimal.h | 6 ++---- 13 files changed, 53 insertions(+), 124 deletions(-) diff --git a/src/dll/eventlog.c b/src/dll/eventlog.c index 3efed0cb..86da7323 100644 --- a/src/dll/eventlog.c +++ b/src/dll/eventlog.c @@ -21,13 +21,14 @@ #define FSP_EVENTLOG_NAME LIBRARY_NAME -static HANDLE FspEventLogHandle; static INIT_ONCE FspEventLogInitOnce = INIT_ONCE_STATIC_INIT; -static BOOL WINAPI FspEventLogRegisterEventSource( - PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context); +static HANDLE FspEventLogHandle; -VOID FspEventLogInitialize(BOOLEAN Dynamic) +static BOOL WINAPI FspEventLogInitialize( + PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { + FspEventLogHandle = RegisterEventSourceW(0, L"" FSP_EVENTLOG_NAME); + return TRUE; } VOID FspEventLogFinalize(BOOLEAN Dynamic) @@ -55,7 +56,7 @@ FSP_API VOID FspEventLog(ULONG Type, PWSTR Format, ...) FSP_API VOID FspEventLogV(ULONG Type, PWSTR Format, va_list ap) { - InitOnceExecuteOnce(&FspEventLogInitOnce, FspEventLogRegisterEventSource, 0, 0); + InitOnceExecuteOnce(&FspEventLogInitOnce, FspEventLogInitialize, 0, 0); if (0 == FspEventLogHandle) return; @@ -85,13 +86,6 @@ FSP_API VOID FspEventLogV(ULONG Type, PWSTR Format, va_list ap) ReportEventW(FspEventLogHandle, (WORD)Type, 0, EventId, 0, 1, 0, Strings, 0); } -static BOOL WINAPI FspEventLogRegisterEventSource( - PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) -{ - FspEventLogHandle = RegisterEventSourceW(0, L"" FSP_EVENTLOG_NAME); - return TRUE; -} - NTSTATUS FspEventLogRegister(VOID) { extern HINSTANCE DllInstance; diff --git a/src/dll/fs.c b/src/dll/fs.c index fcce6bbe..1efd5955 100644 --- a/src/dll/fs.c +++ b/src/dll/fs.c @@ -24,20 +24,16 @@ enum static FSP_FILE_SYSTEM_INTERFACE FspFileSystemNullInterface; +static INIT_ONCE FspFileSystemInitOnce = INIT_ONCE_STATIC_INIT; +static BOOLEAN FspFileSystemInitialized; static CRITICAL_SECTION FspFileSystemMountListGuard; static LIST_ENTRY FspFileSystemMountList = { &FspFileSystemMountList, &FspFileSystemMountList }; -VOID FspFileSystemInitialize(BOOLEAN Dynamic) +static BOOL WINAPI FspFileSystemInitialize( + PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { - /* - * This function is called during DLL_PROCESS_ATTACH. We must therefore keep - * initialization tasks to a minimum. - * - * Initialization of synchronization objects is allowed! See: - * https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx - */ - InitializeCriticalSection(&FspFileSystemMountListGuard); + return FspFileSystemInitialized = TRUE; } VOID FspFileSystemFinalize(BOOLEAN Dynamic) @@ -60,6 +56,9 @@ VOID FspFileSystemFinalize(BOOLEAN Dynamic) * OS will clean it up for us. */ + if (!FspFileSystemInitialized) + return; + FSP_FILE_SYSTEM *FileSystem; PLIST_ENTRY MountEntry; @@ -95,6 +94,10 @@ FSP_API NTSTATUS FspFileSystemCreate(PWSTR DevicePath, if (0 == Interface) Interface = &FspFileSystemNullInterface; + InitOnceExecuteOnce(&FspFileSystemInitOnce, FspFileSystemInitialize, 0, 0); + if (!FspFileSystemInitialized) + return STATUS_INSUFFICIENT_RESOURCES; + FileSystem = MemAlloc(sizeof *FileSystem); if (0 == FileSystem) return STATUS_INSUFFICIENT_RESOURCES; diff --git a/src/dll/fuse/fuse.c b/src/dll/fuse/fuse.c index 34021c92..a32025bd 100644 --- a/src/dll/fuse/fuse.c +++ b/src/dll/fuse/fuse.c @@ -100,8 +100,8 @@ static struct fuse_opt fsp_fuse_core_opts[] = FUSE_OPT_END, }; +static INIT_ONCE fsp_fuse_initonce = INIT_ONCE_STATIC_INIT; static DWORD fsp_fuse_tlskey = TLS_OUT_OF_INDEXES; -static INIT_ONCE fsp_fuse_initonce_v = INIT_ONCE_STATIC_INIT; struct fsp_fuse_obj_hdr { @@ -129,8 +129,11 @@ static inline void fsp_fuse_obj_free(void *obj) hdr->dtor(hdr); } -VOID fsp_fuse_initialize(BOOLEAN Dynamic) +static BOOL WINAPI fsp_fuse_initialize( + PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { + fsp_fuse_tlskey = TlsAlloc(); + return TRUE; } VOID fsp_fuse_finalize(BOOLEAN Dynamic) @@ -171,18 +174,6 @@ VOID fsp_fuse_finalize_thread(VOID) } } -static BOOL WINAPI fsp_fuse_initonce_f( - PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) -{ - fsp_fuse_tlskey = TlsAlloc(); - return TRUE; -} - -static inline VOID fsp_fuse_initonce(VOID) -{ - InitOnceExecuteOnce(&fsp_fuse_initonce_v, fsp_fuse_initonce_f, 0, 0); -} - FSP_FUSE_API int fsp_fuse_version(struct fsp_fuse_env *env) { return FUSE_VERSION; @@ -606,7 +597,7 @@ FSP_FUSE_API struct fuse_context *fsp_fuse_get_context(struct fsp_fuse_env *env) { struct fuse_context *context; - fsp_fuse_initonce(); + InitOnceExecuteOnce(&fsp_fuse_initonce, fsp_fuse_initialize, 0, 0); if (TLS_OUT_OF_INDEXES == fsp_fuse_tlskey) return 0; diff --git a/src/dll/library.c b/src/dll/library.c index a601c31b..119cc319 100644 --- a/src/dll/library.c +++ b/src/dll/library.c @@ -18,7 +18,6 @@ #include HINSTANCE DllInstance; -HANDLE ProcessHeap; BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, PVOID Reserved) { @@ -28,24 +27,6 @@ BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, PVOID Reserved) { case DLL_PROCESS_ATTACH: DllInstance = Instance; - ProcessHeap = GetProcessHeap(); - if (0 == ProcessHeap) - return FALSE; - - /* - * These functions are called during DLL_PROCESS_ATTACH. We must therefore keep - * initialization tasks to a minimum. - * - * See the DLL best practices document: - * https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx - */ - Dynamic = 0 == Reserved; - FspNtStatusInitialize(Dynamic); - FspPosixInitialize(Dynamic); - FspEventLogInitialize(Dynamic); - FspFileSystemInitialize(Dynamic); - FspServiceInitialize(Dynamic); - fsp_fuse_initialize(Dynamic); break; case DLL_PROCESS_DETACH: @@ -64,7 +45,6 @@ BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, PVOID Reserved) FspFileSystemFinalize(Dynamic); FspEventLogFinalize(Dynamic); FspPosixFinalize(Dynamic); - FspNtStatusFinalize(Dynamic); break; case DLL_THREAD_DETACH: diff --git a/src/dll/library.h b/src/dll/library.h index e74cd2d1..23bcc55b 100644 --- a/src/dll/library.h +++ b/src/dll/library.h @@ -36,17 +36,10 @@ #define DEBUGLOGSD(fmt, SD) ((void)0) #endif -VOID FspNtStatusInitialize(BOOLEAN Dynamic); -VOID FspNtStatusFinalize(BOOLEAN Dynamic); -VOID FspPosixInitialize(BOOLEAN Dynamic); VOID FspPosixFinalize(BOOLEAN Dynamic); -VOID FspEventLogInitialize(BOOLEAN Dynamic); VOID FspEventLogFinalize(BOOLEAN Dynamic); -VOID FspFileSystemInitialize(BOOLEAN Dynamic); VOID FspFileSystemFinalize(BOOLEAN Dynamic); -VOID FspServiceInitialize(BOOLEAN Dynamic); VOID FspServiceFinalize(BOOLEAN Dynamic); -VOID fsp_fuse_initialize(BOOLEAN Dynamic); VOID fsp_fuse_finalize(BOOLEAN Dynamic); VOID fsp_fuse_finalize_thread(VOID); diff --git a/src/dll/np.c b/src/dll/np.c index 9bb832b9..727aa488 100644 --- a/src/dll/np.c +++ b/src/dll/np.c @@ -464,10 +464,9 @@ typedef struct static inline BOOLEAN FspNpValidateEnum(FSP_NP_ENUM *Enum) { #if 0 - extern HANDLE ProcessHeap; return 0 != Enum && - HeapValidate(ProcessHeap, 0, Enum) && + HeapValidate(GetProcessHeap(), 0, Enum) && 'munE' == Enum->Signature; #else return diff --git a/src/dll/ntstatus.c b/src/dll/ntstatus.c index 92b71d35..7626d5b8 100644 --- a/src/dll/ntstatus.c +++ b/src/dll/ntstatus.c @@ -17,27 +17,19 @@ #include +static INIT_ONCE FspNtStatusInitOnce = INIT_ONCE_STATIC_INIT; static ULONG (WINAPI *FspRtlNtStatusToDosError)(NTSTATUS Status); -VOID FspNtStatusInitialize(BOOLEAN Dynamic) +static BOOL WINAPI FspNtStatusInitialize( + PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { - /* - * This function is called during DLL_PROCESS_ATTACH. We must therefore keep - * initialization tasks to a minimum. - * - * GetModuleHandle/GetProcAddress is allowed (because they are kernel32 API's)! See: - * https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx - */ - HANDLE Handle; Handle = GetModuleHandleW(L"ntdll.dll"); if (0 != Handle) FspRtlNtStatusToDosError = (PVOID)GetProcAddress(Handle, "RtlNtStatusToDosError"); -} -VOID FspNtStatusFinalize(BOOLEAN Dynamic) -{ + return TRUE; } FSP_API NTSTATUS FspNtStatusFromWin32(DWORD Error) @@ -53,6 +45,7 @@ FSP_API NTSTATUS FspNtStatusFromWin32(DWORD Error) FSP_API DWORD FspWin32FromNtStatus(NTSTATUS Status) { + InitOnceExecuteOnce(&FspNtStatusInitOnce, FspNtStatusInitialize, 0, 0); if (0 == FspRtlNtStatusToDosError) return ERROR_MR_MID_NOT_FOUND; diff --git a/src/dll/posix.c b/src/dll/posix.c index dc609357..d5ec12f7 100644 --- a/src/dll/posix.c +++ b/src/dll/posix.c @@ -35,28 +35,10 @@ static PISID FspPosixCreateSid(BYTE Authority, ULONG Count, ...); -static INIT_ONCE FspPosixInitOnceV = INIT_ONCE_STATIC_INIT; +static INIT_ONCE FspPosixInitOnce = INIT_ONCE_STATIC_INIT; static PISID FspAccountDomainSid, FspPrimaryDomainSid; -VOID FspPosixInitialize(BOOLEAN Dynamic) -{ -} - -VOID FspPosixFinalize(BOOLEAN Dynamic) -{ - /* - * This function is called during DLL_PROCESS_DETACH. We must therefore keep - * finalization tasks to a minimum. - */ - - if (Dynamic) - { - MemFree(FspAccountDomainSid); - MemFree(FspPrimaryDomainSid); - } -} - -static BOOL WINAPI FspPosixInitOnceF( +static BOOL WINAPI FspPosixInitialize( PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { static LSA_OBJECT_ATTRIBUTES Obja; @@ -106,6 +88,20 @@ exit: return TRUE; } +VOID FspPosixFinalize(BOOLEAN Dynamic) +{ + /* + * This function is called during DLL_PROCESS_DETACH. We must therefore keep + * finalization tasks to a minimum. + */ + + if (Dynamic) + { + MemFree(FspAccountDomainSid); + MemFree(FspPrimaryDomainSid); + } +} + FSP_API NTSTATUS FspPosixMapUidToSid(UINT32 Uid, PSID *PSid) { *PSid = 0; @@ -161,7 +157,7 @@ FSP_API NTSTATUS FspPosixMapUidToSid(UINT32 Uid, PSID *PSid) */ else if (0x30000 <= Uid && Uid < 0x40000) { - InitOnceExecuteOnce(&FspPosixInitOnceV, FspPosixInitOnceF, 0, 0); + InitOnceExecuteOnce(&FspPosixInitOnce, FspPosixInitialize, 0, 0); if (0 != FspAccountDomainSid && 5 == FspAccountDomainSid->IdentifierAuthority.Value[5] && @@ -177,7 +173,7 @@ FSP_API NTSTATUS FspPosixMapUidToSid(UINT32 Uid, PSID *PSid) } else if (0x100000 <= Uid && Uid < 0x200000) { - InitOnceExecuteOnce(&FspPosixInitOnceV, FspPosixInitOnceF, 0, 0); + InitOnceExecuteOnce(&FspPosixInitOnce, FspPosixInitialize, 0, 0); if (0 != FspPrimaryDomainSid && 5 == FspPrimaryDomainSid->IdentifierAuthority.Value[5] && @@ -278,7 +274,7 @@ FSP_API NTSTATUS FspPosixMapSidToUid(PSID Sid, PUINT32 PUid) */ else if (5 <= Count && 21 == SubAuthority0) { - InitOnceExecuteOnce(&FspPosixInitOnceV, FspPosixInitOnceF, 0, 0); + InitOnceExecuteOnce(&FspPosixInitOnce, FspPosixInitialize, 0, 0); /* * The order is important! A server that is also a domain controller diff --git a/src/dll/service.c b/src/dll/service.c index 305689fa..382995b8 100644 --- a/src/dll/service.c +++ b/src/dll/service.c @@ -52,10 +52,6 @@ BOOL WINAPI FspServiceConsoleCtrlHandler(DWORD CtrlType); (FSP_SERVICE *)((PUINT8)FspServiceTable[0].lpServiceName - FIELD_OFFSET(FSP_SERVICE, ServiceName)) :\ 0) -VOID FspServiceInitialize(BOOLEAN Dynamic) -{ -} - VOID FspServiceFinalize(BOOLEAN Dynamic) { /* diff --git a/src/dll/util.c b/src/dll/util.c index f3c98c9e..b27c0700 100644 --- a/src/dll/util.c +++ b/src/dll/util.c @@ -21,7 +21,7 @@ static INIT_ONCE FspDiagIdentInitOnce = INIT_ONCE_STATIC_INIT; static WCHAR FspDiagIdentBuf[20] = L"UNKNOWN"; -static BOOL WINAPI FspDiagIdentInit( +static BOOL WINAPI FspDiagIdentInitialize( PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { WCHAR ModuleFileName[MAX_PATH]; @@ -55,7 +55,7 @@ PWSTR FspDiagIdent(VOID) { /* internal only: get a diagnostic identifier (eventlog, debug) */ - InitOnceExecuteOnce(&FspDiagIdentInitOnce, FspDiagIdentInit, 0, 0); + InitOnceExecuteOnce(&FspDiagIdentInitOnce, FspDiagIdentInitialize, 0, 0); return FspDiagIdentBuf; } diff --git a/src/launcher/launchctl.c b/src/launcher/launchctl.c index 0c3a0f9c..6047c9bd 100644 --- a/src/launcher/launchctl.c +++ b/src/launcher/launchctl.c @@ -276,16 +276,9 @@ void wmainCRTStartup(void) DWORD Argc; PWSTR *Argv; - extern HANDLE ProcessHeap; - ProcessHeap = GetProcessHeap(); - if (0 == ProcessHeap) - ExitProcess(GetLastError()); - Argv = CommandLineToArgvW(GetCommandLineW(), &Argc); if (0 == Argv) ExitProcess(GetLastError()); ExitProcess(wmain(Argc, Argv)); } - -HANDLE ProcessHeap; diff --git a/src/launcher/launcher.c b/src/launcher/launcher.c index 1f4cc057..398c206d 100644 --- a/src/launcher/launcher.c +++ b/src/launcher/launcher.c @@ -981,12 +981,5 @@ int wmain(int argc, wchar_t **argv) void wmainCRTStartup(void) { - extern HANDLE ProcessHeap; - ProcessHeap = GetProcessHeap(); - if (0 == ProcessHeap) - ExitProcess(GetLastError()); - ExitProcess(wmain(0, 0)); } - -HANDLE ProcessHeap; diff --git a/src/shared/minimal.h b/src/shared/minimal.h index 537aac54..b5db2559 100644 --- a/src/shared/minimal.h +++ b/src/shared/minimal.h @@ -67,14 +67,12 @@ void *memset(void *dst, int val, size_t siz) static inline void *MemAlloc(size_t Size) { - extern HANDLE ProcessHeap; - return HeapAlloc(ProcessHeap, 0, Size); + return HeapAlloc(GetProcessHeap(), 0, Size); } static inline void MemFree(void *Pointer) { - extern HANDLE ProcessHeap; if (0 != Pointer) - HeapFree(ProcessHeap, 0, Pointer); + HeapFree(GetProcessHeap(), 0, Pointer); } static FORCEINLINE