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.

272 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1991-92 Microsoft Corporation
  3. Module Name:
  4. Alert.c
  5. Abstract:
  6. This file contains NetAlertRaise().
  7. for the NetAlert API.
  8. Author:
  9. John Rogers (JohnRo) 03-Apr-1992
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. 04-Apr-1992 JohnRo
  14. Created NetAlertRaise() API from RitaW's AlTest (alerter svc test).
  15. 06-Apr-1992 JohnRo
  16. Added/improved error checking.
  17. 08-May-1992 JohnRo
  18. Quiet normal debug output.
  19. 08-May-1992 JohnRo
  20. Use <prefix.h> equates.
  21. --*/
  22. // These must be included first:
  23. #include <windows.h> // DWORD, CreateFile(), etc.
  24. #include <lmcons.h> // IN, NET_API_STATUS, etc.
  25. // These may be included in any order:
  26. #include <lmalert.h> // My prototype, ALERTER_MAILSLOT, LPSTD_ALERT, etc.
  27. #include <lmerr.h> // NO_ERROR, NERR_NoRoom, etc.
  28. #include <netdebug.h> // NetpKdPrint(()), FORMAT_ equates, etc.
  29. #include <prefix.h> // PREFIX_ equates.
  30. #include <string.h> // memcpy().
  31. #include <strucinf.h> // NetpAlertStructureInfo().
  32. #include <timelib.h> // time_now().
  33. #include <tstr.h> // TCHAR_EOS.
  34. #if DBG
  35. #define IF_DEBUG( anything ) if (FALSE)
  36. #else
  37. #define IF_DEBUG( anything ) if (FALSE)
  38. #endif
  39. NET_API_STATUS NET_API_FUNCTION
  40. NetAlertRaise(
  41. IN LPCWSTR AlertType,
  42. IN LPVOID Buffer,
  43. IN DWORD BufferSize
  44. )
  45. /*++
  46. Routine Description:
  47. This routine raises an alert to notify the Alerter service by writing to
  48. the Alerter service mailslot.
  49. Arguments:
  50. AlertType - Supplies the name of the alert event which could be one
  51. of the three the Alerter service supports: ADMIN, USER, or PRINTING.
  52. The ALERT_xxx_EVENT equates are used to provide these strings.
  53. Buffer - Supplies the data to be written to the alert mailslot.
  54. This must begin with a STD_ALERT structure.
  55. BufferSize - Supplies the size in number of bytes of Buffer.
  56. Return Value:
  57. NET_API_STATUS - NO_ERROR or reason for failure.
  58. --*/
  59. {
  60. NET_API_STATUS ApiStatus;
  61. HANDLE FileHandle;
  62. DWORD MaxTotalSize;
  63. DWORD NumberOfBytesWritten;
  64. DWORD RequiredFixedSize;
  65. //
  66. // Check for caller errors.
  67. //
  68. if (AlertType == NULL) {
  69. return (ERROR_INVALID_PARAMETER);
  70. } else if ( (*AlertType) == TCHAR_EOS ) {
  71. return (ERROR_INVALID_PARAMETER);
  72. } else if (Buffer == NULL) {
  73. return (ERROR_INVALID_PARAMETER);
  74. }
  75. ApiStatus = NetpAlertStructureInfo(
  76. (LPWSTR)AlertType,
  77. & MaxTotalSize,
  78. & RequiredFixedSize);
  79. if (ApiStatus != NO_ERROR) {
  80. return (ApiStatus);
  81. }
  82. if (BufferSize < ( sizeof(STD_ALERT) + RequiredFixedSize) ) {
  83. return (ERROR_INVALID_PARAMETER);
  84. } else if (BufferSize > MaxTotalSize) {
  85. return (ERROR_INVALID_PARAMETER);
  86. }
  87. //
  88. // Open the Alerter mailslot to write to it.
  89. //
  90. FileHandle = CreateFile(
  91. ALERTER_MAILSLOT,
  92. GENERIC_WRITE,
  93. FILE_SHARE_WRITE | FILE_SHARE_READ,
  94. (LPSECURITY_ATTRIBUTES) NULL,
  95. OPEN_EXISTING,
  96. FILE_ATTRIBUTE_NORMAL,
  97. NULL ); // no template file.
  98. if (FileHandle == INVALID_HANDLE_VALUE) {
  99. ApiStatus = (NET_API_STATUS) GetLastError();
  100. IF_DEBUG( ALERT ) {
  101. NetpKdPrint(( PREFIX_NETAPI
  102. "NetAlertRaise: Problem with opening mailslot "
  103. FORMAT_API_STATUS "\n", ApiStatus ));
  104. }
  105. return (ApiStatus);
  106. }
  107. IF_DEBUG( ALERT ) {
  108. NetpKdPrint(( PREFIX_NETAPI "NetAlertRaise: "
  109. "Successfully opened the mailslot. Message (partial) is:\n"));
  110. NetpDbgHexDump( Buffer, NetpDbgReasonable(BufferSize) );
  111. }
  112. //
  113. // Write alert notification to mailslot to be read by Alerter service.
  114. //
  115. if (WriteFile(
  116. FileHandle,
  117. Buffer,
  118. BufferSize,
  119. &NumberOfBytesWritten,
  120. NULL // no overlapped structure.
  121. ) == FALSE) {
  122. ApiStatus = (NET_API_STATUS) GetLastError();
  123. NetpKdPrint(( PREFIX_NETAPI "NetAlertRaise: Error " FORMAT_API_STATUS
  124. " writing to mailslot.\n", ApiStatus ));
  125. } else {
  126. NetpAssert( NumberOfBytesWritten == BufferSize );
  127. IF_DEBUG(ALERT) {
  128. NetpKdPrint(( PREFIX_NETAPI "NetAlertRaise: "
  129. "Successful in writing to mailslot; length "
  130. FORMAT_DWORD ", bytes written " FORMAT_DWORD "\n",
  131. BufferSize, NumberOfBytesWritten));
  132. }
  133. }
  134. (VOID) CloseHandle(FileHandle);
  135. return (NO_ERROR);
  136. } // NetAlertRaise
  137. NET_API_STATUS NET_API_FUNCTION
  138. NetAlertRaiseEx(
  139. IN LPCWSTR AlertType,
  140. IN LPVOID VariableInfo,
  141. IN DWORD VariableInfoSize,
  142. IN LPCWSTR ServiceName
  143. )
  144. /*++
  145. Routine Description:
  146. This routine raises an alert to notify the Alerter service by writing to
  147. the Alerter service mailslot.
  148. Arguments:
  149. AlertType - Supplies the name of the alert event which could be one
  150. of the three the Alerter service supports: ADMIN, USER, or PRINTING.
  151. The ALERT_xxx_EVENT equates are used to provide these strings.
  152. VariableInfo - Supplies the variable length portion of the alert
  153. notification.
  154. VariableInfoSize - Supplies the size in number of bytes of the variable
  155. portion of the notification.
  156. ServiceName - Supplies the name of the service which raised the alert.
  157. Return Value:
  158. NET_API_STATUS - NO_ERROR or reason for failure.
  159. --*/
  160. {
  161. #define TEMP_VARIABLE_SIZE (512-sizeof(STD_ALERT))
  162. BYTE AlertMailslotBuffer[TEMP_VARIABLE_SIZE + sizeof(STD_ALERT)];
  163. LPSTD_ALERT Alert = (LPSTD_ALERT) AlertMailslotBuffer;
  164. NET_API_STATUS ApiStatus;
  165. DWORD DataSize = VariableInfoSize + sizeof(STD_ALERT);
  166. //
  167. // Check for caller errors.
  168. //
  169. if (AlertType == NULL) {
  170. return (ERROR_INVALID_PARAMETER);
  171. } else if ( (*AlertType) == TCHAR_EOS ) {
  172. return (ERROR_INVALID_PARAMETER);
  173. } else if (VariableInfo == NULL) {
  174. return (ERROR_INVALID_PARAMETER);
  175. } else if (VariableInfoSize > TEMP_VARIABLE_SIZE) {
  176. return (NERR_NoRoom);
  177. } else if (ServiceName == NULL) {
  178. return (ERROR_INVALID_PARAMETER);
  179. } else if ( (*ServiceName) == TCHAR_EOS ) {
  180. return (ERROR_INVALID_PARAMETER);
  181. }
  182. //
  183. // Copy variable portion to end of our buffer.
  184. //
  185. (VOID) memcpy(ALERT_OTHER_INFO(Alert), VariableInfo, VariableInfoSize);
  186. //
  187. // Store current time in seconds since 1970.
  188. //
  189. Alert->alrt_timestamp = (DWORD) time_now();
  190. //
  191. // Put alert event name into AlertMailslotBuffer
  192. //
  193. (VOID) STRCPY(Alert->alrt_eventname, AlertType);
  194. //
  195. // Put service name into AlertMailslotBuffer
  196. //
  197. (VOID) STRCPY(Alert->alrt_servicename, ServiceName);
  198. //
  199. // Write alert notification to mailslot to be read by Alerter service
  200. //
  201. ApiStatus = NetAlertRaise(
  202. AlertType,
  203. Alert, // buffer
  204. DataSize ); // buffer size
  205. return (ApiStatus);
  206. } // NetAlertRaiseEx