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 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
162 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
163 strcat(buf, lpszServiceName);
164 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
168 if (lpszBinaryPathName) {
170 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
171 if (strcmp(lpszBinaryPathName, buf))
181 svc_running (LPTSTR lpszServiceName)
188 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
189 return(GetLastError());
192 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
194 if (!QueryServiceStatus(service, &ss))
196 else if (ss.dwCurrentState != SERVICE_STOPPED)
198 CloseServiceHandle(service);
200 CloseServiceHandle(scm);
205 static void *start_status_routine( void *ptr )
212 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
213 switch ( wait_result )
217 /* the object that we were waiting for has been destroyed (ABANDONED) or
218 * signalled (TIMEOUT_0). We can assume that the startup process is
219 * complete and tell the Service Control Manager that we are now runnng */
220 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
221 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
222 SLAPDServiceStatus.dwCheckPoint++;
223 SLAPDServiceStatus.dwWaitHint = 1000;
224 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
228 /* We've waited for the required time, so send an update to the Service Control
229 * Manager saying to wait again. */
230 SLAPDServiceStatus.dwCheckPoint++;
231 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
232 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
235 /* theres been some problem with WaitForSingleObject so tell the Service
236 * Control Manager to wait 30 seconds before deploying its assasin and
237 * then leave the thread. */
238 SLAPDServiceStatus.dwCheckPoint++;
239 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
240 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
245 ldap_pvt_thread_exit(NULL);
251 static void *stop_status_routine( void *ptr )
258 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
259 switch ( wait_result )
263 /* the object that we were waiting for has been destroyed (ABANDONED) or
264 * signalled (TIMEOUT_0). The shutting down process is therefore complete
265 * and the final SERVICE_STOPPED message will be sent to the service control
266 * manager prior to the process terminating. */
270 /* We've waited for the required time, so send an update to the Service Control
271 * Manager saying to wait again. */
272 SLAPDServiceStatus.dwCheckPoint++;
273 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
274 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
277 /* theres been some problem with WaitForSingleObject so tell the Service
278 * Control Manager to wait 30 seconds before deploying its assasin and
279 * then leave the thread. */
280 SLAPDServiceStatus.dwCheckPoint++;
281 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
282 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
287 ldap_pvt_thread_exit(NULL);
293 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
297 case SERVICE_CONTROL_STOP:
298 case SERVICE_CONTROL_SHUTDOWN:
300 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
301 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
302 SLAPDServiceStatus.dwCheckPoint++;
303 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
304 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
306 ldap_pvt_thread_cond_init( &stopped_event );
307 if ( stopped_event == NULL )
309 /* the event was not created. We will ask the service control manager for 30
310 * seconds to shutdown */
311 SLAPDServiceStatus.dwCheckPoint++;
312 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
313 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
317 /* start a thread to report the progress to the service control manager
318 * until the stopped_event is fired. */
319 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
324 /* failed to create the thread that tells the Service Control Manager that the
325 * service stopping is proceeding.
326 * tell the Service Control Manager to wait another 30 seconds before deploying its
328 SLAPDServiceStatus.dwCheckPoint++;
329 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
330 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
336 case SERVICE_CONTROL_INTERROGATE:
337 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
343 void *getRegParam( char *svc, char *value )
348 static char vValue[1024];
349 DWORD valLen = sizeof( vValue );
352 sprintf ( path, "SOFTWARE\\%s", svc );
354 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
356 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
358 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
362 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
364 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
374 return (void*)&vValue;
376 return (void*)&vValue;
381 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
387 hEventLog = RegisterEventSource( NULL, svc );
389 Inserts[i] = (char *)malloc( 20 );
390 itoa( slap_debug, Inserts[i++], 10 );
391 Inserts[i++] = strdup( configfile );
392 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
393 Inserts[i++] = strdup( is_NT_Service ? "svc" : "cmd" );
395 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
396 MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
398 for ( j = 0; j < i; j++ )
399 ldap_memfree( Inserts[j] );
400 DeregisterEventSource( hEventLog );
405 void LogSlapdStoppedEvent( char *svc )
409 hEventLog = RegisterEventSource( NULL, svc );
410 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
411 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
412 DeregisterEventSource( hEventLog );
416 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
417 void (*stopper)(int) )
419 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
423 /* initialize the Service Status structure */
424 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
425 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
426 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
427 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
428 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
429 SLAPDServiceStatus.dwCheckPoint = 1;
430 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
432 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
434 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
435 * until the slapd listener is completed and listening. Only then should we send
436 * SERVICE_RUNNING to the Service Control Manager. */
437 ldap_pvt_thread_cond_init( &started_event );
438 if ( started_event == NULL)
440 /* failed to create the event to determine when the startup process is complete so
441 * tell the Service Control Manager to wait another 30 seconds before deploying its
443 SLAPDServiceStatus.dwCheckPoint++;
444 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
445 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
449 /* start a thread to report the progress to the service control manager
450 * until the started_event is fired. */
451 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
456 /* failed to create the thread that tells the Service Control Manager that the
457 * service startup is proceeding.
458 * tell the Service Control Manager to wait another 30 seconds before deploying its
460 SLAPDServiceStatus.dwCheckPoint++;
461 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
462 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
467 void ReportSlapdShutdownComplete( )
471 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
472 ldap_pvt_thread_cond_signal( &stopped_event );
473 ldap_pvt_thread_cond_destroy( &stopped_event );
475 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
476 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
477 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
478 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
480 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
481 SLAPDServiceStatus.dwCheckPoint++;
482 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
483 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
487 static char *GetErrorString( int err )
489 static char msgBuf[1024];
492 FORMAT_MESSAGE_FROM_SYSTEM,
494 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
495 msgBuf, 1024, NULL );
500 static char *GetLastErrorString( void )
502 return GetErrorString( GetLastError() );