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.

173 lines
5.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 2000.
  5. //
  6. // File: process.cxx
  7. //
  8. // Contents: CProcess class
  9. // CServiceProcess class
  10. //
  11. // History: 07-Jun-94 DwightKr Created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include <pch.cxx>
  15. #pragma hdrstop
  16. #include <proc32.hxx>
  17. #include <cievtmsg.h>
  18. #include <fwevent.hxx>
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Member: CProcess::CProcess
  22. //
  23. // Purpose: Creates a process on the local machine.
  24. //
  25. // Arguments: [wcsImageName] -- name of EXE to run
  26. // [wcsCommandLine] -- command line passed to EXE
  27. // [fCreateSuspended] -- should the EXE be created suspended?
  28. // [pAdviseStatus] -- ICiCAdviseStatus interface, optional.
  29. //
  30. // History: 06-Jun-94 DwightKr Created
  31. //
  32. //--------------------------------------------------------------------------
  33. CProcess::CProcess( const WCHAR *wcsImageName,
  34. const WCHAR *wcsCommandLine,
  35. BOOL fCreateSuspended,
  36. ICiCAdviseStatus * pAdviseStatus )
  37. {
  38. _pAdviseStatus = pAdviseStatus;
  39. WCHAR wcsEXEName[_MAX_PATH + 1];
  40. if ( ExpandEnvironmentStrings( wcsImageName, wcsEXEName, _MAX_PATH ) == 0 )
  41. {
  42. THROW( CException() );
  43. }
  44. STARTUPINFO siDefault;
  45. RtlZeroMemory( &siDefault, sizeof(siDefault) );
  46. DWORD dwCreateFlags = 0;
  47. siDefault.cb = sizeof(STARTUPINFO); // Size of this struct
  48. siDefault.lpTitle = (WCHAR *) wcsImageName; // Default title
  49. dwCreateFlags = DETACHED_PROCESS;
  50. if ( fCreateSuspended )
  51. dwCreateFlags |= CREATE_SUSPENDED;
  52. if ( !CreateProcess( wcsEXEName, // EXE name
  53. (WCHAR *) wcsCommandLine, // Command line
  54. 0, // Proc sec attrib
  55. 0, // Thread sec attrib
  56. FALSE, // Inherit handles
  57. dwCreateFlags, // Creation flags
  58. NULL, // Environment
  59. NULL, // Startup directory
  60. &siDefault, // Startup Info
  61. &_piProcessInfo ) ) // Process handles
  62. {
  63. DWORD dwLastError = GetLastError();
  64. if ( ( ERROR_FILE_NOT_FOUND == dwLastError ) && _pAdviseStatus )
  65. {
  66. CFwEventItem item( EVENTLOG_AUDIT_FAILURE,
  67. MSG_CI_FILE_NOT_FOUND,
  68. 1 );
  69. item.AddArg( wcsEXEName );
  70. item.ReportEvent(*_pAdviseStatus);
  71. }
  72. THROW( CException( HRESULT_FROM_WIN32( dwLastError) ) );
  73. }
  74. //
  75. // Only AddRef() once we get to a point where the constructor can
  76. // no longer fail (and the destructor is guaranteed to be called)
  77. //
  78. if (_pAdviseStatus)
  79. _pAdviseStatus->AddRef();
  80. } //CProcess
  81. //+-------------------------------------------------------------------------
  82. //
  83. // Member: CProcess::~CProcess
  84. //
  85. // Purpose: destructor
  86. //
  87. // History: 06-Jun-94 DwightKr Created
  88. //
  89. //--------------------------------------------------------------------------
  90. CProcess::~CProcess()
  91. {
  92. if ( 0 != _pAdviseStatus )
  93. _pAdviseStatus->Release();
  94. Terminate();
  95. WaitForDeath();
  96. }
  97. //+-------------------------------------------------------------------------
  98. //
  99. // Member: CProcess::Resume, public
  100. //
  101. // Purpose: Continue a suspended thread
  102. //
  103. // History: 06-Jun-94 DwightKr Created
  104. //
  105. //--------------------------------------------------------------------------
  106. void CProcess::Resume()
  107. {
  108. ResumeThread( _piProcessInfo.hThread );
  109. }
  110. //+-------------------------------------------------------------------------
  111. //
  112. // Member: CProcess::WaitForDeath
  113. //
  114. // Purpose: Blocks until the process has been terminated.
  115. //
  116. // History: 06-Jun-94 DwightKr Created
  117. //
  118. //--------------------------------------------------------------------------
  119. void CProcess::WaitForDeath( DWORD dwTimeout )
  120. {
  121. DWORD res = WaitForSingleObject ( _piProcessInfo.hProcess, dwTimeout );
  122. CloseHandle(_piProcessInfo.hThread);
  123. CloseHandle(_piProcessInfo.hProcess);
  124. }
  125. //+-------------------------------------------------------------------------
  126. //
  127. // Member: CProcess::GetProcessToken, private
  128. //
  129. // Purpose: Returns the process token for the process
  130. //
  131. // History: 06-Jun-94 DwightKr Created
  132. //
  133. //--------------------------------------------------------------------------
  134. HANDLE CProcess::GetProcessToken()
  135. {
  136. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION,
  137. FALSE,
  138. _piProcessInfo.dwProcessId );
  139. if ( NULL == hProcess )
  140. {
  141. THROW( CException() );
  142. }
  143. SWin32Handle process(hProcess); // Save in smart pointer
  144. HANDLE hProcessToken = 0;
  145. if ( !OpenProcessToken( hProcess, TOKEN_ADJUST_DEFAULT | TOKEN_QUERY, &hProcessToken ) )
  146. {
  147. THROW( CException() );
  148. }
  149. return hProcessToken;
  150. }