Source code of Windows XP (NT5)
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.

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