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.

630 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. pshelper.c
  5. Abstract:
  6. EPROCESS and ETHREAD field access for NTOS-external components
  7. Author:
  8. Gerardo Bermudez (gerardob) 10-Aug-1999
  9. Revision History:
  10. 5-Jan-03 msadek - added PsGetProcessSessionIdEx()
  11. --*/
  12. #include "psp.h"
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text (PAGE, PsIsProcessBeingDebugged)
  15. #pragma alloc_text (PAGE, PsIsThreadImpersonating)
  16. #pragma alloc_text (PAGE, PsReferenceProcessFilePointer)
  17. #pragma alloc_text (PAGE, PsSetProcessWin32Process)
  18. #pragma alloc_text (PAGE, PsSetProcessSecurityPort)
  19. #pragma alloc_text (PAGE, PsSetJobUIRestrictionsClass)
  20. #pragma alloc_text (PAGE, PsSetProcessWindowStation)
  21. #pragma alloc_text (PAGE, PsGetProcessSecurityPort)
  22. #pragma alloc_text (PAGE, PsSetThreadWin32Thread)
  23. #pragma alloc_text (PAGE, PsGetProcessExitProcessCalled)
  24. #pragma alloc_text (PAGE, PsGetThreadSessionId)
  25. #pragma alloc_text (PAGE, PsSetProcessPriorityClass)
  26. #endif
  27. /*++
  28. --*/
  29. #undef PsGetCurrentProcess
  30. PEPROCESS
  31. PsGetCurrentProcess(
  32. VOID
  33. )
  34. {
  35. return _PsGetCurrentProcess();
  36. }
  37. /*++
  38. --*/
  39. ULONG PsGetCurrentProcessSessionId(
  40. VOID
  41. )
  42. {
  43. return MmGetSessionId (_PsGetCurrentProcess());
  44. }
  45. /*++
  46. --*/
  47. #undef PsGetCurrentThread
  48. PETHREAD
  49. PsGetCurrentThread(
  50. VOID
  51. )
  52. {
  53. return _PsGetCurrentThread();
  54. }
  55. /*++
  56. --*/
  57. PVOID
  58. PsGetCurrentThreadStackBase(
  59. VOID
  60. )
  61. {
  62. return KeGetCurrentThread()->StackBase;
  63. }
  64. /*++
  65. --*/
  66. PVOID
  67. PsGetCurrentThreadStackLimit(
  68. VOID
  69. )
  70. {
  71. return KeGetCurrentThread()->StackLimit;
  72. }
  73. /*++
  74. --*/
  75. CCHAR
  76. PsGetCurrentThreadPreviousMode(
  77. VOID
  78. )
  79. {
  80. return KeGetPreviousMode();
  81. }
  82. /*++
  83. --*/
  84. PERESOURCE
  85. PsGetJobLock(
  86. PEJOB Job
  87. )
  88. {
  89. return &Job->JobLock;
  90. }
  91. /*++
  92. --*/
  93. ULONG
  94. PsGetJobSessionId(
  95. PEJOB Job
  96. )
  97. {
  98. return Job->SessionId;
  99. }
  100. /*++
  101. --*/
  102. ULONG
  103. PsGetJobUIRestrictionsClass(
  104. PEJOB Job
  105. )
  106. {
  107. return Job->UIRestrictionsClass;
  108. }
  109. /*++
  110. --*/
  111. LONGLONG
  112. PsGetProcessCreateTimeQuadPart(
  113. PEPROCESS Process
  114. )
  115. {
  116. return Process->CreateTime.QuadPart;
  117. }
  118. /*++
  119. --*/
  120. PVOID
  121. PsGetProcessDebugPort(
  122. PEPROCESS Process
  123. )
  124. {
  125. return Process->DebugPort;
  126. }
  127. BOOLEAN
  128. PsIsProcessBeingDebugged(
  129. PEPROCESS Process
  130. )
  131. {
  132. if (Process->DebugPort != NULL) {
  133. return TRUE;
  134. } else {
  135. return FALSE;
  136. }
  137. }
  138. /*++
  139. --*/
  140. BOOLEAN
  141. PsGetProcessExitProcessCalled(
  142. PEPROCESS Process
  143. )
  144. {
  145. return (BOOLEAN) ((Process->Flags&PS_PROCESS_FLAGS_PROCESS_EXITING) != 0);
  146. }
  147. /*++
  148. --*/
  149. NTSTATUS
  150. PsGetProcessExitStatus(
  151. PEPROCESS Process
  152. )
  153. {
  154. return Process->ExitStatus;
  155. }
  156. /*++
  157. --*/
  158. HANDLE
  159. PsGetProcessId(
  160. PEPROCESS Process
  161. )
  162. {
  163. return Process->UniqueProcessId;
  164. }
  165. /*++
  166. --*/
  167. UCHAR *
  168. PsGetProcessImageFileName(
  169. PEPROCESS Process
  170. )
  171. {
  172. return Process->ImageFileName;
  173. }
  174. /*++
  175. --*/
  176. HANDLE
  177. PsGetProcessInheritedFromUniqueProcessId(
  178. PEPROCESS Process
  179. )
  180. {
  181. return Process->InheritedFromUniqueProcessId;
  182. }
  183. /*++
  184. --*/
  185. PEJOB
  186. PsGetProcessJob(
  187. PEPROCESS Process
  188. )
  189. {
  190. return Process->Job;
  191. }
  192. /*++
  193. --*/
  194. ULONG
  195. PsGetProcessSessionId(
  196. PEPROCESS Process
  197. )
  198. {
  199. return MmGetSessionId (Process);
  200. }
  201. /*++
  202. --*/
  203. ULONG
  204. PsGetProcessSessionIdEx(
  205. PEPROCESS Process
  206. )
  207. {
  208. return MmGetSessionIdEx (Process);
  209. }
  210. /*++
  211. --*/
  212. PVOID
  213. PsGetProcessSectionBaseAddress(
  214. PEPROCESS Process
  215. )
  216. {
  217. return Process->SectionBaseAddress;
  218. }
  219. /*++
  220. --*/
  221. PPEB
  222. PsGetProcessPeb(
  223. PEPROCESS Process
  224. )
  225. {
  226. return Process->Peb;
  227. }
  228. /*++
  229. --*/
  230. UCHAR
  231. PsGetProcessPriorityClass(
  232. PEPROCESS Process
  233. )
  234. {
  235. return Process->PriorityClass;
  236. }
  237. /*++
  238. --*/
  239. HANDLE
  240. PsGetProcessWin32WindowStation(
  241. PEPROCESS Process
  242. )
  243. {
  244. return Process->Win32WindowStation;
  245. }
  246. /*++
  247. --*/
  248. PVOID
  249. PsGetProcessWin32Process(
  250. PEPROCESS Process
  251. )
  252. {
  253. return Process->Win32Process;
  254. }
  255. /*++
  256. --*/
  257. PVOID
  258. PsGetProcessWow64Process(
  259. PEPROCESS Process
  260. )
  261. {
  262. return PS_GET_WOW64_PROCESS (Process);
  263. }
  264. /*++
  265. --*/
  266. HANDLE
  267. PsGetThreadId(
  268. PETHREAD Thread
  269. )
  270. {
  271. return Thread->Cid.UniqueThread;
  272. }
  273. /*++
  274. --*/
  275. CCHAR
  276. PsGetThreadFreezeCount(
  277. PETHREAD Thread
  278. )
  279. {
  280. return Thread->Tcb.FreezeCount;
  281. }
  282. /*++
  283. --*/
  284. BOOLEAN
  285. PsGetThreadHardErrorsAreDisabled(
  286. PETHREAD Thread)
  287. {
  288. return (BOOLEAN) (Thread->CrossThreadFlags&PS_CROSS_THREAD_FLAGS_HARD_ERRORS_DISABLED) != 0;
  289. }
  290. /*++
  291. --*/
  292. PEPROCESS
  293. PsGetThreadProcess(
  294. PETHREAD Thread
  295. )
  296. {
  297. return THREAD_TO_PROCESS(Thread);
  298. }
  299. /*++
  300. --*/
  301. HANDLE
  302. PsGetThreadProcessId(
  303. PETHREAD Thread
  304. )
  305. {
  306. return Thread->Cid.UniqueProcess;
  307. }
  308. /*++
  309. --*/
  310. ULONG
  311. PsGetThreadSessionId(
  312. PETHREAD Thread
  313. )
  314. {
  315. return MmGetSessionId (THREAD_TO_PROCESS(Thread));
  316. }
  317. /*++
  318. --*/
  319. PVOID
  320. PsGetThreadTeb(
  321. PETHREAD Thread
  322. )
  323. {
  324. return Thread->Tcb.Teb;
  325. }
  326. /*++
  327. --*/
  328. PVOID
  329. PsGetThreadWin32Thread(
  330. PETHREAD Thread
  331. )
  332. {
  333. return Thread->Tcb.Win32Thread;
  334. }
  335. /*++
  336. --*/
  337. BOOLEAN
  338. PsIsSystemThread(
  339. PETHREAD Thread
  340. )
  341. {
  342. return (BOOLEAN)(IS_SYSTEM_THREAD(Thread));
  343. }
  344. /*++
  345. --*/
  346. VOID
  347. PsSetJobUIRestrictionsClass(
  348. PEJOB Job,
  349. ULONG UIRestrictionsClass
  350. )
  351. {
  352. Job->UIRestrictionsClass = UIRestrictionsClass;
  353. }
  354. /*++
  355. --*/
  356. VOID
  357. PsSetProcessPriorityClass(
  358. PEPROCESS Process,
  359. UCHAR PriorityClass
  360. )
  361. {
  362. Process->PriorityClass = PriorityClass;
  363. }
  364. /*++
  365. --*/
  366. NTSTATUS
  367. PsSetProcessWin32Process(
  368. PEPROCESS Process,
  369. PVOID Win32Process,
  370. PVOID PrevWin32Process
  371. )
  372. {
  373. NTSTATUS Status;
  374. PETHREAD CurrentThread;
  375. Status = STATUS_SUCCESS;
  376. CurrentThread = PsGetCurrentThread ();
  377. PspLockProcessExclusive (Process, CurrentThread);
  378. if (Win32Process != NULL) {
  379. if ((Process->Flags&PS_PROCESS_FLAGS_PROCESS_DELETE) == 0 && Process->Win32Process == NULL) {
  380. Process->Win32Process = Win32Process;
  381. } else {
  382. Status = STATUS_PROCESS_IS_TERMINATING;
  383. }
  384. } else {
  385. if (Process->Win32Process == PrevWin32Process) {
  386. Process->Win32Process = NULL;
  387. } else {
  388. Status = STATUS_UNSUCCESSFUL;
  389. }
  390. }
  391. PspUnlockProcessExclusive (Process, CurrentThread);
  392. return Status;
  393. }
  394. /*++
  395. --*/
  396. VOID
  397. PsSetProcessWindowStation(
  398. PEPROCESS Process,
  399. HANDLE Win32WindowStation
  400. )
  401. {
  402. Process->Win32WindowStation = Win32WindowStation;
  403. }
  404. /*++
  405. --*/
  406. VOID
  407. PsSetThreadHardErrorsAreDisabled(
  408. PETHREAD Thread,
  409. BOOLEAN HardErrorsAreDisabled
  410. )
  411. {
  412. if (HardErrorsAreDisabled) {
  413. PS_SET_BITS (&Thread->CrossThreadFlags, PS_CROSS_THREAD_FLAGS_HARD_ERRORS_DISABLED);
  414. } else {
  415. PS_CLEAR_BITS (&Thread->CrossThreadFlags, PS_CROSS_THREAD_FLAGS_HARD_ERRORS_DISABLED);
  416. }
  417. }
  418. /*++
  419. --*/
  420. VOID
  421. PsSetThreadWin32Thread(
  422. PETHREAD Thread,
  423. PVOID Win32Thread,
  424. PVOID PrevWin32Thread
  425. )
  426. {
  427. if (Win32Thread != NULL) {
  428. InterlockedExchangePointer(&Thread->Tcb.Win32Thread, Win32Thread);
  429. } else {
  430. InterlockedCompareExchangePointer(&Thread->Tcb.Win32Thread, Win32Thread, PrevWin32Thread);
  431. }
  432. }
  433. /*++
  434. --*/
  435. PVOID
  436. PsGetProcessSecurityPort(
  437. PEPROCESS Process
  438. )
  439. {
  440. return Process->SecurityPort ;
  441. }
  442. /*++
  443. --*/
  444. NTSTATUS
  445. PsSetProcessSecurityPort(
  446. PEPROCESS Process,
  447. PVOID Port
  448. )
  449. {
  450. Process->SecurityPort = Port ;
  451. return STATUS_SUCCESS ;
  452. }
  453. BOOLEAN
  454. PsIsThreadImpersonating (
  455. IN PETHREAD Thread
  456. )
  457. /*++
  458. Routine Description:
  459. This routine returns TRUE if the specified thread is impersonating otherwise it returns false.
  460. Arguments:
  461. Thread - Thread to be queried
  462. Return Value:
  463. BOOLEAN - TRUE: Thread is impersonating, FALSE: Thread is not impersonating.
  464. --*/
  465. {
  466. PAGED_CODE ();
  467. return (BOOLEAN) (PS_IS_THREAD_IMPERSONATING (Thread));
  468. }
  469. NTSTATUS
  470. PsReferenceProcessFilePointer (
  471. IN PEPROCESS Process,
  472. OUT PVOID *OutFileObject
  473. )
  474. /*++
  475. Routine Description:
  476. This routine returns a referenced pointer to the FilePointer of Process.
  477. This is a rundown protected wrapper around MmGetFileObjectForSection.
  478. Arguments:
  479. Process - Supplies the process to query.
  480. OutFileObject - Returns the file object backing the requested section if
  481. success is returned.
  482. Return Value:
  483. NTSTATUS.
  484. Environment:
  485. Kernel mode, PASSIVE_LEVEL.
  486. --*/
  487. {
  488. PFILE_OBJECT FileObject;
  489. PAGED_CODE();
  490. if (!ExAcquireRundownProtection (&Process->RundownProtect)) {
  491. return STATUS_UNSUCCESSFUL;
  492. }
  493. if (Process->SectionObject == NULL) {
  494. ExReleaseRundownProtection (&Process->RundownProtect);
  495. return STATUS_UNSUCCESSFUL;
  496. }
  497. FileObject = MmGetFileObjectForSection ((PVOID)Process->SectionObject);
  498. *OutFileObject = FileObject;
  499. ObReferenceObject (FileObject);
  500. ExReleaseRundownProtection (&Process->RundownProtect);
  501. return STATUS_SUCCESS;
  502. }