2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
17 * NT Service manager utilities for OpenLDAP services
22 #ifdef HAVE_NT_SERVICE_MANAGER
24 #include <ac/stdlib.h>
25 #include <ac/string.h>
35 #include "ldap_pvt_thread.h"
38 #include "ldap_defaults.h"
42 #define SCM_NOTIFICATION_INTERVAL 5000
43 #define THIRTY_SECONDS (30 * 1000)
45 int is_NT_Service; /* is this is an NT service? */
47 SERVICE_STATUS lutil_ServiceStatus;
48 SERVICE_STATUS_HANDLE hlutil_ServiceStatus;
50 ldap_pvt_thread_cond_t started_event, stopped_event;
51 ldap_pvt_thread_t start_status_tid, stop_status_tid;
53 void (*stopfunc)(int);
55 static char *GetLastErrorString( void );
57 int lutil_srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
58 LPCTSTR lpszBinaryPathName, int auto_start)
61 DWORD dwValue, dwDisposition;
62 SC_HANDLE schSCManager, schService;
63 char *sp = strchr( lpszBinaryPathName, ' ');
66 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
68 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
70 if ((schService = CreateService(
75 SERVICE_WIN32_OWN_PROCESS,
76 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
79 NULL, NULL, NULL, NULL, NULL)) != NULL)
82 CloseServiceHandle(schService);
83 CloseServiceHandle(schSCManager);
85 snprintf( regpath, sizeof regpath,
86 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
88 /* Create the registry key for event logging to the Windows NT event log. */
89 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
91 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
92 &dwDisposition) != ERROR_SUCCESS)
94 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 lutil_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() );
164 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
172 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
173 strcat(buf, lpszServiceName);
174 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
178 if (lpszBinaryPathName) {
180 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
181 if (strcmp(lpszBinaryPathName, buf))
191 svc_running (LPTSTR lpszServiceName)
198 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
199 return(GetLastError());
202 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
204 if (!QueryServiceStatus(service, &ss))
206 else if (ss.dwCurrentState != SERVICE_STOPPED)
208 CloseServiceHandle(service);
210 CloseServiceHandle(scm);
215 static void *start_status_routine( void *ptr )
222 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
223 switch ( wait_result )
227 /* the object that we were waiting for has been destroyed (ABANDONED) or
228 * signalled (TIMEOUT_0). We can assume that the startup process is
229 * complete and tell the Service Control Manager that we are now runnng */
230 lutil_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
231 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
232 lutil_ServiceStatus.dwCheckPoint++;
233 lutil_ServiceStatus.dwWaitHint = 1000;
234 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
238 /* We've waited for the required time, so send an update to the Service Control
239 * Manager saying to wait again. */
240 lutil_ServiceStatus.dwCheckPoint++;
241 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
242 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
245 /* theres been some problem with WaitForSingleObject so tell the Service
246 * Control Manager to wait 30 seconds before deploying its assasin and
247 * then leave the thread. */
248 lutil_ServiceStatus.dwCheckPoint++;
249 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
250 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
255 ldap_pvt_thread_exit(NULL);
261 static void *stop_status_routine( void *ptr )
268 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
269 switch ( wait_result )
273 /* the object that we were waiting for has been destroyed (ABANDONED) or
274 * signalled (TIMEOUT_0). The shutting down process is therefore complete
275 * and the final SERVICE_STOPPED message will be sent to the service control
276 * manager prior to the process terminating. */
280 /* We've waited for the required time, so send an update to the Service Control
281 * Manager saying to wait again. */
282 lutil_ServiceStatus.dwCheckPoint++;
283 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
284 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
287 /* theres been some problem with WaitForSingleObject so tell the Service
288 * Control Manager to wait 30 seconds before deploying its assasin and
289 * then leave the thread. */
290 lutil_ServiceStatus.dwCheckPoint++;
291 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
292 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
297 ldap_pvt_thread_exit(NULL);
303 static void WINAPI lutil_ServiceCtrlHandler( IN DWORD Opcode)
307 case SERVICE_CONTROL_STOP:
308 case SERVICE_CONTROL_SHUTDOWN:
310 lutil_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
311 lutil_ServiceStatus.dwCheckPoint++;
312 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
313 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
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 lutil_ServiceStatus.dwCheckPoint++;
321 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
322 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
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 lutil_ServiceStatus.dwCheckPoint++;
338 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
339 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
345 case SERVICE_CONTROL_INTERROGATE:
346 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
352 void *lutil_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 )
370 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
381 return (void*)&vValue;
383 return (void*)&vValue;
388 void lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
394 hEventLog = RegisterEventSource( NULL, svc );
396 Inserts[i] = (char *)malloc( 20 );
397 itoa( slap_debug, Inserts[i++], 10 );
398 Inserts[i++] = strdup( configfile );
399 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
401 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
402 MSG_SVC_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
404 for ( j = 0; j < i; j++ )
405 ldap_memfree( Inserts[j] );
406 DeregisterEventSource( hEventLog );
411 void lutil_LogStoppedEvent( char *svc )
415 hEventLog = RegisterEventSource( NULL, svc );
416 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
417 MSG_SVC_STOPPED, NULL, 0, 0, NULL, NULL );
418 DeregisterEventSource( hEventLog );
422 void lutil_CommenceStartupProcessing( char *lpszServiceName,
423 void (*stopper)(int) )
425 hlutil_ServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)lutil_ServiceCtrlHandler);
429 /* initialize the Service Status structure */
430 lutil_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
431 lutil_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
432 lutil_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
433 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
434 lutil_ServiceStatus.dwServiceSpecificExitCode = 0;
435 lutil_ServiceStatus.dwCheckPoint = 1;
436 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
438 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
440 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
441 * until the slapd listener is completed and listening. Only then should we send
442 * SERVICE_RUNNING to the Service Control Manager. */
443 ldap_pvt_thread_cond_init( &started_event );
444 if ( started_event == NULL)
446 /* failed to create the event to determine when the startup process is complete so
447 * tell the Service Control Manager to wait another 30 seconds before deploying its
449 lutil_ServiceStatus.dwCheckPoint++;
450 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
451 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
455 /* start a thread to report the progress to the service control manager
456 * until the started_event is fired. */
457 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
462 /* failed to create the thread that tells the Service Control Manager that the
463 * service startup is proceeding.
464 * tell the Service Control Manager to wait another 30 seconds before deploying its
466 lutil_ServiceStatus.dwCheckPoint++;
467 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
468 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
473 void lutil_ReportShutdownComplete( )
477 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
478 ldap_pvt_thread_cond_signal( &stopped_event );
479 ldap_pvt_thread_cond_destroy( &stopped_event );
481 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
482 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
483 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
484 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
486 lutil_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
487 lutil_ServiceStatus.dwCheckPoint++;
488 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
489 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
493 static char *GetErrorString( int err )
495 static char msgBuf[1024];
498 FORMAT_MESSAGE_FROM_SYSTEM,
500 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
501 msgBuf, 1024, NULL );
506 static char *GetLastErrorString( void )
508 return GetErrorString( GetLastError() );