2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2018 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 = strrchr( lpszBinaryPathName, '\\');
63 if ( sp ) sp = strchr(sp, ' ');
65 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
67 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
69 if ((schService = CreateService(
74 SERVICE_WIN32_OWN_PROCESS,
75 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
78 NULL, NULL, NULL, NULL, NULL)) != NULL)
81 CloseServiceHandle(schService);
82 CloseServiceHandle(schSCManager);
84 snprintf( regpath, sizeof regpath,
85 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
87 /* Create the registry key for event logging to the Windows NT event log. */
88 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
90 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
91 &dwDisposition) != ERROR_SUCCESS)
93 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
98 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
100 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
105 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
106 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
108 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
117 fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
118 CloseServiceHandle(schSCManager);
123 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
128 int lutil_srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
130 SC_HANDLE schSCManager, schService;
132 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
133 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
135 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
137 if ( DeleteService(schService) == TRUE)
139 CloseServiceHandle(schService);
140 CloseServiceHandle(schSCManager);
143 fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
144 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
145 CloseServiceHandle(schService);
146 CloseServiceHandle(schSCManager);
150 fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
151 CloseServiceHandle(schSCManager);
156 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
163 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
171 strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
172 strcat(buf, lpszServiceName);
173 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
177 if (lpszBinaryPathName) {
179 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
180 if (strcmp(lpszBinaryPathName, buf))
190 svc_running (LPTSTR lpszServiceName)
197 if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
198 return(GetLastError());
201 service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
203 if (!QueryServiceStatus(service, &ss))
205 else if (ss.dwCurrentState != SERVICE_STOPPED)
207 CloseServiceHandle(service);
209 CloseServiceHandle(scm);
214 static void *start_status_routine( void *ptr )
221 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
222 switch ( wait_result )
226 /* the object that we were waiting for has been destroyed (ABANDONED) or
227 * signalled (TIMEOUT_0). We can assume that the startup process is
228 * complete and tell the Service Control Manager that we are now runnng */
229 lutil_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
230 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
231 lutil_ServiceStatus.dwCheckPoint++;
232 lutil_ServiceStatus.dwWaitHint = 1000;
233 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
237 /* We've waited for the required time, so send an update to the Service Control
238 * Manager saying to wait again. */
239 lutil_ServiceStatus.dwCheckPoint++;
240 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
241 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
244 /* theres been some problem with WaitForSingleObject so tell the Service
245 * Control Manager to wait 30 seconds before deploying its assasin and
246 * then leave the thread. */
247 lutil_ServiceStatus.dwCheckPoint++;
248 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
249 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
254 ldap_pvt_thread_exit(NULL);
260 static void *stop_status_routine( void *ptr )
267 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
268 switch ( wait_result )
272 /* the object that we were waiting for has been destroyed (ABANDONED) or
273 * signalled (TIMEOUT_0). The shutting down process is therefore complete
274 * and the final SERVICE_STOPPED message will be sent to the service control
275 * manager prior to the process terminating. */
279 /* We've waited for the required time, so send an update to the Service Control
280 * Manager saying to wait again. */
281 lutil_ServiceStatus.dwCheckPoint++;
282 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
283 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
286 /* theres been some problem with WaitForSingleObject so tell the Service
287 * Control Manager to wait 30 seconds before deploying its assasin and
288 * then leave the thread. */
289 lutil_ServiceStatus.dwCheckPoint++;
290 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
291 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
296 ldap_pvt_thread_exit(NULL);
302 static void WINAPI lutil_ServiceCtrlHandler( IN DWORD Opcode)
306 case SERVICE_CONTROL_STOP:
307 case SERVICE_CONTROL_SHUTDOWN:
309 lutil_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
310 lutil_ServiceStatus.dwCheckPoint++;
311 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
312 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
314 ldap_pvt_thread_cond_init( &stopped_event );
315 if ( stopped_event == NULL )
317 /* the event was not created. We will ask the service control manager for 30
318 * seconds to shutdown */
319 lutil_ServiceStatus.dwCheckPoint++;
320 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
321 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
325 /* start a thread to report the progress to the service control manager
326 * until the stopped_event is fired. */
327 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
332 /* failed to create the thread that tells the Service Control Manager that the
333 * service stopping is proceeding.
334 * tell the Service Control Manager to wait another 30 seconds before deploying its
336 lutil_ServiceStatus.dwCheckPoint++;
337 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
338 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
344 case SERVICE_CONTROL_INTERROGATE:
345 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
351 void *lutil_getRegParam( char *svc, char *value )
356 static char vValue[1024];
357 DWORD valLen = sizeof( vValue );
360 snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
362 snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
364 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
369 if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
380 return (void*)&vValue;
382 return (void*)&vValue;
387 void lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
393 hEventLog = RegisterEventSource( NULL, svc );
395 Inserts[i] = (char *)malloc( 20 );
396 itoa( slap_debug, Inserts[i++], 10 );
397 Inserts[i++] = strdup( configfile );
398 Inserts[i++] = strdup( urls ? urls : "ldap:///" );
400 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
401 MSG_SVC_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
403 for ( j = 0; j < i; j++ )
404 ldap_memfree( Inserts[j] );
405 DeregisterEventSource( hEventLog );
410 void lutil_LogStoppedEvent( char *svc )
414 hEventLog = RegisterEventSource( NULL, svc );
415 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
416 MSG_SVC_STOPPED, NULL, 0, 0, NULL, NULL );
417 DeregisterEventSource( hEventLog );
421 void lutil_CommenceStartupProcessing( char *lpszServiceName,
422 void (*stopper)(int) )
424 hlutil_ServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)lutil_ServiceCtrlHandler);
428 /* initialize the Service Status structure */
429 lutil_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
430 lutil_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
431 lutil_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
432 lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
433 lutil_ServiceStatus.dwServiceSpecificExitCode = 0;
434 lutil_ServiceStatus.dwCheckPoint = 1;
435 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
437 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
439 /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
440 * until the slapd listener is completed and listening. Only then should we send
441 * SERVICE_RUNNING to the Service Control Manager. */
442 ldap_pvt_thread_cond_init( &started_event );
443 if ( started_event == NULL)
445 /* failed to create the event to determine when the startup process is complete so
446 * tell the Service Control Manager to wait another 30 seconds before deploying its
448 lutil_ServiceStatus.dwCheckPoint++;
449 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
450 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
454 /* start a thread to report the progress to the service control manager
455 * until the started_event is fired. */
456 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
461 /* failed to create the thread that tells the Service Control Manager that the
462 * service startup is proceeding.
463 * tell the Service Control Manager to wait another 30 seconds before deploying its
465 lutil_ServiceStatus.dwCheckPoint++;
466 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
467 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
472 void lutil_ReportShutdownComplete( )
476 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
477 ldap_pvt_thread_cond_signal( &stopped_event );
478 ldap_pvt_thread_cond_destroy( &stopped_event );
480 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
481 * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
482 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
483 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
485 lutil_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
486 lutil_ServiceStatus.dwCheckPoint++;
487 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
488 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
492 static char *GetErrorString( int err )
494 static char msgBuf[1024];
497 FORMAT_MESSAGE_FROM_SYSTEM,
499 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
500 msgBuf, 1024, NULL );
505 static char *GetLastErrorString( void )
507 return GetErrorString( GetLastError() );