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.

269 lines
9.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: runobj.hxx
  7. //
  8. // Contents: CRun class definition.
  9. //
  10. // Classes: CRun
  11. //
  12. // Functions: None.
  13. //
  14. // History: 08-Sep-95 EricB Created.
  15. // 01-Dec-95 MarkBl Split from util.hxx.
  16. //
  17. //----------------------------------------------------------------------------
  18. #ifndef __RUNOBJ_HXX__
  19. #define __RUNOBJ_HXX__
  20. //#include "time.hxx"
  21. #undef SetFlag
  22. #undef ClearFlag
  23. #undef IsFlagSet
  24. //
  25. // Run processing flags: values 0x00010000 through 0x80000000 for run state:
  26. //
  27. #define RUN_STATUS_RUNNING 0x00010000
  28. #define RUN_STATUS_FINISHED 0x00020000
  29. #define RUN_STATUS_ABORTED 0x00040000
  30. #define RUN_STATUS_TIMED_OUT 0x00080000
  31. #define RUN_STATUS_CLOSE_PENDING 0x00100000
  32. #define RUN_STATUS_CLOSED 0x00200000 // (This is set, but not used yet)
  33. #define RUN_STATUS_RESTART_ON_IDLE_RESUME 0x00400000
  34. //+----------------------------------------------------------------------------
  35. //
  36. // Class: CRun
  37. //
  38. // Synopsis: Lightweight job object. A moniker of sorts to the real thing.
  39. // Represents one run instance of a job.
  40. //
  41. // CODEWORK: (AnirudhS 11/7/96) The inheritance relationship should be:
  42. // class CRun : protected CDLink (instead of public CDLink)
  43. // class CRunList : private CRun (instead of having a private CRun member)
  44. // This would assure us that only CRunList could call CRun's CDLink methods.
  45. //
  46. //-----------------------------------------------------------------------------
  47. class CRun : public CDLink
  48. {
  49. public:
  50. CRun(LPFILETIME pft, LPFILETIME pftDeadline, FILETIME ftKill,
  51. DWORD MaxRunTime, DWORD rgFlags, WORD wIdleWait,
  52. BOOL fKeepAfterRunning);
  53. CRun(DWORD MaxRunTime, DWORD rgFlags, FILETIME ftDeadline,
  54. BOOL fKeepAfterRunning);
  55. CRun(DWORD MaxRunTime, DWORD rgFlags, WORD wIdleWait,
  56. FILETIME ftDeadline, BOOL fKeepAfterRunning);
  57. CRun(CRun * pRun);
  58. CRun(void);
  59. ~CRun();
  60. HRESULT Initialize(LPCTSTR ptszName) {
  61. return(this->SetName(ptszName));
  62. }
  63. void GetSysTime(LPSYSTEMTIME pst) {FileTimeToSystemTime(&m_ft, pst);}
  64. void GetTime(LPFILETIME pft) {*pft = m_ft;}
  65. LPFILETIME GetTime(void) {return &m_ft;}
  66. LPFILETIME GetDeadline(void) {return &m_ftDeadline;}
  67. void RelaxDeadline(FILETIME ftDeadline)
  68. { if (CompareFileTime(&ftDeadline, &m_ftDeadline) > 0)
  69. m_ftDeadline = ftDeadline; }
  70. WORD GetWait(void) {return m_wIdleWait;}
  71. void ReduceWaitTo(WORD wIdleWait) { m_wIdleWait = min(m_wIdleWait, wIdleWait);}
  72. FILETIME GetKillTime() { return m_ftKill; }
  73. void AdvanceKillTime(FILETIME ftKill)
  74. { if (CompareFileTime(&ftKill, &m_ftKill) < 0)
  75. m_ftKill = ftKill; }
  76. void AdjustKillTimeByMaxRunTime(FILETIME ftNow);
  77. void SetMaxRunTime(DWORD dwMaxRunTime) {m_dwMaxRunTime = dwMaxRunTime;}
  78. DWORD GetMaxRunTime(void) {return(m_dwMaxRunTime);}
  79. CRun * Next(void) { return (CRun *)CDLink::Next(); }
  80. CRun * Prev(void) { return (CRun *)CDLink::Prev(); }
  81. BOOL IsNull(void) {return(m_ft.dwLowDateTime == 0 &&
  82. m_ft.dwHighDateTime == 0);};
  83. HRESULT SetName(LPCTSTR ptszName);
  84. LPTSTR GetName(void) { return m_ptszJobName; }
  85. void SetHandle(HANDLE hJob) {
  86. m_hJob = hJob;
  87. this->SetFlag(RUN_STATUS_RUNNING);
  88. }
  89. HANDLE GetHandle(void) const { return(m_hJob); }
  90. // this function takes ownership of the handles
  91. // will release / close them on destruction
  92. // bCloseUserHandle == FALSE will prevent dtor from closing handle
  93. void SetProfileHandles(HANDLE hUser, HANDLE hProfile, BOOL bCloseUserHandle)
  94. {
  95. m_hUserToken = hUser;
  96. m_hProfile = hProfile;
  97. m_bCloseUserHandle = bCloseUserHandle;
  98. }
  99. HRESULT SetDesktop( LPCTSTR ptszDesktop );
  100. LPTSTR GetDesktop( void ) {
  101. return m_ptszDesktop;
  102. }
  103. HRESULT SetStation( LPCTSTR ptszStation );
  104. LPTSTR GetStation( void ) {
  105. return m_ptszStation;
  106. }
  107. void SetFlag(DWORD rgFlag) { m_rgFlags |= rgFlag; }
  108. void ClearFlag(DWORD rgFlag) { m_rgFlags &= ~rgFlag; }
  109. DWORD GetFlags(void) { return(m_rgFlags); }
  110. BOOL IsFlagSet(DWORD rgFlag) {return (m_rgFlags & rgFlag);}
  111. BOOL IsIdleTriggered() { return m_fKeepInList; }
  112. void SetProcessId(DWORD dwProcessId) { m_dwProcessId = dwProcessId; }
  113. DWORD GetProcessId(void) { return(m_dwProcessId); }
  114. private:
  115. FILETIME m_ft; // When this instance is to run
  116. FILETIME m_ftDeadline; // Latest time when this instance may be started
  117. FILETIME m_ftKill; // Time by which this instance must terminate
  118. // (see the detailed comment on this field in
  119. // procssr.cxx)
  120. LPTSTR m_ptszJobName; // Job name.
  121. HANDLE m_hJob; // The process handle when run
  122. DWORD m_dwProcessId; // The process ID when run
  123. HANDLE m_hUserToken; // User token
  124. HANDLE m_hProfile; // User profile
  125. LPTSTR m_ptszDesktop; //Windows Desktop
  126. LPTSTR m_ptszStation; //WindowsStation
  127. DWORD m_rgFlags; // Job Flags
  128. DWORD m_dwMaxRunTime; // The remaining time to wait for the job to
  129. // terminate (see the detailed comment on this
  130. // field in procssr.cxx)
  131. WORD m_wIdleWait; // The idle wait to determine when to run
  132. // idle tasks.
  133. BOOL m_fKeepInList; // Whether to keep this run in the idle list
  134. // after starting it (set for jobs with idle
  135. // triggers)
  136. BOOL m_bCloseUserHandle; // if TRUE, m_hUserToken is ClosedHandle'd in dtor
  137. public:
  138. BOOL m_fStarted; // Whether the run was started since the last
  139. // idle period began
  140. };
  141. //+----------------------------------------------------------------------------
  142. //
  143. // Run object list class
  144. //
  145. //-----------------------------------------------------------------------------
  146. class CRunList
  147. {
  148. public:
  149. CRunList();
  150. ~CRunList();
  151. void FreeList(void);
  152. CRun * GetFirstJob(void) {return m_RunHead.Next();}
  153. BOOL IsEmpty() {return m_RunHead.Next() == &m_RunHead;}
  154. //
  155. // The following members allow the list to be used as a simple, unsorted,
  156. // (doubly) linked list.
  157. //
  158. void Add(CRun * pRun);
  159. HRESULT AddCopy(CRun * pRun);
  160. protected:
  161. CRun m_RunHead;
  162. };
  163. class CTimeRunList : private CRunList
  164. {
  165. public:
  166. void FreeList(void) {CRunList::FreeList();}
  167. CRun * GetFirstJob(void) {return CRunList::GetFirstJob();}
  168. BOOL IsEmpty() {return CRunList::IsEmpty();}
  169. //
  170. // The following members allow the list to be used as a sorted queue:
  171. //
  172. HRESULT AddSorted(FILETIME ftRun, FILETIME ftDeadline,
  173. FILETIME ftKillTime,
  174. LPTSTR ptszJobName, DWORD dwJobFlags,
  175. DWORD dwMaxRunTime, WORD wIdleWait,
  176. WORD * pCount, WORD cLimit);
  177. CRun * Pop(void);
  178. HRESULT PeekHeadTime(LPFILETIME pft);
  179. HRESULT MakeSysTimeArray(LPSYSTEMTIME *, WORD *);
  180. };
  181. class CIdleRunList : private CRunList
  182. {
  183. public:
  184. void FreeList(void);
  185. CRun * GetFirstJob(void) {return CRunList::GetFirstJob();}
  186. //
  187. // The following members allow the list to be used as a sorted queue,
  188. // sorted on idle wait times. The member at the head of the queue has
  189. // the least idle wait time.
  190. //
  191. void AddSortedByIdleWait(CRun * pRun);
  192. WORD GetFirstWait(void);
  193. void MarkNoneStarted(void);
  194. void FreeExpiredOrRegenerated();
  195. };
  196. //+----------------------------------------------------------------------------
  197. //
  198. // Method: CRunList::CRunList
  199. //
  200. // Synopsis: Constructor
  201. //
  202. //-----------------------------------------------------------------------------
  203. inline CRunList::CRunList(void)
  204. {
  205. //
  206. // Set the head node to point to itself. Note that the head node uses
  207. // the default ctor which sets the FILETIME member to {0, 0}. This null
  208. // FILETIME will be used as a marker for the head of the doubly linked
  209. // list.
  210. //
  211. m_RunHead.SetNext(&m_RunHead);
  212. m_RunHead.SetPrev(&m_RunHead);
  213. }
  214. //+----------------------------------------------------------------------------
  215. //
  216. // Method: CRunList::CRunList
  217. //
  218. // Synopsis: Destructor
  219. //
  220. //-----------------------------------------------------------------------------
  221. inline CRunList::~CRunList(void)
  222. {
  223. FreeList();
  224. }
  225. //+----------------------------------------------------------------------------
  226. //
  227. // Member: CRunList::Add
  228. //
  229. // Synopsis: Add an element to the unsorted list.
  230. //
  231. //-----------------------------------------------------------------------------
  232. inline void
  233. CRunList::Add(CRun * pAdd)
  234. {
  235. pAdd->LinkAfter(&m_RunHead);
  236. }
  237. #endif // __RUNOBJ_HXX__