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
13 #ifdef HAVE_NT_SERVICE_MANAGER
15 #include <ac/stdlib.h>
16 #include <ac/string.h>
26 #include "ldap_pvt_thread.h"
29 #include "ldap_defaults.h"
33 #define SCM_NOTIFICATION_INTERVAL 5000
34 #define THIRTY_SECONDS (30 * 1000)
36 int is_NT_Service; /* is this is an NT service? */
38 SERVICE_STATUS lutil_ServiceStatus;
39 SERVICE_STATUS_HANDLE hlutil_ServiceStatus;
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);
46 static char *GetLastErrorString( void );
48 int lutil_srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
49 LPCTSTR lpszBinaryPathName, int auto_start)
52 DWORD dwValue, dwDisposition;
53 SC_HANDLE schSCManager, schService;
54 char *sp = strchr( lpszBinaryPathName, ' ');
57 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
59 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
61 if ((schService = CreateService(
66 SERVICE_WIN32_OWN_PROCESS,
67 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
70 NULL, NULL, NULL, NULL, NULL)) != NULL)
73 CloseServiceHandle(schService);
74 CloseServiceHandle(schSCManager);
76 snprintf( regpath, sizeof regpath,
77 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
79 /* Create the registry key for event logging to the Windows NT event log. */
80 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
82 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
83 &dwDisposition) != ERROR_SUCCESS)
85 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 lutil_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() );
155 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
163 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
164 strcat(buf, lpszServiceName);
165 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
169 if (lpszBinaryPathName) {
171 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
172 if (strcmp(lpszBinaryPathName, buf))
182 svc_running (LPTSTR lpszServiceName)
189 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
190 return(GetLastError());
193 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
195 if (!QueryServiceStatus(service, &ss))
197 else if (ss.dwCurrentState != SERVICE_STOPPED)
199 CloseServiceHandle(service);
201 CloseServiceHandle(scm);
206 static void *start_status_routine( void *ptr )
213 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
214 switch ( wait_result )
218 /* the object that we were waiting for has been destroyed (ABANDONED) or
219 * signalled (TIMEOUT_0). We can assume that the startup process is
220 * complete and tell the Service Control Manager that we are now runnng */
221 lutil_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
222 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
223 lutil_ServiceStatus.dwCheckPoint++;
224 lutil_ServiceStatus.dwWaitHint = 1000;
225 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
229 /* We've waited for the required time, so send an update to the Service Control
230 * Manager saying to wait again. */
231 lutil_ServiceStatus.dwCheckPoint++;
232 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
233 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
236 /* theres been some problem with WaitForSingleObject so tell the Service
237 * Control Manager to wait 30 seconds before deploying its assasin and
238 * then leave the thread. */
239 lutil_ServiceStatus.dwCheckPoint++;
240 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
241 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
246 ldap_pvt_thread_exit(NULL);
252 static void *stop_status_routine( void *ptr )
259 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
260 switch ( wait_result )
264 /* the object that we were waiting for has been destroyed (ABANDONED) or
265 * signalled (TIMEOUT_0). The shutting down process is therefore complete
266 * and the final SERVICE_STOPPED message will be sent to the service control
267 * manager prior to the process terminating. */
271 /* We've waited for the required time, so send an update to the Service Control
272 * Manager saying to wait again. */
273 lutil_ServiceStatus.dwCheckPoint++;
274 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
275 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
278 /* theres been some problem with WaitForSingleObject so tell the Service
279 * Control Manager to wait 30 seconds before deploying its assasin and
280 * then leave the thread. */
281 lutil_ServiceStatus.dwCheckPoint++;
282 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
283 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
288 ldap_pvt_thread_exit(NULL);
294 void WINAPI lutil_ServiceCtrlHandler( IN DWORD Opcode)
298 case SERVICE_CONTROL_STOP:
299 case SERVICE_CONTROL_SHUTDOWN:
301 lutil_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
302 lutil_ServiceStatus.dwCheckPoint++;
303 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
304 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
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 lutil_ServiceStatus.dwCheckPoint++;
312 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
313 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
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 lutil_ServiceStatus.dwCheckPoint++;
329 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
330 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
336 case SERVICE_CONTROL_INTERROGATE:
337 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
343 void *lutil_getRegParam( char *svc, char *value )
348 static char vValue[1024];
349 DWORD valLen = sizeof( vValue );
352 snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
354 snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
356 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
361 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
372 return (void*)&vValue;
374 return (void*)&vValue;
379 void lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
385 hEventLog = RegisterEventSource( NULL, svc );
387 Inserts[i] = (char *)malloc( 20 );
388 itoa( slap_debug, Inserts[i++], 10 );
389 Inserts[i++] = strdup( configfile );
390 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
392 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
393 MSG_SVC_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
395 for ( j = 0; j < i; j++ )
396 ldap_memfree( Inserts[j] );
397 DeregisterEventSource( hEventLog );
402 void lutil_LogStoppedEvent( char *svc )
406 hEventLog = RegisterEventSource( NULL, svc );
407 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
408 MSG_SVC_STOPPED, NULL, 0, 0, NULL, NULL );
409 DeregisterEventSource( hEventLog );
413 void lutil_CommenceStartupProcessing( char *lpszServiceName,
414 void (*stopper)(int) )
416 hlutil_ServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)lutil_ServiceCtrlHandler);
420 /* initialize the Service Status structure */
421 lutil_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
422 lutil_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
423 lutil_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
424 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
425 lutil_ServiceStatus.dwServiceSpecificExitCode = 0;
426 lutil_ServiceStatus.dwCheckPoint = 1;
427 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
429 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
431 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
432 * until the slapd listener is completed and listening. Only then should we send
433 * SERVICE_RUNNING to the Service Control Manager. */
434 ldap_pvt_thread_cond_init( &started_event );
435 if ( started_event == NULL)
437 /* failed to create the event to determine when the startup process is complete so
438 * tell the Service Control Manager to wait another 30 seconds before deploying its
440 lutil_ServiceStatus.dwCheckPoint++;
441 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
442 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
446 /* start a thread to report the progress to the service control manager
447 * until the started_event is fired. */
448 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
453 /* failed to create the thread that tells the Service Control Manager that the
454 * service startup is proceeding.
455 * tell the Service Control Manager to wait another 30 seconds before deploying its
457 lutil_ServiceStatus.dwCheckPoint++;
458 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
459 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
464 void lutil_ReportShutdownComplete( )
468 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
469 ldap_pvt_thread_cond_signal( &stopped_event );
470 ldap_pvt_thread_cond_destroy( &stopped_event );
472 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
473 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
474 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
475 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
477 lutil_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
478 lutil_ServiceStatus.dwCheckPoint++;
479 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
480 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
484 static char *GetErrorString( int err )
486 static char msgBuf[1024];
489 FORMAT_MESSAGE_FROM_SYSTEM,
491 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
492 msgBuf, 1024, NULL );
497 static char *GetLastErrorString( void )
499 return GetErrorString( GetLastError() );