ntevent.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef _WIN32
  39. #include <windows.h>
  40. #include <stdio.h>
  41. #include "ldap.h"
  42. #include "regparms.h"
  43. #include "nspr.h"
  44. #include "plstr.h"
  45. HANDLE hSlapdEventSource;
  46. LPTSTR pszServerName;
  47. void ReportSlapdEvent(WORD wEventType, DWORD dwIdEvent, WORD wNumInsertStrings,
  48. char *pszStrings)
  49. {
  50. LPCTSTR lpszStrings[64];
  51. BOOL bSuccess;
  52. if( hSlapdEventSource )
  53. {
  54. if( pszServerName )
  55. lpszStrings[0] = (LPCTSTR)pszServerName;
  56. if( pszStrings != NULL)
  57. lpszStrings[1] = (LPCTSTR)pszStrings;
  58. wNumInsertStrings++;
  59. /* Now report the event, which will add this event to the event log */
  60. bSuccess = ReportEvent(hSlapdEventSource, /* event-log handle */
  61. wEventType, /* event type */
  62. 0, /* category zero */
  63. dwIdEvent, /* event ID */
  64. NULL, /* no user SID */
  65. wNumInsertStrings, /* number of substr */
  66. 0, /* no binary data */
  67. lpszStrings, /* string array */
  68. NULL); /* address of data */
  69. }
  70. } /* ReportSlapdEvent */
  71. BOOL ReportSlapdStatusToSCMgr(
  72. SERVICE_STATUS *serviceStatus,
  73. SERVICE_STATUS_HANDLE serviceStatusHandle,
  74. HANDLE Event,
  75. DWORD dwCurrentState,
  76. DWORD dwWin32ExitCode,
  77. DWORD dwCheckPoint,
  78. DWORD dwWaitHint)
  79. {
  80. /* Disable control requests until the service is started. */
  81. if (dwCurrentState == SERVICE_START_PENDING)
  82. serviceStatus->dwControlsAccepted = 0;
  83. else
  84. serviceStatus->dwControlsAccepted = SERVICE_ACCEPT_STOP |
  85. SERVICE_ACCEPT_PAUSE_CONTINUE;
  86. serviceStatus->dwCurrentState = dwCurrentState;
  87. serviceStatus->dwWin32ExitCode = dwWin32ExitCode;
  88. serviceStatus->dwCheckPoint = dwCheckPoint;
  89. serviceStatus->dwWaitHint = dwWaitHint;
  90. /* Report the status of the service to the service control manager. */
  91. return SetServiceStatus( serviceStatusHandle, serviceStatus);
  92. } /* ReportSlapdStatusToSCMgr */
  93. // This is a routine that we use to check for multiple instances of a server with
  94. // the same id. We cannot use a shared data section to keep count of instances since
  95. // there will be multiple instances of the server running. MS recommends using a
  96. // sync object to do this. Thus we attempt to create an object with same NAME
  97. // but different TYPE as the server "Done" event.We have a small race condition
  98. // between the check and the creation of the "Done" event.
  99. BOOL
  100. MultipleInstances()
  101. {
  102. HANDLE hServDoneSemaphore;
  103. DWORD result;
  104. CHAR ErrMsg[1024];
  105. char szDoneEvent[256];
  106. if( !pszServerName )
  107. return FALSE;
  108. PR_snprintf(szDoneEvent, sizeof(szDoneEvent), "NS_%s", pszServerName);
  109. hServDoneSemaphore = CreateSemaphore(
  110. NULL, // security attributes
  111. 0, // initial count for semaphore
  112. 1, // maximum count for semaphore
  113. szDoneEvent);
  114. if ( hServDoneSemaphore == NULL) {
  115. result = GetLastError();
  116. if (result == ERROR_INVALID_HANDLE) {
  117. PR_snprintf(ErrMsg, sizeof(ErrMsg), "Server %s is already"
  118. " running. Terminating this instance.", pszServerName);
  119. MessageBox(GetDesktopWindow(), ErrMsg,
  120. "SERVER ALREADY RUNNING", MB_ICONEXCLAMATION | MB_OK);
  121. return TRUE;
  122. } else {
  123. /* We aren't too interested in why the creation failed
  124. * if it is not because of another instance */
  125. return FALSE;
  126. }
  127. } // hServDoneSemaphore == NULL
  128. CloseHandle(hServDoneSemaphore);
  129. return FALSE;
  130. }
  131. BOOL SlapdIsAService()
  132. {
  133. // May change in V2.0
  134. return FALSE;
  135. }
  136. BOOL SlapdGetServerNameFromCmdline(char *szServerName, char *szCmdLine, int dirname)
  137. {
  138. BOOL bReturn = FALSE;
  139. char *szChar = NULL;
  140. char szCmdCopy[_MAX_PATH];
  141. if( szCmdLine )
  142. {
  143. memset(szCmdCopy, 0, _MAX_PATH );
  144. PL_strncpyz( szCmdCopy, szCmdLine , sizeof(szCmdCopy) );
  145. }
  146. else
  147. return(bReturn);
  148. // szCmdCopy should be something like
  149. // c:\navgold\server\slapd-kennedy\config\slapd.conf
  150. // unless dirname is TRUE in which case it should be
  151. // c:\navgold\server\slapd-kennedy
  152. if(szChar = strrchr(szCmdCopy, '\\'))
  153. {
  154. *szChar = 0;
  155. if(dirname)
  156. {
  157. strcpy(szServerName, szChar+1);
  158. bReturn = TRUE;
  159. }
  160. else if(szChar = strrchr(szCmdCopy, '\\'))
  161. {
  162. // szCmdCopy should be c:\navgold\server\slapd-kennedy\config
  163. *szChar = 0;
  164. // szCmdCopy should be c:\navgold\server\slapd-kennedy
  165. if(szChar = strrchr(szCmdCopy, '\\'))
  166. {
  167. szChar++;
  168. // szChar should point to slapd-kennedy
  169. strcpy(szServerName, szChar);
  170. bReturn = TRUE;
  171. }
  172. }
  173. }
  174. else
  175. {
  176. // szCmdCopy should be something like slapd-kennedy
  177. strcpy(szServerName, szCmdCopy);
  178. bReturn = TRUE;
  179. }
  180. if(strlen(szServerName) == 0)
  181. bReturn = FALSE;
  182. return(bReturn);
  183. }
  184. #endif _WIN32