2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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>
34 #include "ldap_pvt_thread.h"
36 #include "ldap_defaults.h"
40 #define SCM_NOTIFICATION_INTERVAL 5000
41 #define THIRTY_SECONDS (30 * 1000)
43 int is_NT_Service; /* is this is an NT service? */
45 SERVICE_STATUS lutil_ServiceStatus;
46 SERVICE_STATUS_HANDLE hlutil_ServiceStatus;
48 ldap_pvt_thread_cond_t started_event, stopped_event;
49 ldap_pvt_thread_t start_status_tid, stop_status_tid;
51 void (*stopfunc)(int);
53 static char *GetLastErrorString( void );
55 int lutil_srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
56 LPCTSTR lpszBinaryPathName, int auto_start)
59 DWORD dwValue, dwDisposition;
60 SC_HANDLE schSCManager, schService;
61 char *sp = strchr( lpszBinaryPathName, ' ');
64 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
66 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
68 if ((schService = CreateService(
73 SERVICE_WIN32_OWN_PROCESS,
74 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
77 NULL, NULL, NULL, NULL, NULL)) != NULL)
80 CloseServiceHandle(schService);
81 CloseServiceHandle(schSCManager);
83 snprintf( regpath, sizeof regpath,
84 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
86 /* Create the registry key for event logging to the Windows NT event log. */
87 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
89 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
90 &dwDisposition) != ERROR_SUCCESS)
92 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
97 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
99 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
104 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
105 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
107 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
116 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
117 CloseServiceHandle(schSCManager);
122 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
127 int lutil_srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
129 SC_HANDLE schSCManager, schService;
131 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
132 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
134 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
136 if ( DeleteService(schService) == TRUE)
138 CloseServiceHandle(schService);
139 CloseServiceHandle(schSCManager);
142 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
143 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
144 CloseServiceHandle(schService);
145 CloseServiceHandle(schSCManager);
149 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
150 CloseServiceHandle(schSCManager);
155 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
162 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
170 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
171 strcat(buf, lpszServiceName);
172 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
176 if (lpszBinaryPathName) {
178 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
179 if (strcmp(lpszBinaryPathName, buf))
189 svc_running (LPTSTR lpszServiceName)
196 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
197 return(GetLastError());
200 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
202 if (!QueryServiceStatus(service, &ss))
204 else if (ss.dwCurrentState != SERVICE_STOPPED)
206 CloseServiceHandle(service);
208 CloseServiceHandle(scm);
213 static void *start_status_routine( void *ptr )
220 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
221 switch ( wait_result )
225 /* the object that we were waiting for has been destroyed (ABANDONED) or
226 * signalled (TIMEOUT_0). We can assume that the startup process is
227 * complete and tell the Service Control Manager that we are now runnng */
228 lutil_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
229 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
230 lutil_ServiceStatus.dwCheckPoint++;
231 lutil_ServiceStatus.dwWaitHint = 1000;
232 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
236 /* We've waited for the required time, so send an update to the Service Control
237 * Manager saying to wait again. */
238 lutil_ServiceStatus.dwCheckPoint++;
239 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
240 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
243 /* theres been some problem with WaitForSingleObject so tell the Service
244 * Control Manager to wait 30 seconds before deploying its assasin and
245 * then leave the thread. */
246 lutil_ServiceStatus.dwCheckPoint++;
247 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
248 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
253 ldap_pvt_thread_exit(NULL);
259 static void *stop_status_routine( void *ptr )
266 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
267 switch ( wait_result )
271 /* the object that we were waiting for has been destroyed (ABANDONED) or
272 * signalled (TIMEOUT_0). The shutting down process is therefore complete
273 * and the final SERVICE_STOPPED message will be sent to the service control
274 * manager prior to the process terminating. */
278 /* We've waited for the required time, so send an update to the Service Control
279 * Manager saying to wait again. */
280 lutil_ServiceStatus.dwCheckPoint++;
281 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
282 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
285 /* theres been some problem with WaitForSingleObject so tell the Service
286 * Control Manager to wait 30 seconds before deploying its assasin and
287 * then leave the thread. */
288 lutil_ServiceStatus.dwCheckPoint++;
289 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
290 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
295 ldap_pvt_thread_exit(NULL);
301 static void WINAPI lutil_ServiceCtrlHandler( IN DWORD Opcode)
305 case SERVICE_CONTROL_STOP:
306 case SERVICE_CONTROL_SHUTDOWN:
308 lutil_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
309 lutil_ServiceStatus.dwCheckPoint++;
310 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
311 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
313 ldap_pvt_thread_cond_init( &stopped_event );
314 if ( stopped_event == NULL )
316 /* the event was not created. We will ask the service control manager for 30
317 * seconds to shutdown */
318 lutil_ServiceStatus.dwCheckPoint++;
319 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
320 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
324 /* start a thread to report the progress to the service control manager
325 * until the stopped_event is fired. */
326 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
331 /* failed to create the thread that tells the Service Control Manager that the
332 * service stopping is proceeding.
333 * tell the Service Control Manager to wait another 30 seconds before deploying its
335 lutil_ServiceStatus.dwCheckPoint++;
336 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
337 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
343 case SERVICE_CONTROL_INTERROGATE:
344 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
350 void *lutil_getRegParam( char *svc, char *value )
355 static char vValue[1024];
356 DWORD valLen = sizeof( vValue );
359 snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
361 snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
363 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
368 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
379 return (void*)&vValue;
381 return (void*)&vValue;
386 void lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
392 hEventLog = RegisterEventSource( NULL, svc );
394 Inserts[i] = (char *)malloc( 20 );
395 itoa( slap_debug, Inserts[i++], 10 );
396 Inserts[i++] = strdup( configfile );
397 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
399 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
400 MSG_SVC_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
402 for ( j = 0; j < i; j++ )
403 ldap_memfree( Inserts[j] );
404 DeregisterEventSource( hEventLog );
409 void lutil_LogStoppedEvent( char *svc )
413 hEventLog = RegisterEventSource( NULL, svc );
414 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
415 MSG_SVC_STOPPED, NULL, 0, 0, NULL, NULL );
416 DeregisterEventSource( hEventLog );
420 void lutil_CommenceStartupProcessing( char *lpszServiceName,
421 void (*stopper)(int) )
423 hlutil_ServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)lutil_ServiceCtrlHandler);
427 /* initialize the Service Status structure */
428 lutil_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
429 lutil_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
430 lutil_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
431 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
432 lutil_ServiceStatus.dwServiceSpecificExitCode = 0;
433 lutil_ServiceStatus.dwCheckPoint = 1;
434 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
436 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
438 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
439 * until the slapd listener is completed and listening. Only then should we send
440 * SERVICE_RUNNING to the Service Control Manager. */
441 ldap_pvt_thread_cond_init( &started_event );
442 if ( started_event == NULL)
444 /* failed to create the event to determine when the startup process is complete so
445 * tell the Service Control Manager to wait another 30 seconds before deploying its
447 lutil_ServiceStatus.dwCheckPoint++;
448 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
449 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
453 /* start a thread to report the progress to the service control manager
454 * until the started_event is fired. */
455 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
460 /* failed to create the thread that tells the Service Control Manager that the
461 * service startup is proceeding.
462 * tell the Service Control Manager to wait another 30 seconds before deploying its
464 lutil_ServiceStatus.dwCheckPoint++;
465 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
466 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
471 void lutil_ReportShutdownComplete( )
475 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
476 ldap_pvt_thread_cond_signal( &stopped_event );
477 ldap_pvt_thread_cond_destroy( &stopped_event );
479 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
480 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
481 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
482 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
484 lutil_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
485 lutil_ServiceStatus.dwCheckPoint++;
486 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
487 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
491 static char *GetErrorString( int err )
493 static char msgBuf[1024];
496 FORMAT_MESSAGE_FROM_SYSTEM,
498 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
499 msgBuf, 1024, NULL );
504 static char *GetLastErrorString( void )
506 return GetErrorString( GetLastError() );