14 #define ldap_debug slap_debug
15 extern int slap_debug;
18 #include "ldap_pvt_thread.h"
21 #include "ldap_defaults.h"
25 #define SCM_NOTIFICATION_INTERVAL 5000
26 #define THIRTY_SECONDS (30 * 1000)
28 int is_NT_Service = 1; /* assume this is an NT service until determined that */
29 /* startup was from the command line */
31 SERVICE_STATUS SLAPDServiceStatus;
32 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
34 ldap_pvt_thread_cond_t started_event, stopped_event;
35 ldap_pvt_thread_t start_status_tid, stop_status_tid;
37 void (*stopfunc)(int);
40 char *GetLastErrorString( void );
42 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
45 DWORD dwValue, dwDisposition;
46 SC_HANDLE schSCManager, schService;
48 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
49 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
51 if ((schService = CreateService(
54 TEXT("OpenLDAP Directory Service"),
55 SC_MANAGER_CREATE_SERVICE,
56 SERVICE_WIN32_OWN_PROCESS,
60 NULL, NULL, NULL, NULL, NULL)) != NULL)
63 CloseServiceHandle(schService);
64 CloseServiceHandle(schSCManager);
66 sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
68 /* Create the registry key for event logging to the Windows NT event log. */
69 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
71 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
72 &dwDisposition) != ERROR_SUCCESS)
74 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
78 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
80 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
85 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
86 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
88 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
97 fprintf( stderr, "CreateService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
98 CloseServiceHandle(schSCManager);
103 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
108 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
110 SC_HANDLE schSCManager, schService;
112 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
113 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
115 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
117 if ( DeleteService(schService) == TRUE)
119 CloseServiceHandle(schService);
120 CloseServiceHandle(schSCManager);
123 fprintf( stderr, "DeleteService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
124 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
125 CloseServiceHandle(schService);
126 CloseServiceHandle(schSCManager);
130 fprintf( stderr, "OpenService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
131 CloseServiceHandle(schSCManager);
136 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
142 static void *start_status_routine( void *ptr )
149 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
150 switch ( wait_result )
154 /* the object that we were waiting for has been destroyed (ABANDONED) or
155 * signalled (TIMEOUT_0). We can assume that the startup process is
156 * complete and tell the Service Control Manager that we are now runnng */
157 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
158 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
159 SLAPDServiceStatus.dwCheckPoint++;
160 SLAPDServiceStatus.dwWaitHint = 1000;
161 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
165 /* We've waited for the required time, so send an update to the Service Control
166 * Manager saying to wait again. */
167 SLAPDServiceStatus.dwCheckPoint++;
168 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
169 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
172 /* theres been some problem with WaitForSingleObject so tell the Service
173 * Control Manager to wait 30 seconds before deploying its assasin and
174 * then leave the thread. */
175 SLAPDServiceStatus.dwCheckPoint++;
176 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
177 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
182 ldap_pvt_thread_exit(NULL);
188 static void *stop_status_routine( void *ptr )
195 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
196 switch ( wait_result )
200 /* the object that we were waiting for has been destroyed (ABANDONED) or
201 * signalled (TIMEOUT_0). The shutting down process is therefore complete
202 * and the final SERVICE_STOPPED message will be sent to the service control
203 * manager prior to the process terminating. */
207 /* We've waited for the required time, so send an update to the Service Control
208 * Manager saying to wait again. */
209 SLAPDServiceStatus.dwCheckPoint++;
210 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
211 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
214 /* theres been some problem with WaitForSingleObject so tell the Service
215 * Control Manager to wait 30 seconds before deploying its assasin and
216 * then leave the thread. */
217 SLAPDServiceStatus.dwCheckPoint++;
218 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
219 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
224 ldap_pvt_thread_exit(NULL);
230 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
234 case SERVICE_CONTROL_STOP:
235 case SERVICE_CONTROL_SHUTDOWN:
237 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
238 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
239 SLAPDServiceStatus.dwCheckPoint++;
240 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
241 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
243 ldap_pvt_thread_cond_init( &stopped_event );
244 if ( stopped_event == NULL )
246 /* the event was not created. We will ask the service control manager for 30
247 * seconds to shutdown */
248 SLAPDServiceStatus.dwCheckPoint++;
249 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
250 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
254 /* start a thread to report the progress to the service control manager
255 * until the stopped_event is fired. */
256 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
261 /* failed to create the thread that tells the Service Control Manager that the
262 * service stopping is proceeding.
263 * tell the Service Control Manager to wait another 30 seconds before deploying its
265 SLAPDServiceStatus.dwCheckPoint++;
266 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
267 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
273 case SERVICE_CONTROL_INTERROGATE:
274 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
280 void *getRegParam( char *svc, char *value )
285 static char vValue[1024];
286 DWORD valLen = sizeof( vValue );
289 sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
291 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
293 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
295 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
299 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
301 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
311 return (void*)&vValue;
313 return (void*)&vValue;
318 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
324 hEventLog = RegisterEventSource( NULL, svc );
326 Inserts[i] = (char *)malloc( 20 );
327 itoa( slap_debug, Inserts[i++], 10 );
328 Inserts[i++] = ldap_pvt_strdup( configfile );
329 Inserts[i++] = ldap_pvt_strdup( urls ? urls : "ldap:///" );
330 Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
332 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
333 MSG_SLAPD_STARTED, NULL, i, 0, Inserts, NULL );
335 for ( j = 0; j < i; j++ )
336 ldap_memfree( Inserts[j] );
337 DeregisterEventSource( hEventLog );
342 void LogSlapdStoppedEvent( char *svc )
346 hEventLog = RegisterEventSource( NULL, svc );
347 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
348 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
349 DeregisterEventSource( hEventLog );
353 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
354 void (*stopper)(int) )
356 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
360 /* initialize the Service Status structure */
361 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
362 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
363 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
364 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
365 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
366 SLAPDServiceStatus.dwCheckPoint = 1;
367 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
369 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
371 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
372 * until the slapd listener is completed and listening. Only then should we send
373 * SERVICE_RUNNING to the Service Control Manager. */
374 ldap_pvt_thread_cond_init( &started_event );
375 if ( started_event == NULL)
377 /* failed to create the event to determine when the startup process is complete so
378 * tell the Service Control Manager to wait another 30 seconds before deploying its
380 SLAPDServiceStatus.dwCheckPoint++;
381 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
382 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
386 /* start a thread to report the progress to the service control manager
387 * until the started_event is fired. */
388 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
393 /* failed to create the thread that tells the Service Control Manager that the
394 * service startup is proceeding.
395 * tell the Service Control Manager to wait another 30 seconds before deploying its
397 SLAPDServiceStatus.dwCheckPoint++;
398 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
399 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
404 void ReportSlapdShutdownComplete( )
408 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
409 ldap_pvt_thread_cond_signal( &stopped_event );
410 ldap_pvt_thread_cond_destroy( &stopped_event );
412 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
413 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
414 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
415 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
417 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
418 SLAPDServiceStatus.dwCheckPoint++;
419 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
420 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);