3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 #ifdef HAVE_NT_SERVICE_MANAGER
11 #include <ac/stdlib.h>
12 #include <ac/string.h>
21 #define ldap_debug slap_debug
22 extern int slap_debug;
25 #include "ldap_pvt_thread.h"
28 #include "ldap_defaults.h"
32 #define SCM_NOTIFICATION_INTERVAL 5000
33 #define THIRTY_SECONDS (30 * 1000)
35 int is_NT_Service = 1; /* assume this is an NT service until determined that */
36 /* startup was from the command line */
38 SERVICE_STATUS SLAPDServiceStatus;
39 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
41 ldap_pvt_thread_cond_t started_event, stopped_event;
42 ldap_pvt_thread_t start_status_tid, stop_status_tid;
44 void (*stopfunc)(int);
47 char *GetLastErrorString( void );
49 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
52 DWORD dwValue, dwDisposition;
53 SC_HANDLE schSCManager, schService;
55 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
56 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
58 if ((schService = CreateService(
61 TEXT("OpenLDAP Directory Service"),
62 SC_MANAGER_CREATE_SERVICE,
63 SERVICE_WIN32_OWN_PROCESS,
67 NULL, NULL, NULL, NULL, NULL)) != NULL)
70 CloseServiceHandle(schService);
71 CloseServiceHandle(schSCManager);
73 sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
75 /* Create the registry key for event logging to the Windows NT event log. */
76 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
78 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
79 &dwDisposition) != ERROR_SUCCESS)
81 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
85 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
87 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
92 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
93 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
95 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
104 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
105 CloseServiceHandle(schSCManager);
110 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
115 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
117 SC_HANDLE schSCManager, schService;
119 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
120 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
122 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
124 if ( DeleteService(schService) == TRUE)
126 CloseServiceHandle(schService);
127 CloseServiceHandle(schSCManager);
130 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
131 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
132 CloseServiceHandle(schService);
133 CloseServiceHandle(schSCManager);
137 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
138 CloseServiceHandle(schSCManager);
143 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
149 static void *start_status_routine( void *ptr )
156 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
157 switch ( wait_result )
161 /* the object that we were waiting for has been destroyed (ABANDONED) or
162 * signalled (TIMEOUT_0). We can assume that the startup process is
163 * complete and tell the Service Control Manager that we are now runnng */
164 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
165 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
166 SLAPDServiceStatus.dwCheckPoint++;
167 SLAPDServiceStatus.dwWaitHint = 1000;
168 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
172 /* We've waited for the required time, so send an update to the Service Control
173 * Manager saying to wait again. */
174 SLAPDServiceStatus.dwCheckPoint++;
175 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
176 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
179 /* theres been some problem with WaitForSingleObject so tell the Service
180 * Control Manager to wait 30 seconds before deploying its assasin and
181 * then leave the thread. */
182 SLAPDServiceStatus.dwCheckPoint++;
183 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
184 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
189 ldap_pvt_thread_exit(NULL);
195 static void *stop_status_routine( void *ptr )
202 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
203 switch ( wait_result )
207 /* the object that we were waiting for has been destroyed (ABANDONED) or
208 * signalled (TIMEOUT_0). The shutting down process is therefore complete
209 * and the final SERVICE_STOPPED message will be sent to the service control
210 * manager prior to the process terminating. */
214 /* We've waited for the required time, so send an update to the Service Control
215 * Manager saying to wait again. */
216 SLAPDServiceStatus.dwCheckPoint++;
217 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
218 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
221 /* theres been some problem with WaitForSingleObject so tell the Service
222 * Control Manager to wait 30 seconds before deploying its assasin and
223 * then leave the thread. */
224 SLAPDServiceStatus.dwCheckPoint++;
225 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
226 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
231 ldap_pvt_thread_exit(NULL);
237 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
241 case SERVICE_CONTROL_STOP:
242 case SERVICE_CONTROL_SHUTDOWN:
244 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
245 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
246 SLAPDServiceStatus.dwCheckPoint++;
247 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
248 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
250 ldap_pvt_thread_cond_init( &stopped_event );
251 if ( stopped_event == NULL )
253 /* the event was not created. We will ask the service control manager for 30
254 * seconds to shutdown */
255 SLAPDServiceStatus.dwCheckPoint++;
256 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
257 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
261 /* start a thread to report the progress to the service control manager
262 * until the stopped_event is fired. */
263 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
268 /* failed to create the thread that tells the Service Control Manager that the
269 * service stopping is proceeding.
270 * tell the Service Control Manager to wait another 30 seconds before deploying its
272 SLAPDServiceStatus.dwCheckPoint++;
273 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
274 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
280 case SERVICE_CONTROL_INTERROGATE:
281 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
287 void *getRegParam( char *svc, char *value )
292 static char vValue[1024];
293 DWORD valLen = sizeof( vValue );
296 sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
298 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
300 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
302 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
306 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
308 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
318 return (void*)&vValue;
320 return (void*)&vValue;
325 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
331 hEventLog = RegisterEventSource( NULL, svc );
333 Inserts[i] = (char *)malloc( 20 );
334 itoa( slap_debug, Inserts[i++], 10 );
335 Inserts[i++] = ldap_pvt_strdup( configfile );
336 Inserts[i++] = ldap_pvt_strdup( urls ? urls : "ldap:///" );
337 Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
339 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
340 MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
342 for ( j = 0; j < i; j++ )
343 ldap_memfree( Inserts[j] );
344 DeregisterEventSource( hEventLog );
349 void LogSlapdStoppedEvent( char *svc )
353 hEventLog = RegisterEventSource( NULL, svc );
354 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
355 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
356 DeregisterEventSource( hEventLog );
360 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
361 void (*stopper)(int) )
363 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
367 /* initialize the Service Status structure */
368 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
369 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
370 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
371 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
372 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
373 SLAPDServiceStatus.dwCheckPoint = 1;
374 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
376 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
378 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
379 * until the slapd listener is completed and listening. Only then should we send
380 * SERVICE_RUNNING to the Service Control Manager. */
381 ldap_pvt_thread_cond_init( &started_event );
382 if ( started_event == NULL)
384 /* failed to create the event to determine when the startup process is complete so
385 * tell the Service Control Manager to wait another 30 seconds before deploying its
387 SLAPDServiceStatus.dwCheckPoint++;
388 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
389 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
393 /* start a thread to report the progress to the service control manager
394 * until the started_event is fired. */
395 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
400 /* failed to create the thread that tells the Service Control Manager that the
401 * service startup is proceeding.
402 * tell the Service Control Manager to wait another 30 seconds before deploying its
404 SLAPDServiceStatus.dwCheckPoint++;
405 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
406 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
411 void ReportSlapdShutdownComplete( )
415 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
416 ldap_pvt_thread_cond_signal( &stopped_event );
417 ldap_pvt_thread_cond_destroy( &stopped_event );
419 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
420 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
421 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
422 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
424 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
425 SLAPDServiceStatus.dwCheckPoint++;
426 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
427 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);