3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * NT Service manager utilities for OpenLDAP services
9 * these should NOT be slapd specific, but are
14 #ifdef HAVE_NT_SERVICE_MANAGER
16 #include <ac/stdlib.h>
17 #include <ac/string.h>
26 #define ldap_debug slap_debug
27 extern int slap_debug;
30 #include "ldap_pvt_thread.h"
33 #include "ldap_defaults.h"
37 #define SCM_NOTIFICATION_INTERVAL 5000
38 #define THIRTY_SECONDS (30 * 1000)
40 int is_NT_Service = 1; /* assume this is an NT service until determined that */
41 /* startup was from the command line */
43 SERVICE_STATUS SLAPDServiceStatus;
44 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
46 ldap_pvt_thread_cond_t started_event, stopped_event;
47 ldap_pvt_thread_t start_status_tid, stop_status_tid;
49 void (*stopfunc)(int);
51 static char *GetLastErrorString( void );
53 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
54 LPCTSTR lpszBinaryPathName, BOOL auto_start)
57 DWORD dwValue, dwDisposition;
58 SC_HANDLE schSCManager, schService;
60 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
61 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
63 if ((schService = CreateService(
68 SERVICE_WIN32_OWN_PROCESS,
69 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
72 NULL, NULL, NULL, NULL, NULL)) != NULL)
75 CloseServiceHandle(schService);
76 CloseServiceHandle(schSCManager);
78 sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
80 /* Create the registry key for event logging to the Windows NT event log. */
81 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
83 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
84 &dwDisposition) != ERROR_SUCCESS)
86 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
90 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
92 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
97 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
98 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
100 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
109 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
110 CloseServiceHandle(schSCManager);
115 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
120 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
122 SC_HANDLE schSCManager, schService;
124 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
125 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
127 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
129 if ( DeleteService(schService) == TRUE)
131 CloseServiceHandle(schService);
132 CloseServiceHandle(schSCManager);
135 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
136 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
137 CloseServiceHandle(schService);
138 CloseServiceHandle(schSCManager);
142 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
143 CloseServiceHandle(schSCManager);
148 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
154 static void *start_status_routine( void *ptr )
161 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
162 switch ( wait_result )
166 /* the object that we were waiting for has been destroyed (ABANDONED) or
167 * signalled (TIMEOUT_0). We can assume that the startup process is
168 * complete and tell the Service Control Manager that we are now runnng */
169 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
170 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
171 SLAPDServiceStatus.dwCheckPoint++;
172 SLAPDServiceStatus.dwWaitHint = 1000;
173 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
177 /* We've waited for the required time, so send an update to the Service Control
178 * Manager saying to wait again. */
179 SLAPDServiceStatus.dwCheckPoint++;
180 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
181 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
184 /* theres been some problem with WaitForSingleObject so tell the Service
185 * Control Manager to wait 30 seconds before deploying its assasin and
186 * then leave the thread. */
187 SLAPDServiceStatus.dwCheckPoint++;
188 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
189 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
194 ldap_pvt_thread_exit(NULL);
200 static void *stop_status_routine( void *ptr )
207 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
208 switch ( wait_result )
212 /* the object that we were waiting for has been destroyed (ABANDONED) or
213 * signalled (TIMEOUT_0). The shutting down process is therefore complete
214 * and the final SERVICE_STOPPED message will be sent to the service control
215 * manager prior to the process terminating. */
219 /* We've waited for the required time, so send an update to the Service Control
220 * Manager saying to wait again. */
221 SLAPDServiceStatus.dwCheckPoint++;
222 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
223 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
226 /* theres been some problem with WaitForSingleObject so tell the Service
227 * Control Manager to wait 30 seconds before deploying its assasin and
228 * then leave the thread. */
229 SLAPDServiceStatus.dwCheckPoint++;
230 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
231 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
236 ldap_pvt_thread_exit(NULL);
242 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
246 case SERVICE_CONTROL_STOP:
247 case SERVICE_CONTROL_SHUTDOWN:
249 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
250 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
251 SLAPDServiceStatus.dwCheckPoint++;
252 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
253 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
255 ldap_pvt_thread_cond_init( &stopped_event );
256 if ( stopped_event == NULL )
258 /* the event was not created. We will ask the service control manager for 30
259 * seconds to shutdown */
260 SLAPDServiceStatus.dwCheckPoint++;
261 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
262 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
266 /* start a thread to report the progress to the service control manager
267 * until the stopped_event is fired. */
268 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
273 /* failed to create the thread that tells the Service Control Manager that the
274 * service stopping is proceeding.
275 * tell the Service Control Manager to wait another 30 seconds before deploying its
277 SLAPDServiceStatus.dwCheckPoint++;
278 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
279 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
285 case SERVICE_CONTROL_INTERROGATE:
286 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
292 void *getRegParam( char *svc, char *value )
297 static char vValue[1024];
298 DWORD valLen = sizeof( vValue );
301 sprintf ( path, "SOFTWARE\\%s", svc );
303 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
305 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
307 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
311 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
313 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
323 return (void*)&vValue;
325 return (void*)&vValue;
330 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
336 hEventLog = RegisterEventSource( NULL, svc );
338 Inserts[i] = (char *)malloc( 20 );
339 itoa( slap_debug, Inserts[i++], 10 );
340 Inserts[i++] = strdup( configfile );
341 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
342 Inserts[i++] = strdup( is_NT_Service ? "svc" : "cmd" );
344 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
345 MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
347 for ( j = 0; j < i; j++ )
348 ldap_memfree( Inserts[j] );
349 DeregisterEventSource( hEventLog );
354 void LogSlapdStoppedEvent( char *svc )
358 hEventLog = RegisterEventSource( NULL, svc );
359 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
360 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
361 DeregisterEventSource( hEventLog );
365 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
366 void (*stopper)(int) )
368 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
372 /* initialize the Service Status structure */
373 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
374 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
375 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
376 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
377 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
378 SLAPDServiceStatus.dwCheckPoint = 1;
379 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
381 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
383 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
384 * until the slapd listener is completed and listening. Only then should we send
385 * SERVICE_RUNNING to the Service Control Manager. */
386 ldap_pvt_thread_cond_init( &started_event );
387 if ( started_event == NULL)
389 /* failed to create the event to determine when the startup process is complete so
390 * tell the Service Control Manager to wait another 30 seconds before deploying its
392 SLAPDServiceStatus.dwCheckPoint++;
393 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
394 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
398 /* start a thread to report the progress to the service control manager
399 * until the started_event is fired. */
400 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
405 /* failed to create the thread that tells the Service Control Manager that the
406 * service startup is proceeding.
407 * tell the Service Control Manager to wait another 30 seconds before deploying its
409 SLAPDServiceStatus.dwCheckPoint++;
410 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
411 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
416 void ReportSlapdShutdownComplete( )
420 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
421 ldap_pvt_thread_cond_signal( &stopped_event );
422 ldap_pvt_thread_cond_destroy( &stopped_event );
424 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
425 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
426 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
427 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
429 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
430 SLAPDServiceStatus.dwCheckPoint++;
431 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
432 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
436 static char *GetErrorString( int err )
438 static char msgBuf[1024];
441 FORMAT_MESSAGE_FROM_SYSTEM,
443 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
444 msgBuf, 1024, NULL );
449 static char *GetLastErrorString( void )
451 return GetErrorString( GetLastError() );