Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
11 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. csrsrv.h
  5. Abstract:
  6. Main include file for Server side of the Client Server Runtime (CSR)
  7. Author:
  8. Steve Wood (stevewo) 8-Oct-1990
  9. Revision History:
  10. --*/
  11. //
  12. // Include definitions common between the Client and Server portions.
  13. //
  14. #include <csr.h>
  15. //
  16. // Include definitions specific to the Server portion.
  17. //
  18. #include <ntcsrsrv.h>
  19. #define NOEXTAPI
  20. #include <wdbgexts.h>
  21. #include <ntdbg.h>
  22. //
  23. // Define debugging flags and macro for testing them. All debug code
  24. // should be contained within a IF_CSR_DEBUG macro call so that when
  25. // the system is compiled with debug code disabled, none of the code
  26. // is generated.
  27. //
  28. #if DBG
  29. #define CSR_DEBUG_INIT 0x00000001
  30. #define CSR_DEBUG_LPC 0x00000002
  31. #define CSR_DEBUG_FLAG3 0x00000004
  32. #define CSR_DEBUG_FLAG4 0x00000008
  33. #define CSR_DEBUG_FLAG5 0x00000010
  34. #define CSR_DEBUG_FLAG6 0x00000020
  35. #define CSR_DEBUG_FLAG7 0x00000040
  36. #define CSR_DEBUG_FLAG8 0x00000080
  37. #define CSR_DEBUG_FLAG9 0x00000100
  38. #define CSR_DEBUG_FLAG10 0x00000200
  39. #define CSR_DEBUG_FLAG11 0x00000400
  40. #define CSR_DEBUG_FLAG12 0x00000800
  41. #define CSR_DEBUG_FLAG13 0x00001000
  42. #define CSR_DEBUG_FLAG14 0x00002000
  43. #define CSR_DEBUG_FLAG15 0x00004000
  44. #define CSR_DEBUG_FLAG16 0x00008000
  45. #define CSR_DEBUG_FLAG17 0x00010000
  46. #define CSR_DEBUG_FLAG18 0x00020000
  47. #define CSR_DEBUG_FLAG19 0x00040000
  48. #define CSR_DEBUG_FLAG20 0x00080000
  49. #define CSR_DEBUG_FLAG21 0x00100000
  50. #define CSR_DEBUG_FLAG22 0x00200000
  51. #define CSR_DEBUG_FLAG23 0x00400000
  52. #define CSR_DEBUG_FLAG24 0x00800000
  53. #define CSR_DEBUG_FLAG25 0x01000000
  54. #define CSR_DEBUG_FLAG26 0x02000000
  55. #define CSR_DEBUG_FLAG27 0x04000000
  56. #define CSR_DEBUG_FLAG28 0x08000000
  57. #define CSR_DEBUG_FLAG29 0x10000000
  58. #define CSR_DEBUG_FLAG30 0x20000000
  59. #define CSR_DEBUG_FLAG31 0x40000000
  60. #define CSR_DEBUG_FLAG32 0x80000000
  61. ULONG CsrDebug;
  62. #define IF_CSR_DEBUG( ComponentFlag ) \
  63. if (CsrDebug & (CSR_DEBUG_ ## ComponentFlag))
  64. #define SafeBreakPoint() \
  65. if (NtCurrentPeb()->BeingDebugged) { \
  66. DbgBreakPoint(); \
  67. }
  68. #else
  69. #define IF_CSR_DEBUG( ComponentFlag ) if (FALSE)
  70. #define SafeBreakPoint()
  71. #endif
  72. #if DBG
  73. #define CSRSS_PROTECT_HANDLES 1
  74. BOOLEAN
  75. ProtectHandle(
  76. HANDLE hObject
  77. );
  78. BOOLEAN
  79. UnProtectHandle(
  80. HANDLE hObject
  81. );
  82. #else
  83. #define CSRSS_PROTECT_HANDLES 0
  84. #define ProtectHandle( hObject )
  85. #define UnProtectHandle( hObject )
  86. #endif
  87. //
  88. // Include NT Session Manager and Debug SubSystem Interfaces
  89. //
  90. #include <ntsm.h>
  91. typedef BOOLEAN (*PSB_API_ROUTINE)( IN PSBAPIMSG SbApiMsg );
  92. //
  93. // Global data accessed by Client-Server Runtime Server
  94. //
  95. PVOID CsrHeap;
  96. HANDLE CsrObjectDirectory;
  97. #define CSR_SBAPI_PORT_NAME L"SbApiPort"
  98. UNICODE_STRING CsrDirectoryName;
  99. UNICODE_STRING CsrApiPortName;
  100. UNICODE_STRING CsrSbApiPortName;
  101. HANDLE CsrApiPort;
  102. HANDLE CsrSbApiPort;
  103. HANDLE CsrSmApiPort;
  104. ULONG CsrMaxApiRequestThreads;
  105. #define CSR_MAX_THREADS 16
  106. #define CSR_STATIC_API_THREAD 0x00000010
  107. PCSR_THREAD CsrSbApiRequestThreadPtr;
  108. #define FIRST_SEQUENCE_COUNT 5
  109. //
  110. // Routines defined in srvinit.c
  111. //
  112. //
  113. // Hydra Specific Globals and prototypes
  114. //
  115. #define SESSION_ROOT L"\\Sessions"
  116. #define DOSDEVICES L"\\DosDevices"
  117. #define MAX_SESSION_PATH 256
  118. ULONG SessionId;
  119. HANDLE SessionObjectDirectory;
  120. HANDLE DosDevicesDirectory;
  121. HANDLE BNOLinksDirectory;
  122. HANDLE SessionsObjectDirectory;
  123. NTSTATUS
  124. CsrCreateSessionObjectDirectory( ULONG SessionID );
  125. //
  126. // The CsrNtSysInfo global variable contains NT specific constants of
  127. // interest, such as page size, allocation granularity, etc. It is filled
  128. // in once during process initialization.
  129. //
  130. SYSTEM_BASIC_INFORMATION CsrNtSysInfo;
  131. #define ROUND_UP_TO_PAGES(SIZE) (((ULONG)(SIZE) + CsrNtSysInfo.PageSize - 1) & ~(CsrNtSysInfo.PageSize - 1))
  132. #define ROUND_DOWN_TO_PAGES(SIZE) (((ULONG)(SIZE)) & ~(CsrNtSysInfo.PageSize - 1))
  133. #define QUAD_ALIGN(VALUE) ( ((ULONG_PTR)(VALUE) + 7) & ~7 )
  134. NTSTATUS
  135. CsrParseServerCommandLine(
  136. IN ULONG argc,
  137. IN PCH argv[]
  138. );
  139. NTSTATUS
  140. CsrServerDllInitialization(
  141. IN PCSR_SERVER_DLL LoadedServerDll
  142. );
  143. NTSTATUS
  144. CsrSrvUnusedFunction(
  145. IN OUT PCSR_API_MSG m,
  146. IN OUT PCSR_REPLY_STATUS ReplyStatus
  147. );
  148. NTSTATUS
  149. CsrEnablePrivileges(
  150. VOID
  151. );
  152. //
  153. // Routines define in srvdebug.c
  154. //
  155. #if DBG
  156. #else
  157. #endif // DBG
  158. //
  159. // Routines defined in sbinit.c
  160. //
  161. NTSTATUS
  162. CsrSbApiPortInitialize( VOID );
  163. VOID
  164. CsrSbApiPortTerminate(
  165. NTSTATUS Status
  166. );
  167. //
  168. // Routines defined in sbreqst.c
  169. //
  170. NTSTATUS
  171. CsrSbApiRequestThread(
  172. IN PVOID Parameter
  173. );
  174. //
  175. // Routines defined in sbapi.c
  176. //
  177. BOOLEAN
  178. CsrSbCreateSession(
  179. IN PSBAPIMSG Msg
  180. );
  181. BOOLEAN
  182. CsrSbTerminateSession(
  183. IN PSBAPIMSG Msg
  184. );
  185. BOOLEAN
  186. CsrSbForeignSessionComplete(
  187. IN PSBAPIMSG Msg
  188. );
  189. //
  190. // Routines defined in session.c
  191. //
  192. RTL_CRITICAL_SECTION CsrNtSessionLock;
  193. LIST_ENTRY CsrNtSessionList;
  194. #define LockNtSessionList() RtlEnterCriticalSection( &CsrNtSessionLock )
  195. #define UnlockNtSessionList() RtlLeaveCriticalSection( &CsrNtSessionLock )
  196. NTSTATUS
  197. CsrInitializeNtSessionList( VOID );
  198. PCSR_NT_SESSION
  199. CsrAllocateNtSession(
  200. ULONG SessionId
  201. );
  202. VOID
  203. CsrReferenceNtSession(
  204. PCSR_NT_SESSION Session
  205. );
  206. VOID
  207. CsrDereferenceNtSession(
  208. PCSR_NT_SESSION Session,
  209. NTSTATUS ExitStatus
  210. );
  211. //
  212. // Routines defined in apiinit.c
  213. //
  214. NTSTATUS
  215. CsrApiPortInitialize( VOID );
  216. //
  217. // Routines defined in apireqst.c
  218. //
  219. NTSTATUS
  220. CsrApiRequestThread(
  221. IN PVOID Parameter
  222. );
  223. BOOLEAN
  224. CsrCaptureArguments(
  225. IN PCSR_THREAD t,
  226. IN PCSR_API_MSG m
  227. );
  228. VOID
  229. CsrReleaseCapturedArguments(
  230. IN PCSR_API_MSG m
  231. );
  232. ULONG
  233. CsrSrvNullApiCall(
  234. IN OUT PCSR_API_MSG m,
  235. IN OUT PCSR_REPLY_STATUS ReplyStatus
  236. );
  237. //
  238. // Routines and data defined in srvloadr.c
  239. //
  240. #define CSR_MAX_SERVER_DLL 4
  241. PCSR_SERVER_DLL CsrLoadedServerDll[ CSR_MAX_SERVER_DLL ];
  242. ULONG CsrTotalPerProcessDataLength;
  243. HANDLE CsrSrvSharedSection;
  244. ULONG CsrSrvSharedSectionSize;
  245. PVOID CsrSrvSharedSectionBase;
  246. PVOID CsrSrvSharedSectionHeap;
  247. PVOID *CsrSrvSharedStaticServerData;
  248. #define CSR_BASE_PATH L"\\REGISTRY\\MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Subsystems\\CSRSS"
  249. #define IsTerminalServer() (BOOLEAN)(USER_SHARED_DATA->SuiteMask & (1 << TerminalServer))
  250. NTSTATUS
  251. CsrLoadServerDll(
  252. IN PCH ModuleName,
  253. IN PCH InitRoutineString,
  254. IN ULONG ServerDllIndex
  255. );
  256. ULONG
  257. CsrSrvClientConnect(
  258. IN OUT PCSR_API_MSG m,
  259. IN OUT PCSR_REPLY_STATUS ReplyStatus
  260. );
  261. NTSTATUS
  262. CsrSrvCreateSharedSection(
  263. IN PCH SizeParameter
  264. );
  265. NTSTATUS
  266. CsrSrvAttachSharedSection(
  267. IN PCSR_PROCESS Process OPTIONAL,
  268. OUT PCSR_API_CONNECTINFO p
  269. );
  270. //
  271. // Routines and data defined in process.c
  272. //
  273. //
  274. // The CsrProcessStructureLock critical section protects all of the link
  275. // fields of the Windows Process objects. You must own this lock to examine
  276. // or modify any of the following fields of the CSR_PROCESS structure:
  277. //
  278. // ListLink
  279. //
  280. // It also protects the following variables:
  281. //
  282. // CsrRootProcess
  283. //
  284. RTL_CRITICAL_SECTION CsrProcessStructureLock;
  285. #define AcquireProcessStructureLock() RtlEnterCriticalSection( &CsrProcessStructureLock )
  286. #define ReleaseProcessStructureLock() RtlLeaveCriticalSection( &CsrProcessStructureLock )
  287. #define ProcessStructureListLocked() \
  288. (CsrProcessStructureLock.OwningThread == NtCurrentTeb()->ClientId.UniqueThread)
  289. //
  290. // The following is a dummy process that acts as the root of the Windows Process
  291. // Structure. It has a ClientId of -1.-1 so it does not conflict with actual
  292. // Windows Processes. All processes created via the session manager are children
  293. // of this process, as are all orphaned processes. The ListLink field of this
  294. // process is the head of a list of all Windows Processes.
  295. //
  296. PCSR_PROCESS CsrRootProcess;
  297. //
  298. // reference/dereference thread are public in ntcsrsrv.h
  299. //
  300. VOID
  301. CsrLockedReferenceProcess(
  302. PCSR_PROCESS p
  303. );
  304. VOID
  305. CsrLockedReferenceThread(
  306. PCSR_THREAD t
  307. );
  308. VOID
  309. CsrLockedDereferenceProcess(
  310. PCSR_PROCESS p
  311. );
  312. VOID
  313. CsrLockedDereferenceThread(
  314. PCSR_THREAD t
  315. );
  316. NTSTATUS
  317. CsrInitializeProcessStructure( VOID );
  318. PCSR_PROCESS
  319. CsrAllocateProcess( VOID );
  320. VOID
  321. CsrDeallocateProcess(
  322. IN PCSR_PROCESS Process
  323. );
  324. VOID
  325. CsrInsertProcess(
  326. IN PCSR_PROCESS CallingProcess,
  327. IN PCSR_PROCESS Process
  328. );
  329. VOID
  330. CsrRemoveProcess(
  331. IN PCSR_PROCESS Process
  332. );
  333. PCSR_THREAD
  334. CsrAllocateThread(
  335. IN PCSR_PROCESS Process
  336. );
  337. VOID
  338. CsrDeallocateThread(
  339. IN PCSR_THREAD Thread
  340. );
  341. VOID
  342. CsrInsertThread(
  343. IN PCSR_PROCESS Process,
  344. IN PCSR_THREAD Thread
  345. );
  346. VOID
  347. CsrRemoveThread(
  348. IN PCSR_THREAD Thread
  349. );
  350. PCSR_THREAD
  351. CsrLocateThreadByClientId(
  352. OUT PCSR_PROCESS *Process,
  353. IN PCLIENT_ID ClientId
  354. );
  355. PCSR_THREAD
  356. CsrLocateServerThread(
  357. IN PCLIENT_ID ClientId);
  358. //
  359. // Routines and data defined in csrdebug.c
  360. //
  361. VOID
  362. CsrSuspendProcess(
  363. IN PCSR_PROCESS Process
  364. );
  365. VOID
  366. CsrResumeProcess(
  367. IN PCSR_PROCESS Process
  368. );
  369. //
  370. // Routines and data defined in wait.c
  371. //
  372. #define AcquireWaitListsLock() RtlEnterCriticalSection( &CsrWaitListsLock )
  373. #define ReleaseWaitListsLock() RtlLeaveCriticalSection( &CsrWaitListsLock )
  374. RTL_CRITICAL_SECTION CsrWaitListsLock;
  375. BOOLEAN
  376. CsrInitializeWait(
  377. IN CSR_WAIT_ROUTINE WaitRoutine,
  378. IN PCSR_THREAD WaitingThread,
  379. IN OUT PCSR_API_MSG WaitReplyMessage,
  380. IN PVOID WaitParameter,
  381. OUT PCSR_WAIT_BLOCK *WaitBlockPtr
  382. );
  383. BOOLEAN
  384. CsrNotifyWaitBlock(
  385. IN PCSR_WAIT_BLOCK WaitBlock,
  386. IN PLIST_ENTRY WaitQueue,
  387. IN PVOID SatisfyParameter1,
  388. IN PVOID SatisfyParameter2,
  389. IN ULONG WaitFlags,
  390. IN BOOLEAN DereferenceThread
  391. );
  392. ULONG CsrBaseTag;
  393. ULONG CsrSharedBaseTag;
  394. #define MAKE_TAG( t ) (RTL_HEAP_MAKE_TAG( CsrBaseTag, t ))
  395. #define TMP_TAG 0
  396. #define INIT_TAG 1
  397. #define CAPTURE_TAG 2
  398. #define PROCESS_TAG 3
  399. #define THREAD_TAG 4
  400. #define SECURITY_TAG 5
  401. #define SESSION_TAG 6
  402. #define WAIT_TAG 7
  403. #define MAKE_SHARED_TAG( t ) (RTL_HEAP_MAKE_TAG( CsrSharedBaseTag, t ))
  404. #define SHR_INIT_TAG 0
  405. //
  406. // Routines and data defined in process.c
  407. //
  408. BOOLEAN
  409. CsrSbCreateProcess(
  410. IN OUT PSBAPIMSG m
  411. );
  412. #if DBG
  413. typedef struct _LPC_TRACK_NODE {
  414. PORT_MESSAGE Message;
  415. NTSTATUS Status;
  416. CLIENT_ID ClientCid;
  417. CLIENT_ID ServerCid;
  418. USHORT MessageType;
  419. } LPC_TRACK_NODE, *PLPC_TRACK_NODE;
  420. RTL_CRITICAL_SECTION CsrTrackLpcLock;
  421. ULONG LpcTrackIndex;
  422. LPC_TRACK_NODE LpcTrackNodes[4096];
  423. #endif
  424. #ifndef ARRAY_SIZE
  425. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
  426. #endif