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.

162 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. dbmon.c
  5. Abstract:
  6. A simple program to print strings passed to OutputDebugString when
  7. the app printing the strings is not being debugged.
  8. Author:
  9. Kent Forschmiedt (kentf) 30-Sep-1994
  10. Revision History:
  11. --*/
  12. #define UNICODE
  13. #define _UNICODE
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. int _cdecl
  18. wmain(
  19. )
  20. /*++
  21. Routine Description:
  22. Arguments:
  23. Return Value:
  24. --*/
  25. {
  26. HANDLE AckEvent;
  27. HANDLE ReadyEvent;
  28. HANDLE SharedFile;
  29. LPVOID SharedMem;
  30. LPSTR String;
  31. DWORD ret;
  32. DWORD LastPid;
  33. LPDWORD pThisPid;
  34. BOOL DidCR;
  35. SECURITY_ATTRIBUTES sa;
  36. SECURITY_DESCRIPTOR sd;
  37. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  38. sa.bInheritHandle = TRUE;
  39. sa.lpSecurityDescriptor = &sd;
  40. if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
  41. fwprintf(stderr,L"unable to InitializeSecurityDescriptor, err == %d\n",
  42. GetLastError());
  43. exit(1);
  44. }
  45. if(!SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE)) {
  46. fwprintf(stderr,L"unable to SetSecurityDescriptorDacl, err == %d\n",
  47. GetLastError());
  48. exit(1);
  49. }
  50. AckEvent = CreateEvent(&sa, FALSE, FALSE, L"DBWIN_BUFFER_READY");
  51. if (!AckEvent) {
  52. fwprintf(stderr,
  53. L"dbmon: Unable to create synchronization object, err == %d\n",
  54. GetLastError());
  55. exit(1);
  56. }
  57. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  58. fputws(L"dbmon: already running\n", stderr);
  59. exit(1);
  60. }
  61. ReadyEvent = CreateEvent(&sa, FALSE, FALSE, L"DBWIN_DATA_READY");
  62. if (!ReadyEvent) {
  63. fwprintf(stderr,
  64. L"dbmon: Unable to create synchronization object, err == %d\n",
  65. GetLastError());
  66. exit(1);
  67. }
  68. SharedFile = CreateFileMapping(
  69. (HANDLE)-1,
  70. &sa,
  71. PAGE_READWRITE,
  72. 0,
  73. 4096,
  74. L"DBWIN_BUFFER");
  75. if (!SharedFile) {
  76. fwprintf(stderr,
  77. L"dbmon: Unable to create file mapping object, err == %d\n",
  78. GetLastError());
  79. exit(1);
  80. }
  81. SharedMem = MapViewOfFile(
  82. SharedFile,
  83. FILE_MAP_READ,
  84. 0,
  85. 0,
  86. 512);
  87. if (!SharedMem) {
  88. fwprintf(stderr,
  89. L"dbmon: Unable to map shared memory, err == %d\n",
  90. GetLastError());
  91. exit(1);
  92. }
  93. String = (LPSTR)SharedMem + sizeof(DWORD);
  94. pThisPid = SharedMem;
  95. LastPid = 0xffffffff;
  96. DidCR = TRUE;
  97. SetEvent(AckEvent);
  98. for (;;) {
  99. ret = WaitForSingleObject(ReadyEvent, INFINITE);
  100. if (ret != WAIT_OBJECT_0) {
  101. fwprintf(stderr, L"dbmon: wait failed; err == %d\n", GetLastError());
  102. exit(1);
  103. } else {
  104. if (LastPid != *pThisPid) {
  105. LastPid = *pThisPid;
  106. if (!DidCR) {
  107. putchar('\n');
  108. DidCR = TRUE;
  109. }
  110. }
  111. if (DidCR) {
  112. printf("%3u: ", LastPid);
  113. }
  114. fputs(String, stdout);
  115. DidCR = (*String && (String[strlen(String) - 1] == '\n'));
  116. SetEvent(AckEvent);
  117. }
  118. }
  119. return 0;
  120. }