ntevent.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include "ldap.h"
  10. #include "regparms.h"
  11. #include "nspr.h"
  12. #include "plstr.h"
  13. HANDLE hSlapdEventSource;
  14. LPTSTR pszServerName;
  15. void ReportSlapdEvent(WORD wEventType, DWORD dwIdEvent, WORD wNumInsertStrings,
  16. char *pszStrings)
  17. {
  18. LPCTSTR lpszStrings[64];
  19. BOOL bSuccess;
  20. if( hSlapdEventSource )
  21. {
  22. if( pszServerName )
  23. lpszStrings[0] = (LPCTSTR)pszServerName;
  24. if( pszStrings != NULL)
  25. lpszStrings[1] = (LPCTSTR)pszStrings;
  26. wNumInsertStrings++;
  27. /* Now report the event, which will add this event to the event log */
  28. bSuccess = ReportEvent(hSlapdEventSource, /* event-log handle */
  29. wEventType, /* event type */
  30. 0, /* category zero */
  31. dwIdEvent, /* event ID */
  32. NULL, /* no user SID */
  33. wNumInsertStrings, /* number of substr */
  34. 0, /* no binary data */
  35. lpszStrings, /* string array */
  36. NULL); /* address of data */
  37. }
  38. } /* ReportSlapdEvent */
  39. BOOL ReportSlapdStatusToSCMgr(
  40. SERVICE_STATUS *serviceStatus,
  41. SERVICE_STATUS_HANDLE serviceStatusHandle,
  42. HANDLE Event,
  43. DWORD dwCurrentState,
  44. DWORD dwWin32ExitCode,
  45. DWORD dwCheckPoint,
  46. DWORD dwWaitHint)
  47. {
  48. /* Disable control requests until the service is started. */
  49. if (dwCurrentState == SERVICE_START_PENDING)
  50. serviceStatus->dwControlsAccepted = 0;
  51. else
  52. serviceStatus->dwControlsAccepted = SERVICE_ACCEPT_STOP |
  53. SERVICE_ACCEPT_PAUSE_CONTINUE;
  54. serviceStatus->dwCurrentState = dwCurrentState;
  55. serviceStatus->dwWin32ExitCode = dwWin32ExitCode;
  56. serviceStatus->dwCheckPoint = dwCheckPoint;
  57. serviceStatus->dwWaitHint = dwWaitHint;
  58. /* Report the status of the service to the service control manager. */
  59. return SetServiceStatus( serviceStatusHandle, serviceStatus);
  60. } /* ReportSlapdStatusToSCMgr */
  61. // This is a routine that we use to check for multiple instances of a server with
  62. // the same id. We cannot use a shared data section to keep count of instances since
  63. // there will be multiple instances of the server running. MS recommends using a
  64. // sync object to do this. Thus we attempt to create an object with same NAME
  65. // but different TYPE as the server "Done" event.We have a small race condition
  66. // between the check and the creation of the "Done" event.
  67. BOOL
  68. MultipleInstances()
  69. {
  70. HANDLE hServDoneSemaphore;
  71. DWORD result;
  72. CHAR ErrMsg[1024];
  73. char szDoneEvent[256];
  74. if( !pszServerName )
  75. return FALSE;
  76. PR_snprintf(szDoneEvent, sizeof(szDoneEvent), "NS_%s", pszServerName);
  77. hServDoneSemaphore = CreateSemaphore(
  78. NULL, // security attributes
  79. 0, // initial count for semaphore
  80. 1, // maximum count for semaphore
  81. szDoneEvent);
  82. if ( hServDoneSemaphore == NULL) {
  83. result = GetLastError();
  84. if (result == ERROR_INVALID_HANDLE) {
  85. PR_snprintf(ErrMsg, sizeof(ErrMsg), "Server %s is already"
  86. " running. Terminating this instance.", pszServerName);
  87. MessageBox(GetDesktopWindow(), ErrMsg,
  88. "SERVER ALREADY RUNNING", MB_ICONEXCLAMATION | MB_OK);
  89. return TRUE;
  90. } else {
  91. /* We aren't too interested in why the creation failed
  92. * if it is not because of another instance */
  93. return FALSE;
  94. }
  95. } // hServDoneSemaphore == NULL
  96. CloseHandle(hServDoneSemaphore);
  97. return FALSE;
  98. }
  99. BOOL SlapdIsAService()
  100. {
  101. // May change in V2.0
  102. return FALSE;
  103. }
  104. BOOL SlapdGetServerNameFromCmdline(char *szServerName, char *szCmdLine, int dirname)
  105. {
  106. BOOL bReturn = FALSE;
  107. char *szChar = NULL;
  108. char szCmdCopy[_MAX_PATH];
  109. if( szCmdLine )
  110. {
  111. memset(szCmdCopy, 0, _MAX_PATH );
  112. PL_strncpyz( szCmdCopy, szCmdLine , sizeof(szCmdCopy) );
  113. }
  114. else
  115. return(bReturn);
  116. // szCmdCopy should be something like
  117. // c:\navgold\server\slapd-kennedy\config\slapd.conf
  118. // unless dirname is TRUE in which case it should be
  119. // c:\navgold\server\slapd-kennedy
  120. if(szChar = strrchr(szCmdCopy, '\\'))
  121. {
  122. *szChar = 0;
  123. if(dirname)
  124. {
  125. strcpy(szServerName, szChar+1);
  126. bReturn = TRUE;
  127. }
  128. else if(szChar = strrchr(szCmdCopy, '\\'))
  129. {
  130. // szCmdCopy should be c:\navgold\server\slapd-kennedy\config
  131. *szChar = 0;
  132. // szCmdCopy should be c:\navgold\server\slapd-kennedy
  133. if(szChar = strrchr(szCmdCopy, '\\'))
  134. {
  135. szChar++;
  136. // szChar should point to slapd-kennedy
  137. strcpy(szServerName, szChar);
  138. bReturn = TRUE;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. // szCmdCopy should be something like slapd-kennedy
  145. strcpy(szServerName, szCmdCopy);
  146. bReturn = TRUE;
  147. }
  148. if(strlen(szServerName) == 0)
  149. bReturn = FALSE;
  150. return(bReturn);
  151. }
  152. #endif _WIN32