]> git.sur5r.net Git - openldap/blob - libraries/liblutil/ntservice.c
Cleanup slapd-specific NT service support
[openldap] / libraries / liblutil / ntservice.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  * NT Service manager utilities for OpenLDAP services
9  */
10
11 #include "portable.h"
12
13 #ifdef HAVE_NT_SERVICE_MANAGER
14
15 #include <ac/stdlib.h>
16 #include <ac/string.h>
17
18 #include <stdio.h>
19
20 #include <windows.h>
21 #include <winsvc.h>
22
23 #include <ldap.h>
24
25 #include "ldap_log.h"
26 #include "ldap_pvt_thread.h"
27
28
29 #include "ldap_defaults.h"
30
31 #include "slapdmsg.h"
32
33 #define SCM_NOTIFICATION_INTERVAL       5000
34 #define THIRTY_SECONDS                          (30 * 1000)
35
36 int       is_NT_Service;        /* is this is an NT service? */
37
38 SERVICE_STATUS                  lutil_ServiceStatus;
39 SERVICE_STATUS_HANDLE   hlutil_ServiceStatus;
40
41 ldap_pvt_thread_cond_t  started_event,          stopped_event;
42 ldap_pvt_thread_t               start_status_tid,       stop_status_tid;
43
44 void (*stopfunc)(int);
45
46 static char *GetLastErrorString( void );
47
48 int lutil_srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
49                 LPCTSTR lpszBinaryPathName, int auto_start)
50 {
51         HKEY            hKey;
52         DWORD           dwValue, dwDisposition;
53         SC_HANDLE       schSCManager, schService;
54         char *sp = strchr( lpszBinaryPathName, ' ');
55
56         if ( sp ) *sp = '\0';
57         fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
58         if ( sp ) *sp = ' ';
59         if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
60         {
61                 if ((schService = CreateService( 
62                                                         schSCManager, 
63                                                         lpszServiceName, 
64                                                         lpszDisplayName, 
65                                                         SERVICE_ALL_ACCESS, 
66                                                         SERVICE_WIN32_OWN_PROCESS, 
67                                                         auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START, 
68                                                         SERVICE_ERROR_NORMAL, 
69                                                         lpszBinaryPathName, 
70                                                         NULL, NULL, NULL, NULL, NULL)) != NULL)
71                 {
72                         char regpath[132];
73                         CloseServiceHandle(schService);
74                         CloseServiceHandle(schSCManager);
75
76                         snprintf( regpath, sizeof regpath,
77                                 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
78                                 lpszServiceName );
79                         /* Create the registry key for event logging to the Windows NT event log. */
80                         if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
81                                 regpath, 0, 
82                                 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, 
83                                 &dwDisposition) != ERROR_SUCCESS)
84                         {
85                                 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
86                                 RegCloseKey(hKey);
87                                 return(0);
88                         }
89                         if ( sp ) *sp = '\0';
90                         if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
91                         {
92                                 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
93                                 RegCloseKey(hKey);
94                                 return(0);
95                         }
96
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) 
99                         {
100                                 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
101                                 RegCloseKey(hKey);
102                                 return(0);
103                         }
104                         RegCloseKey(hKey);
105                         return(1);
106                 }
107                 else
108                 {
109                         fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
110                         CloseServiceHandle(schSCManager);
111                         return(0);
112                 }
113         }
114         else
115                 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
116         return(0);
117 }
118
119
120 int lutil_srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
121 {
122         SC_HANDLE schSCManager, schService;
123
124         fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
125         if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL ) 
126         {
127                 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL) 
128                 {
129                         if ( DeleteService(schService) == TRUE) 
130                         {
131                                 CloseServiceHandle(schService);
132                                 CloseServiceHandle(schSCManager);
133                                 return(1);
134                         } else {
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);
139                                 return(0);
140                         }
141                 } else {
142                         fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
143                         CloseServiceHandle(schSCManager);
144                         return(0);
145                 }
146         }
147         else
148                 fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
149         return(0);
150 }
151
152
153 #if 0 /* unused */
154 DWORD
155 svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
156 {
157         char buf[256];
158         HKEY key;
159         DWORD rc;
160         DWORD type;
161         long len;
162
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)
166                 return(-1);
167
168         rc = 0;
169         if (lpszBinaryPathName) {
170                 len = sizeof(buf);
171                 if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
172                         if (strcmp(lpszBinaryPathName, buf))
173                                 rc = -1;
174                 }
175         }
176         RegCloseKey(key);
177         return(rc);
178 }
179
180
181 DWORD
182 svc_running (LPTSTR lpszServiceName)
183 {
184         SC_HANDLE service;
185         SC_HANDLE scm;
186         DWORD rc;
187         SERVICE_STATUS ss;
188
189         if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
190                 return(GetLastError());
191
192         rc = 1;
193         service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
194         if (service) {
195                 if (!QueryServiceStatus(service, &ss))
196                         rc = GetLastError();
197                 else if (ss.dwCurrentState != SERVICE_STOPPED)
198                         rc = 0;
199                 CloseServiceHandle(service);
200         }
201         CloseServiceHandle(scm);
202         return(rc);
203 }
204 #endif
205
206 static void *start_status_routine( void *ptr )
207 {
208         DWORD   wait_result;
209         int             done = 0;
210
211         while ( !done )
212         {
213                 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
214                 switch ( wait_result )
215                 {
216                         case WAIT_ABANDONED:
217                         case WAIT_OBJECT_0:
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);
226                                 done = 1;
227                                 break;
228                         case WAIT_TIMEOUT:
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);
234                                 break;
235                         case WAIT_FAILED:
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);
242                                 done = 1;
243                                 break;
244                 }
245         }
246         ldap_pvt_thread_exit(NULL);
247         return NULL;
248 }
249
250
251
252 static void *stop_status_routine( void *ptr )
253 {
254         DWORD   wait_result;
255         int             done = 0;
256
257         while ( !done )
258         {
259                 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
260                 switch ( wait_result )
261                 {
262                         case WAIT_ABANDONED:
263                         case WAIT_OBJECT_0:
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. */
268                                 done = 1;
269                                 break;
270                         case WAIT_TIMEOUT:
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);
276                                 break;
277                         case WAIT_FAILED:
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);
284                                 done = 1;
285                                 break;
286                 }
287         }
288         ldap_pvt_thread_exit(NULL);
289         return NULL;
290 }
291
292
293
294 void WINAPI lutil_ServiceCtrlHandler( IN DWORD Opcode)
295 {
296         switch (Opcode)
297         {
298         case SERVICE_CONTROL_STOP:
299         case SERVICE_CONTROL_SHUTDOWN:
300
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);
305
306                 ldap_pvt_thread_cond_init( &stopped_event );
307                 if ( stopped_event == NULL )
308                 {
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);
314                 }
315                 else
316                 {
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 )
320                         {
321                                 
322                         }
323                         else {
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
327                                  * assasin.  */
328                                 lutil_ServiceStatus.dwCheckPoint++;
329                                 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
330                                 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
331                         }
332                 }
333                 stopfunc( -1 );
334                 break;
335
336         case SERVICE_CONTROL_INTERROGATE:
337                 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
338                 break;
339         }
340         return;
341 }
342
343 void *lutil_getRegParam( char *svc, char *value )
344 {
345         HKEY hkey;
346         char path[255];
347         DWORD vType;
348         static char vValue[1024];
349         DWORD valLen = sizeof( vValue );
350
351         if ( svc != NULL )
352                 snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
353         else
354                 snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
355         
356         if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
357         {
358                 return NULL;
359         }
360
361         if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
362         {
363                 RegCloseKey( hkey );
364                 return NULL;
365         }
366         RegCloseKey( hkey );
367         
368         switch ( vType )
369         {
370         case REG_BINARY:
371         case REG_DWORD:
372                 return (void*)&vValue;
373         case REG_SZ:
374                 return (void*)&vValue;
375         }
376         return (void*)NULL;
377 }
378
379 void lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
380 {
381         char *Inserts[5];
382         WORD i = 0, j;
383         HANDLE hEventLog;
384         
385         hEventLog = RegisterEventSource( NULL, svc );
386
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:///" );
391
392         ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
393                 MSG_SVC_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
394
395         for ( j = 0; j < i; j++ )
396                 ldap_memfree( Inserts[j] );
397         DeregisterEventSource( hEventLog );
398 }
399
400
401
402 void lutil_LogStoppedEvent( char *svc )
403 {
404         HANDLE hEventLog;
405         
406         hEventLog = RegisterEventSource( NULL, svc );
407         ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
408                 MSG_SVC_STOPPED, NULL, 0, 0, NULL, NULL );
409         DeregisterEventSource( hEventLog );
410 }
411
412
413 void lutil_CommenceStartupProcessing( char *lpszServiceName,
414                                                            void (*stopper)(int) )
415 {
416         hlutil_ServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)lutil_ServiceCtrlHandler);
417
418         stopfunc = stopper;
419
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;
428
429         SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
430
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)
436         {
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
439                  * assasin  */
440                 lutil_ServiceStatus.dwCheckPoint++;
441                 lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
442                 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
443         }
444         else
445         {
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 )
449                 {
450                         
451                 }
452                 else {
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
456                          * assasin.  */
457                         lutil_ServiceStatus.dwCheckPoint++;
458                         lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
459                         SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
460                 }
461         }
462 }
463
464 void lutil_ReportShutdownComplete(  )
465 {
466         if ( is_NT_Service )
467         {
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 );
471
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 );
476
477                 lutil_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
478                 lutil_ServiceStatus.dwCheckPoint++;
479                 lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
480                 SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
481         }
482 }
483
484 static char *GetErrorString( int err )
485 {
486         static char msgBuf[1024];
487
488         FormatMessage(
489                 FORMAT_MESSAGE_FROM_SYSTEM,
490                 NULL,
491                 err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
492                 msgBuf, 1024, NULL );
493
494         return msgBuf;
495 }
496
497 static char *GetLastErrorString( void )
498 {
499         return GetErrorString( GetLastError() );
500 }
501 #endif