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 lpszDisplayName,
50 LPCTSTR lpszBinaryPathName, BOOL auto_start)
53 DWORD dwValue, dwDisposition;
54 SC_HANDLE schSCManager, schService;
56 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
57 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
59 if ((schService = CreateService(
64 SERVICE_WIN32_OWN_PROCESS,
65 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
68 NULL, NULL, NULL, NULL, NULL)) != NULL)
71 CloseServiceHandle(schService);
72 CloseServiceHandle(schSCManager);
74 sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
76 /* Create the registry key for event logging to the Windows NT event log. */
77 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
79 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
80 &dwDisposition) != ERROR_SUCCESS)
82 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
86 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
88 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
93 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
94 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
96 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
105 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
106 CloseServiceHandle(schSCManager);
111 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
116 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
118 SC_HANDLE schSCManager, schService;
120 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
121 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
123 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
125 if ( DeleteService(schService) == TRUE)
127 CloseServiceHandle(schService);
128 CloseServiceHandle(schSCManager);
131 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
132 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
133 CloseServiceHandle(schService);
134 CloseServiceHandle(schSCManager);
138 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
139 CloseServiceHandle(schSCManager);
144 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
150 static void *start_status_routine( void *ptr )
157 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
158 switch ( wait_result )
162 /* the object that we were waiting for has been destroyed (ABANDONED) or
163 * signalled (TIMEOUT_0). We can assume that the startup process is
164 * complete and tell the Service Control Manager that we are now runnng */
165 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
166 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
167 SLAPDServiceStatus.dwCheckPoint++;
168 SLAPDServiceStatus.dwWaitHint = 1000;
169 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
173 /* We've waited for the required time, so send an update to the Service Control
174 * Manager saying to wait again. */
175 SLAPDServiceStatus.dwCheckPoint++;
176 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
177 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
180 /* theres been some problem with WaitForSingleObject so tell the Service
181 * Control Manager to wait 30 seconds before deploying its assasin and
182 * then leave the thread. */
183 SLAPDServiceStatus.dwCheckPoint++;
184 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
185 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
190 ldap_pvt_thread_exit(NULL);
196 static void *stop_status_routine( void *ptr )
203 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
204 switch ( wait_result )
208 /* the object that we were waiting for has been destroyed (ABANDONED) or
209 * signalled (TIMEOUT_0). The shutting down process is therefore complete
210 * and the final SERVICE_STOPPED message will be sent to the service control
211 * manager prior to the process terminating. */
215 /* We've waited for the required time, so send an update to the Service Control
216 * Manager saying to wait again. */
217 SLAPDServiceStatus.dwCheckPoint++;
218 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
219 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
222 /* theres been some problem with WaitForSingleObject so tell the Service
223 * Control Manager to wait 30 seconds before deploying its assasin and
224 * then leave the thread. */
225 SLAPDServiceStatus.dwCheckPoint++;
226 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
227 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
232 ldap_pvt_thread_exit(NULL);
238 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
242 case SERVICE_CONTROL_STOP:
243 case SERVICE_CONTROL_SHUTDOWN:
245 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
246 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
247 SLAPDServiceStatus.dwCheckPoint++;
248 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
249 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
251 ldap_pvt_thread_cond_init( &stopped_event );
252 if ( stopped_event == NULL )
254 /* the event was not created. We will ask the service control manager for 30
255 * seconds to shutdown */
256 SLAPDServiceStatus.dwCheckPoint++;
257 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
258 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
262 /* start a thread to report the progress to the service control manager
263 * until the stopped_event is fired. */
264 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
269 /* failed to create the thread that tells the Service Control Manager that the
270 * service stopping is proceeding.
271 * tell the Service Control Manager to wait another 30 seconds before deploying its
273 SLAPDServiceStatus.dwCheckPoint++;
274 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
275 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
281 case SERVICE_CONTROL_INTERROGATE:
282 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
288 void *getRegParam( char *svc, char *value )
293 static char vValue[1024];
294 DWORD valLen = sizeof( vValue );
296 if ( svc && strcmp(svc, SERVICE_NAME) )
297 sprintf ( path, "SOFTWARE\\%s", svc );
299 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
301 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
303 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
307 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
309 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
319 return (void*)&vValue;
321 return (void*)&vValue;
326 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
332 hEventLog = RegisterEventSource( NULL, svc );
334 Inserts[i] = (char *)malloc( 20 );
335 itoa( slap_debug, Inserts[i++], 10 );
336 Inserts[i++] = ldap_pvt_strdup( configfile );
337 Inserts[i++] = ldap_pvt_strdup( urls ? urls : "ldap:///" );
338 Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
340 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
341 MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
343 for ( j = 0; j < i; j++ )
344 ldap_memfree( Inserts[j] );
345 DeregisterEventSource( hEventLog );
350 void LogSlapdStoppedEvent( char *svc )
354 hEventLog = RegisterEventSource( NULL, svc );
355 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
356 MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
357 DeregisterEventSource( hEventLog );
361 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
362 void (*stopper)(int) )
364 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
368 /* initialize the Service Status structure */
369 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
370 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
371 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
372 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
373 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
374 SLAPDServiceStatus.dwCheckPoint = 1;
375 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
377 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
379 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
380 * until the slapd listener is completed and listening. Only then should we send
381 * SERVICE_RUNNING to the Service Control Manager. */
382 ldap_pvt_thread_cond_init( &started_event );
383 if ( started_event == NULL)
385 /* failed to create the event to determine when the startup process is complete so
386 * tell the Service Control Manager to wait another 30 seconds before deploying its
388 SLAPDServiceStatus.dwCheckPoint++;
389 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
390 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
394 /* start a thread to report the progress to the service control manager
395 * until the started_event is fired. */
396 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
401 /* failed to create the thread that tells the Service Control Manager that the
402 * service startup is proceeding.
403 * tell the Service Control Manager to wait another 30 seconds before deploying its
405 SLAPDServiceStatus.dwCheckPoint++;
406 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
407 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
412 void ReportSlapdShutdownComplete( )
416 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
417 ldap_pvt_thread_cond_signal( &stopped_event );
418 ldap_pvt_thread_cond_destroy( &stopped_event );
420 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
421 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
422 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
423 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
425 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
426 SLAPDServiceStatus.dwCheckPoint++;
427 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
428 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);