[CMAKE]
[reactos.git] / subsystems / win32 / csrss / win32csr / dllmain.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: subsys/csrss/win32csr/dllmain.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Dmitry Philippov (shedon@mail.ru)
7 */
8
9 /* INCLUDES ******************************************************************/
10 #define NDEBUG
11 #include "w32csr.h"
12 #include <debug.h>
13
14 /* Not defined in any header file */
15 extern VOID WINAPI PrivateCsrssManualGuiCheck(LONG Check);
16 extern VOID WINAPI InitializeAppSwitchHook();
17
18 /* GLOBALS *******************************************************************/
19
20 HANDLE Win32CsrApiHeap;
21 HINSTANCE Win32CsrDllHandle = NULL;
22 static CSRSS_EXPORTED_FUNCS CsrExports;
23
24 static CSRSS_API_DEFINITION Win32CsrApiDefinitions[] =
25 {
26 CSRSS_DEFINE_API(GET_INPUT_HANDLE, CsrGetHandle),
27 CSRSS_DEFINE_API(GET_OUTPUT_HANDLE, CsrGetHandle),
28 CSRSS_DEFINE_API(CLOSE_HANDLE, CsrCloseHandle),
29 CSRSS_DEFINE_API(VERIFY_HANDLE, CsrVerifyHandle),
30 CSRSS_DEFINE_API(DUPLICATE_HANDLE, CsrDuplicateHandle),
31 CSRSS_DEFINE_API(GET_INPUT_WAIT_HANDLE, CsrGetInputWaitHandle),
32 CSRSS_DEFINE_API(WRITE_CONSOLE, CsrWriteConsole),
33 CSRSS_DEFINE_API(READ_CONSOLE, CsrReadConsole),
34 CSRSS_DEFINE_API(ALLOC_CONSOLE, CsrAllocConsole),
35 CSRSS_DEFINE_API(FREE_CONSOLE, CsrFreeConsole),
36 CSRSS_DEFINE_API(SCREEN_BUFFER_INFO, CsrGetScreenBufferInfo),
37 CSRSS_DEFINE_API(SET_CURSOR, CsrSetCursor),
38 CSRSS_DEFINE_API(FILL_OUTPUT, CsrFillOutputChar),
39 CSRSS_DEFINE_API(READ_INPUT, CsrReadInputEvent),
40 CSRSS_DEFINE_API(WRITE_CONSOLE_OUTPUT_CHAR, CsrWriteConsoleOutputChar),
41 CSRSS_DEFINE_API(WRITE_CONSOLE_OUTPUT_ATTRIB, CsrWriteConsoleOutputAttrib),
42 CSRSS_DEFINE_API(FILL_OUTPUT_ATTRIB, CsrFillOutputAttrib),
43 CSRSS_DEFINE_API(GET_CURSOR_INFO, CsrGetCursorInfo),
44 CSRSS_DEFINE_API(SET_CURSOR_INFO, CsrSetCursorInfo),
45 CSRSS_DEFINE_API(SET_ATTRIB, CsrSetTextAttrib),
46 CSRSS_DEFINE_API(GET_CONSOLE_MODE, CsrGetConsoleMode),
47 CSRSS_DEFINE_API(SET_CONSOLE_MODE, CsrSetConsoleMode),
48 CSRSS_DEFINE_API(CREATE_SCREEN_BUFFER, CsrCreateScreenBuffer),
49 CSRSS_DEFINE_API(SET_SCREEN_BUFFER, CsrSetScreenBuffer),
50 CSRSS_DEFINE_API(SET_TITLE, CsrSetTitle),
51 CSRSS_DEFINE_API(GET_TITLE, CsrGetTitle),
52 CSRSS_DEFINE_API(WRITE_CONSOLE_OUTPUT, CsrWriteConsoleOutput),
53 CSRSS_DEFINE_API(FLUSH_INPUT_BUFFER, CsrFlushInputBuffer),
54 CSRSS_DEFINE_API(SCROLL_CONSOLE_SCREEN_BUFFER, CsrScrollConsoleScreenBuffer),
55 CSRSS_DEFINE_API(READ_CONSOLE_OUTPUT_CHAR, CsrReadConsoleOutputChar),
56 CSRSS_DEFINE_API(READ_CONSOLE_OUTPUT_ATTRIB, CsrReadConsoleOutputAttrib),
57 CSRSS_DEFINE_API(GET_NUM_INPUT_EVENTS, CsrGetNumberOfConsoleInputEvents),
58 CSRSS_DEFINE_API(EXIT_REACTOS, CsrExitReactos),
59 CSRSS_DEFINE_API(PEEK_CONSOLE_INPUT, CsrPeekConsoleInput),
60 CSRSS_DEFINE_API(READ_CONSOLE_OUTPUT, CsrReadConsoleOutput),
61 CSRSS_DEFINE_API(WRITE_CONSOLE_INPUT, CsrWriteConsoleInput),
62 CSRSS_DEFINE_API(SETGET_CONSOLE_HW_STATE, CsrHardwareStateProperty),
63 CSRSS_DEFINE_API(GET_CONSOLE_WINDOW, CsrGetConsoleWindow),
64 CSRSS_DEFINE_API(CREATE_DESKTOP, CsrCreateDesktop),
65 CSRSS_DEFINE_API(SHOW_DESKTOP, CsrShowDesktop),
66 CSRSS_DEFINE_API(HIDE_DESKTOP, CsrHideDesktop),
67 CSRSS_DEFINE_API(SET_CONSOLE_ICON, CsrSetConsoleIcon),
68 CSRSS_DEFINE_API(SET_LOGON_NOTIFY_WINDOW, CsrSetLogonNotifyWindow),
69 CSRSS_DEFINE_API(REGISTER_LOGON_PROCESS, CsrRegisterLogonProcess),
70 CSRSS_DEFINE_API(GET_CONSOLE_CP, CsrGetConsoleCodePage),
71 CSRSS_DEFINE_API(SET_CONSOLE_CP, CsrSetConsoleCodePage),
72 CSRSS_DEFINE_API(GET_CONSOLE_OUTPUT_CP, CsrGetConsoleOutputCodePage),
73 CSRSS_DEFINE_API(SET_CONSOLE_OUTPUT_CP, CsrSetConsoleOutputCodePage),
74 CSRSS_DEFINE_API(GET_PROCESS_LIST, CsrGetProcessList),
75 CSRSS_DEFINE_API(ADD_CONSOLE_ALIAS, CsrAddConsoleAlias),
76 CSRSS_DEFINE_API(GET_CONSOLE_ALIAS, CsrGetConsoleAlias),
77 CSRSS_DEFINE_API(GET_ALL_CONSOLE_ALIASES, CsrGetAllConsoleAliases),
78 CSRSS_DEFINE_API(GET_ALL_CONSOLE_ALIASES_LENGTH, CsrGetAllConsoleAliasesLength),
79 CSRSS_DEFINE_API(GET_CONSOLE_ALIASES_EXES, CsrGetConsoleAliasesExes),
80 CSRSS_DEFINE_API(GET_CONSOLE_ALIASES_EXES_LENGTH, CsrGetConsoleAliasesExesLength),
81 CSRSS_DEFINE_API(GENERATE_CTRL_EVENT, CsrGenerateCtrlEvent),
82 CSRSS_DEFINE_API(SET_SCREEN_BUFFER_SIZE, CsrSetScreenBufferSize),
83 CSRSS_DEFINE_API(GET_CONSOLE_SELECTION_INFO, CsrGetConsoleSelectionInfo),
84 CSRSS_DEFINE_API(GET_COMMAND_HISTORY_LENGTH, CsrGetCommandHistoryLength),
85 CSRSS_DEFINE_API(GET_COMMAND_HISTORY, CsrGetCommandHistory),
86 CSRSS_DEFINE_API(EXPUNGE_COMMAND_HISTORY, CsrExpungeCommandHistory),
87 CSRSS_DEFINE_API(SET_HISTORY_NUMBER_COMMANDS, CsrSetHistoryNumberCommands),
88 CSRSS_DEFINE_API(GET_HISTORY_INFO, CsrGetHistoryInfo),
89 CSRSS_DEFINE_API(SET_HISTORY_INFO, CsrSetHistoryInfo),
90 CSRSS_DEFINE_API(GET_TEMP_FILE, CsrGetTempFile),
91 { 0, 0, NULL }
92 };
93
94 /* FUNCTIONS *****************************************************************/
95
96 BOOL WINAPI
97 DllMain(HANDLE hDll,
98 DWORD dwReason,
99 LPVOID lpReserved)
100 {
101 if (DLL_PROCESS_ATTACH == dwReason)
102 {
103 Win32CsrDllHandle = hDll;
104 InitializeAppSwitchHook();
105 }
106
107 return TRUE;
108 }
109
110 /* Ensure that a captured buffer is safe to access */
111 BOOL FASTCALL
112 Win32CsrValidateBuffer(PCSRSS_PROCESS_DATA ProcessData, PVOID Buffer,
113 SIZE_T NumElements, SIZE_T ElementSize)
114 {
115 /* Check that the following conditions are true:
116 * 1. The start of the buffer is somewhere within the process's
117 * shared memory section view.
118 * 2. The remaining space in the view is at least as large as the buffer.
119 * (NB: Please don't try to "optimize" this by using multiplication
120 * instead of division; remember that 2147483648 * 2 = 0.)
121 * 3. The buffer is DWORD-aligned.
122 */
123 ULONG_PTR Offset = (BYTE *)Buffer - (BYTE *)ProcessData->CsrSectionViewBase;
124 if (Offset >= ProcessData->CsrSectionViewSize
125 || NumElements > (ProcessData->CsrSectionViewSize - Offset) / ElementSize
126 || (Offset & (sizeof(DWORD) - 1)) != 0)
127 {
128 DPRINT1("Invalid buffer %p(%u*%u); section view is %p(%u)\n",
129 Buffer, NumElements, ElementSize,
130 ProcessData->CsrSectionViewBase, ProcessData->CsrSectionViewSize);
131 return FALSE;
132 }
133 return TRUE;
134 }
135
136 NTSTATUS FASTCALL
137 Win32CsrEnumProcesses(CSRSS_ENUM_PROCESS_PROC EnumProc,
138 PVOID Context)
139 {
140 return (CsrExports.CsrEnumProcessesProc)(EnumProc, Context);
141 }
142
143 static BOOL WINAPI
144 Win32CsrInitComplete(void)
145 {
146 return TRUE;
147 }
148
149 VOID
150 WINAPI
151 PrivateCsrssManualGuiCheck(LONG Check)
152 {
153 NtUserCallOneParam(Check, ONEPARAM_ROUTINE_CSRSS_GUICHECK);
154 }
155
156 BOOL WINAPI
157 Win32CsrInitialization(PCSRSS_API_DEFINITION *ApiDefinitions,
158 PCSRPLUGIN_SERVER_PROCS ServerProcs,
159 PCSRSS_EXPORTED_FUNCS Exports,
160 HANDLE CsrssApiHeap)
161 {
162 NTSTATUS Status;
163 CsrExports = *Exports;
164 Win32CsrApiHeap = CsrssApiHeap;
165
166 Status = NtUserInitialize(0, NULL, NULL);
167
168 PrivateCsrssManualGuiCheck(0);
169 CsrInitConsoleSupport();
170
171 *ApiDefinitions = Win32CsrApiDefinitions;
172 ServerProcs->InitCompleteProc = Win32CsrInitComplete;
173 ServerProcs->HardErrorProc = Win32CsrHardError;
174 ServerProcs->ProcessInheritProc = Win32CsrDuplicateHandleTable;
175 ServerProcs->ProcessDeletedProc = Win32CsrReleaseConsole;
176
177 return TRUE;
178 }
179
180 /* EOF */