3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
19 #define ldap_debug slap_debug
20 extern int slap_debug;
23 #include "ldap_pvt_thread.h"
26 #include "ldap_defaults.h"
30 #define SCM_NOTIFICATION_INTERVAL 5000
31 #define THIRTY_SECONDS (30 * 1000)
33 int is_NT_Service = 1; /* assume this is an NT service until determined that */
34 /* startup was from the command line */
36 SERVICE_STATUS SLAPDServiceStatus;
37 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
39 ldap_pvt_thread_cond_t started_event, stopped_event;
40 ldap_pvt_thread_t start_status_tid, stop_status_tid;
42 void (*stopfunc)(int);
45 char *GetLastErrorString( void );
47 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
50 DWORD dwValue, dwDisposition;
51 SC_HANDLE schSCManager, schService;
53 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
54 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
56 if ((schService = CreateService(
59 TEXT("OpenLDAP Directory Service"),
60 SC_MANAGER_CREATE_SERVICE,
61 SERVICE_WIN32_OWN_PROCESS,
65 NULL, NULL, NULL, NULL, NULL)) != NULL)
68 CloseServiceHandle(schService);
69 CloseServiceHandle(schSCManager);
71 sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
73 /* Create the registry key for event logging to the Windows NT event log. */
74 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
76 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
77 &dwDisposition) != ERROR_SUCCESS)
79 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
83 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
85 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
90 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
91 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
93 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
102 fprintf( stderr, "CreateService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
103 CloseServiceHandle(schSCManager);
108 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
113 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
115 SC_HANDLE schSCManager, schService;
117 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
118 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
120 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
122 if ( DeleteService(schService) == TRUE)
124 CloseServiceHandle(schService);
125 CloseServiceHandle(schSCManager);
128 fprintf( stderr, "DeleteService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
129 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
130 CloseServiceHandle(schService);
131 CloseServiceHandle(schSCManager);
135 fprintf( stderr, "OpenService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
136 CloseServiceHandle(schSCManager);
141 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
147 static void *start_status_routine( void *ptr )
154 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
155 switch ( wait_result )
159 /* the object that we were waiting for has been destroyed (ABANDONED) or
160 * signalled (TIMEOUT_0). We can assume that the startup process is
161 * complete and tell the Service Control Manager that we are now runnng */
162 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
163 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
164 SLAPDServiceStatus.dwCheckPoint++;
165 SLAPDServiceStatus.dwWaitHint = 1000;
166 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
170 /* We've waited for the required time, so send an update to the Service Control
171 * Manager saying to wait again. */
172 SLAPDServiceStatus.dwCheckPoint++;
173 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
174 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
177 /* theres been some problem with WaitForSingleObject so tell the Service
178 * Control Manager to wait 30 seconds before deploying its assasin and
179 * then leave the thread. */
180 SLAPDServiceStatus.dwCheckPoint++;
181 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
182 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
187 ldap_pvt_thread_exit(NULL);
193 static void *stop_status_routine( void *ptr )
200 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
201 switch ( wait_result )
205 /* the object that we were waiting for has been destroyed (ABANDONED) or
206 * signalled (TIMEOUT_0). The shutting down process is therefore complete
207 * and the final SERVICE_STOPPED message will be sent to the service control
208 * manager prior to the process terminating. */
212 /* We've waited for the required time, so send an update to the Service Control
213 * Manager saying to wait again. */
214 SLAPDServiceStatus.dwCheckPoint++;
215 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
216 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
219 /* theres been some problem with WaitForSingleObject so tell the Service
220 * Control Manager to wait 30 seconds before deploying its assasin and
221 * then leave the thread. */
222 SLAPDServiceStatus.dwCheckPoint++;
223 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
224 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
229 ldap_pvt_thread_exit(NULL);
235 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
239 case SERVICE_CONTROL_STOP:
240 case SERVICE_CONTROL_SHUTDOWN:
242 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
243 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
244 SLAPDServiceStatus.dwCheckPoint++;
245 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
246 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
248 ldap_pvt_thread_cond_init( &stopped_event );
249 if ( stopped_event == NULL )
251 /* the event was not created. We will ask the service control manager for 30
252 * seconds to shutdown */
253 SLAPDServiceStatus.dwCheckPoint++;
254 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
255 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
259 /* start a thread to report the progress to the service control manager
260 * until the stopped_event is fired. */
261 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
266 /* failed to create the thread that tells the Service Control Manager that the
267 * service stopping is proceeding.
268 * tell the Service Control Manager to wait another 30 seconds before deploying its
270 SLAPDServiceStatus.dwCheckPoint++;
271 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
272 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
278 case SERVICE_CONTROL_INTERROGATE:
279 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
285 void *getRegParam( char *svc, char *value )
290 static char vValue[1024];
291 DWORD valLen = sizeof( vValue );
294 sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
296 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
298 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
300 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
304 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
306 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
316 return (void*)&vValue;
318 return (void*)&vValue;
323 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
329 hEventLog = RegisterEventSource( NULL, svc );
331 Inserts[i] = (char *)malloc( 20 );
332 itoa( slap_debug, Inserts[i++], 10 );
333 Inserts[i++] = ldap_pvt_strdup( configfile );
334 Inserts[i++] = ldap_pvt_strdup( urls ? urls : "ldap:///" );
335 Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
337 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
338 MSG_SLAPD_STARTED, NULL, i, 0, Inserts, NULL );
340 for ( j = 0; j < i; j++ )
341 ldap_memfree( Inserts[j] );
342 DeregisterEventSource( hEventLog );
347 void LogSlapdStoppedEvent( char *svc )
351 hEventLog = RegisterEventSource( NULL, svc );
352 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
353 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
354 DeregisterEventSource( hEventLog );
358 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
359 void (*stopper)(int) )
361 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
365 /* initialize the Service Status structure */
366 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
367 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
368 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
369 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
370 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
371 SLAPDServiceStatus.dwCheckPoint = 1;
372 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
374 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
376 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
377 * until the slapd listener is completed and listening. Only then should we send
378 * SERVICE_RUNNING to the Service Control Manager. */
379 ldap_pvt_thread_cond_init( &started_event );
380 if ( started_event == NULL)
382 /* failed to create the event to determine when the startup process is complete so
383 * tell the Service Control Manager to wait another 30 seconds before deploying its
385 SLAPDServiceStatus.dwCheckPoint++;
386 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
387 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
391 /* start a thread to report the progress to the service control manager
392 * until the started_event is fired. */
393 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
398 /* failed to create the thread that tells the Service Control Manager that the
399 * service startup is proceeding.
400 * tell the Service Control Manager to wait another 30 seconds before deploying its
402 SLAPDServiceStatus.dwCheckPoint++;
403 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
404 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
409 void ReportSlapdShutdownComplete( )
413 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
414 ldap_pvt_thread_cond_signal( &stopped_event );
415 ldap_pvt_thread_cond_destroy( &stopped_event );
417 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
418 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
419 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
420 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
422 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
423 SLAPDServiceStatus.dwCheckPoint++;
424 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
425 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);