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.

258 lines
7.8 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. uexec.c
  5. Abstract:
  6. Test program for the NT OS User Mode Runtime Library (URTL)
  7. Author:
  8. Mark Lucovsyt (markl) 14-Jun-1990
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. NTSTATUS
  15. main(
  16. IN ULONG argc,
  17. IN PCH argv[],
  18. IN PCH envp[],
  19. IN ULONG DebugParameter OPTIONAL
  20. )
  21. {
  22. NTSTATUS st;
  23. STRING ImagePathName;
  24. UNICODE_STRING ConfigFilePathname;
  25. RTL_USER_PROCESS_INFORMATION ProcessInformation;
  26. PEB_SM_DATA PebSessionInformation;
  27. HANDLE FileHandle;
  28. ULONG FileIndexNumber;
  29. IO_STATUS_BLOCK IoStatus;
  30. OBJECT_ATTRIBUTES ObjectAttributes;
  31. FILE_INTERNAL_INFORMATION FileInformation;
  32. PPEB Peb;
  33. Peb = NtCurrentPeb();
  34. RtlZeroMemory(&PebSessionInformation,sizeof(PebSessionInformation));
  35. //
  36. // If we started from cli then do all this work to
  37. // pass thru stdin
  38. //
  39. if ( !Peb->Sm.StandardInput.FileHandle ) {
  40. RtlInitUnicodeString(&ConfigFilePathname,L"\\SystemRoot\\nt.cfg");
  41. //
  42. // Open the file
  43. //
  44. InitializeObjectAttributes(
  45. &ObjectAttributes,
  46. &ConfigFilePathname,
  47. OBJ_CASE_INSENSITIVE,
  48. NULL,
  49. NULL
  50. );
  51. st = NtOpenFile(
  52. &FileHandle,
  53. SYNCHRONIZE | FILE_READ_DATA,
  54. &ObjectAttributes,
  55. &IoStatus,
  56. FILE_SHARE_READ,
  57. 0
  58. );
  59. if (!NT_SUCCESS( st )) {
  60. DbgPrint("NtOpenFile: %wZ failed 0x%lx\n",&ConfigFilePathname,st);
  61. ASSERT(NT_SUCCESS(st));
  62. }
  63. //
  64. // get the file serial number
  65. //
  66. st = NtQueryInformationFile(
  67. FileHandle,
  68. &IoStatus,
  69. (PVOID) &FileInformation,
  70. sizeof(FileInformation),
  71. FileInternalInformation
  72. );
  73. if (!NT_SUCCESS( st )) {
  74. DbgPrint("NtQueryInformationFile: %wZ failed 0x%lx\n",&ConfigFilePathname,st);
  75. ASSERT(NT_SUCCESS(st));
  76. }
  77. PebSessionInformation.Length = sizeof(PebSessionInformation);
  78. PebSessionInformation.StandardInput.FileHandle = FileHandle;
  79. PebSessionInformation.StandardInput.Context = (PVOID) FileInformation.IndexNumber;
  80. RtlInitString(&ImagePathName,"\\A:\\uexec2.exe");
  81. st = RtlCreateUserProcess(
  82. &ImagePathName,
  83. NULL,
  84. NULL,
  85. NULL,
  86. FALSE,
  87. NULL,
  88. NULL,
  89. NULL,
  90. &ProcessInformation,
  91. &PebSessionInformation
  92. );
  93. ASSERT(NT_SUCCESS(st));
  94. NtResumeThread(ProcessInformation.Thread,NULL);
  95. NtWaitForSingleObject(ProcessInformation.Process,FALSE,NULL);
  96. NtClose(ProcessInformation.Process);
  97. NtClose(ProcessInformation.Thread);
  98. NtTerminateProcess(NtCurrentProcess(),STATUS_SUCCESS);
  99. } else {
  100. if ( !Peb->Sm.StandardOutput.FileHandle ) {
  101. //
  102. // Started from this program. Stdin is inherited
  103. //
  104. st = NtQueryInformationFile(
  105. Peb->Sm.StandardInput.FileHandle,
  106. &IoStatus,
  107. (PVOID) &FileInformation,
  108. sizeof(FileInformation),
  109. FileInternalInformation
  110. );
  111. if (!NT_SUCCESS( st )) {
  112. DbgPrint("NtQueryInformationFile: failed 0x%lx\n",st);
  113. ASSERT(NT_SUCCESS(st));
  114. }
  115. ASSERT(Peb->Sm.StandardInput.Context == (PVOID) FileInformation.IndexNumber);
  116. PebSessionInformation.Length = sizeof(PebSessionInformation);
  117. PebSessionInformation.StandardInput.FileHandle = (HANDLE)PEB_STDIO_HANDLE_SUBSYS;
  118. PebSessionInformation.StandardOutput.FileHandle = Peb->Sm.StandardInput.FileHandle;
  119. PebSessionInformation.StandardOutput.Context = (PVOID) FileInformation.IndexNumber;
  120. RtlInitString(&ImagePathName,"\\A:\\uexec2.exe");
  121. st = RtlCreateUserProcess(
  122. &ImagePathName,
  123. NULL,
  124. NULL,
  125. NULL,
  126. FALSE,
  127. NULL,
  128. NULL,
  129. NULL,
  130. &ProcessInformation,
  131. &PebSessionInformation
  132. );
  133. ASSERT(NT_SUCCESS(st));
  134. NtResumeThread(ProcessInformation.Thread,NULL);
  135. NtWaitForSingleObject(ProcessInformation.Process,FALSE,NULL);
  136. NtClose(ProcessInformation.Process);
  137. NtClose(ProcessInformation.Thread);
  138. NtTerminateProcess(NtCurrentProcess(),STATUS_SUCCESS);
  139. } else {
  140. ASSERT(Peb->Sm.StandardInput.FileHandle == (HANDLE)PEB_STDIO_HANDLE_SUBSYS);
  141. if ( !Peb->Sm.StandardError.FileHandle ) {
  142. //
  143. // Started by this program with StandardOutput Inherited
  144. //
  145. st = NtQueryInformationFile(
  146. Peb->Sm.StandardOutput.FileHandle,
  147. &IoStatus,
  148. (PVOID) &FileInformation,
  149. sizeof(FileInformation),
  150. FileInternalInformation
  151. );
  152. if (!NT_SUCCESS( st )) {
  153. DbgPrint("NtQueryInformationFile: failed 0x%lx\n",st);
  154. ASSERT(NT_SUCCESS(st));
  155. }
  156. ASSERT(Peb->Sm.StandardOutput.Context == (PVOID) FileInformation.IndexNumber);
  157. PebSessionInformation.Length = sizeof(PebSessionInformation);
  158. PebSessionInformation.StandardInput.FileHandle = (HANDLE)PEB_STDIO_HANDLE_SUBSYS;
  159. PebSessionInformation.StandardOutput.FileHandle = (HANDLE)PEB_STDIO_HANDLE_PM;
  160. PebSessionInformation.StandardError.FileHandle = Peb->Sm.StandardOutput.FileHandle;
  161. PebSessionInformation.StandardError.Context = (PVOID) FileInformation.IndexNumber;
  162. RtlInitString(&ImagePathName,"\\A:\\uexec2.exe");
  163. st = RtlCreateUserProcess(
  164. &ImagePathName,
  165. NULL,
  166. NULL,
  167. NULL,
  168. FALSE,
  169. NULL,
  170. NULL,
  171. NULL,
  172. &ProcessInformation,
  173. &PebSessionInformation
  174. );
  175. ASSERT(NT_SUCCESS(st));
  176. NtResumeThread(ProcessInformation.Thread,NULL);
  177. NtWaitForSingleObject(ProcessInformation.Process,FALSE,NULL);
  178. NtClose(ProcessInformation.Process);
  179. NtClose(ProcessInformation.Thread);
  180. NtTerminateProcess(NtCurrentProcess(),STATUS_SUCCESS);
  181. } else {
  182. ASSERT(Peb->Sm.StandardOutput.FileHandle == (HANDLE)PEB_STDIO_HANDLE_PM);
  183. //
  184. // Started by this program with StandardError Inherited
  185. //
  186. st = NtQueryInformationFile(
  187. Peb->Sm.StandardError.FileHandle,
  188. &IoStatus,
  189. (PVOID) &FileInformation,
  190. sizeof(FileInformation),
  191. FileInternalInformation
  192. );
  193. if (!NT_SUCCESS( st )) {
  194. DbgPrint("NtQueryInformationFile: failed 0x%lx\n",st);
  195. ASSERT(NT_SUCCESS(st));
  196. }
  197. ASSERT(Peb->Sm.StandardError.Context == (PVOID) FileInformation.IndexNumber);
  198. NtTerminateProcess(NtCurrentProcess(),STATUS_SUCCESS);
  199. }
  200. }
  201. }
  202. }