2012-11-02 18:28:42 +00:00
|
|
|
/*
|
1998-12-04 18:28:13 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
2015-11-14 14:57:11 +00:00
|
|
|
* FILE: dll/win32/kernel32/client/dllmain.c
|
2005-05-09 01:46:57 +00:00
|
|
|
* PURPOSE: Initialization
|
2012-11-02 18:28:42 +00:00
|
|
|
* PROGRAMMERS: Ariadne (ariadne@xs4all.nl)
|
2011-03-29 21:48:13 +00:00
|
|
|
* Aleksey Bragin (aleksey@reactos.org)
|
1998-12-04 18:28:13 +00:00
|
|
|
*/
|
|
|
|
|
2001-04-04 22:21:32 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
2003-01-15 21:24:36 +00:00
|
|
|
#include <k32.h>
|
1999-04-10 12:08:24 +00:00
|
|
|
|
2013-01-03 17:42:27 +00:00
|
|
|
#define NDEBUG
|
2007-09-02 19:42:22 +00:00
|
|
|
#include <debug.h>
|
1998-12-04 18:28:13 +00:00
|
|
|
|
2001-04-04 22:21:32 +00:00
|
|
|
/* GLOBALS *******************************************************************/
|
2000-08-15 12:44:47 +00:00
|
|
|
|
2011-07-25 03:28:33 +00:00
|
|
|
PBASE_STATIC_SERVER_DATA BaseStaticServerData;
|
2014-12-17 22:07:41 +00:00
|
|
|
BOOLEAN BaseRunningInServerProcess = FALSE;
|
2011-07-23 11:38:58 +00:00
|
|
|
|
2011-03-29 21:48:13 +00:00
|
|
|
WCHAR BaseDefaultPathBuffer[6140];
|
2000-08-15 12:44:47 +00:00
|
|
|
|
2011-07-25 20:23:43 +00:00
|
|
|
HANDLE BaseNamedObjectDirectory;
|
2004-06-26 20:07:40 +00:00
|
|
|
HMODULE hCurrentModule = NULL;
|
2011-07-21 02:55:56 +00:00
|
|
|
HMODULE kernel32_handle = NULL;
|
2005-09-26 18:03:17 +00:00
|
|
|
PPEB Peb;
|
|
|
|
ULONG SessionId;
|
2004-01-23 17:18:16 +00:00
|
|
|
static BOOL DllInitialized = FALSE;
|
2000-08-15 12:44:47 +00:00
|
|
|
|
2001-04-04 22:21:32 +00:00
|
|
|
/* Critical section for various kernel32 data structures */
|
2011-03-29 21:48:13 +00:00
|
|
|
RTL_CRITICAL_SECTION BaseDllDirectoryLock;
|
2013-01-01 23:36:19 +00:00
|
|
|
|
2009-08-24 20:10:05 +00:00
|
|
|
extern BOOL FASTCALL NlsInit(VOID);
|
|
|
|
extern VOID FASTCALL NlsUninit(VOID);
|
2004-08-24 17:21:12 +00:00
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
#define WIN_OBJ_DIR L"\\Windows"
|
|
|
|
#define SESSION_DIR L"\\Sessions"
|
|
|
|
|
2001-04-04 22:21:32 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
1998-12-04 18:28:13 +00:00
|
|
|
|
[RTL/NTDLL/KERNEL32]: Rtl provides worker queue and timer queue functionality, which queues a worker thread associated with a caller-supplied callback. In Windows, Rtl by default calls RtlCreateUserThread, but as soon as kernel32 loads, it's DllMain calls an exported function RtlSetThreadPoolStartFunc which changes that default to a special Base function that calls CreateRemoteThread instead. The net result is that Win32 processes using the Rtl functionality get their threads properly registered with CSRSS. In ReactOS, this did not happen, so when those threads called into CSRSS, CSRSS had no CSR_THREAD structure/state for them, which is why CsrCreateThread (and the API loop) are so badly hacked. This commit implements RtlSetThreadPoolStartFunc, implements the kernel32 base functions which wrap CreateRemoteThread, and implements the rtl functions which wrap RtlCreateUserThread. Services, Setup, and any ReactOS application using RPC now have the worker threads correctly registered.
svn path=/trunk/; revision=55706
2012-02-19 10:06:31 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
BaseCreateThreadPoolThread(IN PTHREAD_START_ROUTINE Function,
|
|
|
|
IN PVOID Parameter,
|
|
|
|
OUT PHANDLE ThreadHandle)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Create a Win32 thread */
|
|
|
|
*ThreadHandle = CreateRemoteThread(NtCurrentProcess(),
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
Function,
|
|
|
|
Parameter,
|
|
|
|
CREATE_SUSPENDED,
|
|
|
|
NULL);
|
|
|
|
if (!(*ThreadHandle))
|
|
|
|
{
|
|
|
|
/* Get the status value if we couldn't get a handle */
|
|
|
|
Status = NtCurrentTeb()->LastStatusValue;
|
|
|
|
if (NT_SUCCESS(Status)) Status = STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set success code */
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
BaseExitThreadPoolThread(IN NTSTATUS ExitStatus)
|
|
|
|
{
|
|
|
|
/* Exit the thread */
|
|
|
|
ExitThread(ExitStatus);
|
2013-04-13 18:49:50 +00:00
|
|
|
return STATUS_SUCCESS;
|
[RTL/NTDLL/KERNEL32]: Rtl provides worker queue and timer queue functionality, which queues a worker thread associated with a caller-supplied callback. In Windows, Rtl by default calls RtlCreateUserThread, but as soon as kernel32 loads, it's DllMain calls an exported function RtlSetThreadPoolStartFunc which changes that default to a special Base function that calls CreateRemoteThread instead. The net result is that Win32 processes using the Rtl functionality get their threads properly registered with CSRSS. In ReactOS, this did not happen, so when those threads called into CSRSS, CSRSS had no CSR_THREAD structure/state for them, which is why CsrCreateThread (and the API loop) are so badly hacked. This commit implements RtlSetThreadPoolStartFunc, implements the kernel32 base functions which wrap CreateRemoteThread, and implements the rtl functions which wrap RtlCreateUserThread. Services, Setup, and any ReactOS application using RPC now have the worker threads correctly registered.
svn path=/trunk/; revision=55706
2012-02-19 10:06:31 +00:00
|
|
|
}
|
|
|
|
|
2005-09-07 19:37:28 +00:00
|
|
|
BOOL
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2003-03-05 22:51:48 +00:00
|
|
|
DllMain(HANDLE hDll,
|
2005-09-07 19:37:28 +00:00
|
|
|
DWORD dwReason,
|
|
|
|
LPVOID lpReserved)
|
1998-12-04 18:28:13 +00:00
|
|
|
{
|
2005-09-07 19:37:28 +00:00
|
|
|
NTSTATUS Status;
|
2014-12-16 21:48:23 +00:00
|
|
|
BASESRV_API_CONNECTINFO ConnectInfo;
|
|
|
|
ULONG ConnectInfoSize = sizeof(ConnectInfo);
|
2005-09-26 18:03:17 +00:00
|
|
|
WCHAR SessionDir[256];
|
2000-02-27 02:12:07 +00:00
|
|
|
|
2013-08-29 20:37:02 +00:00
|
|
|
DPRINT("DllMain(hInst %p, dwReason %lu)\n",
|
2005-09-07 19:37:28 +00:00
|
|
|
hDll, dwReason);
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2006-03-04 17:27:40 +00:00
|
|
|
Basep8BitStringToUnicodeString = RtlAnsiStringToUnicodeString;
|
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
/* Cache the PEB and Session ID */
|
|
|
|
Peb = NtCurrentPeb();
|
|
|
|
SessionId = Peb->SessionId;
|
|
|
|
|
2005-09-07 19:37:28 +00:00
|
|
|
switch (dwReason)
|
2003-03-05 22:51:48 +00:00
|
|
|
{
|
2005-09-07 19:37:28 +00:00
|
|
|
case DLL_PROCESS_ATTACH:
|
2013-02-10 13:59:09 +00:00
|
|
|
{
|
2016-11-12 21:53:33 +00:00
|
|
|
/* Set no filter initially */
|
2013-02-10 13:59:09 +00:00
|
|
|
GlobalTopLevelExceptionFilter = RtlEncodePointer(NULL);
|
2021-09-13 01:33:14 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Enable the Rtl thread pool and timer queue to use proper Win32 thread */
|
|
|
|
RtlSetThreadPoolStartFunc(BaseCreateThreadPoolThread, BaseExitThreadPoolThread);
|
2005-09-07 19:37:28 +00:00
|
|
|
|
2016-01-07 12:09:03 +00:00
|
|
|
/* Register the manifest prober routine */
|
|
|
|
LdrSetDllManifestProber(BasepProbeForDllManifest);
|
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Don't bother us for each thread */
|
|
|
|
LdrDisableThreadCalloutsForDll((PVOID)hDll);
|
2005-09-07 19:37:28 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize default path to NULL */
|
|
|
|
RtlInitUnicodeString(&BaseDefaultPath, NULL);
|
2011-03-29 21:48:13 +00:00
|
|
|
|
2014-12-17 22:07:41 +00:00
|
|
|
/* Setup the Object Directory path */
|
2013-02-10 13:59:09 +00:00
|
|
|
if (!SessionId)
|
|
|
|
{
|
|
|
|
/* Use the raw path */
|
|
|
|
wcscpy(SessionDir, WIN_OBJ_DIR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the session path */
|
|
|
|
swprintf(SessionDir,
|
|
|
|
L"%ws\\%ld%ws",
|
|
|
|
SESSION_DIR,
|
|
|
|
SessionId,
|
|
|
|
WIN_OBJ_DIR);
|
|
|
|
}
|
2005-09-26 18:03:17 +00:00
|
|
|
|
2014-12-16 21:48:23 +00:00
|
|
|
/* Connect to the Base Server */
|
2013-02-10 13:59:09 +00:00
|
|
|
Status = CsrClientConnectToServer(SessionDir,
|
|
|
|
BASESRV_SERVERDLL_INDEX,
|
2014-12-16 21:48:23 +00:00
|
|
|
&ConnectInfo,
|
|
|
|
&ConnectInfoSize,
|
2013-02-10 13:59:09 +00:00
|
|
|
&BaseRunningInServerProcess);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to connect to CSR (Status %lx)\n", Status);
|
|
|
|
NtTerminateProcess(NtCurrentProcess(), Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-25 20:23:43 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Get the server data */
|
|
|
|
ASSERT(Peb->ReadOnlyStaticServerData);
|
|
|
|
BaseStaticServerData = Peb->ReadOnlyStaticServerData[BASESRV_SERVERDLL_INDEX];
|
|
|
|
ASSERT(BaseStaticServerData);
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Check if we are running a CSR Server */
|
|
|
|
if (!BaseRunningInServerProcess)
|
|
|
|
{
|
|
|
|
/* Set the termination port for the thread */
|
|
|
|
DPRINT("Creating new thread for CSR\n");
|
|
|
|
CsrNewThread();
|
|
|
|
}
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize heap handle table */
|
|
|
|
BaseDllInitializeMemoryManager();
|
2005-09-26 18:03:17 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Set HMODULE for our DLL */
|
|
|
|
kernel32_handle = hCurrentModule = hDll;
|
2011-07-25 20:23:43 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Set the directories */
|
|
|
|
BaseWindowsDirectory = BaseStaticServerData->WindowsDirectory;
|
|
|
|
BaseWindowsSystemDirectory = BaseStaticServerData->WindowsSystemDirectory;
|
2005-09-26 18:03:17 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Construct the default path (using the static buffer) */
|
2018-02-15 21:34:48 +00:00
|
|
|
Status = RtlStringCbPrintfW(BaseDefaultPathBuffer,
|
|
|
|
sizeof(BaseDefaultPathBuffer),
|
2022-05-16 10:34:40 +00:00
|
|
|
L"%wZ;%wZ\\system;%wZ;",
|
2018-02-15 21:34:48 +00:00
|
|
|
&BaseWindowsSystemDirectory,
|
|
|
|
&BaseWindowsDirectory,
|
|
|
|
&BaseWindowsDirectory);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("NLS Init failed\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-03-29 21:48:13 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
BaseDefaultPath.Buffer = BaseDefaultPathBuffer;
|
2018-02-15 21:34:48 +00:00
|
|
|
BaseDefaultPath.Length = (USHORT)wcslen(BaseDefaultPathBuffer) * sizeof(WCHAR);
|
2013-02-10 13:59:09 +00:00
|
|
|
BaseDefaultPath.MaximumLength = sizeof(BaseDefaultPathBuffer);
|
2011-03-29 21:48:13 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Use remaining part of the default path buffer for the append path */
|
|
|
|
BaseDefaultPathAppend.Buffer = (PWSTR)((ULONG_PTR)BaseDefaultPathBuffer + BaseDefaultPath.Length);
|
|
|
|
BaseDefaultPathAppend.Length = 0;
|
|
|
|
BaseDefaultPathAppend.MaximumLength = BaseDefaultPath.MaximumLength - BaseDefaultPath.Length;
|
2011-03-29 21:48:13 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize command line */
|
|
|
|
InitCommandLines();
|
2010-05-06 10:50:26 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize the DLL critical section */
|
|
|
|
RtlInitializeCriticalSection(&BaseDllDirectoryLock);
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize the National Language Support routines */
|
|
|
|
if (!NlsInit())
|
|
|
|
{
|
|
|
|
DPRINT1("NLS Init failed\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize Console Support */
|
2014-09-07 22:53:49 +00:00
|
|
|
if (!ConDllInitialize(dwReason, SessionDir))
|
2013-02-10 13:59:09 +00:00
|
|
|
{
|
|
|
|
DPRINT1("Failed to set up console\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Initialize application certification globals */
|
|
|
|
InitializeListHead(&BasepAppCertDllsList);
|
|
|
|
RtlInitializeCriticalSection(&gcsAppCert);
|
Note, none of this code is being used/called yet.
[KERNEL32]: Implement BasepSxsCloseHandles, BasepReplaceProcessThreadTokens, BasepIsProcessAllowed, BasepCheckWebBladeHashes, BasepIsImageVersionOk which will be needed for the future CreateProcess re-implementation. These functions partly support SxS, Safer (Authz) and Application Certification features added in XP/2003. We also emulate support for Computer Server, Web Blade Server and Embedded ReactOS. The last function does correct image version checks to prevent invalid binaries from running.
[KERNEL32]: Implement BaseUpdateVDMEntry and BaseCheckForVDM using the new CSRSS messages (not implemented on the server-side yet). Stubplement BaseCheckVDM. These will be needed for future VDM-stub-support (primarly so we can run 16-bit installers).
[KERNEL32]: Implement BasepFreeAppCompatData, BasepCheckBadapp, and IsShimInfrastructureDisabled (exported as BaseIsAppcompatInfrastructureDisabled). These stub most of the required/exported application compatibility APIs, as long as someone sets DisableAppCompat in \\Registry\\MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\AppCompatibility.
svn path=/trunk/; revision=54975
2012-01-15 14:49:49 +00:00
|
|
|
|
2013-02-10 13:59:09 +00:00
|
|
|
/* Insert more dll attach stuff here! */
|
|
|
|
DllInitialized = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
case DLL_PROCESS_DETACH:
|
2013-02-10 13:59:09 +00:00
|
|
|
{
|
2017-10-01 14:42:04 +00:00
|
|
|
if (DllInitialized != FALSE)
|
2005-09-26 18:03:17 +00:00
|
|
|
{
|
2014-09-09 21:24:44 +00:00
|
|
|
/* Uninitialize console support */
|
|
|
|
ConDllInitialize(dwReason, NULL);
|
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
/* Insert more dll detach stuff here! */
|
|
|
|
NlsUninit();
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
/* Delete DLL critical section */
|
2013-01-01 23:36:19 +00:00
|
|
|
RtlDeleteCriticalSection(&BaseDllDirectoryLock);
|
2005-09-26 18:03:17 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-02-10 13:59:09 +00:00
|
|
|
}
|
2005-09-26 18:03:17 +00:00
|
|
|
|
2014-09-09 21:24:44 +00:00
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
{
|
|
|
|
/* ConDllInitialize sets the current console locale for the new thread */
|
|
|
|
return ConDllInitialize(dwReason, NULL);
|
|
|
|
}
|
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
default:
|
|
|
|
break;
|
1998-12-04 18:28:13 +00:00
|
|
|
}
|
2003-03-05 22:51:48 +00:00
|
|
|
|
2005-09-26 18:03:17 +00:00
|
|
|
return TRUE;
|
1998-12-04 18:28:13 +00:00
|
|
|
}
|
2000-02-27 02:12:07 +00:00
|
|
|
|
2000-03-22 18:36:00 +00:00
|
|
|
/* EOF */
|