7 #include "ldap_pvt_thread.h"
10 #include <ac/string.h>
12 #include "ldapconfig.h"
17 #define SCM_NOTIFICATION_INTERVAL 5000
18 #define THIRTY_SECONDS (30 * 1000)
20 int is_NT_Service = 1; // assume this is an NT service until determined that
21 // startup was from the command line
23 SERVICE_STATUS SLAPDServiceStatus;
24 SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
26 ldap_pvt_thread_cond_t started_event, stopped_event;
27 ldap_pvt_thread_t start_status_tid, stop_status_tid;
29 void (*stopfunc)(int);
32 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
36 char *WSAGetLastErrorString( void );
39 char *GetLastErrorString( void );
41 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
44 DWORD dwValue, dwDisposition;
45 SC_HANDLE schSCManager, schService;
47 fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
48 if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
50 if ((schService = CreateService(
53 TEXT("OpenLDAP Directory Service"),
54 SC_MANAGER_CREATE_SERVICE,
55 SERVICE_WIN32_OWN_PROCESS,
59 NULL, NULL, NULL, NULL, NULL)) != NULL)
62 CloseServiceHandle(schService);
63 CloseServiceHandle(schSCManager);
65 sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
67 // Create the registry key for event logging to the Windows NT event log.
68 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
70 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
71 &dwDisposition) != ERROR_SUCCESS)
73 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
77 if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
79 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
84 dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
85 if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
87 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
96 fprintf( stderr, "CreateService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
97 CloseServiceHandle(schSCManager);
102 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
107 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
109 SC_HANDLE schSCManager, schService;
111 fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
112 if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
114 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
116 if ( DeleteService(schService) == TRUE)
118 CloseServiceHandle(schService);
119 CloseServiceHandle(schSCManager);
122 fprintf( stderr, "DeleteService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
123 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
124 CloseServiceHandle(schService);
125 CloseServiceHandle(schSCManager);
129 fprintf( stderr, "OpenService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
130 CloseServiceHandle(schSCManager);
135 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
141 static void *start_status_routine( void *ptr )
148 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
149 switch ( wait_result )
153 // the object that we were waiting for has been destroyed (ABANDONED) or
154 // signalled (TIMEOUT_0). We can assume that the startup process is
155 // complete and tell the Service Control Manager that we are now runnng
156 SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING;
157 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
158 SLAPDServiceStatus.dwCheckPoint++;
159 SLAPDServiceStatus.dwWaitHint = 1000;
160 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
164 // We've waited for the required time, so send an update to the Service Control
165 // Manager saying to wait again.
166 SLAPDServiceStatus.dwCheckPoint++;
167 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
168 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
171 // theres been some proble with WaitForSingleObject so tell the Service
172 // Control Manager to wait 30 seconds before deploying its assasin and
173 // then leave the thread.
174 SLAPDServiceStatus.dwCheckPoint++;
175 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
176 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
181 ldap_pvt_thread_exit(NULL);
187 static void *stop_status_routine( void *ptr )
194 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
195 switch ( wait_result )
199 // the object that we were waiting for has been destroyed (ABANDONED) or
200 // signalled (TIMEOUT_0). The shutting down process is therefore complete
201 // and the final SERVICE_STOPPED message will be sent to the service control
202 // manager prior to the process terminating.
206 // We've waited for the required time, so send an update to the Service Control
207 // Manager saying to wait again.
208 SLAPDServiceStatus.dwCheckPoint++;
209 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
210 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
213 // theres been some proble with WaitForSingleObject so tell the Service
214 // Control Manager to wait 30 seconds before deploying its assasin and
215 // then leave the thread.
216 SLAPDServiceStatus.dwCheckPoint++;
217 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
218 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
223 ldap_pvt_thread_exit(NULL);
229 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
233 case SERVICE_CONTROL_STOP:
234 case SERVICE_CONTROL_SHUTDOWN:
236 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0 );
237 SLAPDServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
238 SLAPDServiceStatus.dwCheckPoint++;
239 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
240 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
242 ldap_pvt_thread_cond_init( &stopped_event );
243 if ( stopped_event == NULL )
245 // the event was not created. We will ask the service control manager for 30
246 // seconds to shutdown
247 SLAPDServiceStatus.dwCheckPoint++;
248 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
249 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
253 // start a thread to report the progress to the service control manager
254 // until the stopped_event is fired.
255 if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
260 // failed to create the thread that tells the Service Control Manager that the
261 // service stopping is proceeding.
262 // tell the Service Control Manager to wait another 30 seconds before deploying its
264 SLAPDServiceStatus.dwCheckPoint++;
265 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
266 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
272 case SERVICE_CONTROL_INTERROGATE:
273 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
280 void *getRegParam( char *svc, char *value )
286 DWORD vNameLen = 255;
288 static char vValue[1024];
292 sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
294 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
296 if( (rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
297 path, 0, KEY_READ, &hkey )) != ERROR_SUCCESS )
299 //Debug( LDAP_DEBUG_ANY, "%s\n", GetLastErrorString(), 0, 0 );
303 while ( !RegEnumValue( hkey, i, vName, &vNameLen, NULL,
304 &vType, vValue, &valLen ) )
306 if ( !strcasecmp( value, vName ) )
312 return (void*)&vValue;
314 return (void*)strdup( vValue );
324 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, short port, int udp )
330 hEventLog = RegisterEventSource( NULL, svc );
332 Inserts[i] = (char *)malloc( 20 );
333 itoa( slap_debug, Inserts[i++], 10 );
334 Inserts[i++] = ldap_pvt_strdup( configfile );
335 Inserts[i] = (char *)malloc( 20 );
336 itoa( port, Inserts[i++], 10 );
337 Inserts[i++] = ldap_pvt_strdup( udp ? "udp" : "tcp" );
338 Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
340 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0, MSG_SLAPD_STARTED, NULL, i, 0, Inserts, NULL );
342 for ( j = 0; j < i; j++ )
343 ldap_memfree( Inserts[j] );
344 DeregisterEventSource( hEventLog );
349 void LogSlapdStoppedEvent( char *svc )
353 hEventLog = RegisterEventSource( NULL, svc );
354 ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0, MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
355 DeregisterEventSource( hEventLog );
359 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
360 void (*stopper)(int) )
362 hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
366 /* initialize the Service Status structure */
367 SLAPDServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
368 SLAPDServiceStatus.dwCurrentState = SERVICE_START_PENDING;
369 SLAPDServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
370 SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR;
371 SLAPDServiceStatus.dwServiceSpecificExitCode = 0;
372 SLAPDServiceStatus.dwCheckPoint = 1;
373 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
375 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
377 // start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
378 // until the slapd listener is completed and listening. Only then should we send
379 // SERVICE_RUNNING to the Service Control Manager.
380 ldap_pvt_thread_cond_init( &started_event );
381 if ( started_event == NULL)
383 // failed to create the event to determine when the startup process is complete so
384 // tell the Service Control Manager to wait another 30 seconds before deploying its
386 SLAPDServiceStatus.dwCheckPoint++;
387 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
388 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
392 // start a thread to report the progress to the service control manager
393 // until the started_event is fired.
394 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
399 // failed to create the thread that tells the Service Control Manager that the
400 // service startup is proceeding.
401 // tell the Service Control Manager to wait another 30 seconds before deploying its
403 SLAPDServiceStatus.dwCheckPoint++;
404 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
405 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
410 void ReportSlapdShutdownComplete( )
414 // stop sending SERVICE_STOP_PENDING messages to the Service Control Manager
415 ldap_pvt_thread_cond_signal( &stopped_event );
416 ldap_pvt_thread_cond_destroy( &stopped_event );
418 // wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
419 // if the wait fails then put ourselves to sleep for half the Service Control Manager update interval
420 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
421 ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
423 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
424 SLAPDServiceStatus.dwCheckPoint++;
425 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
426 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);