3 * Copyright 1998-2002 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 = 1; /* assume this is an NT service until determined that */
49 /* startup was from the command line */
51 SERVICE_STATUS SLAPDServiceStatus;
52 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
54 ldap_pvt_thread_cond_t started_event, stopped_event;
55 ldap_pvt_thread_t start_status_tid, stop_status_tid;
57 void (*stopfunc)(int);
59 static char *GetLastErrorString( void );
61 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
62 LPCTSTR lpszBinaryPathName, BOOL auto_start)
65 DWORD dwValue, dwDisposition;
66 SC_HANDLE schSCManager, schService;
68 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
69 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
71 if ((schService = CreateService(
76 SERVICE_WIN32_OWN_PROCESS,
77 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
80 NULL, NULL, NULL, NULL, NULL)) != NULL)
83 CloseServiceHandle(schService);
84 CloseServiceHandle(schSCManager);
86 snprintf( regpath, sizeof regpath,
87 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
89 /* Create the registry key for event logging to the Windows NT event log. */
90 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
92 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
93 &dwDisposition) != ERROR_SUCCESS)
95 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
99 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
101 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
106 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
107 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
109 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
118 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
119 CloseServiceHandle(schSCManager);
124 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
129 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
131 SC_HANDLE schSCManager, schService;
133 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
134 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
136 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
138 if ( DeleteService(schService) == TRUE)
140 CloseServiceHandle(schService);
141 CloseServiceHandle(schSCManager);
144 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
145 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
146 CloseServiceHandle(schService);
147 CloseServiceHandle(schSCManager);
151 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
152 CloseServiceHandle(schSCManager);
157 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
163 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
171 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
172 strcat(buf, lpszServiceName);
173 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
177 if (lpszBinaryPathName) {
179 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
180 if (strcmp(lpszBinaryPathName, buf))
190 svc_running (LPTSTR lpszServiceName)
197 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
198 return(GetLastError());
201 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
203 if (!QueryServiceStatus(service, &ss))
205 else if (ss.dwCurrentState != SERVICE_STOPPED)
207 CloseServiceHandle(service);
209 CloseServiceHandle(scm);
214 static void *start_status_routine( void *ptr )
221 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
222 switch ( wait_result )
226 /* the object that we were waiting for has been destroyed (ABANDONED) or
227 * signalled (TIMEOUT_0). We can assume that the startup process is
228 * complete and tell the Service Control Manager that we are now runnng */
229 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
230 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
231 SLAPDServiceStatus.dwCheckPoint++;
232 SLAPDServiceStatus.dwWaitHint = 1000;
233 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
237 /* We've waited for the required time, so send an update to the Service Control
238 * Manager saying to wait again. */
239 SLAPDServiceStatus.dwCheckPoint++;
240 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
241 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
244 /* theres been some problem with WaitForSingleObject so tell the Service
245 * Control Manager to wait 30 seconds before deploying its assasin and
246 * then leave the thread. */
247 SLAPDServiceStatus.dwCheckPoint++;
248 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
249 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
254 ldap_pvt_thread_exit(NULL);
260 static void *stop_status_routine( void *ptr )
267 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
268 switch ( wait_result )
272 /* the object that we were waiting for has been destroyed (ABANDONED) or
273 * signalled (TIMEOUT_0). The shutting down process is therefore complete
274 * and the final SERVICE_STOPPED message will be sent to the service control
275 * manager prior to the process terminating. */
279 /* We've waited for the required time, so send an update to the Service Control
280 * Manager saying to wait again. */
281 SLAPDServiceStatus.dwCheckPoint++;
282 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
283 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
286 /* theres been some problem with WaitForSingleObject so tell the Service
287 * Control Manager to wait 30 seconds before deploying its assasin and
288 * then leave the thread. */
289 SLAPDServiceStatus.dwCheckPoint++;
290 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
291 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
296 ldap_pvt_thread_exit(NULL);
302 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
306 case SERVICE_CONTROL_STOP:
307 case SERVICE_CONTROL_SHUTDOWN:
309 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
310 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
311 SLAPDServiceStatus.dwCheckPoint++;
312 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
313 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
315 ldap_pvt_thread_cond_init( &stopped_event );
316 if ( stopped_event == NULL )
318 /* the event was not created. We will ask the service control manager for 30
319 * seconds to shutdown */
320 SLAPDServiceStatus.dwCheckPoint++;
321 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
322 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
326 /* start a thread to report the progress to the service control manager
327 * until the stopped_event is fired. */
328 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
333 /* failed to create the thread that tells the Service Control Manager that the
334 * service stopping is proceeding.
335 * tell the Service Control Manager to wait another 30 seconds before deploying its
337 SLAPDServiceStatus.dwCheckPoint++;
338 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
339 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
345 case SERVICE_CONTROL_INTERROGATE:
346 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
352 void *getRegParam( char *svc, char *value )
357 static char vValue[1024];
358 DWORD valLen = sizeof( vValue );
361 snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
363 snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
365 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
367 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
371 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
373 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
383 return (void*)&vValue;
385 return (void*)&vValue;
390 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
396 hEventLog = RegisterEventSource( NULL, svc );
398 Inserts[i] = (char *)malloc( 20 );
399 itoa( slap_debug, Inserts[i++], 10 );
400 Inserts[i++] = strdup( configfile );
401 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
402 Inserts[i++] = strdup( is_NT_Service ? "svc" : "cmd" );
404 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
405 MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
407 for ( j = 0; j < i; j++ )
408 ldap_memfree( Inserts[j] );
409 DeregisterEventSource( hEventLog );
414 void LogSlapdStoppedEvent( char *svc )
418 hEventLog = RegisterEventSource( NULL, svc );
419 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
420 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
421 DeregisterEventSource( hEventLog );
425 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
426 void (*stopper)(int) )
428 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
432 /* initialize the Service Status structure */
433 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
434 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
435 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
436 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
437 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
438 SLAPDServiceStatus.dwCheckPoint = 1;
439 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
441 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
443 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
444 * until the slapd listener is completed and listening. Only then should we send
445 * SERVICE_RUNNING to the Service Control Manager. */
446 ldap_pvt_thread_cond_init( &started_event );
447 if ( started_event == NULL)
449 /* failed to create the event to determine when the startup process is complete so
450 * tell the Service Control Manager to wait another 30 seconds before deploying its
452 SLAPDServiceStatus.dwCheckPoint++;
453 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
454 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
458 /* start a thread to report the progress to the service control manager
459 * until the started_event is fired. */
460 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
465 /* failed to create the thread that tells the Service Control Manager that the
466 * service startup is proceeding.
467 * tell the Service Control Manager to wait another 30 seconds before deploying its
469 SLAPDServiceStatus.dwCheckPoint++;
470 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
471 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
476 void ReportSlapdShutdownComplete( )
480 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
481 ldap_pvt_thread_cond_signal( &stopped_event );
482 ldap_pvt_thread_cond_destroy( &stopped_event );
484 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
485 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
486 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
487 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
489 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
490 SLAPDServiceStatus.dwCheckPoint++;
491 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
492 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
496 static char *GetErrorString( int err )
498 static char msgBuf[1024];
501 FORMAT_MESSAGE_FROM_SYSTEM,
503 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
504 msgBuf, 1024, NULL );
509 static char *GetLastErrorString( void )
511 return GetErrorString( GetLastError() );