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.

212 lines
4.9 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1998
  4. // All rights reserved
  5. //
  6. // events.cxx
  7. //
  8. //*************************************************************
  9. #include "fdeploy.hxx"
  10. CEvents * gpEvents = 0;
  11. CEvents::CEvents()
  12. {
  13. _hEventLog = NULL;
  14. _pUserSid = NULL;
  15. _Refs = 0;
  16. }
  17. CEvents::~CEvents()
  18. {
  19. if ( _hEventLog )
  20. CloseEventLog( _hEventLog );
  21. if ( _pUserSid )
  22. LocalFree( _pUserSid );
  23. }
  24. DWORD
  25. CEvents::Init()
  26. {
  27. DWORD Size;
  28. DWORD Type;
  29. DWORD Status;
  30. HKEY hKey;
  31. Status = ERROR_SUCCESS;
  32. if ( ! _hEventLog )
  33. _hEventLog = OpenEventLog( NULL, FDEPLOY_EVENT_SOURCE );
  34. if ( ! _hEventLog )
  35. return GetLastError();
  36. return ERROR_SUCCESS;
  37. }
  38. PSID
  39. CEvents::UserSid()
  40. {
  41. GetUserSid();
  42. // The caller does not own this sid and should not attempt to free it
  43. return _pUserSid;
  44. }
  45. void
  46. CEvents::GetUserSid()
  47. {
  48. DWORD Size;
  49. DWORD Status;
  50. HANDLE hToken;
  51. PTOKEN_USER pTokenUserData;
  52. BOOL bStatus;
  53. if ( _pUserSid )
  54. return;
  55. bStatus = OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken );
  56. if ( ! bStatus )
  57. return;
  58. Size = sizeof(TOKEN_USER) + sizeof(SID) + ((SID_MAX_SUB_AUTHORITIES-1) * sizeof(ULONG));
  59. pTokenUserData = (PTOKEN_USER) alloca( Size );
  60. bStatus = GetTokenInformation(
  61. hToken,
  62. TokenUser,
  63. pTokenUserData,
  64. Size,
  65. &Size );
  66. CloseHandle( hToken );
  67. if ( ! bStatus )
  68. return;
  69. Size = GetLengthSid( pTokenUserData->User.Sid );
  70. _pUserSid = (PSID) LocalAlloc( 0, Size );
  71. if ( _pUserSid )
  72. {
  73. bStatus = CopySid( Size, _pUserSid, pTokenUserData->User.Sid );
  74. if ( ! bStatus )
  75. {
  76. LocalFree( _pUserSid );
  77. _pUserSid = NULL;
  78. }
  79. }
  80. }
  81. void
  82. CEvents::Report(
  83. DWORD EventID,
  84. WORD Strings,
  85. ...
  86. )
  87. {
  88. va_list VAList;
  89. WCHAR ** ppwszStrings;
  90. WORD Type;
  91. DWORD DbgMsgLevel;
  92. switch ( EventID >> 30 )
  93. {
  94. case 3:
  95. Type = EVENTLOG_ERROR_TYPE;
  96. DbgMsgLevel = DM_WARNING;
  97. break;
  98. case 2:
  99. Type = EVENTLOG_WARNING_TYPE;
  100. DbgMsgLevel = DM_VERBOSE;
  101. break;
  102. case 1:
  103. case 0:
  104. Type = EVENTLOG_INFORMATION_TYPE;
  105. DbgMsgLevel = DM_VERBOSE;
  106. break;
  107. default:
  108. return;
  109. }
  110. ppwszStrings = 0;
  111. if ( Strings > 0 )
  112. {
  113. ppwszStrings = (WCHAR **) alloca( Strings * sizeof(WCHAR *) );
  114. if ( ! ppwszStrings )
  115. return;
  116. va_start( VAList, Strings );
  117. for ( DWORD n = 0; n < Strings; n++ )
  118. ppwszStrings[n] = va_arg( VAList, WCHAR * );
  119. va_end( VAList );
  120. }
  121. GetUserSid();
  122. (void) ReportEvent(
  123. _hEventLog,
  124. Type,
  125. 0,
  126. EventID,
  127. _pUserSid,
  128. Strings,
  129. 0,
  130. (LPCWCH *) ppwszStrings,
  131. NULL );
  132. //
  133. // Also make sure the event messages get sent to the debugger and log file.
  134. // Kind of hacky method, but makes it so every caller to ::Report doesn't
  135. // have to call _DebugMsg as well.
  136. // However, don't do this for the verbose messages otherwise it will be
  137. // dumped to the debugger twice.
  138. //
  139. if ( EVENT_FDEPLOY_VERBOSE == EventID )
  140. return;
  141. switch ( Strings )
  142. {
  143. case 0 :
  144. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID );
  145. break;
  146. case 1 :
  147. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0] );
  148. break;
  149. case 2 :
  150. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0], ppwszStrings[1] );
  151. break;
  152. case 3 :
  153. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0], ppwszStrings[1], ppwszStrings[2] );
  154. break;
  155. case 4 :
  156. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0], ppwszStrings[1], ppwszStrings[2], ppwszStrings[3] );
  157. break;
  158. case 5 :
  159. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0],
  160. ppwszStrings[1], ppwszStrings[2], ppwszStrings[3],
  161. ppwszStrings[4]);
  162. break;
  163. case 6 :
  164. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0],
  165. ppwszStrings[1], ppwszStrings[2], ppwszStrings[3],
  166. ppwszStrings[4], ppwszStrings[5]);
  167. break;
  168. case 7 :
  169. _DebugMsg( DbgMsgLevel | DM_NO_EVENTLOG, EventID, ppwszStrings[0],
  170. ppwszStrings[1], ppwszStrings[2], ppwszStrings[3],
  171. ppwszStrings[4], ppwszStrings[5], ppwszStrings[6]);
  172. break;
  173. default :
  174. VerboseDebugDump( L"CEvents::Report called with more params then expected\n" );
  175. break;
  176. }
  177. }