3 * Copyright 1998-2003 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>
27 * The whole debug implementation is a bit of a hack.
28 * We have to define this LDAP_SLAPD_V macro here since the slap.h
29 * header file isn't included (and really shouldn't be).
30 * TODO: re-implement debug functions so that the debug level can
31 * be passed around instead of having to count on the existence of
32 * ldap_debug, slap_debug, etc.
34 #define ldap_debug slap_debug
35 LDAP_SLAPD_V (int) slap_debug;
38 #include "ldap_pvt_thread.h"
41 #include "ldap_defaults.h"
45 #define SCM_NOTIFICATION_INTERVAL 5000
46 #define THIRTY_SECONDS (30 * 1000)
48 int is_NT_Service; /* is this is an NT service? */
50 SERVICE_STATUS SLAPDServiceStatus;
51 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
53 ldap_pvt_thread_cond_t started_event, stopped_event;
54 ldap_pvt_thread_t start_status_tid, stop_status_tid;
56 void (*stopfunc)(int);
58 static char *GetLastErrorString( void );
60 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
61 LPCTSTR lpszBinaryPathName, BOOL auto_start)
64 DWORD dwValue, dwDisposition;
65 SC_HANDLE schSCManager, schService;
66 char *sp = strchr( lpszBinaryPathName, ' ');
69 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
71 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
73 if ((schService = CreateService(
78 SERVICE_WIN32_OWN_PROCESS,
79 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
82 NULL, NULL, NULL, NULL, NULL)) != NULL)
85 CloseServiceHandle(schService);
86 CloseServiceHandle(schSCManager);
88 snprintf( regpath, sizeof regpath,
89 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
91 /* Create the registry key for event logging to the Windows NT event log. */
92 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
94 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
95 &dwDisposition) != ERROR_SUCCESS)
97 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
101 if ( sp ) *sp = '\0';
102 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
104 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
109 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
110 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
112 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
121 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
122 CloseServiceHandle(schSCManager);
127 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
132 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
134 SC_HANDLE schSCManager, schService;
136 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
137 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
139 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
141 if ( DeleteService(schService) == TRUE)
143 CloseServiceHandle(schService);
144 CloseServiceHandle(schSCManager);
147 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
148 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
149 CloseServiceHandle(schService);
150 CloseServiceHandle(schSCManager);
154 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
155 CloseServiceHandle(schSCManager);
160 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
166 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
174 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
175 strcat(buf, lpszServiceName);
176 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
180 if (lpszBinaryPathName) {
182 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
183 if (strcmp(lpszBinaryPathName, buf))
193 svc_running (LPTSTR lpszServiceName)
200 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
201 return(GetLastError());
204 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
206 if (!QueryServiceStatus(service, &ss))
208 else if (ss.dwCurrentState != SERVICE_STOPPED)
210 CloseServiceHandle(service);
212 CloseServiceHandle(scm);
217 static void *start_status_routine( void *ptr )
224 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
225 switch ( wait_result )
229 /* the object that we were waiting for has been destroyed (ABANDONED) or
230 * signalled (TIMEOUT_0). We can assume that the startup process is
231 * complete and tell the Service Control Manager that we are now runnng */
232 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
233 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
234 SLAPDServiceStatus.dwCheckPoint++;
235 SLAPDServiceStatus.dwWaitHint = 1000;
236 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
240 /* We've waited for the required time, so send an update to the Service Control
241 * Manager saying to wait again. */
242 SLAPDServiceStatus.dwCheckPoint++;
243 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
244 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
247 /* theres been some problem with WaitForSingleObject so tell the Service
248 * Control Manager to wait 30 seconds before deploying its assasin and
249 * then leave the thread. */
250 SLAPDServiceStatus.dwCheckPoint++;
251 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
252 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
257 ldap_pvt_thread_exit(NULL);
263 static void *stop_status_routine( void *ptr )
270 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
271 switch ( wait_result )
275 /* the object that we were waiting for has been destroyed (ABANDONED) or
276 * signalled (TIMEOUT_0). The shutting down process is therefore complete
277 * and the final SERVICE_STOPPED message will be sent to the service control
278 * manager prior to the process terminating. */
282 /* We've waited for the required time, so send an update to the Service Control
283 * Manager saying to wait again. */
284 SLAPDServiceStatus.dwCheckPoint++;
285 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
286 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
289 /* theres been some problem with WaitForSingleObject so tell the Service
290 * Control Manager to wait 30 seconds before deploying its assasin and
291 * then leave the thread. */
292 SLAPDServiceStatus.dwCheckPoint++;
293 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
294 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
299 ldap_pvt_thread_exit(NULL);
305 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
309 case SERVICE_CONTROL_STOP:
310 case SERVICE_CONTROL_SHUTDOWN:
312 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
313 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
314 SLAPDServiceStatus.dwCheckPoint++;
315 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
316 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
318 ldap_pvt_thread_cond_init( &stopped_event );
319 if ( stopped_event == NULL )
321 /* the event was not created. We will ask the service control manager for 30
322 * seconds to shutdown */
323 SLAPDServiceStatus.dwCheckPoint++;
324 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
325 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
329 /* start a thread to report the progress to the service control manager
330 * until the stopped_event is fired. */
331 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
336 /* failed to create the thread that tells the Service Control Manager that the
337 * service stopping is proceeding.
338 * tell the Service Control Manager to wait another 30 seconds before deploying its
340 SLAPDServiceStatus.dwCheckPoint++;
341 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
342 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
348 case SERVICE_CONTROL_INTERROGATE:
349 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
355 void *getRegParam( char *svc, char *value )
360 static char vValue[1024];
361 DWORD valLen = sizeof( vValue );
364 snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
366 snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
368 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
370 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
374 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
376 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
386 return (void*)&vValue;
388 return (void*)&vValue;
393 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
399 hEventLog = RegisterEventSource( NULL, svc );
401 Inserts[i] = (char *)malloc( 20 );
402 itoa( slap_debug, Inserts[i++], 10 );
403 Inserts[i++] = strdup( configfile );
404 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
405 Inserts[i++] = strdup( is_NT_Service ? "svc" : "cmd" );
407 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
408 MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
410 for ( j = 0; j < i; j++ )
411 ldap_memfree( Inserts[j] );
412 DeregisterEventSource( hEventLog );
417 void LogSlapdStoppedEvent( char *svc )
421 hEventLog = RegisterEventSource( NULL, svc );
422 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
423 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
424 DeregisterEventSource( hEventLog );
428 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
429 void (*stopper)(int) )
431 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
435 /* initialize the Service Status structure */
436 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
437 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
438 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
439 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
440 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
441 SLAPDServiceStatus.dwCheckPoint = 1;
442 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
444 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
446 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
447 * until the slapd listener is completed and listening. Only then should we send
448 * SERVICE_RUNNING to the Service Control Manager. */
449 ldap_pvt_thread_cond_init( &started_event );
450 if ( started_event == NULL)
452 /* failed to create the event to determine when the startup process is complete so
453 * tell the Service Control Manager to wait another 30 seconds before deploying its
455 SLAPDServiceStatus.dwCheckPoint++;
456 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
457 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
461 /* start a thread to report the progress to the service control manager
462 * until the started_event is fired. */
463 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
468 /* failed to create the thread that tells the Service Control Manager that the
469 * service startup is proceeding.
470 * tell the Service Control Manager to wait another 30 seconds before deploying its
472 SLAPDServiceStatus.dwCheckPoint++;
473 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
474 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
479 void ReportSlapdShutdownComplete( )
483 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
484 ldap_pvt_thread_cond_signal( &stopped_event );
485 ldap_pvt_thread_cond_destroy( &stopped_event );
487 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
488 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
489 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
490 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
492 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
493 SLAPDServiceStatus.dwCheckPoint++;
494 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
495 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
499 static char *GetErrorString( int err )
501 static char msgBuf[1024];
504 FORMAT_MESSAGE_FROM_SYSTEM,
506 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
507 msgBuf, 1024, NULL );
512 static char *GetLastErrorString( void )
514 return GetErrorString( GetLastError() );